Educational Codeforces Round 21 D. Array Division 前缀和
链接:
http://codeforces.com/contest/808/problem/D
题意:
给你一个数组a,问能否移动一个数的位置使得可以分成两部分,这两个部分的和相等
题解:
要么把后面的数移到前面,要么把前面的数移到后面,分别算一下,先用map存一下,不过还要特判当有一个数为sum/2的时候
代码:
31 ll n; 32 ll a[MAXN]; 33 map<ll, ll> mmp; 34 35 int main() { 36 ios::sync_with_stdio(false), cin.tie(0); 37 cin >> n; 38 ll sum = 0; 39 ll Max = 0; 40 rep(i, 1, n + 1) { 41 cin >> a[i]; 42 Max = max(Max, a[i]); 43 sum += a[i]; 44 mmp[a[i]]++; 45 } 46 if (sum & 1) return (cout << "NO" << endl), 0; 47 sum /= 2; 48 if (Max == sum) return (cout << "YES" << endl), 0; 49 if (Max > sum)return (cout << "NO" << endl), 0; 50 ll s = 0; 51 rep(i, 1, n + 1) { 52 s += a[i]; 53 mmp[a[i]]--; 54 if (mmp[sum - s]) return (cout << "YES" << endl), 0; 55 } 56 mmp.clear(); 57 rep(i, 1, n + 1) mmp[a[i]]++; 58 s = 0; 59 per(i, 1, n + 1) { 60 s += a[i]; 61 mmp[a[i]]--; 62 if (mmp[sum - s]) return (cout << "YES" << endl), 0; 63 } 64 cout << "NO" << endl; 65 return 0; 66 }