rt,就是:
算出所有区间和。
把相同区间和的区间做一遍贪心。
看上去好像是 O(n4) 的,但是不知道怎么的跑过去了这道题目。请问这个复杂度到底是多少,或者说,是数据水了?
# include <bits/stdc++.h>
# define int long long
# define pb push_back
using namespace std;
const int N = 2005;
int a[N],n,sum[N],ans;
map < int , vector< pair<int,int> > > sm;
vector < pair<int,int> > czzl;
int zy[N * N] , cnt;
signed main () {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n;
for (int i = 1;i <= n;++i) cin >> a[i],sum[i] = sum[i - 1] + a[i];
for (int l = 1;l <= n;++l) {
for (int r = l;r <= n;++r) {
sm[sum[r] - sum[l - 1]].pb({r,l});
zy[++cnt] = sum[r] - sum[l - 1];
}
}
sort (zy + 1,zy + cnt + 1);
int len = unique(zy + 1,zy + cnt + 1) - zy - 1;
cnt = len;
for (int i = 1;i <= cnt;++i) {
sort (sm[zy[i]].begin(),sm[zy[i]].end());
int nl = sm[zy[i]][0].second,nr = sm[zy[i]][0].first,cnt = 1;
vector < pair<int,int> > czl;
czl.pb({nl,nr});
for (int j = 1;j < sm[zy[i]].size();++j) {
pair<int,int> now = sm[zy[i]][j];
if (now.second > nr) nl = min(nl,now.second),nr = max(nr,now.first),++cnt,czl.pb({now.second,now.first});
}
if (cnt > ans) {
ans = cnt;
czzl.clear();
for (auto x : czl) czzl.pb({x.first,x.second});
}
}
cout << ans << "\n";
for (auto x : czzl) cout << x.first << " " << x.second << "\n";
return 0;
}