bzoj 3544: [ONTAK2010]Creative Accounting (STL)

3544: [ONTAK2010]Creative Accounting

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 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

Sample Output

11


HINT

【数据范围】

N<=200000,M,a_i<=10^18

Source

[ Submit][ Status][ Discuss]

题解: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);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值