TOJ-1321 The Brick Stops Here

You have been hired by several clients of a factory that manufactures brass bricks. Brass is an alloy of copper and zinc; each brick weighs 1000 grams, and the copper content of a brick can range from 1 to 999 grams. (Note that brass with less than 55% or more than 62% of copper is practically useless; however, this is irrelevant for this question) The factory manufactures, through various processes, different types of brick, each of which has a different copper concentration and price. It distributes a catalog of these types to its customers.

Your clients desire to buy a certain number (M) of bricks, which for, uh, religious reasons must be of different types. They will be melted together, and the resultant mixture must have a concentration of at least CMin and at most CMax grams of copper per kilogram. Their goal is to pick the M types of brick so that the mixture has the correct concentration and the price of the collection is minimized. You must figure out how to do this. M, CMin, and CMax will vary depending on the client.

Input

The first part of input consists of a line containing a number N (1 ≤ N ≤ 200), the number of brick types, and then N lines containing the copper concentration (between 1 and 999) and price (in cents) of each brick type. No brick costs more than 10 dollars.

The second part consists of a line containing a number C (1 ≤ C ≤ 100), the number of clients you are serving, followed by C lines containing M (1 ≤ M ≤ 20), CMin (1 ≤ CMin ≤ 999), and CMax (1 ≤ CMax ≤ 999) for each client.

All input numbers will be positive integers.

Output

Output consists of a line for each client containing the minimum possible price for which they can purchase bricks to meet their demands. If there is no way to match their specifications, output "impossible".

Sample Input

11
550 300
550 200
700 340
300 140
600 780
930 785
730 280
678 420
999 900
485 390
888 800
3
2 500 620
9 550 590
9 610 620

Sample Output

420
impossible
3635

Source: Waterloo
Local Contest Jun. 19, 1999

dp过程:

dp[0][0] = 0;//dp[i][j]表示i块砖铜总量j最小价格
        d = min(20,n);
        for(k=1;k<=n;k++){
            for(j=20000;j>=con[k];j--){
                for(i=1;i<=d;i++){
                    if(dp[i-1][j-con[k]]!=INF){
                        dp[i][j] = min(dp[i][j],dp[i-1][j-con[k]]+prc[k]);
                    }
                }
            }
        }

开始时先循环i后循环j,结果导致每个铜砖被重复使用。

全部代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <memory.h>
using namespace std;
const int INF=0x3f3f3f3f;
int con[202],prc[202],dp[22][20002];
int n,c,m,cmin,cmax;
int main(){
    int i,j,k,d;
    while(~scanf("%d",&n)){
        memset(dp,INF,sizeof(dp));
        for(i=1;i<=n;i++){
            scanf("%d%d",&con[i],&prc[i]);
        }
        dp[0][0] = 0;//dp[i][j]表示i块砖铜总量j最小价格
        d = min(20,n);
        for(k=1;k<=n;k++){
            for(j=20000;j>=con[k];j--){
                for(i=1;i<=d;i++){
                    if(dp[i-1][j-con[k]]!=INF){
                        dp[i][j] = min(dp[i][j],dp[i-1][j-con[k]]+prc[k]);
                    }
                }
            }
        }
        /*for(i=0;i<=d;i++){
            for(j=0;j<=20000;j++){
                if(dp[i][j]==INF) continue;
                else printf("%d %d %d   ",i,j,dp[i][j]);
            }
            printf("\n");
        }*/
        scanf("%d",&c);
        while(c--){
            scanf("%d%d%d",&m,&cmin,&cmax);
            if(m>n)    printf("impossible\n");
            else {
                int ans = INF;
                int l = m*cmin;    int h = m*cmax;
                for(i=l;i<=h;i++){
                    if(dp[m][i]<ans){
                        ans = dp[m][i];
                    }
                }
                if(ans!=INF)    printf("%d\n",ans);
                else printf("impossible\n");
            }
        }

    }
    return 0;
}
时间: 2024-12-19 05:38:21

TOJ-1321 The Brick Stops Here的相关文章

poj2642 The Brick Stops Here(DP基础题)

比基础的多一点东西的背包问题. 链接:POJ2642 大意:有N种砖,每种花费p[i],含铜量c[i],现需要用M种不同的砖融成含铜量在Cmin到Cmax之间(可等于)的砖,即这M种砖的含铜量平均值在这个范围内,求最小花费.(M.Cmin.Cmax有多种需求,分别输出花费) 题解: DP, f[i][j]表示选i种砖,含铜量的和为j时的最小花费.这样在询问M.Cmin.Cmax之前,先将各种砖数.组成各种含铜量的花费都算好. DP方程:f[k][j]=min(f[k][j],f[k-1][j-c

ZOJ 3299-Fall the Brick(线段树+离散化)

题意: n个区间 ,给出区间的左右坐标 ,区间内填满宽度为1的箱子,有m个板子给出板子的高度和左右坐标(同高度不重叠) 所有箱子从上向下落,求每块板子能接到的箱子数. 分析: 首先给的区间很大,一开始直接存ME了,所以要先把给定的区间离散化 箱子的宽度是1则使维护区间左闭右开,才能得正确的数量. #include <map> #include <set> #include <list> #include <cmath> #include <queue&

poj 1321

A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1321 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每

[LeetCode] Brick Wall 砖头墙壁

There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the leastbricks. The brick wall

POJ 1321 棋盘问题 --- DFS

POJ 1321 棋盘问题 http://poj.org/problem?id=1321 /*POJ 1321 棋盘问题 --- DFS*/ #include <cstdio> #include <cstring> int n, k, cnt; bool visit[10]; //标记列的访问状态 char mapp[10][10]; /*从第r行开始正确放置p个棋子*/ void dfs(int r, int p){ if (p == 0){ ++cnt; return; } i

TOJ 2850 Corn Fields 状压dp

Source: http://acm.tju.edu.cn/toj/showp.php?pid=2850 题意:n*m的土地上种东西,每个位置分为可以种和不能,种的方案要求不能相邻地种,问合法方案数.(写得有点乱) 分析:做过炮兵阵地,这题就是秒杀了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int mo = 1000000

TOJ 2294 POJ 3286 How many 0&#39;s? 数位dp

http://acm.tju.edu.cn/toj/showp2294.html http://poj.org/problem?id=3284 题外话:集训结束,回学校了.在宿舍看了这题,没什么好想法,去洗澡了.转了两个澡堂都特么没开..倒是在路上把这题想了.用回自己的电脑,不得不说苹果的字体渲染,真心高一个等级. 题意:给定两个数a和b,从a写到b,问一共写了多少个0. 分析:当然先转化为求0..a写多少个0.网上有更简单的做法,就是枚举每位作为0,则如果这一位本来是0,左边取1..a-1(不

MySQL 主主报错: Fatal error: The slave I/O thread stops because master and slave have

Mysql 主主启动错误处理 error 信息: Slave_IO_State: Master_Host: 192.168.6.87 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 106 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 4 Relay_

TOJ 4105 Lines Counting(离线树状数组)

4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Runs: 47 On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two interval