Advertisement
Dmaxiya

拔河 参考代码

Mar 17th, 2025
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 1000 + 100;
  6. int n;
  7. LL ans;
  8. map<LL, int> cnt;
  9. map<LL, int>::iterator it;
  10. LL sum[maxn];
  11.  
  12. void add(LL x) {
  13.     ++cnt[x];
  14. }
  15.  
  16. void del(LL x) {
  17.     it = cnt.find(x);
  18.     if (it == cnt.end()) {
  19.         return ;
  20.     }
  21.     if (it->second == 1) {
  22.         cnt.erase(it);
  23.         return ;
  24.     }
  25.     --it->second;
  26. }
  27.  
  28. LL solve(LL x) {
  29.     LL ret = 1e18;
  30.     it = cnt.lower_bound(x);
  31.     if (it != cnt.end()) {
  32.         ret = min(ret, it->first - x);
  33.     }
  34.     if(it != cnt.begin()) {
  35.         --it;
  36.         ret = min(ret, x - it->first);
  37.     }
  38.     return ret;
  39. }
  40.  
  41. int main() {
  42. #ifdef ExRoc
  43.     freopen("test.txt", "r", stdin);
  44. #endif
  45.     ios::sync_with_stdio(false);
  46.  
  47.     cin >> n;
  48.     for (int i = 1; i <= n; ++i) {
  49.         cin >> sum[i];
  50.         sum[i] += sum[i - 1];
  51.     }
  52.     for (int i = 1; i <= n; ++i) {
  53.         for (int j = i; j <= n; ++j) {
  54.             add(sum[j] - sum[i - 1]);
  55.         }
  56.     }
  57.     ans = 1e18;
  58.     for (int i = 1; i < n; ++i) {
  59.         for (int j = i; j <= n; ++j) {
  60.             del(sum[j] - sum[i - 1]);
  61.         }
  62.         for (int j = i; j >= 1; --j) {
  63.             ans = min(ans, solve(sum[i] - sum[j - 1]));
  64.         }
  65.     }
  66.     cout << ans << endl;
  67.  
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement