Codeforces Round #353 (Div. 2) C. Money Transfers
May 31, 2016
Codeforces
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.
Solution
Let the array be: a[n], n is the lengh of the array.
cut the original circle array into linear segments in the following rule:
each segment can contain only a single element `0`, or its first and last
elements are non-zero number. Note a segment can contain suffix of original
array appended by prefix of the original array.
For example:
Original array is: 0 1 0 0 0 -1 0
, there are two ways to cut it:
0
,1 0 0 0 -1
,0
0
,0
,0
,-1 0 0 1
Now, we cut the original array into small liner segments, let their length be len0, len1, len2, …, lenk. We assume there are k segments. to make all elements in each segments to be 0, we must take leni - 1 operations. for i from 0 to k, ∑(leni -1) = n - k, the total number of operations is n - k, where k is the number of segments. To make n - k minimum, we can find the biggest k. In other words, we must find the biggest number of segments which satisfy above requirements.
let sum(i, j) represents the sum of elements a[i..j], if i > j, it represents the sum of elements a[i..n-1, 0..j]
let pre_sum(i) represents the sum of elements a[0..i].
Now, we prove there is a one-to-one map from the number of the same pre_sum(i) to the number of segments whose sum is zero.
let the the number of the same pre_sum(i) be N.
if N == 2:
then if pre_sum(i) == pre_sum(j) → sum(i+1, j-1) == 0, so there are two segments whose sum is zero: a[i+1..j-1] and a[j..n-1, 0..i]
if N == 3:
if pre_sum(i) == pre_sum(j) == pre_sum(k) and i < j < k, then: sum(i+1, j-1) == sum(j+1, k-1) == 0. Besides, because sum(0,n-1) == 0, so sum(k+1, n-1) + sum(0, i) == 0. So there are three segments whose sum is zero: a[i+1..j-1], a[j+1..k-1], a[k+1..n-1, 0..i].
We can prove it the say way if N ⩾ 3.
So, we prove there is one-to-one map from the number of the same pre_sum(i) to the number of segments whose sum is zero. In order to find the biggest number of segments, we can find the biggest number of the same pre_sum(i).
Problem solved. :joy: :joy:
Code
int n, a[100000+10], segNumMax;
map<ll,int> preSum;
int
main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("c.in", "r", stdin);
#endif /* ----- ONLINE_JUDGE ----- */
while (~scanf("%d", &n)) {
preSum.clear();
segNumMax = 0;
ll sum = 0;
rep(i, 0, n) {
scanf("%d", a + i);
sum += a[i];
preSum[sum]++;
segNumMax = max(preSum[sum], segNumMax);
}
printf ( "%d\n", n - segNumMax );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
日常锻炼
事实上,跑之前我根本不知道能不能跑完,然而,最终,还是跑完了……很多事情就是这样吧。 :joy: :joy: