Mr. Pote's shop sells beans now. He has N bags of beans in his warehouse, and he has numbered them with 1, 2, …, N according to their expired dates. The i-th bag contains Wi units of beans. For selling at retail makes only a little profit, Mr. Pote want to pack beans in small packets with certain size and sell them in packets. Here comes his packing way:
      Suppose the size of the packet is P units. Firstly, Mr. Pote selects some bags (at least one) of beans with consecutive number in his warehouse. Then he takes out the beans from all selected bags, and puts them together on the desktop. To pack the beans, he take P units of beans from desktop and fill in a new packet each time, until the beans left are less than P units. Finally the beans left on the desktop are eaten by a lucky dog.

      Mr. Pote doesn't want the dog eat too many beans, so he prefers to solutions that resulting no more than K units of beans eaten by the dog. Moreover, he also wants to pack as many packets as possible. Could you tell him how many packets he can pack at most without breaking his preference?

Input      On the first line of input, there is a single positive integer T <= 20 specifying the number of test cases to follow.

      Each test case contains two lines.

      There are three integers in the first line, N, P, K as described above. (0 < N, P < 1000001, 0 <= K < P)

      Next follow a line with N integers W1, W2, ..., WN. The i-th integers describes the amount of beans in the bags numbered i. (0 <= Wi < 32768)

      Numbers are separated by spaces.

Output      For each test case you should output a single line containing "Case X: Y" (quotes for clarity) where X is the number of the test case (starting at 1) and Y is the maximum number of packets that Mr. Pote can pack following his way.

      In case there's no solution avoiding the dog eats more than K units of beans, Y should be equal to -1.

Sample Input

3
10 20 10
0 3 1 8 19 39 2 9 1 8
3 100 10
32 34 23
1 5 3
1

Sample Output

Case 1: 4
Case 2: -1
Case 3: 0

题意:

先t组输入,之后输入n、p、k

n:有n袋豆子

p:重新装袋后每袋中豆子的数量

k:狗粮不能超过多少豆子

后边在输入n袋豆子中,每一袋里面豆子的数量

让你从n袋豆子中挑选出来连续的袋子,再将所有豆子重装进p数量的袋子,问最多能装多少袋(在狗粮不超过k的情况下)

题解:

详解原文:传送门

首先我们要求选出来连续的袋子,那么肯定要预处理一下前缀和(这里记为sum[i],rem[i]=sum[i]%p)

我们就是再求(sum[i]-sum[j])/p  (i>j)的最大值,要保证(sum[i]-sum[j])%p<=k

当sum[i]>=sum[j]时:

可化简至:sum[i]%p-sum[j]%p<=k  ===>>>   rem[i]-rem[j]<=k

只需要在满足此条件下,找到最大的sum[i]-sum[j]就可以了,而且这一点还可以用单调递增队列来维护,每次取队头来和sum[i]做计算就可以了

当sum[i]<sum[j]时

有sum[i]%p-sum[j]%p+p<=k   =====>>>>     rem[i]-rem[j]+p<=k    =====>>>>       rem[i]<=k+(rem[j]-p)

因为rem<p 所以   rem[i]<k

而且(sum[i]-sum[j])<sum[i]

所以我们可以处理一下前缀和sum[i],来取最大的sum[i]/p

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 const int maxn=1e5+10;
7 struct shudui
8 {
9 int sum,id,rem;
10 }m[maxn];
11 bool mmp(shudui x,shudui y)
12 {
13 if(x.rem==y.rem)
14 return x.id<y.id;
15 else return x.rem<y.rem;
16 }
17 int que[maxn];
18 int main()
19 {
20 int t,tt=0;
21 scanf("%d",&t);
22 while(t--)
23 {
24 int n,p,k;
25 scanf("%d%d%d",&n,&p,&k);
26 m[0].sum=0;
27 for(int i=1;i<=n;++i)
28 {
29 int q;
30 scanf("%d",&q);
31 m[i].sum=m[i-1].sum+q;
32 m[i].rem=m[i].sum%p;
33 m[i].id=i;
34 }
35 sort(m+1,m+1+n,mmp);
36 int s=1,e=0,ans=0,flag=0;
37 for(int i=1;i<=n;++i)
38 {
39 while(e>=s && m[que[e]].id>m[i].id)
40 e--;
41 while(e>=s && m[i].rem-m[que[s]].rem>k)
42 s++;
43 que[++e]=i;
44 if(m[i].rem<=k)
45 ans=max(ans,m[i].sum/p),flag=1;
46 if(e>s && m[i].rem-m[que[s]].rem<=k)
47 ans=max(ans,(m[i].sum-m[que[s]].sum)/p),flag=1;
48 }
49 if(flag)
50 printf("Case %d: %d\n",++tt,ans);
51 else printf("Case %d: -1\n",++tt);
52 }
53 return 0;
54 }

hdu2430Beans(单调队列)的更多相关文章

  1. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  2. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  3. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  4. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  5. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  6. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  7. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

  9. hdu3530 单调队列

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  10. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

随机推荐

  1. 【RAC】10grac添加节点,详细步骤

    RAC物理结构 现在的RAC环境是二个节点: dbp,dbs, 这个实验就是添加节点dbi. dbp,dbs和dbi节点的信息规划如下: 服务器主机名 dbp dbs dbi 公共IP地址(eth0) ...

  2. ctfhub技能树—RCE—命令注入

    打开靶机 查看页面信息 输入127.0.0.1进行测试 构造payload 127.0.0.1&ls 查看文件内容信息 127.0.0.1 & cat 179852221619745. ...

  3. vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】

    vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...

  4. 为什么不建议用var

    看了这个例子估计你就会明白了 var a = 'global'; function test() { if (!a) { var a = 'part'; } console.log(a); } tes ...

  5. SAP 修改数据元素 注意事项

    在修改数据元素的时候,通常要注意一下几点: 1.在修改完数据元素后,如果激活不成功,那么就要通过SE14进入数据库实用程序,在对象名处输入数据元素相关联的表的名称 下面词典对象选择表,然后点击编辑,处 ...

  6. [Cerc2005]Knights of the Round Table

    题目描述 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少有3个骑士参加,且相互憎恨的骑士不能坐在圆桌的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是大于1的奇数,以防 ...

  7. python工业互联网应用实战3—Django Admin列表

    Django Admin笔者使用下来可以说是Django框架的开发利器,业务model构建完成后,我们就能快速的构建一个增删查改的后台管理框架.对于大量的企业管理业务开发来说,可以快速的构建一个可发布 ...

  8. (04)-Python3之--字典(dict)操作

    1.定义 字典的关键字:dict 字典由多个键和其对应的值构成的 键-值 对组成,每个键值对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中. {key1:value1 ...

  9. 基于go-cqhttp实现QQ机器人

    本篇文章记录一下自己在编写QQ机器人的时候所遇到的一些问题和核心功能的实现. QQ机器人RabbitBot采用python编写,由于是个人学习使用,故目前不会开源完整代码,只会放出核心代码供学习参考. ...

  10. 【汇编实践】go assembly

    https://mp.weixin.qq.com/s/B577CdUkWCp_XgUc1VVvSQ asmshare/layout.md at master · cch123/asmshare htt ...