2019沈阳网络赛签到题-dp完全背包
- 题目链接Dawn-K’s water
- 题解:
- C. Dawn-K’s water
完全背包加枚举,设 f[i]表示恰好买 i 单位的水最少花费的价格(如果无法恰好
买 i 单位则赋值无穷大)
因为题目要求的是至少买 m 单位水的质量所花的最大价格,并且答案价格小于
1e4,所以在用完全背包求出所有的 fx后
从 m 开始往后更新答案即可: 若 ans>=f[i], 则 ans=fi.
由于 n 的总和<=5e4,答案又保证了<=1e4,所以时间复杂度大概为 O(5e8). - 题目
Dawn-K recently discovered a very magical phenomenon in the supermarket of Northeastern University: The large package is not necessarily more expensive than the small package.
On this day, Dawn-K came to the supermarket to buy mineral water, he found that there are nn types of mineral water, and he already knew the price pp and the weight cc (kg) of each type of mineral water. Now Dawn-K wants to know the least money aa he needs to buy no less than mm kilograms of mineral water and the actual weight bb of mineral water he will get. Please help him to calculate them.
Input
The input consists of multiple test cases, each test case starts with a number nn (1 \le n \le 10^31≤n≤10
3
) – the number of types, and mm (1 \le m \le 10^41≤m≤10
4
) – the least kilograms of water he needs to buy. For each set of test cases, the sum of nn does not exceed 5e45e4.
Then followed n lines with each line two integers pp (1 \le p \le 10^91≤p≤10
9
) – the price of this type, and cc (1 \le c \le 10^41≤c≤10
4
) – the weight of water this type contains.
Output
For each test case, you should output one line contains the minimum cost aa and the weight of water Dawn-K will get bb. If this minimum cost corresponds different solution, output the maximum weight he can get.
(The answer aa is between 11 and 10^910
9
, and the answer bb is between 11 and 10^410
4
)
样例输入 复制
3 3
2 1
3 1
1 1
3 5
2 3
1 2
3 3
样例输出 复制
3 3
3 6
- 题目大意:有l
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std ;
const int maxn = 1e4+5;
const int maxm=1e3+5;
const int inf=1e9+5;
int f[maxn],wei[maxm],cost[maxm];
int main()
{
int n,m;
while(cin>>n>>m)
{
fill(f,f+maxn,inf);
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
wei[i]=y;cost[i]=x;
}
f[0]=0;
for(int i=1;i<=maxn;i++)
for(int j=1;j<=n;j++)
if(i>=wei[j] && f[i-wei[j]]!=inf )
f[i]=min(f[i],cost[j]+f[i-wei[j]]);
int ans=inf,idx;
for(int i=m;i<maxn;i++)
if(ans>=f[i])
{
ans=f[i];idx=i;
}
cout<<ans<<' '<<idx<<endl;
}
return 0;
}