zoukankan      html  css  js  c++  java
  • sgu548 Dragons and Princesses   贪心+优先队列

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548

    题目意思:

      有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者公主,遇到龙,可以决定杀或者不杀,如果杀,就可以得到相应的珠宝;如果遇到公主,如果这个骑士此时杀过的龙的数目大于等于公主的美貌值,那么这个骑士必须marry这个公主,不能拒绝..^_^,但是骑士的真爱是在最后一个房间里面的公主,问骑士能不能到达最后一个房间?如果能的话,求出能够到达最后一个房间的情况下,得到的最大的珠宝数.

    做法:

      优先队列+贪心.

    遇到龙就杀,用优先队列维护得到的珠宝数目,遇到公主就检查目前的杀的龙的数目是不是大于等于公主的美貌值,如果大于等于,就把有限队列里面珠宝值小的房间出队,直到杀的龙的数目小于美貌值为止.

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <queue>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <cstring>
     7 using namespace std;
     8 struct Node {
     9     int pos, n;
    10     bool operator < (const Node &other) const  {
    11         return n < other.n;
    12     }
    13 }node;
    14 int array[200009];
    15 priority_queue<Node> a;
    16 char ch[4];
    17 int main(void) {
    18     int i, n, tmp, j, sum, cnt;
    19     while (~scanf("%d", &n)) {
    20         sum = 0, cnt = 0, j = 0, i = 2;
    21         for (int f = 0; f < n-2; ++f, ++j, ++i) {
    22             scanf("%s%d", ch, &tmp);
    23             node.pos = i, node.n = -tmp; 
    24             if (ch[0] == 'd') {
    25                 a.push(node); cnt++; sum += tmp;
    26             } 
    27             else {
    28                 while (!a.empty() && cnt >= tmp) {
    29                     int hehe = a.top().n; a.pop(); cnt--; sum += hehe;
    30                 }
    31             }
    32         }
    33         scanf("%s%d", ch, &tmp);
    34         if (cnt >= tmp) {
    35             printf("%d
    %d
    ", sum, cnt);
    36             int e = 0;
    37             while (!a.empty()) {
    38                 array[e++] = a.top().pos; a.pop();
    39             }
    40             sort(array, array+e);
    41             for (int i = 0; i < e; ++i) {
    42                 printf("%d", array[i]);
    43                 if (i != e-1) printf(" ");
    44             }
    45             printf("
    ");
    46         } 
    47         else printf("-1
    ");
    48     }
    49 
    50     return 0;
    51 }

    这题想明白就行了,可惜比赛的时候没有时间做...==

  • 相关阅读:
    poj 3662 Telephone Lines
    费马小定理证明
    CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数
    共价大爷游长沙 lct 维护子树信息
    牛客暑假多校 F RIKKA with Line Graph
    牛客暑假多校 H Prefix sum
    HDU-6437 Videos
    模板汇总——AC自动机
    模板汇总——逆元
    模板汇总——LCT
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/3198702.html
Copyright © 2011-2022 走看看