bzoj3544

set+贪心

感觉当div2C挺好的...

set维护前缀和%m,当前答案为sum[r]-sum[l-1],我们当然希望sum[l-1]是sum[r]的后继或者最小的数,所以求出来比较一下就行了

#include<bits/stdc++.h>
using namespace std;
int n;
long long ans = -(1ll << 60), sum, m;
set<long long> s;
int main()
{
    cin >> n >> m;
    s.insert(0);
    for(int i = 1; i <= n; ++i)
    {
        long long x;
        scanf("%lld", &x);
        sum = ((sum + x) % m + m) % m;
        set<long long> :: iterator it = s.upper_bound(sum);    
        if(it != s.end()) ans = max(ans, ((sum - *it) % m + m) % m);
        ans = max(ans, (sum - *(s.begin()) + m % m) % m);
        s.insert(sum);
    }
    cout << ans << endl;
    return 0;
}
View Code

 

posted @ 2017-09-07 19:06  19992147  阅读(173)  评论(0编辑  收藏  举报