Codeforces Round #353 (Div. 2)C
来源:互联网 发布:张宴 实战nginx 编辑:程序博客网 时间:2024/06/04 19:23
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.
Print the minimum number of operations required to change balance in each bank to zero.
35 0 -5
1
4-1 0 1 0
2
41 2 3 -6
3
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer:
- transfer 1 from the first bank to the second bank;
- transfer 3 from the second bank to the third;
- transfer 6 from the third bank to the fourth.
/* ***********************************************Author : rycCreated Time : 2016-08-04 ThursdayFile Name : E:\acm\codeforces\353C.cppLanguage : c++Copyright 2016 ryc All Rights Reserved************************************************ */#include<bits/stdc++.h>using namespace std;map<long long ,long long>vis;int main(){ long long n;cin>>n; long long sum=0,num,ans=n-1l; for(int i=1;i<=n;++i){ scanf("%lld",&num); sum+=num;vis[sum]++; ans=min(ans,n-vis[sum]); } printf("%lld\n",ans); return 0;}
- Codeforces Round #353 (Div. 2) C 思维
- Codeforces Round #353 (Div. 2)C
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Codeforces Round #353(Div 2)
- Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)
- Codeforces Round #353 (Div. 2) A,B,C,D
- Codeforces Round #353 (Div. 2) C. Money Transfers
- Codeforces Round #353 (Div. 2)C. Money Transfers
- Codeforces Round #353 (Div. 2) C. Money Transfers(数学题)
- Codeforces Round #353 (Div. 2) C.Money Transfers
- Codeforces Round #353 (Div. 2) C. Money Transfers (贪心)
- Codeforces Round #353 (Div. 2) C. Money Transfers (map)
- Codeforces Round #353 (Div. 2) C. Money Transfers 数学
- Codeforces Round #105 (Div. 2) C
- Codeforces Round 134 div 2 C题
- Codeforces Round #137 (Div. 2), problem: (C)
- Codeforces Round #153 (Div. 2) C题
- Codeforces Round #158 (Div. 2) C题
- Java笔记5:解决MyEclipse控制台输入中文乱码输出问题(Eclipse也可以用)
- Java笔记6:循环
- Java笔记7:数组
- HDU:5137 How Many Maos Does the Guanxi Worth(dijkstra算法求最短路径+小技巧)
- 《软件工程里面的大学十年》(转载上半部分)
- Codeforces Round #353 (Div. 2)C
- 帧动画(FrameAnimation) 补间动画(TweeAnimation) 属性动画(ObjectAnimation)
- 《软件工程里面的大学十年》(转载下半部分)
- Java笔记8:java类,对象,方法
- Install MongoDB on Ubuntu16.04
- Java笔记9:构造方法,内部类
- Java笔记10:参数方法
- Java笔记11:RETURN注意事项
- Java笔记12: String,StringBuffer与StringBuilder的区别