Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There aren available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.
Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.
The first line contains three integers n,c and d (2 ≤ n ≤ 100 000,0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.
The next n lines describe fountains. Each of these lines contain two integersbi andpi (1 ≤ bi, pi ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountaini: in coins or in diamonds, respectively.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print0.
3 7 6 10 8 C 4 3 C 5 6 D
9
2 4 5 2 5 C 2 1 D
0
3 10 10 5 5 C 5 5 C 10 11 D
10
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty5 which costs 6 diamonds. Thus the total beauty of built fountains is9.
In the second example there are two fountains, but Arkady can't build both of them, because he needs5 coins for the first fountain, and Arkady has only4 coins.
题解:首先对于三种情况,分别为买两个货币喷泉,两个钻石喷泉,一货币一钻石喷泉,分别找出最大值,最后三者最大值就是答案了。
难点在于买两个同类型喷泉的处理。我写的比较乱,具体是:对价格排序,枚举第一个喷泉,找出剩下的钱能买到的最大价值。想要在O(1)的时间内
找出的话,先预处理出当前价格所能买的价值最大和第二大的喷泉价值,要求第一个喷泉的花费一定是小于等于第二个喷泉花费,那么如果第一个喷泉价值
等于剩余钱能买的最大价值,那么第二个喷泉取第二大价值,否则取第一大价值,最后找出最大的结果,这样如果买不了答案即为0.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm = 100005;
typedef struct
{
int be, cost;
}H;
H p[2][maxm];
int len[2], dp1[2][maxm] = { 0 }, dp2[2][maxm] = { 0 }, flag[2][maxm] = { 0 };
char ch;
int cmp(H x, H y);
int main()
{
int n, i, j, k, sum, a, b, x, y, ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0, ans = 0;
scanf("%d%d%d", &n, &a, &b);
for (i = 1;i <= n;i++)
{
scanf("%d%d %c", &x, &y, &ch);
if (ch == 'C')
{
p[0][++len[0]].be = x;
p[0][len[0]].cost = y;
flag[0][y] = 1;
}
else
{
p[1][++len[1]].be = x;
p[1][len[1]].cost = y;
flag[1][y] = 1;
}
}
sort(p[0] + 1, p[0] + 1 + len[0], cmp);
sort(p[1] + 1, p[1] + 1 + len[1], cmp);
p[0][0].cost = -1;p[1][0].cost = -1;
for (i = 1;i <= len[0];i++)
{
if (p[0][i].cost != p[0][i + 1].cost)
{
flag[0][p[0][i].cost] = i;
for (j = p[0][i].cost + 1;j <= 100000;j++)
{
if (flag[0][j])
break;
flag[0][j] = i;
}
}
}
for (i = 1;i <= len[0];i++)
{
dp1[0][i] = dp1[0][i - 1];
dp2[0][i] = dp2[0][i - 1];
if (p[0][i].be > dp1[0][i - 1])
dp1[0][i] = p[0][i].be;
else if (p[0][i].be <= dp1[0][i - 1] && p[0][i].be > dp2[0][i - 1])
dp2[0][i] = p[0][i].be;
//printf("%d %d\n", dp1[0][i], dp2[0][i]);
}
int rev;
for (i = 1;i <= len[0];i++)
{
if (p[0][i].cost <= a)
ans3 = max(ans3, p[0][i].be);
if (p[0][i].cost > a / 2)
continue;
if (p[0][i].cost != p[0][i - 1].cost)
{
rev = a - p[0][i].cost;
if (flag[0][rev] <= 1)
break;
if (dp1[0][i] == dp1[0][flag[0][rev]])
ans1 = max(ans1, p[0][i].be + dp2[0][flag[0][rev]]);
else
ans1 = max(ans1, p[0][i].be + dp1[0][flag[0][rev]]);
}
}
ans = 0;
for (i = 1;i <= len[1];i++)
{
if (p[1][i].cost != p[1][i + 1].cost)
{
flag[1][p[1][i].cost] = i;
for (j = p[1][i].cost + 1;j <= 100000;j++)
{
if (flag[1][j])
break;
flag[1][j] = i;
}
}
}
for (i = 1;i <= len[1];i++)
{
dp1[1][i] = dp1[1][i - 1];
dp2[1][i] = dp2[1][i - 1];
if (p[1][i].be > dp1[1][i - 1])
dp1[1][i] = p[1][i].be;
else if (p[1][i].be <= dp1[1][i - 1] && p[1][i].be > dp2[1][i - 1])
dp2[1][i] = p[1][i].be;
//printf("%d %d\n", dp1[1][i], dp2[1][i]);
}
for (i = 1;i <= len[1];i++)
{
if (p[1][i].cost <= b)
ans4 = max(ans4, p[1][i].be);
if (p[1][i].cost > b / 2)
continue;
if (p[1][i].cost != p[1][i - 1].cost)
{
rev = b - p[1][i].cost;
//printf("%d %d\n", rev, flag[1][rev]);
if (flag[1][rev] <= 1)
break;
if (dp1[1][i] == dp1[1][flag[1][rev]])
ans2 = max(ans2, p[1][i].be + dp2[1][flag[1][rev]]);
else
ans2 = max(ans2, p[1][i].be + dp1[1][flag[1][rev]]);
}
}
sum = 0;
if (ans3 != 0 && ans4 != 0)
sum = ans3 + ans4;
//printf("%d %d %d\n", ans3, ans4, sum);
ans = max(ans1, ans2);
ans = max(ans, sum);
printf("%d\n", ans);
return 0;
}
int cmp(H x, H y)
{
if (x.cost != y.cost)
return x.cost < y.cost;
else
return x.be > y.be;
}
/*
3 7 6
10 5 C
12 5 C
13 5 C
*/