1125 咸鱼商店(“玲珑杯”线上赛 Round #15 河南专场)
题目链接:http://ifrog.cc/acm/problem/1125
思路就是二分最大值然后用O1背包判断就好了
代码如下:
#include<bits/stdc++.h>
using namespace std;
int s[1007], v[1007];
int dp[1000007];
int n, m, k;
bool judge(int x)
{
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; ++ i)
{
if(v[i] < x)
continue;
for(int j=m; j>=s[i]; -- j)
dp[j] = max(dp[j], dp[j-s[i]] + v[i]);
}
return dp[m] >= k;
}
int main()
{
while(scanf("%d%d%d", &n, &m, &k)!=EOF)
{
for(int i=1; i<=n; ++ i)
scanf("%d%d", &s[i], &v[i]);
if(judge(0) == false)
{
printf("-1\n");
continue;
}
int b = 0, e = 1000000;
while(b < e)
{
int mid = b + (e - b + 1) / 2;
if(judge(mid))
b = mid;
else
e = mid - 1;
}
printf("%d\n", b);
}
return 0;
}