DESCRIPTION
你现在在咸鱼商店,你有M元钱。咸鱼商店有N个物品,每个物品有两个属性,一个是他的价格S[i],另外一个是他的价值V[i]。现在你想买一些物品,使得这些物品的价值和大于等于K,并且使得其中价值最低的商品的价值尽量高。请你输出这个最大价值。
INPUT
第一行三个整数N,M,K。
接下来N行,每行两个整数S和V,分别表示价格和价值。
满足:1 <= N, M, S <= 10^3, 0 <= V, K <= 10^6
OUTPUT
输出价值最低的商品能够达到的最大价值。
如果无解,输出-1
SAMPLE INPUT
3 10 1
1 2
10 1
5 5
SAMPLE OUTPUT
5
题解:求最小值最大,尝试二分,每次判断需要用01包验证是否可行。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef long long int ll;
typedef pair<int,int>pa;
const int N=1e5+10;
const int MOD=1e9+7;
int read()
{
int x=0;
char ch = getchar();
while('0'>ch||ch>'9')ch=getchar();
while('0'<=ch&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x;
}
/************************************************************/
struct node
{
int s,v;
bool operator <(const node &t)const
{
return v>t.v||(v==t.v&&s<t.s);
}
}a[N];
int dp[N];
int n,m,k;
bool solve(int x)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
if(a[i].v<x) break;
for(int j=m;j>=a[i].s;j--)
{
dp[j]=max(dp[j],dp[j-a[i].s]+a[i].v);
}
}
if(dp[m]>=k) return 1;
return 0;
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i].s>>a[i].v;
}
sort(a+1,a+1+n);
int l=0;
int r=1e6;
int ans=-1;
while(r>=l)
{
int mid=(r+l)>>1;
if(solve(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
cout<<ans<<endl;
}