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 |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
6 1 2 3 4 5 6
Sample Output 1
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
2 10 -10
Sample Output 2
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.
题意:选取顶端区间数字求和,再取剩余数字求和,问他们的绝对值之差最小为多少?
解法:当然是前缀和啦~~~~
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~