[UVA][dp] 10980 - Lowest Price in Town@Morris' Blog|PChome Online 人新台
2013-05-27 21:52:39| 人2,068| 回0 | 上一篇 | 下一篇

[UVA][dp] 10980 - Lowest Price in Town

0 收藏 0 0 站台

Problem E: Lowest Price in Town

Time limit: 1 second

It is sometimes tricky to figure out the cheapest way to buy things, even in the supermarket where the price of all goods are listed clearly. Just consider what I saw last Saturday about the price of cooking oil: (notice the difference in the sizes of the two price tags)

Having a sharp mind (a consequence of regularly taking part in online programming contests), you should have no problem in seeing that the 'buy-1-get-1-free' scheme is preferable. But what about your Mum? It is your responsibility as her son/daughter to write her a program that computes the lowest price to buy things in the supermarket, thus helps her to save money.

Input and Output

The input consists of more than a hundred test cases, each concerning a different item. The first line of each case gives the unit price of buying an item, then a non-negative integer M (≤ 20). This is followed by M lines each containing two numbers N and P (1 < N ≤ 100), which means that you can buy N such items for $P. Finally there is a line containing a list of positive integers K (≤ 100); for each of them your program should print the lowest price you need to get K items. Note that you do not have to buy exactly K items; you may consider buying more than K items, and giving the unneeded items to your dear neighbours, if you can save money in this way.

Note that all prices P given in the input are floating-point numbers in exactly 2 decimal places, with 0 < P < 1000.

Sample Input

22.00 2 2 22.00 4 60.00 2 4 25.00 2 2 48.00 2 46.00 2 22.00 2 2 22.00 4 40.00 1 2 3 

Sample Output

Case 1: Buy 2 for $22.00 Buy 4 for $44.00 Case 2: Buy 2 for $46.00 Case 3: Buy 1 for $22.00 Buy 2 for $22.00 Buy 3 for $40.00 

Problemsetter: Mak Yan Kei

最痛苦的地方在於-一直看目想表什。
首先,"一送一"。
一始,之後 合量 合,使用 Dynamic Programming 解之。

由於可以量多的方式去完成最低需求的目,因此表要建大一,估是倍以上的空,
然後取 min 值回。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
    /*freopen("in.txt", "r+t", stdin);
    freopen("out.txt", "w+t", stdout);*/
    int a, b, n, w, v, q;
    int i, j, k;
    char c;
    int cases = 0;
    while(scanf("%d.%d %d", &a, &b ,&n) == 3) {
        int m = a*100 + b;
        int dp[205] = {};
        for(i = 1; i < 205; i++)    dp[i] = 0xfffffff;
        for(i = 1; i < 205; i++) {
            dp[i] = min(dp[i], dp[i-1]+m);
        }
        for(i = 0; i < n; i++) {
            scanf("%d %d.%d", &w, &a, &b);
            v = a*100 + b;
            for(j = w; j <= 200; j++)
                dp[j] = min(dp[j], dp[j-w]+v);
        }
        for(j = 200; j >= 0; j--)
            dp[j] = min(dp[j],dp[j+1]);
        printf("Case %d:\n", ++cases);
        while(getchar() != '\n');
        string line;
        getline(cin, line);
        stringstream sin(line);
        while(sin >> q) {
            printf("Buy %d for $%d.%02d\n", q, dp[q]/100, dp[q]%100);
        }
    }
    return 0;
}

台: Morris
人(2,068) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][合dp] 11026 - A Grouping Problem
此分上一篇:[UVA][2SAT] 10319 - Manhattan

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86