codeforces 675C Money Transfers map

上面是官方题解,写的很好,然后就A了,就是找到前缀和相等的最多区间,这样就可以减去更多的1

然后肯定很多人肯定很奇怪为什么从1开始数,其实从2开始也一样,因为是个环,从哪里开始记录前缀和都一样

我们的目的是为了找到更多的区间和为0,从哪开始都一样,只是一个标准罢了

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=1e3+5;
map<LL,int>mp;
int main(){
    int n;
    scanf("%d",&n);
    int ans=n-1;
    LL sum=0;
    for(int i=0;i<n;++i){
      int t;
      scanf("%d",&t);
      sum+=t;
      ++mp[sum];
      ans=min(ans,n-mp[sum]);
    }
    printf("%d
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/shuguangzw/p/5532843.html