您的位置:首页 > 大数据 > 人工智能

CF - 799C. Fountains - 树状数组/线段树

2017-05-24 18:24 309 查看
1.题目描述:

C. Fountains

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n 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.

Input

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 integers bi and pi (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 fountain i:
in coins or in diamonds, respectively.

Output

Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.

Examples

input
3 7 6
10 8 C
4 3 C
5 6 D


output
9


input
2 4 5
2 5 C
2 1 D


output
0


input
3 10 105 5 C
5 5 C
10 11 D


output
10


Note

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 beauty 5 which costs 6 diamonds.
Thus the total beauty of built fountains is 9.

In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady
has only 4 coins.

2.题意概述:

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

3.解题思路:

乍一看像01背包啊,但是看看数据范围就gg了, 仔细分析一下选法,三种:

第一种,买两件C物品;第二种,买两件D物品;第三种,买一件C一件D。我们可以很容易预处理出在C和D限制下能买的物品,显然第三种情况就是C的最大值加上D的最大值。那么对于前两种方案,如果暴力查询那么复杂度肯定是不能接受的O(n^2),考虑怎么优化,例如C条件下买两件c种类物品,我们考虑确定第二件物品以后,第一件物品实际上就是在C-c[i]下去选取最大的beauty值物品,怎么优化地查询呢?很容易想到是线段树或者BIT,这里我比较懒用的bit,我们可以在O(n)扫物品时候去log查询C-c[i]约束下能够获得的最大beauty值,再去update,这里有个小技巧,查询的是后一件物品,这样查询完update一次就行。这样总的复杂度是O(nlogn)的。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100010#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int b, p;
} c[maxn], d[maxn];
int t[maxn];
int lowbit(int x)
{
return x & -x;
}
void update(int x, int y, int data)
{
while (x < y)
{
t[x] = max(t[x], data);
x += lowbit(x);
}
}
int
12fa7
query(int x)
{
int ans = 0;
while (x > 0)
{
ans = max(ans, t[x]);
x -= lowbit(x);
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, C, D;
while (~scanf("%d%d%d", &n, &C, &D))
{
int cnt1 = 0, cnt2 = 0, ans1 = -1, ans2 = -1;
for (int i = 0; i < n; i++)
{
int b, p;
char ch[2];
scanf("%d%d%s", &b, &p, ch);
if (ch[0] == 'C')
{
if (p <= C)
{
c[cnt1].b = b;
c[cnt1++].p = p;
ans1 = max(ans1, b);
}
}
else
{
if (p <= D)
{
d[cnt2].b = b;
d[cnt2++].p = p;
ans2 = max(ans2, b);
}
}
}
int ans = 0;
if (ans1 + 1 && ans2 + 1)
ans = ans1 + ans2;
memset(t, 0, sizeof t);
for (int i = 0; i < cnt1; i++)
{
int cur = query(C - c[i].p);
if (cur > 0)
ans = max(ans, cur + c[i].b);
update(c[i].p, maxn, c[i].b);
}
memset(t, 0, sizeof t);
for (int i = 0; i < cnt2; i++)
{
int cur = query(D - d[i].p);
if (cur > 0)
ans = max(ans, cur + d[i].b);
update(d[i].p, maxn, d[i].b);
}
printf("%d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm codeforces