AtCoder Regular Contest 078 C
2017-07-16 01:03
441 查看
C - Splitting Pile
Time limit : 2sec / Memory limit : 256MBScore : 300 points
Problem Statement
Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer ai written on it.They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.
Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively. They would like to minimize |x−y|. Find the minimum possible value of |x−y|.
Constraints
2≤N≤2×105−109≤ai≤109
ai is an integer.
Input
Input is given from Standard Input in the following format:N a1 a2 … aN
Output
Print the answer.Sample Input 1
Copy6 1 2 3 4 5 6
Sample Output 1
Copy1
If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x−y|=1. This is the minimum possible value.
Sample Input 2
Copy2 10 -10
Sample Output 2
Copy20
Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=−10, and thus |x−y|=20.
题意:选取顶端区间数字求和,再取剩余数字求和,问他们的绝对值之差最小为多少?
解法:当然是前缀和啦~~~~
#include<bits/stdc++.h> #define N 2*123456 using namespace std; #define LL long long LL a ; LL sum[N+100],sum2[N+100]; LL Min=0x3f3f3f3f3f3f3f3f; int main(){ LL n,m; scanf("%lld",&n); sum[0]=0; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); sum[i]=sum[i-1]+a[i]; } for(int i=1;i<=n-1;i++){ // cout<<abs(sum[i]-(sum -sum[i]))<<" "<<Min<<endl; Min=min(abs(sum[i]-(sum -sum[i])),Min); } printf("%lld\n",Min); return 0; }
相关文章推荐
- AtCoder Regular Contest 078
- AtCoder Regular Contest 078 E Awkward Response
- AtCoder Regular Contest 078 D - Fennec VS. Snuke 树型DP/博弈
- AtCoder Regular Contest 078-C - Splitting Pile
- AtCoder Regular Contest 078 F - Mole and Abandoned Mine 状压dp
- AtCoder Regular Contest 078-D - Fennec VS. Snuke
- AtCoder Regular Contest 078 D
- AtCoder Regular Contest 078
- AtCoder Regular Contest 078
- AtCoder Regular Contest 089
- [AtCoder Regular Contest 091 F] Strange Nim
- AtCoder Regular Contest 095E - Symmetric Grid
- Atcoder Regular Contest 072 E Alice in Linear Land
- 背包——AtCoder Regular Contest 073 #D
- AtCoder Regular Contest 099
- AtCoder Regular Contest 078D
- 【构造】AtCoder Regular Contest 079 D - Decrease (Contestant ver.)
- AtCoder Regular Contest 079
- (线段树/贪心)AtCoder Regular Contest 080 E : Old Maid
- AtCoder Regular Contest 080-C - 4-adjacent