HDU2430-Beans

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



这道题我是用单调队列,现根据前缀和的余数进行排序,单调队列放的是原下标。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1000000+10;
struct node{
    int mod,pos;
    node(int mod = 0,int pos=0):mod(mod),pos(pos){}
    friend bool operator <(node a,node b){
        if(a.mod != b.mod) return a.mod < b.mod;
        else return a.pos < b.pos;
    }
};
int n,p,k;
long long sum[maxn],num[maxn];
int que[maxn];
vector<node> vn;
int main(){
    int ncase,T=1;
    cin >> ncase;
    while(ncase--){
        vn.clear();
        sum[0] = 0;
        vn.push_back(node(0,0));
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 1; i <= n; i++){
            scanf("%lld",&num[i]);
            sum[i] = sum[i-1]+num[i];
            vn.push_back(node(sum[i]%p,i));
        }
        sort(vn.begin(),vn.end());
        int head = 1,tail = 0;
        long long ans = -1;
        que[head] = 0;
        for(int i = 0 ; i <= n; i++){
            while(head <= tail && vn[i].pos < vn[que[tail]].pos) tail--;
            que[++tail] = i;
            while(head <= tail && (vn[i].mod - vn[que[head]].mod) > k) head++;
            if(head==tail) continue;
            long long res = sum[vn[i].pos] - sum[vn[que[head]].pos];
            ans = max(res/p,ans);
        }
        printf("Case %d: %lld\n",T++,ans);
    }
    return 0;
}

HDU2430-Beans,布布扣,bubuko.com

时间: 2024-10-10 14:06:25

HDU2430-Beans的相关文章

hdu2430 Beans 单调队列

// hdu2430 Beans 单调队列 // // 题目意思: // 求一个sum%p<=k的max(sum/p) // // 结题报告: // 技巧,先求出前缀和,并记录前i项对p取余的值记为x,并记下位置pos // 按照先按x从小到大,如果x相同按pos从小到大排序.这样,问题就转换为 // 求一个最小的pos使得pos到i的值最大. // // 单调队列里面保持的就是对于当前的i最小的pos值(满足pos到i区间的sum是满足条件的) // 因为数组递增,这样求得的sum一定是最大的

Spring之 Auto-Wiring All Beans of Compatible Type

Auto-Wiring All Beans of Compatible Type @Autowired 注解按类型(type)依赖入住的时候,可以把类型兼容的所有类注入到数组.链表.map等集合数据结构中.如:mybatis中TypeHandler为例: package com.doctor.practice01; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.Resul

XML中&lt;beans&gt;属性

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring

springsecurity启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.

在换了spring-security的jar包以后启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.Please update your schema declarations to the 3.0.3 schema (spring-securi

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;App&#39; is defined

工具:Eclipse mars 环境:jdk1.8 说明:这是在学习Spring Task时遇到的一个bug,代码如下: 定时任务类: package com.task.test; import java.util.Date; import org.springframework.stereotype.Component; @Component public class App { public void execute1(){ System.out.printf("Task: %s, Curr

spring beans源码解读

spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.annotation, 支持包,提供对java 5注解处理bean样式的支持.org.springframework.beans.factory, 实现spring轻量级IoC容器的核心包.org.springframework.beans.factory.access, 定位和获取bean工程的辅助工具类

异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundExcepti

hdu2845——Beans

Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3011    Accepted Submission(s): 1450 Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled w

关于命名空间beans错误

命名空间自动生成xmlns:beans="http://www.springframework.org/schema/beans"  测试正常 加载tomcat会报出   cvc-elt.1: 找不到元素 'beans' 的声明    的异常 修改成      xmlns="http://www.springframework.org/schema/beans"

源码跟读,Spring是如何解析和加载xml中配置的beans

Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086cd440b7799a63 1.配置启动Spring所需的监听器 web.xml中配置监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-cla