CF19b: Checkout Assistant(类01背包)

本文探讨了一个有趣的问题:如何在购买商品时通过巧妙安排商品顺序来最小化支付总额。具体而言,当顾客可以在收银员处理商品时偷取其他商品的情况下,如何确定最优的商品支付顺序。

B. Checkout Assistant
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob came to a cash & carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his trolley. To steal one item Bob needs exactly 1 second. What is the minimum amount of money that Bob will have to pay to the checkout assistant? Remember, please, that it is Bob, who determines the order of items for the checkout assistant.

Input

The first input line contains number n (1 ≤ n ≤ 2000). In each of the following n lines each item is described by a pair of numbers tici(0 ≤ ti ≤ 2000, 1 ≤ ci ≤ 109). If ti is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item i.

Output

Output one number — answer to the problem: what is the minimum amount of money that Bob will have to pay.

Examples
input
4
2 10
0 20
1 5
1 3
output
8
input
3
0 1
0 10
0 100
output
111
题意:买N件商品,每件商品有收钱时间ti,价格ci,收钱时可以偷窃其他ti件商品,求带走这N件商品需要支付的最小费用。

思路:可以理解为花ci的钱能得到ti+1件商品,于是用01背包做法dp[i]表示在这N件商品中带走i件支付的最小费用dp[i] = min( dp[i], dp[i-(ti+1)] + ci )。

http://blog.youkuaiyun.com/lvshubao1314/article/details/42505443

# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
int main()
{
    long long dp[2001], n, i, j, x, y;
    while(~scanf("%I64d",&n))
    {
        memset(dp, 0x3f3f3f3f, sizeof(dp));
        dp[0] = 0;
        for(i=1; i<=n; ++i)
        {
            scanf("%I64d%I64d",&x, &y);
            ++x;
            for(j=n; j>=x; --j)
                dp[j] = min(dp[j], dp[j-x]+y);
            for(; j>0; --j) //这里表示当前物品数小于第i件物品的ti时,收费第i件物品时能偷走完j件。
                dp[j] = min(dp[j], y);
        }
        printf("%I64d\n",dp[n]);
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值