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.node
Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.ios
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.c++
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.ide
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.ui
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 beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.spa
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.code
題意:你有兩種貨幣 C和D 能夠買屬性爲C和D的貨物,每種貨物有他的beauty值,你如今有若干貨幣,須要你去買貨物C和D獲得最大的beauty值blog
題解:這題理論上來講用n2暴力會T,emmmm可是實際上咱們每次取到最大值後break一下的話,能夠在1700ms的極限時間跑過去,正解應該是按照beauty值排序後線段樹RMQ取區間最值排序
暴力代碼以下:three


#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <bitset> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define fuck(x) cout<<"["<<x<<"]"; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w+",stdout); //#pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long LL; typedef pair<int, int> PII; const int maxn = 1e5+5; const int INF = 0x3f3f3f3f; const int MOD = 1e9+7; struct node{ int p,c; bool operator < (const node &u) const{ return p>u.p; } }a[maxn],b[maxn]; int ta,tb; char ch[5]; int u,v; int main(){ #ifndef ONLINE_JUDGE FIN #endif int n,c,d; cin>>n>>c>>d; ta=tb=0; for(int i=0;i<n;i++){ cin>>u>>v>>ch; if(ch[0]=='C'){ a[ta].p=u; a[ta++].c=v; }else{ b[tb].p=u; b[tb++].c=v; } } sort(a,a+ta); sort(b,b+tb); int ans=0; int pa=0,pb=0; for(int i=0;i<ta;i++){ if(a[i].c<=c){ pa=a[i].p; break; } } for(int i=0;i<tb;i++){ if(b[i].c<=d){ pb=b[i].p; break; } } if(pa&&pb) ans=max(ans,pa+pb); for(int i=0;i<ta;i++){ for(int j=i+1;j<ta;j++){ if(a[i].c+a[j].c<=c){ ans=max(ans,a[i].p+a[j].p); break; } } } for(int i=0;i<tb;i++){ for(int j=i+1;j<tb;j++){ if(b[i].c+b[j].c<=d){ ans=max(ans,b[i].p+b[j].p); break; } } } printf("%d\n",ans); return 0; }
日天學長寫的RMQ代碼以下:


#include <bits/stdc++.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define x first #define y second #define rep(i,a,b) for(int i=a;i<b;++i) #define per(i,a,b) for(int i=a-1;i>=b;--i) #define fuck(x) cout<<'['<<#x<<' '<<(x)<<']' #define eps 1e-12 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const int mod = 998244353; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3fll; const int MX = 1e5 + 5; int v[MX], w[MX]; char t[MX][2]; PII p1[MX], p2[MX]; int sz1, sz2; int solve1(int a, int b) { int mx1 = 0, mx2 = 0; for(int i = 1; i <= sz1; i++) { if(p1[i].x <= a) mx1 = max(mx1, p1[i].y); } for(int i = 1; i <= sz2; i++) { if(p2[i].x <= b) mx2 = max(mx2, p2[i].y); } return (mx1 > 0 && mx2 > 0) ? (mx1 + mx2) : 0; } int dp[MX][30]; void ST(int n) { for (int i = 1; i <= n; i++) dp[i][0] = v[i]; for (int j = 1; (1 << j) <= n; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) { int a = dp[i][j - 1] , b = dp[i + (1 << (j - 1))][j - 1]; dp[i][j] = max(a, b); } } } int RMQ(int l, int r) { if(l>r) return 0; int k = 0; while ((1 << (k + 1)) <= r - l + 1) k++; int a = dp[l][k], b = dp[r - (1 << k) + 1][k]; return max(a, b); } int solve2(PII p[], int sz, int m) { if(sz <= 1) return 0; int ret = 0; sort(p + 1, p + sz + 1); rep(i, 1, sz + 1) w[i] = p[i].x; rep(i, 1, sz + 1) v[i] = p[i].y; rep(i, 2, sz + 1) assert(w[i] >= w[i - 1]); ST(sz); for(int i = 1, j = sz; i <= sz; i++) { while(w[i] + w[j] > m && j > i) j--; if(i < j) ret = max(ret, v[i] + RMQ(i + 1, j)); } return ret; } int main() { #ifdef local freopen("in.txt", "r", stdin); #endif // local int n, a, b; cin >> n >> a >> b; rep(i, 1, n + 1) scanf("%d%d%s", &v[i], &w[i], t[i]); rep(i, 1, n + 1) { if(t[i][0] == 'C') p1[++sz1] = PII(w[i], v[i]); else p2[++sz2] = PII(w[i], v[i]); } int ans=solve1(a,b); ans=max(ans,solve2(p1,sz1,a)); ans=max(ans,solve2(p2,sz2,b)); cout<<ans<<endl; #ifdef local cout << "time cost:" << clock() << "ms" << endl; #endif // local return 0; }