AtCoder Regular Contest 078 C

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 |xy|. Find the minimum possible value of |xy|.

Constraints

  • 2≤N≤2×105
  • −109ai≤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=10y=11, and thus |xy|=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=10y=−10, and thus |xy|=20.

题意:选取顶端区间数字求和,再取剩余数字求和,问他们的绝对值之差最小为多少?

解法:当然是前缀和啦~~~~

 1 #include<bits/stdc++.h>
 2 #define N 2*123456
 3 using namespace std;
 4 #define LL long long
 5 LL a[N];
 6 LL sum[N+100],sum2[N+100];
 7 LL Min=0x3f3f3f3f3f3f3f3f;
 8 int main(){
 9     LL n,m;
10     scanf("%lld",&n);
11     sum[0]=0;
12     for(int i=1;i<=n;i++){
13         scanf("%lld",&a[i]);
14         sum[i]=sum[i-1]+a[i];
15     }
16     for(int i=1;i<=n-1;i++){
17      //   cout<<abs(sum[i]-(sum[n]-sum[i]))<<" "<<Min<<endl;
18         Min=min(abs(sum[i]-(sum[n]-sum[i])),Min);
19     }
20     printf("%lld\n",Min);
21     return 0;
22 }

 

转载于:https://www.cnblogs.com/yinghualuowu/p/7189409.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值