您的位置:首页 > 其它

AtCoder Regular Contest 078 C

2017-07-16 01:03 441 查看

C - Splitting Pile

Time limit : 2sec / Memory limit : 256MB

Score : 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

Copy
6
1 2 3 4 5 6


Sample Output 1

Copy
1

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

Copy
2
10 -10


Sample Output 2

Copy
20

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: