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

简介: 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;
}
目录
相关文章
|
算法
Light oj 1082 - Array Queries(区间最小值)
这里只要知道这种算法即可,因为数据量过大,都编译不通过,不过思想算法没有任何问题。
30 0
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
47 0
51nod 1292 字符串中的最大值 V2 (后缀数组)
51nod 1292 字符串中的最大值 V2 (后缀数组)
57 0
LeetCode contest 183 5376. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order
LeetCode contest 183 5376. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
Python
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
124 0
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
|
人工智能
【待补】UPC No Need(二分+bitset || 背包dp)
【待补】UPC No Need(二分+bitset || 背包dp)
57 0
|
人工智能
[Codeforces 1579G] Minimal Coverage | dp最小区间覆盖
题意: 给出n个线段,以及一个无限大的坐标轴,第一个线段以0为起点进行放置,后面的线段必须以前一个的终点为起点放置,这就有两种方式,向左向右