HDU2430-Beans

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
  
  
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
 

Source
 

题意:有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 long
const 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值