题目描述:
Dragons and Princesses
Time limit per test: 2 second(s)
Memory limit: 262144 kilobytes
Memory limit: 262144 kilobytes
input: standard
output: standard
output: standard
Once upon a time there lived the Knight. Being very courageous he decided to make a long journey full of fights and adventures. The map of this journey can be represented as a row with n cells numbered from 1 to n from left to right. Initially the Knight is located at the leftmost cell (cell number 1). He should pass all the cells one by one and finish his way at the rightmost cell (cell number n). He is not allowed to move back or skip some cells, he will visit all the cells from the first to the last.
Each cell except the first one contains either a dragon or a princess. Each dragon has a chest with gold coins. The dragon at the cell i keeps gi coins. Every time the Knight steps to a cell with a dragon he has a choice — to kill the dragon or just to pass through. The Knight is very strong and dexterous, so it is not a problem for him to kill any dragon on his way. If a dragon is killed the Knight gets all the gold dragon possessed.
When the Knight steps to the cell with a princess, she wonders how many dragons he has killed. If that number is greater or equal to her beauty bi, the princess considers the Knight brave enough and instantly asks him to marry her. Being a true gentleman, the Knight cannot refuse and his adventure immediately ends.
The Knight loves the princess who lives in the cell number n and wants to marry her. Also during the journey he wants to collect as much gold as possible. Please help him to accomplish this task.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105) — the number of cells. The next n-1 lines describe cells from 2 to n. If the cell number i contains a dragon, the i-th line of the input contains letter "
d
" followed by a single integer gi (1 ≤ gi ≤ 104) — the number of coins the dragon keeps. The letter and the integer are separated by a single space.If the cell number i contains a princess, the i-th line of the input contains letter "
p
" followed by a single integer bi (1 ≤ bi ≤ 2·105) — the beauty of the princess. The letter and the integer are separated by a single space. It is guaranteed that the last cell contains a princess.
Output
On the first line of the output print a single integer — the maximum number of gold coins the Knight can collect. On the second line print a single integer k — the number of dragons to kill. The third line should contain k integers — the numbers of the cells where the Knight should kill a dragon. The cell numbers should be printed in the increasing order.If there are several optimal solutions, output any of them. If the Knight can't marry his beloved princess, just print
-1
in the first line of the output.
Example(s)
sample input | sample output |
6
d 10
d 12
p 2
d 1
p 2
| 13
2
3 5
|
sample input | sample output |
6
d 10
d 12
p 2
d 1
p 3
| -1
|
贪心;
其实核心就是维护一个不减的队列,实现的时候我用的是set ,其实是一样的。
假设当前节点是公主,那么我们队列中元素的个数是不能超过bi的,大概就是这样就可以过了。
真心表示比赛的时候弱爆了!
附上代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
int const nMax = 200010;
typedef int LL;
typedef pair<LL,LL> pij;
set<pij > Q;
set<pij >::iterator f;
int n;
char s[39];
int b;
int ans[nMax];
int main(){
scanf("%d",&n);
for(int i=2;i<n;i++){
scanf("%s%d",s,&b);
if(s[0]=='p'){
// printf("size=%d\n",Q.size());
while(Q.size()>=b)Q.erase(Q.begin());
}else {
Q.insert(pij(b,i));
}
}
scanf("%s%d",s,&b);
// printf("size=%d\n",Q.size());
if(Q.size()<b){
puts("-1");
}else{
int k=0;
int mmax=0;
for(f=Q.begin();f!=Q.end();f++){
ans[k++]=(*f).second;
mmax+=(*f).first;
}
printf("%d\n",mmax);
printf("%d\n",k);
sort(ans,ans+k);
for(int i=0;i<k;i++){
if(i==0)printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
return 0;
}