Codeforces 808D
2017-07-11 10:45
369 查看
题目链接
【题意】
【分析】
【Code】
【题意】
给定一个长度为n的序列,问是否可以最多移动一个数字,使该数列可以分成两部分且这两部分和相等,n<=100000
【分析】
可以假设有个隔板从左向右扫(假设这就是最终的划分结果),每次两边的和的差值为x,若能在和较大的序列找到一个数为x/2,则将它放到另一边就满足条件。 这样扫一遍是O(n)的,但是数据是无序的,由于n<=100000,不能通过O(n^2)的暴力查找来做,可以通过平衡树来维护查找是O(logn)的,利用STL就好了 最终复杂度O(nlogn) 注意: x,sum1,sum2都要用long long,即使a[i]<=10^9,x也可能爆int,因为x如果不在a[i]中,是可能超过int范围的.
【Code】
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> typedef long long LL; using namespace std; const double PI = acos(-1.0); const int MAX_N = 100000 + 10; multiset<int> s1, s2; int a[MAX_N]; int main(){ int n; LL sum1 = 0LL, sum2 = 0LL; s1.clear();s2.clear(); scanf("%d",&n); for (int i=0;i<n;i++){ scanf("%d",&a[i]); sum2 += a[i]; s2.insert(a[i]); } if (sum2%2!=0) { printf("NO\n"); return 0; } multiset<int>::iterator it; bool ok = false; for (int i=0;i<n;i++){ sum1 += a[i]; sum2 -= a[i]; s1.insert(a[i]); it = s2.find(a[i]); if (it != s2.end()) s2.erase(it); if (sum1 == sum2) { ok = true; break; }else if (sum1 < sum2){ if ((sum2 - sum1)%2 == 0){ LL x = (sum2 - sum1)/2; if (x<=1e9) if (s2.count(x)){ ok = true; break; } } }else if (sum1 > sum2){ if ((sum1 - sum2)%2 == 0){ LL x = (sum1 - sum2)/2; if (x<=1e9) if (s1.count(x)){ ok = true; break; } } } } printf("%s\n",ok ? "YES":"NO"); return 0; }
相关文章推荐
- codeforces 808D
- Educational Codeforces Round 21 Problem D(Codeforces 808D)
- CodeForces - 808D-J - Array Division -set或者二分
- CodeForces - 808D(STL+思维)
- codeforces 808D - Array Division
- Codeforces 808D 数组分割
- codeforces 808D - Array Division
- [codeforces]808D
- Codeforces 382A Ksenia and Pan Scales(水题)
- codeforces 567 E. President and Roads 【 最短路 桥 】
- Codeforces 246A-Buggy Sorting【模拟】
- Codeforces 529B Group Photos 2 (one mirror version) [贪心]
- Codeforces 803C Maximal GCD【思维】
- 【36.86%】【codeforces 558B】Amr and The Large Array
- codeforces Fox and Cross
- codeforces 235 B. Let's Play Osu!
- Codeforces 119C DP
- Codeforces 416C Booking System [贪心]
- codeforces 803C Maximal GCD(GCD数学)
- 【codeforces 257D】Sum