Checkout Assistant CodeForces - 19B(01背包变形)

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 ti, ci (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个东西,在售货员处理第i件物品的时候,需要花费ti的时间,这段时间他可以偷ti件物品,问最后需要花费的最小价值是多少。
思路:一开始以为是给排序+dp,结果一直wa在第七个样例。。
我们用dp[i]代表着买i件物品花费的最小价值。我们买第i件物品,一共可以获得ti+1件物品。就是花费了ci钱,获得了体积为ti+1的物品,就转化成了01背包。但是有一点不一样的是,如果当前背包中的体积装不下ti+1件物品,这样的话,就直接和当前价值取最小值就好了。
代码如下:

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

const int maxx=2e3+100;
struct node{
	int t;
	ll c;
}p[maxx];
ll dp[maxx];
int n;

int main()
{
	scanf("%d",&n);
	ll sum=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%lld",&p[i].t,&p[i].c);
		p[i].t++;
		dp[i]=inf;
	}
	dp[0]=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=n;j>=p[i].t;j--) dp[j]=min(dp[j],dp[j-p[i].t]+p[i].c);//01背包
		for(int j=p[i].t-1;j>=0;j--) dp[j]=min(dp[j],p[i].c);//!!!!
	}
	cout<<dp[n]<<endl;
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值