贪心的策略:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=26467
给龙建个优先队列, 按他的价值从小到大。
每遇到一个princess,如果在Q中的个数大于这个princess所需要的龙,就pop。因为肯定不会取已经pop了的龙。 然后输出就行了。
以下是董事的代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
typedef long long LL;
using namespace std;
struct Dragon{
LL v ;
int id;
void input(int _id){
scanf("%lld" , &v);
id = _id;
}
bool operator < (const Dragon & A) const{
return v > A.v;
}
}now;
priority_queue<Dragon, vector<Dragon>, less<Dragon> > Q;
int n ;
string character;
vector<int> ans;
void solve(){
while(!Q.empty()) Q.pop();
for (int i = 2 ; i < n ; ++i){
cin >> character;
if (character == "d"){
now.input(i);
Q.push(now);
}
else{
int x ;
cin >> x;
while (Q.size() >= x) Q.pop();
}
}
int x ;
cin >> character >> x;
if (Q.size() < x) cout << "-1" << endl;
else{
LL sum = 0 ; ans.clear();
while (!Q.empty()){
sum += Q.top().v;
ans.push_back(Q.top().id);
Q.pop();
}
sort( ans.begin(), ans.end() );
cout << sum << endl << ans.size() << endl;
for (int i = 0 ; i < ans.size() ; ++i){
if (i) cout << " ";
cout << ans[i];
}
cout << endl;
}
}
int main(){
while (cin >> n) solve();
}