跳过正文
  1. Posts/

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

·2 分钟
目录

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}

相关文章

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

·2 分钟
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.

最长上升子序列nlogn解法

·2 分钟
首先回顾一下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]了。通俗得说。。我们要求的是最长上升子序列。。你一开始就弄那么大。。。后面还上哪上升去啊。。。长度小啊。。。