BZOJ3544 [ONTAK2010]Creative Accounting

看不懂题,就不能写的稍微像人话点吗我去。。。

题目就是要找一段区间使得Σai mod m的值最大。

于是嘛。。。前缀和一下再贪心就好了。

先求出前i个数的前缀和s,然后用s更新解。

还有可能就是前面的某个前缀和s1刚好在mod m意义下大于s且是最小的一个,那么这一段的和就是m + s - s1,再用它来更新解。

 1 /**************************************************************
 2     Problem: 3544
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:2056 ms
 7     Memory:7012 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <algorithm>
12 #include <set>
13
14 using namespace std;
15 typedef long long ll;
16 int n;
17 ll mod, s, a, ans, p;
18 set <ll> S;
19
20 inline ll read(){
21     ll x = 0, sgn = 1;
22     char ch = getchar();
23     while (ch < ‘0‘ || ch > ‘9‘){
24         if (ch == ‘-‘) sgn = -1;
25         ch = getchar();
26     }
27     while (ch >= ‘0‘ && ch <= ‘9‘){
28         x = (ll) x * 10 + ch - ‘0‘;
29         ch = getchar();
30     }
31     return sgn * x;
32 }
33
34 int main(){
35     scanf("%d", &n); mod = read();
36     for (int i = 1; i <= n; ++i){
37         a = read();
38         if (a < 0) a += (abs(a / mod) + 1) * mod;
39         s += a, s %= mod;
40         ans = max(ans, s);
41         if (S.upper_bound(s) != S.end()){
42             p = *(S.upper_bound(s));
43             ans = max(ans, (s + mod - p) % mod);
44         }
45         S.insert(s);
46     }
47     printf("%lld\n", ans);
48     return 0;
49 }

(p.s. 无耻的我又用了set。。。)

时间: 2024-10-05 05:02:08

BZOJ3544 [ONTAK2010]Creative Accounting的相关文章

【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set

[BZOJ3544][ONTAK2010]Creative Accounting 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

BZOJ 3544 ONTAK 2010 Creative Accounting 贪心+平衡树

题目大意:给出一段区间,和一个树p,请找出一段区间,使得这段区间和%p的值最大. 思路:利用前缀和的思想,用set维护出现过的所有的前缀和.对于一个前缀和m来说,如果之前出现过(m + 1) % p是最好的,这样就可以达到最大.所以就找之前出现过比(m + 1)大的数,如果没有就贪心的取begin().然后更新答案. 负数取模还是要好好搞搞. CODE: #include <set> #include <cstdio> #include <cstring> #inclu

noip知识点总结之--贪心

一.什么是贪心 贪心算法嘛... 就是在对某个问题求解时,总是做出在当前看来是最好的选择 In other wors,并不是从整体最优上加以考虑,而是在获得某种意义上的局部最优解 二.贪心算法的适用前提 局部的最优解能导致最后整体的最优解,即局部的最优解不受该部分以外的东西的影响 对于贪心算法,我们需要证明:整个问题的最优解一定由在贪心策略中存在的子问题的最优解得来的 实际上,能用贪心算法的问题很少,大部分看上去能用贪心算法去做的题目,其实都得不到最优解T T(这时候就需要运用动态规划了) 而看

AAA(Authentication, Authorization, Accounting)

1. Introduction Managing network access using only the user mode or privilege mode password commands is limited and does not scale well. Instead, using the Authentication, Authorization, and Accounting (AAA) protocol provides the necessary framework

POJ 2586 Y2K Accounting Bug(枚举大水题)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10674   Accepted: 5344 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc

poj 2586 Y2K Accounting Bug (贪心)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9632   Accepted: 4806 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

TOJ-1335 Y2K Accounting Bug

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc. All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS In

Codeforces Gym 100513G G. FacePalm Accounting

G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/G Description An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to partic

bzoj3545 [ONTAK2010]Peaks

3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 572  Solved: 164[Submit][Status] Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰,如果无解输出-1. I