zoukankan      html  css  js  c++  java
  • Codeforces Round #353 (Div. 2) C. Money Transfers

    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
    Copy
    3
    5 0 -5
    output
    Copy
    1
    input
    Copy
    4
    -1 0 1 0
    output
    Copy
    2
    input
    Copy
    4
    1 2 3 -6
    output
    Copy
    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:

    1. transfer 1 from the first bank to the second bank;
    2. transfer 3 from the second bank to the third;
    3. transfer 6 from the third bank to the fourth.

    题意:

      n家银行,相邻两个银行可以互相周转要使得最后每家银行欠款为0,问最少交换次数。

    分析:

      最多最多需要n-1次,推一下可以发现,1~n中每有一个区间内的和为0,交换次数就减一。所以求一下区间和为0的区间有几个就行了。

      然后可以推一下得一个结论就是前缀和中出现次数最多的数就是区间内有几个区间为0。

      

    ///  author:Kissheart  ///
    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    #include<vector>
    #include<stdlib.h>
    #include<math.h>
    #include<queue>
    #include<deque>
    #include<ctype.h>
    #include<map>
    #include<set>
    #include<stack>
    #include<string>
    #define INF 0x3f3f3f3f
    #define FAST_IO ios::sync_with_stdio(false)
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int MAX=1e5+10;
    const int mod=1e9+7;
    typedef long long ll;
    using namespace std;
    #define gcd(a,b) __gcd(a,b)
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
    inline ll inv1(ll b){return qpow(b,mod-2);}
    inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
    inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
    //freopen( "in.txt" , "r" , stdin );
    //freopen( "data.txt" , "w" , stdout );
    ll n;
    map<ll,ll>mp;
    int main()
    {
        scanf("%lld",&n);
        ll ans=n-1,sum=0;
        for(ll i=1;i<=n;i++)
        {
            ll x;
            scanf("%lld",&x);
            sum+=x;
            mp[sum]++;
            ans=min(ans,n-mp[sum]);
        }
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    THINKphp学习笔记
    Js获取当前日期时间及其它操作
    Oracle数据导入导出imp/eXP
    SQL中的单记录函数
    Windows服务C#/VS2003
    oracle数据库开发的一些经验积累
    Oracle 数据库的安全策略
    高兴
    无法正确运行的C#程序
    最详细的Visual C++ 2008 Express Edition使用方法(图文)
  • 原文地址:https://www.cnblogs.com/Kissheart/p/10453079.html
Copyright © 2011-2022 走看看