zoukankan      html  css  js  c++  java
  • 勇士出征[HUST 1439]

    勇士出征[HUST 1439]

    时间1000ms,内存64MB

    第十届“北大青鸟”杯浙江师范大学程序设计竞赛

    这道题跟UVA-12100是一样的题目。我这里用的是STL的双端队列deque容器配合优先队列priority_queue,写起来会比较轻松;依次将输入压入队列,然后不断扫描队列,符合最大优先级的(优先队列的顶部元素)将其送出,而不再压入队尾。直到找到符合自己的标记的为止。

    当然这道题也有用数组使用滚雪球的方式实现的,也就是开一个大的数组,每次将元素后挪时,直接将其放在数组末尾,而不用整体向前移动,这时候只需要将队首指针同时向后挪就可以了。总体来说这个题是用的队列思想,理解了队列就OK了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<deque>
     5 #include<queue>
     6 using namespace std;
     7 struct person
     8 {
     9     int level;
    10     bool me;
    11     person(int lv,int m):level(lv),me(m){}
    12 };
    13 int main()
    14 {
    15     int T;
    16     scanf("%d",&T);
    17     while(T--)
    18     {
    19         priority_queue<int> pq;
    20         deque<person> dq;
    21         int n,mypos,i;
    22         scanf("%d%d",&n,&mypos);
    23         for(i=0;i<n;i++)
    24         {
    25             int tmp;
    26             scanf("%d",&tmp);
    27             if(i+1==mypos)
    28                 dq.push_back(person(tmp,true));
    29             else
    30                 dq.push_back(person(tmp,false));
    31             pq.push(tmp);
    32         }
    33         int cnt=1;
    34         while(!pq.empty())
    35         {
    36             if(dq.front().level!=pq.top())
    37             {
    38                 dq.push_back(dq.front());
    39             }
    40             else
    41             {
    42                 if(dq.front().me) break;
    43                 else 
    44                 {
    45                     pq.pop();
    46                     cnt++;
    47                 }
    48             }
    49             dq.pop_front();
    50         }
    51         printf("%d
    ",cnt);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    20140710 sequence 前缀和
    20140709 testC 数学题
    20140708 testA 组合数学
    20140708 testB DP 组合数学
    Sad :(
    已经是一个废人了……
    Game Theory
    HDU Math Problems
    2-sat问题
    并查集
  • 原文地址:https://www.cnblogs.com/ggggg63/p/6777631.html
Copyright © 2011-2022 走看看