題意:
n個負擔着不同債務的銀行圍成環,每次每個銀行只能向自己左邊或者右邊的銀行轉移資金。所有銀行的債務和爲0,問你最少轉移多少次能讓所有銀行債務都爲0。
分析:
n個銀行,最多n-1次即可將所有銀行債務變爲0,即按順序從第一個開始向後一個銀行轉移債務即可,到第n個時所有債務必爲0。
在此轉移過程中可以發現任何一個爲0的區間都可以減少1次轉移,即該區間和爲0時,右邊界不需要向後一個銀行繼續轉移債務。
數出存在多少個0區間即可得到答案爲n-0區間個數。
假設區間[i,j]和爲0,那麼[1,i-1]的區間和與[1,j]和相同。
通過map存下區間和大小出現的次數,即可得到哪些區間爲0。
但從1-n遞推時是否沒考慮到環?由於計算[1,x](x從1-n)的區間和並存入map中,因此存在環的情況並不影響和的出現。
思路來自:http://m.blog.csdn.net/article/details?id=51438865
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=100005;
const int mod=1e9+7;
int a;
map<long long, int> findsum;
int main() {
int n;
long long sum=0;
int ans=0;
cin>>n;
for (int i=0; i<n; i++) {
scanf("%d",&a);
sum+=a;
ans=max(ans, ++findsum[sum]);
}
cout<<n-ans<<endl;
return 0;
}