Codeforces Round #353 (Div. 2) C. Money Transfers 数学

C. Money Transfers

题目连接:

http://www.codeforces.com/contest/675/problem/C

Description

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.

Sample Input

3
5 0 -5

Sample Output

1

Hint

题意

现在有n个银行,每个银行有a[i]元,可以像周围的银行转移钱

你的目标是使得所有银行钱的个数都是0,问你最少操作多少次

题解:

原题,hdu 2590

考虑一个连续的段,长度为l,如果该段的区间和为0的话,那么就操作l-1次就够了

现在有n个数,你分成了k段,那么你就需要操作n-k次

那么只要k最大就好了

然后暴力枚举前缀和相等的,取最大就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int a[maxn];
map<long long,long long>cnt;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    long long sum=0,mx=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i];
        mx=max(mx,++cnt[sum]);
    }
    cout<<n-mx<<endl;
}
posted @ 2016-05-17 21:46  qscqesze  阅读(436)  评论(0编辑  收藏  举报