玲珑学院 1125 咸鱼商店(二分+01背包)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值