Dragons and Princesses(优先队列)

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  nfrom 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  g i 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  b i, 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·10 5) — 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  g i(1 ≤  g i ≤ 10 4) — 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  b i(1 ≤  b i ≤ 2·10 5) — 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  kintegers — 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.

Sample Input

Example(s)
sample input
sample output
6
d 10
d 12
p 2
d 1
p 2
13
2
3 5
题意 :   有一条牢房,n个牢房排成一条线,骑士从左第一个牢房往右走,不能回头.每个牢房有一条龙或者有一个公主..  杀了龙获得黄金,遇见公主,如果杀的龙的数量不小于公主的期望值,则娶公主.......骑士尽可能的获得多的黄金,同时他只想取最后一个牢房的公主(保证最后一个牢房一定有公主) ,这就要求在娶最后公主之前,不能娶别的公主,也就是杀龙的数量的不能超过公主的期望值,不然就会娶了别的公主....
 

· 
#include
<iostream>

·  #include<cstdio>

·  #include<cstdlib>

·  #include<cmath>

·  #include<cstring>

·  #include<string>

·  #include<vector>

·  #include<list>

·  #include<map>

·  #include<set>

·  #include<stack>

·  #include<queue>

·  #include<algorithm>

·  #include<functional

·  usingnamespace std;

·  #define N200005

·  ·  structnode

·  {

·  int x,id; //x是黄金数,或者公主期望值.  id为第几个牢房.

·  char tp[2];

·  booloperator<(nodea)const{ //重载运算符,按升序排列

·  if(x<a.x)return0;

·  elsereturn1;

·  }

·  }a[N],ans[N];

·  priority_queue<node>q;     //优先队列

·  intcmp(nodex,nodey)         //升序排列牢房编号

·  {

·  return x.id<y.id;

·  }

·  //priority_queue<int,vector<int>,greater<int>> q;

·  intmain()

·  {

·  int n,i;

·  while(scanf("%d",&n)==1)

·  {

·  //getchar();

·  for(i=1;i<n;i++)

·  scanf("%s%d",&a[i].tp,&a[i].x),a[i].id=i+1;      //因为牢房是从2到n;骑士刚开始在第一个牢房出发,所以加1

·  while(!q.empty())q.pop();             //初始化容器,使容器为空.

·  for(i=1;i<n-1;i++)

·  {

·  if(a[i].tp[0]=='d')q.push(a[i]);        //如牢房是龙,加入容器

·  else{                       //q.size()是返回容器的容量,即元素的个数.

·                    //若是公主,判断容器里龙的数量是否大于公主的期望值,大于,就移除容器元素.

·  while(q.size()>=a[i].x)q.pop()   //知道龙的数量小于公主的期望,移除的龙是按黄金数小的优先出来.

·  }    //最后容器的里面的龙斗士要杀的,并且黄金数是最大的.

·  }    //若容器的容量小于最后一个公主的期望,即不能娶最后一个公主,则 -1 ;

·  if(q.size()<a[i].x)printf("-1\n");

·  else{

·  int l=0,cnt=0;

·  while(!q.empty())

·  ans[l++]=q.top(),q.pop();   //取容器里的龙,

·  sort(ans,ans+l,cmp);   //按牢房编号升序排列

·  for(i=0;i<l;i++)cnt+=ans[i].x; //总获得的黄金

·  printf("%d\n",cnt);printf("%d\n",l);

·  for(i=0;i<l-1;i++)printf("%d",ans[i].id);printf("%d\n",ans[i].id); //输出杀龙的编号.

·  }

·  }

·  return0;

·  }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值