BNUOJ 26475 Cookie Selection

LINK:BNUOJ 26475 Cookie Selection

题意:

你在不停的输入数字a1,a2,a3,......,ak,当你输入#时,就把已输入数字中的第k/2+1删除,然后剩下的数字又组成一个新的数列a1,a2,......,a(k-1)。把删除的数输出来~╮(╯▽╰)╭

刚开始理解错了题意......各种WA......想半天还不知道错哪.....最后搞清,自己压根连题目都弄错了......就没有再继续战斗的想法了~~~~(>_<)~~~~

该题要用到优先队列......具体方法参见代码ORZ......

代码【一】:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 priority_queue<int> p;
 7 priority_queue<int,vector<int>,greater<int> > q;
 8 char str[50];
 9 void doit(){
10     while(p.size()>q.size()){
11         q.push(p.top());
12         p.pop();
13     }
14     while(q.size()>p.size()+1){
15         p.push(q.top());
16         q.pop();
17     }
18 }
19 int main(){
20     int i,j,k;
21     while(~scanf("%s",str)){
22         if(str[0]=='#'){
23             printf("%d\n",q.top());
24             q.pop();
25             doit();
26         }
27         else{
28             sscanf(str,"%d",&k);
29             if(!q.empty()&&k>q.top()) q.push(k);
30             else p.push(k);
31             doit();
32         }
33     }
34     return 0;
35 }

//memory:3132KB    time:712ms

代码【二】://这代码.......不是很懂啊~

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <set>
 6 using namespace std;
 7 typedef set<double> si;
 8 int main(void)
 9 {
10     si X;
11     si::iterator it = X.begin();
12     char str[100];
13     while (scanf("%s", str) == 1)
14         {
15         if (str[0]== '#')
16         {
17             printf("%.0lf\n", *it);
18             X.erase(it++);
19             if (X.size() % 2)
20                 --it;
21         }
22         else
23             {
24             double x = atoi(str)+0.4*drand48();   //atoi(str)能把字符串str转化为数字,drand48()产生一个随机的double数
25             X.insert(x);
26             if (X.size() == 1)
27             it = X.begin();
28             else
29             {
30                 if (x < *it)
31                     --it;
32                 if (X.size() % 2 == 0) ++it;
33             }
34         }
35     }
36     return 0;
37 }

//memory:10792KB       time:1288ms

posted @ 2013-08-25 22:33  Teilwall  阅读(416)  评论(0编辑  收藏  举报