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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章