Http://codeforces.com/contest/799/problem/C
Test instructions
There are n do garden, someone has c coins, D diamonds (2≤ n ≤100, 0≤ c, D ≤100 000), each garden with three dimensions description (a,b,c), respectively, is the beauty, the flower The number of coins, the type of coin, of course, the coin can not be exchanged, the person must build two gardens, if possible, output the total beauty of the two gardens, otherwise output 0;
Ideas:
First think of three kinds of situation discussion, two garden all with coins, two garden all with diamonds, one with diamond one with a coin.
The Great God code really is very powerful, first can not afford directly to omit, next first according to the price of the garden sort, next this for_max write very strong, recorded the first I before the biggest beauty degree. , the next cycle, I start from 0 + +, and then determine whether to meet I<j, and then two gardens need less money than the current number of coins, if meet constantly make i++, this is to meet the greedy requirements, you currently seek is in the current condition of J the largest, and ultimately find the answer.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <vector>6#include <stack>7#include <queue>8#include <cmath>9#include <map>Ten using namespacestd; One A Const intmaxn=100000+5; -typedef pair<int,int>PLL; - the intn,c,d; -Vector<pll>Cc,dd; - intFOR_MAX[MAXN]; - + intSolve (Vector<pll> Z,intMoney ) - { + intlen=z.size (); A if(len<2)return 0; at intans=0; -Sort (Z.begin (), Z.end ());//Sort by Price first -for_max[0]=0; - for(intI=0; i<len;i++) for_max[i+1]=max (For_max[i],z[i].second);//pretreatment to calculate the greatest degree of beauty in the first I - intI=0; - for(intj=len-1; j>=0; j--) in { - while(I<j && Z[i].first+z[j].first<=money) i++;//because For_max[i] records the maximum beauty of the first I-1, so as long as the total price is not exceeded, you can always add toI=min (i,j); + if(i>0) -Ans=max (ans,for_max[i]+z[j].second); the } * returnans; $ }Panax Notoginseng - intMain () the { + //freopen ("D:\\input.txt", "R", stdin); A while(~SCANF ("%d%d%d",&n,&c,&d)) the { + cc.clear (); Dd.clear (); - intcc_max=0, dd_max=0; $ intans=0; $ intB,p;Chars[5]; - for(intI=0; i<n;i++) - { thescanf"%d%d%s",&b,&p,&s); - if(s[0]=='C')Wuyi { the if(P>C)Continue;//can't afford to save directly - Cc.push_back (Make_pair (p,b)); Wucc_max=Max (cc_max,b); - } About Else $ { - if(p>d)Continue; - Dd.push_back (Make_pair (p,b)); -dd_max=Max (dd_max,b); A } + } the if(cc_max!=0&& dd_max!=0) Ans=cc_max+dd_max;//first Case -Ans=max (Ans,solve (cc,c));//Two of them are coins . $Ans=max (Ans,solve (dd,d));//Two of them are diamonds . theprintf"%d\n", ans); the } the return 0; the}
In addition, the tree array can also be done, you can refer to http://blog.csdn.net/ssimple_y/article/details/71702915.
Codeforces Round #413, rated, Div. 1 + div. 2 C. Fountains (greedy or tree-like array)