当前位置: 代码迷 >> 综合 >> Contest3184 - 2021级新生个人训练赛第40场_G: Splitting Pile
  详细解决方案

Contest3184 - 2021级新生个人训练赛第40场_G: Splitting Pile

热度:34   发布时间:2023-12-06 05:32:59.0

//
问题 G: Splitting Pile
时间限制: 1.000 Sec  内存限制: 128 MB题目描述
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 is given from Standard Input in the following format:
N
a1 a2 … aN
输出
Print the answer.
样例输入 Copy
6
1 2 3 4 5 6
样例输出 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.

//
#include<bits/stdc++.h>
using namespace std;// 2≤N≤2*10e5
// -10e9≤ai≤10e9    
// max= 1e9*2e5 == 2e14 > int           // 注意分析规模
typedef long long LL;
const LL INF=0x3f3f3f3f3f3f3f3f;        // 8*3f
const int MAXN=2e5+6;
LL a[MAXN];int main()
{LL n,i,temp,sum,ans;while( ~scanf("%lld",&n) ){sum=0;memset( a,0,sizeof( a ) );for( i=0;i<n;i++ ){scanf("%lld",&temp);a[i]=sum+=temp;         // }ans=INF;for( i=0;i<n-1;i++ ) ans=min( ans,abs( sum-2*a[i] ) );printf("%lld\n",ans);}return 0;
}