cf645c Money Transfers(貪心)

想了很久,隔了一段時間,又想了很久,還是不會,看別人的代碼,簡直嚇哭了。。。。。。

妙,

題意相當於:
給定一個區間[1,n],區間首尾相連,最多把它分成幾塊,使得每一塊的和都是0.(假設答案是p,那麼n-p就是這道題的答案)
所以維護一個前綴和,然後前綴和出現的最多次數就是答案,
突然覺得好蠢。。。。。。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <stack>
#include <vector>
#include <string.h>
#include <queue>
#define msc(X) memset(X,-1,sizeof(X))
#define ms(X) memset(X,0,sizeof(X))
typedef long long LL;
using namespace std;
const int MAXN=1e5+5;
int bank[MAXN];
int main(int argc, char const *argv[])
{
    int n,res=0;
    LL sum=0ll;
    map<LL,int> mmp;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",bank+i);
        sum+=bank[i];
        if(++mmp[sum]>res)
            res=mmp[sum];
    }
    printf("%d\n", n-res);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章