• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

练习赛 XOR 优先级队列

XOR

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other)
Total Submission(s) : 105   Accepted Submission(s) : 21

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

给出Q个指令,指令有两类
(1)"insert N",将N插入表L,N是非负整数小于2^31
(2)"print", 输出表L中最大的K个元素的XOR和(1<=K<=Q) 。如果表L中的元素少于K,则输出表L中所有元素的XOR和.

Input

第一行是整数T (1 <= T <= 30) ,测试实例数目.
每一个测试实例的第一行包含两个整数Q和K (1 <= Q,K <= 100,000). 随后有Q行,每行一条指令.

Output

针对每一个print指令,输出其结果。注意每个测试实例开始时L是空的

Sample Input

1
5 2
insert 1
insert 2
print
insert 3
print

Sample Output

3
1

Author

hnust_loser
 
 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <vector>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 101
12 #define mod 1000000007
13 #define INF 0x7fffffff
14 #define ll long long
15 //#define ll __int64
16 struct cmp{
17     bool operator()(int x,int y){
18         return x>y;
19     }
20 };
21 int n,m;
22 int main(){
23     int x,s;
24     int t,k;
25     scanf("%d",&t);
26     while(t--){
27         int i=0;
28         scanf("%d%d",&n,&k);
29         priority_queue<int,vector<int>,cmp>q;
30         while(n--){
31             char op[10];
32             scanf("%s",op);
33             if(op[0]=='i'){
34                 i++;
35                 scanf("%d",&x);
36                 if(i==1){s=x;q.push(x);continue;}
37                 if(q.size()<k){s^=x;q.push(x);}
38                 else if(x>q.top()){s^=q.top();s^=x;q.pop();q.push(x);}
39             }
40             else printf("%d\n",s);
41         }
42         while(!q.empty())q.pop();
43     }
44     return 0;
45 }
View Code 2013-09-25 12:10:30
 
posted @ 2013-09-25 10:02  HaibaraAi  阅读(126)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3