Codeforces 675C Money Transfers【贪心】

来源:互联网 发布:软件外包项目管理制度 编辑:程序博客网 时间:2024/05/22 09:03

题目链接:

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

题意:

给定几个数,有正有负,每个数可以向相邻的数转移,问最少的转移次数使得最后所有数均为0。

分析:

我们可以将数列化为几个连续的区间,其中每个区间的数和为0,且在区间长度为K的区间中,操作数为K-1,我们就是要最大化这样的区间个数。可以维护一个前缀和,这样两个相同的前缀之间的区间和即为0。

代码:

/*On a hill is a tree, on a tree is a bough;My heart for the Lord, but he never knows.--Created by jiangyuzhu--2016/5/17*/#include<cstdio>#include<iostream>#include<queue>#include<cstring>#include<stack>#include<vector>#include<algorithm>#include<map>#include<set>#include<cmath>using namespace std;#define pr(x) cout << #x << ": " << x << "  "#define pl(x) cout << #x << ": " << x << endl;#define sa(x) scanf("%d",&(x))#define sal(x) scanf("%I64d",&(x))#define mdzz cout<<"mdzz"<<endl;typedef long long ll;const int maxn = 1e5 + 5, mod = 1e9 + 7;ll a[maxn];map<ll, int>m;int main(void){    int n;sa(n);    int ans = 0;    ll tot = 0;    for(int i = 0; i < n; i++){        sal(a[i]);        tot += a[i];        m[tot]++;        ans = max(ans, m[tot]);    }    printf("%d", n - ans);   return 0;}
0 0