HDU2430-Beans

来源:互联网 发布:java开发实战1200 编辑:程序博客网 时间:2024/05/13 05:14

Beans

                                                                            Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                       Total Submission(s): 1362    Accepted Submission(s): 399


Problem Description
      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
310 20 100 3 1 8 19 39 2 9 1 83 100 1032 34 231 5 31
 

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

Source
2008 Asia Chengdu Regional Contest Online
 

题意:有n堆豌豆,每堆都有w[i]个,现在要从中选择连续的若干堆,然后用一个能装p个豌豆的背包装豆子,直到剩下的豌豆数量<=p为止,并且剩下的豌豆数量不大于k,问最多能装多少包。

解题思路:题意就是是求(sum[i] - sum[j](i != j)) % p <= k && 使得sum[i] - sum[j]最大。可以按sum[i] % p,给数组排序,用单调队列来解决这个问题。


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;struct node{    int id,mod;}a[2000010],q[2000010];int sum[2000010];int cmp(node a,node b){    if(a.mod!=b.mod) return a.mod<b.mod;    return a.id<b.id;}int main(){    int t,n,p,k,cas=0;    scanf("%d",&t);    while(t--)    {        sum[0]=0;        scanf("%d %d %d",&n,&p,&k);        for(int i=1; i<=n; i++)        {            scanf("%d",&sum[i]);            sum[i]=sum[i]+sum[i-1];            a[i].mod=sum[i]%p,a[i].id=i;        }        sort(a+1,a+n+1,cmp);        int ans=-1,head=0,rear=0;        for(int i=1; i<=n; i++)        {            while(head<rear&&a[i].id<q[rear-1].id) rear--;            q[rear++]=a[i];            while(head<rear&&a[i].mod-q[head].mod>k) head++;            int x=a[i].id;            if(sum[x]%p<=k) ans=max(ans,sum[x]/p);            else if(head+1<rear) ans=max(ans,(sum[x]-sum[q[head].id])/p);        }        printf("Case %d: %d\n",++cas,ans);    }    return 0;}

0 0
原创粉丝点击