题目大意:有一些喷泉,分为两种,一种只可以硬币购买,一种只可以用钻石购买,它们都有各自的价值,现在你有一些钻石和硬币,要买两座喷泉,问最大可获得的价值。
解题思路:其实思路挺好出,就是复杂度不够,用树状数组优化即可
感觉下一次出这样的我还是不会呀嘤嘤嘤
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#define FAST ios::sync_with_stdio(false)
#define lowbit(x) x&(-x)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e5 + 5;
using namespace std;
//需要维护最大值
//循环判断显然会超时
//树状数组操作
int a[maxn], b[maxn];
int sum(int *t, int x){
int ret = 0;
for( ; x > 0; ret = max(ret, t[x]), x -= lowbit(x));//维护的是最大值,不再是和了
return ret;
}
void update(int *t, int x, int val){
for( ; x <= maxn; t[x] = max(t[x], val), x += lowbit(x));//同
}
int main()
{
int n, c, d; scanf("%d %d %d", &n, &c, &d);
int ans = 0;
for(int i = 1; i <= n; i++){
int val, w; scanf("%d %d ", &val, &w);
char type; scanf("%c", &type);
int Max = 0;
if(type == 'C'){//分别维护
Max = sum(b, d);
if(w > c) continue;
Max = max(Max, sum(a, c - w)); //更新,维护Max
update(a, w, val);
}
else{//
Max = sum(a, c);
if(w > d) continue;
Max = max(Max, sum(b, d - w));
update(b, w, val);
}
if(Max) ans = max(Max + val, ans);
}
printf("%d\n", ans);
return 0;
}
over