↓ Skip to main content
  1. Posts/

BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草 (完全背包)

·537 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

1618: [Usaco2008 Nov]Buying Hay 购买干草
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 906 Solved: 456 [Submit][Status][Discuss]

Description
#

约翰的干草库存已经告罄,他打算为奶牛们采购日(1≤日≤50000)磅干草.




他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5000)美元.每个干草公司的货源都十分充足,可以卖出无限多的干草包.    帮助约翰找到最小的开销来满足需要,即采购到至少H磅干草.

Input
#

第1行输入N和日,之后N行每行输入一个Pi和Ci.

Output
#

最小的开销.

Sample Input
#

2 15 3 2 5 3

Sample Output
#

9

FJ can buy three packages from the second supplier for a total cost of 9.

思路:完全背包。。。注意是买至少V,可以超过。我的做法是算了两倍,然后取最小值(V..2V)

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月03日 星期日 19时40分29秒
 4File Name :code/bzoj/1618.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int p[N],c[N];
35int n;
36int V;
37int dp[1000005];
38
39void solve (int val,int cost)
40{
41    for ( int i = value ; i <= V ; i++)
42	dp[i] = min(dp[i],dp[i-value]+cost);
43}
44int main()
45{
46	#ifndef  ONLINE_JUDGE
47	freopen("code/in.txt","r",stdin);
48  #endif
49	cin>>n>>V;
50	V*=2;
51	for  ( int i = 1 ; i <= n ; i++) cin>>p[i]>>c[i];
52
53	ms(dp,0x3f);
54	dp[0]=0;
55	for ( int i = 1 ; i <= n ; i++) solve(p[i],c[i]);
56	int ans = inf;
57	for ( int i = 1 ; i <= n ; i++)  ans = min(ans,dp[i]);
58	cout<<ans<<endl;
59
60  #ifndef ONLINE_JUDGE
61  fclose(stdin);
62  #endif
63    return 0;
64}

Related

hdu 1114 - Piggy-Bank (完全背包)

·774 words·2 mins
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题 (DP)

·1188 words·3 mins
1617: [Usaco2008 Mar]River Crossing渡河问题 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 837 Solved: 606 [Submit][Status][Discuss] Description # Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏。 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上。在这个基础上,木筏上的奶牛数目每增加1,FJ把木筏划到对岸就得花更多的时间。 当FJ一个人坐在木筏上,他把木筏划到对岸需要M(1 <= M <= 1000)分钟。当木筏搭载的奶牛数目从i-1增加到i时,FJ得多花M_i(1 <= M_i <= 1000)分钟才能把木筏划过河(也就是说,船上有1头奶牛时,FJ得花M+M_1分钟渡河;船上有2头奶牛时,时间就变成M+M_1+M_2分钟。后面的依此类推)。那么,FJ最少要花多少时间,才能把所有奶牛带到对岸呢?当然,这个时间得包括FJ一个人把木筏从对岸划回来接下一批的奶牛的时间。

BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(DP)

·1424 words·3 mins
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1012 Solved: 553 [Submit][Status][Discuss] Description # 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草。Farmer John在某个时刻看见贝茜在位置 (R1, C1),恰好T (0 < T <= 15)秒后,FJ又在位置(R2, C2)与贝茜撞了正着。 FJ并不知道在这T秒内贝茜是否曾经到过(R2, C2),他能确定的只是,现在贝茜在那里。 设S为奶牛在T秒内从(R1, C1)走到(R2, C2)所能选择的路径总数,FJ希望有一个程序来帮他计算这个值。每一秒内,奶牛会水平或垂直地移动1单位距离(奶牛总是在移动,不会在某秒内停在它上一秒所在的点)。草地上的某些地方有树,自然,奶牛不能走到树所在的位置,也不会走出草地。 现在你拿到了一张整块草地的地形图,其中’.‘表示平坦的草地,’*‘表示挡路的树。你的任务是计算出,一头在T秒内从(R1, C1)移动到(R2, C2)的奶牛可能经过的路径有哪些。

BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划 (dp)

·1168 words·3 mins
1613: [Usaco2007 Jan]Running贝茜的晨练计划 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1468 Solved: 706 [Submit][Status][Discuss] Description # 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= N <= 10,000)分钟的晨跑。在每分钟的开始,贝茜会选择下一分钟是用来跑步还是休息。 贝茜的体力限制了她跑步的距离。更具体地,如果贝茜选择在第i分钟内跑步,她可以在这一分钟内跑D_i(1 <= D_i <= 1,000)米,并且她的疲劳度会增加 1。不过,无论何时贝茜的疲劳度都不能超过M(1 <= M <= 500)。如果贝茜选择休息,那么她的疲劳度就会每分钟减少1,但她必须休息到疲劳度恢复到0为止。在疲劳度为0时休息的话,疲劳度不会再变动。晨跑开始时,贝茜的疲劳度为0。 还有,在N分钟的锻炼结束时,贝茜的疲劳度也必须恢复到0,否则她将没有足够的精力来对付这一整天中剩下的事情。 请你计算一下,贝茜最多能跑多少米。

最长上升子序列nlogn解法

·856 words·2 mins
首先回顾一下n^2的做法。 状态转移方程为dp[i] =max(1,dp[j]) (1=<j<=i-1&&a[i]>a[j]) 1for ( int i = 1 ; i <= n ; i++) cin>>a[i]; 2 3 for ( int i = 1 ; i <= n ; i++) 4 { 5 dp[i] = 1; 6 for ( int j = 1 ; j < i ; j++) 7 { 8 if (a[i]>a[j]) 9 dp[i] = max(dp[i],dp[j]+1); 10 } 11 ans = max(ans,dp[i]); 12 } 然后我们发现,使得dp[i]得到同一个值的dp[j]可能有多个,那么选择哪个呢? 假设 x<y<i,a[x]<a[y],dp[x]==dp[y],那么我们选择x好还是y好呢? 显然是x好。为什么?因为选择x潜力大。因为可能在x,y之间存在一个z,满足a[x]<a[z]<a[y],如果选择a[y],就没有办法选择可能使长度更长的a[z]了。通俗得说。。我们要求的是最长上升子序列。。你一开始就弄那么大。。。后面还上哪上升去啊。。。长度小啊。。。