【codeforces 799C】Fountains

【题目链接】:http://codeforces.com/contest/799/problem/C

【题意】

你有两种不同的货币;
余额分别为c和d
然后有n种商品;
每种商品只能由两种货币中的某一种购买;
(且每件商品都有它的美丽值和对应货币的花费)
让你用这两种货币的余额严格买两件商品;
要求让美丽值最大;
输出最大的美丽值;

【题解】

可以预处理出
pre1[N][2]
表示花费N个金币,能够获得的最大美丽值和次小美丽值;
pre2[N][2]…..
然后对以下3种情况枚举其中的一个;
一.
两个商品都是金币买的
则枚举其中一个商品是哪一个;
然后看看余额还剩多少->rest;
直接获取pre1[rest][0]
如果枚举的商品的价格<=rest且pre1[rest][0]和这个枚举的商品的美丽值一样
就取pre1[rest][1];即次小值;
二.
两个商品都是钻石买的
三.
一个商品是钻石买的,另外一个是金币买的;

【Number Of WA

1

【完整代码】

#include <bits/stdc++.h>
#define pb push_back
#define rep1(i,x,y) for (int i =x;i<= y;i++)
#define rep2(i,x,y) for (int i = y;i >=x;i--)
using namespace std;

const int N = 1e5+100;

struct node
{
    int b,p,id;
};

vector <int> g[2][N];
int bo[2][N][2];
int n,c,d;
node a[N];

int main()
{
    //freopen("D:\\rush.txt","r",stdin);
    ios_base::sync_with_stdio(0);
    cin >>n>>c>>d;
    char s[3];
    rep1(i,1,n)
    {
        cin>>a[i].b>>a[i].p;
        cin>>s;
        if (s[0]=='C')
            a[i].id = 0;
        else
            a[i].id = 1;
        g[a[i].id][a[i].p].pb(a[i].b);
    }
    rep1(k,0,1)
    {
        rep1(i,1,100000)
        {
            bo[k][i][1] = bo[k][i-1][1];
            bo[k][i][0] = bo[k][i-1][0];
            int len = g[k][i].size();
            rep1(j,0,len-1)
            {
                if (g[k][i][j]>bo[k][i][0])
                {
                    bo[k][i][1] = bo[k][i][0];
                    bo[k][i][0] = g[k][i][j];
                }
                else
                    if (g[k][i][j]>bo[k][i][1])
                        bo[k][i][1] = g[k][i][j];
            }
        }
    }
    //all coins
    int ans = 0;
    rep1(i,1,n)
        if (a[i].id==0)
        {
            int t1 = a[i].b,t2 = a[i].p;
            int rest = c-t2;
            if (rest<=0) continue;
            int t3 = bo[0][rest][0];
            if (t2<=rest && t1==t3)
                t3 = bo[0][rest][1];
            if (t3<=0) continue;
            ans = max(ans,t1+t3);
        }
    //all d
    rep1(i,1,n)
        if (a[i].id==1)
        {
            int t1 = a[i].b,t2 = a[i].p;
            int rest = d-t2;
            if (rest<=0) continue;
            int t3 = bo[1][rest][0];
            if (t2<=rest && t1==t3)
                t3 = bo[1][rest][1];
            if (t3<=0) continue;
            ans = max(ans,t1+t3);
        }
    //half half
    rep1(i,1,n)
        {
            int t1 = a[i].b,t2 = a[i].p;
            int rest,t3;
            if (a[i].id==0)
            {
                rest = d;
                if (t2>c) continue;
            }
            else
            {
                rest = c;
                if (t2>d) continue;
            }
            if (rest<=0)  continue;
            t3 = bo[1-a[i].id][rest][0];
            if (t3<=0) continue;
            ans = max(ans,t1+t3);
        }
    cout << ans <<endl;
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(258)  评论(0编辑  收藏  举报