Advertisement
Dmaxiya

回文数组 参考代码

Mar 15th, 2025
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100000 + 100;
  6. int n;
  7. LL ans;
  8. int num[maxn];
  9.  
  10. int main() {
  11. #ifdef ExRoc
  12.     freopen("test.txt", "r", stdin);
  13. #endif
  14.     ios::sync_with_stdio(false);
  15.  
  16.     cin >> n;
  17.     for (int i = 1; i <= n; ++i) {
  18.         cin >> num[i];
  19.     }
  20.     for (int i = 1; i <= n / 2; ++i) {
  21.         int d = num[n - i + 1] - num[i];
  22.         ans += abs(d);
  23.         num[i] += d;
  24.         if (d > 0) {
  25.             num[i + 1] += min(max(num[n - i] - num[i + 1], 0), d);
  26.         }
  27.         if (d < 0) {
  28.             num[i + 1] += max(min(num[n - i] - num[i + 1], 0), d);
  29.         }
  30.     }
  31.     cout << ans << endl;
  32.  
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement