鱼C论坛

 找回密码
 立即注册
查看: 98|回复: 1

问一下各位佬,为什么这段代码只能过10%的数据?

[复制链接]
发表于 2024-4-20 14:53:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述:
小明是学校里的一名老师,他带的班级共有 n 名同学,第 i 名同学力量值为 ai。在闲暇之余,小明决定在班级里组织一场拔河比赛。

为了保证比赛的双方实力尽可能相近,需要在这 n 名同学中挑选出两个队伍,队伍内的同学编号连续:{al1, al1+1, ..., ar1&#8722;1, ar1} 和 {al2, al2+1, ..., ar2&#8722;1, ar2},其中 l1 ≤ r1 < l2 ≤ r2。

两个队伍的人数不必相同,但是需要让队伍内的同学们的力量值之和尽可能相近。请计算出力量值之和差距最小的挑选队伍的方式。


【评测用例规模与约定】

对于 20% 的评测用例,保证 n ≤ 50。

对于 100% 的评测用例,保证 n ≤ 1e3,ai ≤ 1e9。



说一下我的思路:
把数组从小到大排列好。对于这个已经排列好的数组而言,有一个头指针top,一个尾指针rear,还有一个mid指针 =(top + rear) / 2 。

将从top加到mid的值即为lef,mid加到rear的值记录为rig

如果lef<rig,则记录temp = rig-lef

如果lef>=rig,则判断temp,lef-rig谁更小,输出小的那一个

但是不知道为什么只能过10%的数据,希望各位大佬能够帮小萌新解惑

以下是源代码
  1. //////////
  2. //////////#include<bits/stdc++.h>
  3. //////////using namespace std;
  4. //////////
  5. //////////static int N = 1e5;
  6. //////////int str;
  7. //////////int end1;
  8. //////////int length;
  9. //////////bool isPrime(int n)        //为true时为素数
  10. //////////{
  11. //////////        if (n < 2)
  12. //////////                return false;
  13. //////////        for (int i = 2;i * i <= n;i++)
  14. //////////        {
  15. //////////                if (n % i == 0)
  16. //////////                        return false;
  17. //////////        }
  18. //////////        return true;
  19. //////////}
  20. //////////
  21. //////////int Prime()
  22. //////////{
  23. //////////        vector<bool>flag(N);
  24. //////////        vector<int>st;
  25. //////////        cin >> str >> end1;
  26. //////////        for (int i = str;i <= end1;i++)
  27. //////////        {
  28. //////////                st.push_back(i);
  29. //////////        }
  30. //////////        length = st.size();
  31. //////////        for (int i = 0;i < length;i++)
  32. //////////        {
  33. //////////                if (isPrime(st[i]))//isPrime为真的时候是素数
  34. //////////                {
  35. //////////                        flag[i] = true;
  36. //////////                        for (int j = 1;j * st[i] <= st[length - 1];j++)
  37. //////////                        {
  38. //////////                                flag[j*st[i]] = false;
  39. //////////                        }
  40. //////////                }
  41. //////////        }
  42. //////////        for (int i = 0;i < length;i++)
  43. //////////        {
  44. //////////                if (flag[i])
  45. //////////                        cout << st[i] << " ";
  46. //////////        }
  47. //////////        puts("");
  48. //////////        return 0;
  49. //////////}
  50. //////////
  51. //////////
  52. //////////int main()
  53. //////////{
  54. //////////        ios::sync_with_stdio(0);
  55. //////////        cin.tie(0);
  56. //////////        cout.tie(0);
  57. //////////        int i =1;
  58. //////////        cin >> i;
  59. //////////        while (i--)
  60. //////////                Prime();
  61. //////////        return 0;
  62. //////////}
  63. //////////
  64. ////////#include <iostream>
  65. ////////#include <vector>
  66. ////////#include <cmath>
  67. ////////using namespace std;
  68. ////////
  69. ////////void sieveOfEratosthenes(int start, int end1) {
  70. ////////    vector<bool> isPrime(end1 - start + 1, true);
  71. ////////    for (int i = 2; i * i <= end1; ++i) {
  72. ////////        for (int j = max(2, (start + i - 1) / i) * i; j <= end1; j += i) {
  73. ////////            isPrime[j - start] = false;
  74. ////////        }
  75. ////////    }
  76. ////////    for (int i = max(2, start); i <= end1; ++i) {
  77. ////////        if (isPrime[i - start]) {
  78. ////////            cout << i << " ";
  79. ////////        }
  80. ////////    }
  81. ////////    cout << endl;
  82. ////////}
  83. ////////
  84. ////////int main() {
  85. ////////    int t;
  86. ////////    cin >> t;
  87. ////////    while (t--) {
  88. ////////        int start, end1;
  89. ////////        cin >> start >> end1;
  90. ////////        sieveOfEratosthenes(start, end1);
  91. ////////    }
  92. ////////    return 0;
  93. ////////}
  94. //////全排列
  95. //////
  96. //////#include<bits/stdc++.h>
  97. //////using namespace std;
  98. //////typedef long long ll;
  99. //////int st[10];
  100. //////bool ise[10];
  101. //////int num = 3;
  102. //////
  103. //////void dfs(int step)        //好像又犯了相同的错误
  104. //////{
  105. //////        //一定要明确每个参数的意义
  106. //////        //边界条件
  107. //////        if (step == num+1)
  108. //////        {
  109. //////                for (int i = 1; i < step;i++)
  110. //////                {
  111. //////                        cout << st[i];
  112. //////                }
  113. //////                cout << endl;
  114. //////                return ;
  115. //////        }
  116. //////        //循环条件
  117. //////        for (int i = 1;i < num + 1;i++)//切记i表示的是手中的牌
  118. //////        {
  119. //////                if (!ise[i])
  120. //////                {
  121. //////                        st[step] = i;
  122. //////                        ise[i] = true;
  123. //////                        dfs(step + 1);
  124. //////                        ise[i] = false;
  125. //////                }
  126. //////        }
  127. //////}
  128. //////void solve()
  129. //////{
  130. //////        dfs(1);
  131. //////}
  132. //////
  133. //////int main()
  134. //////{
  135. //////        ios::sync_with_stdio(0);
  136. //////        cout.tie(0);
  137. //////        cin.tie(0);
  138. //////        int i = 1;
  139. //////        while (i--)
  140. //////                solve();
  141. //////        return 0;
  142. //////}
  143. ////
  144. ////#include<bits/stdc++.h>
  145. ////using namespace std;
  146. ////#define int long long
  147. ////#define endl '\n'
  148. ////#define pp ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
  149. ////const int N = 1e5 + 1;
  150. ////vector<int>a[N];
  151. ////int s[N];
  152. ////int n;
  153. ////void solve() {
  154. ////    pp;
  155. ////    cin >> n;
  156. ////    for (int i = 0;i < n;i++)cin >> s[i];
  157. ////    sort(s, s + n);//s表示h
  158. ////    for (int i = 0;i < n;i++)
  159. ////    {
  160. ////        for (int j = 1;j <= sqrt(s[i]);j++)
  161. ////        {
  162. ////            if (s[i] % j == 0)
  163. ////            {
  164. ////                a[j].push_back(s[i]);
  165. ////                //需要在满足是因子的基础上判断是否是乘方因子,放在外面是错的
  166. ////                if (s[i] / j != j)
  167. ////                    a[s[i] / j].push_back(s[i]);
  168. ////            }
  169. ////        }
  170. ////    }
  171. ////
  172. ////    for (int i = N - 1;i >= 0;i--)
  173. ////    {
  174. ////        if (a[i].size() >= 3)
  175. ////        {
  176. ////            cout << a[i][0];
  177. ////            for (int j = 1;j < 3;j++)cout << " " << a[i][j];
  178. ////            break;
  179. ////        }
  180. ////    }
  181. ////    return;
  182. ////}
  183. ////
  184. ////signed main() {
  185. ////    int T = 1;
  186. ////    while (T--)solve();
  187. ////    return 0;
  188. ////}
  189. ////
  190. //
  191. //
  192. ////不会写就暴力
  193. //#include <iostream>
  194. //#include<vector>
  195. //#include<algorithm>
  196. //using namespace std;
  197. //typedef long long ll;
  198. //ll temp;
  199. //int cmp(ll x,ll y)
  200. //{
  201. //        return x > y;
  202. //}
  203. //void solve()
  204. //{
  205. //        ll n, p, q;
  206. //        cin >> n>>p>>q;
  207. //        vector<ll>a(n);
  208. //        {
  209. //                for (ll i = 0;i < n;i++)
  210. //                {
  211. //                        cin >> a[i];
  212. //                }
  213. //        }
  214. //        ll ans = 0;
  215. //        sort(a.begin(), a.end());
  216. //        for (temp = n - 1;temp > n - q - 1;temp--)
  217. //        {
  218. //                a[temp] = sqrt(a[temp]);
  219. //        }
  220. //        sort(a.begin(), a.end(),cmp);
  221. //        for (ll i = 0;i < q;i++)
  222. //        {
  223. //                a[i] = a[i] / 2;
  224. //        }
  225. //        for (ll i = 0;i < n;i++)
  226. //        {
  227. //                ans += a[i];
  228. //        }
  229. //        cout <<ans;
  230. //}
  231. //int main()
  232. //{
  233. //        ios::sync_with_stdio(0);
  234. //        cin.tie(0);
  235. //        cout.tie(0);
  236. //        int i = 1;
  237. //        while (i--)
  238. //        {
  239. //                solve();
  240. //        }
  241. //}

  242. //拔河,简单解法
  243. #include<iostream>
  244. #include<math.h>
  245. #include<algorithm>
  246. #include<vector>
  247. using namespace std;
  248. typedef long long ll;
  249. #define def(x) cout<<x
  250. #define endl '\n'
  251. #define M 0x3f3f3f3f3f3f3f3f
  252. //const int N = 10e3 + 10;
  253. ll num;
  254. //int a[N];                //这边有点问题,数组太大了怎么办?
  255. void solve()
  256. {
  257.        
  258.         cin >> num;
  259.         vector<ll>a;
  260.         ll temp;
  261.         for (ll i = 0;i < num;i++)
  262.         {
  263.                 cin >> temp;
  264.                 a.push_back(temp);
  265.         }
  266.         sort(a.begin(), a.end());
  267.         ll top = 0, rear = num - 1;
  268.         ll mid = (top+rear)/2;
  269.         ll lef, rig;
  270.         ll temp1 = 0;
  271.         while (1)
  272.         {
  273.                 //每次更新的时候进行重置
  274.                 lef = 0;
  275.                 rig = 0;
  276.                 for (ll i = 0; i < mid; i++)
  277.                 {
  278.                         lef += a[i];
  279.                 }
  280.                 for (ll i = mid;i <= rear;i++)
  281.                 {
  282.                         rig = rig + a[i];
  283.                 }
  284.                 //只要少给一个,右边一定大
  285.                 if (lef >= rig )
  286.                 {
  287.                         if (temp1 < lef - rig)
  288.                         {
  289.                                 def(temp1);
  290.                         }
  291.                         else
  292.                         {
  293.                                 def(lef - rig);
  294.                         }
  295.                         return;
  296.                 }
  297.                 temp1 = rig - lef;
  298.                 mid++;       
  299.         }

  300. }
  301. int main()
  302. {
  303.         ios::sync_with_stdio(0);
  304.         cin.tie(0);
  305.         cout.tie(0);
  306.         int t = 1;
  307.         while (t--)
  308.         {
  309.                 solve();
  310.         }
  311.         return 0;
  312. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-20 14:53:51 | 显示全部楼层
看起来你的代码逻辑出现了一些问题。首先,你的思路是正确的,需要将数组按升序排列,并找出两个队伍的分界点mid。但是在找到mid之后,你的代码逻辑出现了问题。

首先,temp1应该在while循环外面进行定义,否则每次循环都会重置为0,导致计算错误。

其次,你计算lef和rig的方式有误。对于lef,应该累加从top到mid的所有元素,而不是只累加a[mid]。同样地,rig也应该累加从mid+1到rear的所有元素。

最后,你的条件判断有误,应该判断temp1和abs(lef-rig)的大小关系,而不是temp1和lef-rig的大小关系。

下面是修改后的代码:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;

  5. void solve() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> a(n);
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     sort(a.begin(), a.end());  // 对数组进行排序

  13.     int top = 0, rear = n - 1;
  14.     int mid = (top + rear) / 2;
  15.     int lef, rig;
  16.     int temp1 = 0;
  17.     while (1) {
  18.         lef = 0;
  19.         rig = 0;
  20.         for (int i = top; i <= mid; i++) {
  21.             lef += a[i];
  22.         }
  23.         for (int i = mid + 1; i <= rear; i++) {
  24.             rig += a[i];
  25.         }
  26.         if (temp1 < abs(lef - rig)) {
  27.             temp1 = abs(lef - rig);
  28.         } else {
  29.             break;
  30.         }
  31.         mid++;
  32.     }
  33.     cout << temp1 << endl;
  34. }

  35. int main() {
  36.     solve();
  37.     return 0;
  38. }
复制代码


现在你可以试着运行这段代码,并测试一下是否能够正确解决问题。如果还有其他问题,请随时向我提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 15:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表