Codeforces 675C Money Transfers【贪心】

题目链接:

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值