HDU 5349 MZL's simple problem(队列)
2015-08-06 12:34
295 查看
题目地址:点击打开链接
思路:刚开始还以为每次输出最大值还要删除呢,调了半天没有过,看别人的代码,没看到把最大值弹出,每次只保留最大值,以为是数据的BUG,结果又一看题query的意思是查询.。。。把自己的代码改了一下,没过,直接参考写了个用优先队列和队列写的代码,有时间改一下,还有就是注意有多组测试数据,看到有人用multiset也A了,有时间学一下
错误代码1:
错误代码2:
AC代码:
AC代码:
大神地址:点击打开链接
思路:刚开始还以为每次输出最大值还要删除呢,调了半天没有过,看别人的代码,没看到把最大值弹出,每次只保留最大值,以为是数据的BUG,结果又一看题query的意思是查询.。。。把自己的代码改了一下,没过,直接参考写了个用优先队列和队列写的代码,有时间改一下,还有就是注意有多组测试数据,看到有人用multiset也A了,有时间学一下
错误代码1:
#include <iostream> #include <queue> #include <cstdio> using namespace std; struct num { int x; bool operator < (const num &a) const { return x < a.x;//最大值优先 } }; int main() { priority_queue<num> pq; num out,in; int n,a,b,sum; while(scanf("%d",&n) != EOF) { sum = 0; while(n--) { while(!pq.empty()) pq.pop(); scanf("%d",&a); if(a == 1) { scanf("%d",&b); in.x = b; pq.push(in); } else if(a == 2) { if(!pq.empty()) sum++; else sum = 0; } else { if(sum < pq.size() && !pq.empty()) { out = pq.top(); pq.pop(); printf("%d\n",out.x); } else { printf("0\n"); } } } } return 0; }
错误代码2:
#include <iostream> #include <queue> #include <cstdio> using namespace std; struct num { int x; bool operator < (const num &a) const { return x < a.x;//最大值优先 } }; int main() { priority_queue<num> pq; num out,in; int n,a,b,sum; while(scanf("%d",&n) != EOF) { sum = 0; while(n--) { while(!pq.empty()) pq.pop(); scanf("%d",&a); if(a == 1) { scanf("%d",&b); in.x = b; pq.push(in); } else if(a == 2) { if(!pq.empty()) sum++; } else { if(sum < pq.size()) { out = pq.top(); printf("%d\n",out.x); } else { printf("0\n"); } } } } return 0; }
AC代码:
#include <iostream> #include <queue> #include <cstdio> using namespace std; const int inf = 0x3f3f3f3f; int main() { int n,a,b,max1; while(scanf("%d",&n) != EOF) { queue<int> q;//在里面定义省得清空 max1 = -inf; while(n--) { scanf("%d",&a); if(a == 1) { scanf("%d",&b); q.push(b); max1 = max(max1,b); } else if(a == 2) { if(!q.empty()) { q.pop(); } if(q.empty()) max1 = -inf; } else { if(q.empty()) { printf("0\n"); } else { printf("%d\n",max1); } } } } return 0; }
AC代码:
#include <iostream> #include <queue> #include <cstdio> using namespace std; const int inf = 0x3f3f3f3f; struct num//不用重新定义,因为默认排序是升序, { int x; bool operator < (const num &a) const { return x < a.x;//最大值优先 } }; int main() { num in; int n,a,b,max1; while(scanf("%d",&n) != EOF) { priority_queue<num> pq;//在里面定义省得清空 max1 = -inf; while(n--) { scanf("%d",&a); if(a == 1) { scanf("%d",&b); in.x = b; pq.push(in); max1 = max(max1,b); } else if(a == 2) { /*if(!pq.empty()) { pq.pop();不能这样搞,会导致队列没值,最大值没变 } else max1 = -inf;*/ if(!pq.empty()) { pq.pop(); } if(pq.empty()) max1 = -inf; } else { if(pq.empty()) { printf("0\n"); } else { printf("%d\n",max1); } } } } return 0; }
大神地址:点击打开链接
相关文章推荐
- excel 关联数据
- AppDelegate.h
- 【暑期基础2】J HDU 2035 人见人爱A^B
- 替换空格
- ABAP 面向对象(Object Orien…
- SAP ERP系统如何通过LS…
- SAP MM Purchase Order里
- C++中的深浅复制_实践示例
- 【Python】Python与文本处理langid工具包的文本语言检测和歧视
- SAP MM 评估收货…
- SAP MM ME51N&nbs…
- SAP MM ME51N&nbs…
- SAP MM ME51N&nbs…
- 把已经释放的CR的对象包到现有CR中
- 灰掉工具栏上部分按钮
- WPF Button 鼠标移动改变样式
- Changing Font style in ALV
- SAP系统开发时查询库存的方…
- 台币 日元 没有小数位问题
- ABAP把系统金额转换成字符串…