这题目用线段树超时了,其实也差不多应该超时。10^6大数据量。看了一下网上的解法是单调队列。大概了解了一下,是个挺有意思的数据结构。
首先,需要求满足0<=(S[r]-S[l])%p<=k时,(S[r]-S[l])的最大值。
由于S[r]>=S[l],因此即求S[r]%p-k <= S[l]%p <= S[r]%p的最优解。
单调队列可解,按S[i]%p和i排列,每次固定r,求最优的l值,l在队头。

 /* 2430 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int q, p; node_t() {}
node_t(int q, int p):
q(q), p(p) {} friend bool operator< (const node_t& a, const node_t& b) {
if (a.q == b.q)
return a.p < b.p;
return a.q < b.q;
} } node_t; const int maxn = 1e6+;
int a[maxn];
__int64 sum[maxn];
node_t nd[maxn];
int n, p, k;
int Q[maxn];
__int64 ans; void solve() {
int l = , r = ;
__int64 tmp;
ans = -; rep(i, , n+) {
while (l<=r && nd[i].p < nd[Q[r]].p)
--r;
Q[++r] = i;
while (l<=r && (nd[i].q - nd[Q[l]].q) > k)
++l;
if (l >= r)
continue;
tmp = sum[nd[i].p] - sum[nd[Q[l]].p];
ans = max(ans, tmp/p);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d %d %d", &n, &p, &k);
nd[].p = nd[].q = ;
sum[] = ;
rep(i, , n+) {
scanf("%d", &a[i]);
sum[i] = sum[i-] + a[i];
nd[i].p = i;
nd[i].q = sum[i] % p;
}
sort(nd, nd+n+);
solve();
printf("Case %d: %I64d\n", tt, ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】2430 Beans的更多相关文章

  1. 【HDOJ】【3037】Saving Beans

    排列组合 啊……这题是要求c(n-1,0)+c(n,1)+c(n+1,2)+......+c(n+m-1,m) 这个玩意……其实就等于c(n+m,m) 好吧然后就是模P……Lucas大法好= = 我S ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. [Guava官方文档翻译] 2.使用和避免使用null (Using And Avoiding Null Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3536647.html "null很恶心." -Doug Lea "这是一个令我追悔莫及的错误 ...

  2. BootstrapDialog点击空白处禁止关闭

    在乐学一百的项目当中引用到了BootstrapDialog,其中后台发送短信时,为了防止管理员编辑了半天的短息,突然间因为点击某个空白区域导致丢失,所以在此禁用掉点击空白关闭弹出框. 主要属性为: c ...

  3. MySQL5.7 linux二进制安装

    200 ? "200px" : this.width)!important;} --> 介绍 MySQL5.7出来也有大半年了,业内也一直在宣传5.7有多么的N,官网的也是宣 ...

  4. Linux C 程序 空语句-gcc编译命令(SIX)

    C语言语句1.空语句:当一个if或者while判断部分能完成工作,空语句可以用来表示if或者while内的空循环体 if(a == b ); 2.gcc编译器 //程序的编译过程: // 预处理:pr ...

  5. SharePoint2013TimerJob计时器发送邮件

    http://www.3fwork.com/b500/000307MYM008190/

  6. 使用WebClient上传文件时的一些问题

    最近在使用WebClient做一个客户端上传图片到IIS虚拟目录的程序的时候,遇到了一些问题,这里主要给出参考步骤分享给大家. 测试环境 服务器端:Windows Server 2003,IIS6.0 ...

  7. 图片裁切插件jCrop的使用心得(二)

    上一篇简单的介绍了一下开发的背景以及一些学习资料,下面开始介绍如何上手. 一.下载jCrop http://deepliquid.com/content/Jcrop_Download.html 直接去 ...

  8. PYTHON代码摘录

    文件处理 #典型的读取文件代码 row_data = {} with open('PaceData.csv') as paces: column_heading = paces.readline(). ...

  9. 求给定数据中最小的K个数

    public class MinHeap { /* * * Top K个问题,求给定数据中最小的K个数 * * 最小堆解决:堆顶元素为堆中最大元素 * * * */ private int MAX_D ...

  10. ?Object-C获取手机设备信息

    一.获取UiDevice设备信息 // 获取设备名称 NSString *name = [[UIDevice currentDevice] name]; // 获取设备系统名称 NSString *s ...