C. Money Transfers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
Print the minimum number of operations required to change balance in each bank to zero.
Examples
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
Note
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.
题意:有n个银行,给出每个银行的存款数,有负数,求要使所有银行的存款数都为0,需要转移多少次,每次只能转移相邻的银行里的钱
思路:依照题意可化为一个环,然后分割是得和为0的环最多,所以,处理前缀和即可。
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-23-14.52 Tuesday
File Name : D:\MyCode\2016-8月\2016-8-23.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e9+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head
map<ll,ll>mp;
ll sum;
int main()
{
int n;ll ans=-1;cin>>n;
sum=0;mp.clear();
for(int i=1;i<=n;i++)
{
ll a;cin>>a;sum+=a;
mp[sum]++;
ans=max(ans,mp[sum]);
}
printf("%I64d\n",n-ans);
return 0;
}
最小操作次数平衡银行账户
本文介绍了一道算法题目,要求通过最少的操作次数使得一圈银行的账户余额全部归零。文章给出了具体的输入输出示例,并提供了AC代码实现,利用前缀和的方法解决了问题。
1334

被折叠的 条评论
为什么被折叠?



