UPC Splitting Pile(前缀和+最大值的选择)

问题 F: Splitting Pile

题目描述
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.
题意: 给你一个序列,让你把序列分为两部分,使得两部分的和的差值最小。
解题思路: 维护一个前缀和就可以了~首先要计算出总和,总和减去前缀和就是剩余序列的和,再减去一次前缀和就是所需答案。然后找出最小值就可。
注意: 看一眼数据范围的话,初始化最小值要为long long 的最大值,不然有一个测试点卡不过去qwq 也可能是我太弱了(暴风哭泣)
我以前一直以为inf是int型最大值(手动滑稽)

 1. 0x7fffffff :int的最大值, 0x7 后跟7个f,memset (num,0x7f,sizeof(num));
 2. 0x3f3f3f3f, 1061109567, 0x后跟4个3f, memset(num,0x3f,sizeof(num))

代码:

#include<bits/stdc++.h>
const int inf=0x3f3f3f3f;
#define cutele main
using namespace std;
typedef long long ll;
const int N=1e6+7;
ll n,a[N],sum[N],s;
void solve(){
   ll minn=0x7fffffff;
   for(ll i=1;i<n;i++)
        minn=min(minn,abs(s-sum[i]*2));
   cout<<minn<<endl;
}
//前缀和
int cutele(){
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
        s+=a[i];
    }
    solve();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值