Codeforces Beta Round #19:Checkout Assistant [01背包变形]

Codeforces Beta Round #19:Checkout Assistant

题意

Checkout Assistant

题面翻译
题目描述

Bob 来到一家现购自运商店,将 n n n 件商品放入了他的手推车,然后到收银台付款。每件商品由它的价格 c i c_i ci 和收银员扫描它的时间 t i t_i ti 秒定义。

当收银员正在扫描某件商品时,Bob 可以从他的手推车中偷走某些其它商品。Bob 需要恰好 1 1 1 秒来偷走一件商品。Bob 需要付给收银员的最少钱数是多少?请记住,收银员扫描商品的顺序由 Bob 决定。

输入格式

输入第一行包含数 n n n 1 ≤ n ≤ 2000 1 \le n \le 2000 1n2000)。接下来 n n n 行每行每件商品由一对数 t i t_i ti c i c_i ci 0 ≤ t i ≤ 2000 0 \le t_i \le 2000 0ti2000 1 ≤ c i ≤ 1 0 9 1 \le c_i \le 10^9 1ci109)描述。如果 t i t_i ti 0 0 0,那么当收银员扫描商品 i i i 时,Bob 不能偷任何东西。

输出格式

输出一个数字—— Bob 需要支付的最小金额是多少。

题目描述

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 $ c_{i} $ and time $ t_{i} $ 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.

输入格式

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 $ t_{i} $ , $ c_{i} $ ( $ 0<=t_{i}<=2000,1<=c_{i}<=10^{9} $ ). If $ t_{i} $ is 0, Bob won’t be able to steal anything, while the checkout assistant is occupied with item $ i $ .

输出格式

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

样例 #1
样例输入 #1
4
2 10
0 20
1 5
1 3
样例输出 #1
8
样例 #2
样例输入 #2
3
0 1
0 10
0 100
样例输出 #2
111

tags

01背包变形

思路

  1. 对于每一个物品,付了它的钱后可以得到总共ti+1个物品
  2. 我们可以把
    1. ti+1看成物品的体积
    2. vi看成物品的价格
    3. 我们至少要得到n个物品(没有多的物品偷时就不用偷了,偷到n结束但计算不到n结束),此时容量最大为n+max(ti+1)
  3. 注意开long long
  4. 最后要比较一下n ~ n+max(ti+1)的最小值

AC代码

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;

const int maxn=2e3+5;
int n;
int t[maxn],c[maxn];
ll dp[maxn*4];

int main(){
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++){
        cin>>t[i]>>c[i];
        t[i]=t[i]+1;
        ans=max(t[i],ans);
    }
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int j=n+ans;j>=t[i];j--){
            dp[j]=min(dp[j],dp[j-t[i]]+c[i]);
        }
    }
    ll res=inf;
    for(int i=n;i<=ans+n;i++)res=min(res,dp[i]);
    cout<<res<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值