3544: [ONTAK2010]Creative Accounting
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 548 Solved: 265
[ Submit][ Status][ Discuss]
Description
给定一个长度为N的数组a和M,求一个区间[l,r],使得(\sum_{i=l}^{r}{a_i}) mod M的值最大,求出这个值,注意这里的mod是数学上的mod
Input
第一行两个整数N,M。
第二行N个整数a_i。
Output
输出一行,表示答案。
Sample Input
5 13
10 9 5 -5 7
10 9 5 -5 7
Sample Output
11
HINT
【数据范围】
N<=200000,M,a_i<=10^18
Source
题解:STL
用set维护每一位的前缀和,如果未加入这一位之前,set中有比他大的,那么就弹出比他大的第一个,否则弹出set中最小的元素。
#include<iostream>
#include<cstdio>
#include<set>
#define LL long long
using namespace std;
int n;
LL a,m,mx;
set<LL> p;
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%I64d",&n,&m);
LL ans=0,t; p.insert(ans);
for (int i=1;i<=n;i++) {
scanf("%I64d",&a); a=(a%m+m)%m;
ans=(ans+a%m)%m;
if (p.upper_bound(ans)!=p.end()) t=*p.upper_bound(ans);
else t=*p.begin();
mx=max(mx,(ans-t+m)%m); //cout<<ans<<" "<<t<<endl;
p.insert(ans); //cout<<ans<<endl;
}
printf("%I64d\n",mx);
}