codeforces 799c

Fountains

题意:有c个金币d个宝石,n个物品,有2种物品C和D,C只能用金币获得,D只能用宝石获得,每个物品有价值和花费,求获得2件物品的最大价值

思路:分3种情况,1、2个物品都是C,2、2个物品都是D,3、1个C一个D。将所有物品按花费排序,预处理出前缀最大价值(表示花费不超过x的最大价值),枚举第一件物品后,二分花费找到第二件物品的最大价值,为了不重复选,枚举i的时候二分上届为i-1

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=1e5+100;

struct Node{
    int b, p;
    friend operator< (Node a, Node b){
        return a.b<b.b;
    }
};
Node C[N],D[N];
int ma[N];
int n,c,d,ans;
int solve(int w, Node *a, int r){
    int l=0, ans=0;
    while(l<=r){
        int mid=l+r>>1;
        if(a[mid].b<=w){
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    if(ans==0) return -inf;
    else return ma[ans];
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>c>>d;
    int ci=0, di=0, b, p; char s;
    int ans1=0, ans2=0;
    for(int i=1; i<=n; ++i){
        cin>>p>>b>>s;
        if(s=='C'){
            C[++ci].b=b;
            C[ci].p=p;
            if(b<=c) ans1=max(ans1,p);
        }
        else{
            D[++di].b=b;
            D[di].p=p;
            if(b<=d) ans2=max(ans2,p);
        }
    }
    if(ans1!=0 && ans2!=0) ans=max(ans,ans1+ans2);
    sort(C+1,C+1+ci);
    for(int i=1; i<=ci; ++i){
        if(C[i].p>ma[i-1]){
            ma[i]=C[i].p;
        }
        else ma[i]=ma[i-1];
    }
    for(int i=1; i<=ci; ++i){
        if(C[i].b>=c) continue;
        ans=max(ans,C[i].p+solve(c-C[i].b, C, i-1));
    }
    sort(D+1,D+1+di);
    for(int i=1; i<=di; ++i){
        if(D[i].p>ma[i-1]){
            ma[i]=D[i].p;
        }
        else ma[i]=ma[i-1];
    }
    for(int i=1; i<=di; ++i){
        if(D[i].b>=d) continue;
        ans=max(ans,D[i].p+solve(d-D[i].b, D, i-1));
    }
    if(ans<0) cout<<"0\n";
    else cout<<ans<<endl;
    return 0;
}

/*
3 3 6
10 8 C
4 3 C
5 6 D
*/

 

posted on 2017-10-07 19:08  lazzzy  阅读(239)  评论(0编辑  收藏  举报

导航