CodeForce 681C 模拟题
来源:互联网 发布:网站编程常见单词 编辑:程序博客网 时间:2024/06/15 23:27
题意
- 有一个数据结构,能执行三种命令:getMin x,removeMin,insert x。
- 现有一些随机的命令,补全这些命令使所有的命令合法,并且最终的命令数最少。
题目分析
- 这个数据结构可以用优先队列实现
- insert x永远是合法的命令。removeMin在数据结构为空时不合法,不合法时,我们可以先插入一个任意数使它合法。
- getMin会在数据空,第一个数比它大,第一个数比它小时不合法。分情况解决
注意
- 这道题我runtimeError了很久。
- 要特别注意RE,有两种常发生的re的情况。一种情况是数组越界,要么数组开小了,要么程序写错了。
- 另一种情况是容器类,在pop,top这种操作前一定要检查容器是否空。
输出
- 这道题输出有点麻烦,得先把输出结果都存储下来,最后再一并输出才行。这里有两种方法。两种方法都得用到sprintf()
- 一是用vector<string>保存结果,直接用size(),一并输出。
- 另一种是用一个超长字符串,手动记录命令个数,手动维护超长字符串的指针。
#include<cstdio>#include<queue>#include<vector>#include<string>#include<iostream>#include<cstring>using namespace std;char op[30];char tmp[30];int oprand;vector<string> log;priority_queue<int,vector<int>,greater<int> > q;void logRemoveMin(){ log.push_back("removeMin");}void queInsert(int i){ q.push(i); sprintf(tmp,"insert %d",i); log.push_back(tmp);}void logGetMin(){ sprintf(tmp,"getMin %d",oprand); log.push_back(tmp);}int main(){ int t; while(~scanf("%d",&t)) { log.clear(); while(!q.empty()) q.pop(); while(t--) { scanf("%s",op); if(op[0] == 'r')//removeMin { if(q.empty()) queInsert(1); q.pop(); logRemoveMin(); } else if(op[0] == 'g')//getMin { scanf("%d",&oprand); while(!q.empty() && (q.top() < oprand)) { q.pop(); logRemoveMin(); } if(q.empty() || (q.top() > oprand)) queInsert(oprand); logGetMin(); } else if(op[0] == 'i')//insert { scanf("%d",&oprand); queInsert(oprand); } } int size = log.size(); printf("%lu\n",size); for(int i=0;i<size;i++) cout<<log[i]<<endl; }}
#include<cstdio>#include<queue>#include<cstring>using namespace std;char op[30];int oprand;int cnt;char s[10000000];int p;priority_queue<int,vector<int>,greater<int> > q;void inputOpration(){ scanf("%s",op); if(strcmp(op,"removeMin")) scanf("%d",&oprand);}void printInsert(int i){ char tmp[100]; sprintf(tmp,"insert %d\n",i); int tmpLen = strlen(tmp); sprintf(s+p,"%s",tmp); p += tmpLen;}void printRemoveMin(){ cnt++; sprintf(s+p,"removeMin\n"); p += 10;}void queInsert(int i){ cnt++; q.push(i); printInsert(i);}int quePop(){ q.pop(); int result = q.top(); printRemoveMin(); return result;}void printGetMin(){ cnt++; char tmp[100]; sprintf(tmp,"getMin %d\n",oprand); int tmpLen = strlen(tmp); sprintf(s+p,"%s",tmp); p += tmpLen;}int main(){ int t; while(~scanf("%d",&t)) { cnt = p = 0; while(!q.empty()) q.pop(); while(t--) { inputOpration(); if(!strcmp(op,"removeMin")) { if(q.empty()) queInsert(1); q.pop(); printRemoveMin(); } else if(!strcmp(op,"getMin")) { while(!q.empty() && q.top() < oprand) quePop(); if(q.empty() || q.top() > oprand) queInsert(oprand); printGetMin(); } else if(!strcmp(op,"insert")) queInsert(oprand); } printf("%d\n%s",cnt,s); }}
阅读全文
0 0
- CodeForce 681C 模拟题
- codeforce 864C Bus(模拟)
- codeforce 509C (很有趣的贪心模拟构造)
- CodeForce 508C Anya and Ghosts (贪心+模拟)
- CodeForce 439C Devu and Partitioning of the Array(模拟)
- 【模拟】Codeforce 302B
- codeforce 215c 想法题 crosses
- codeforce round #295(div2) C题
- 简单dp(codeforce #336(c)补题
- codeforce 343div2 C dp好题
- 细节(水)题codeforce#378C
- Codeforce Round #400 C 签到题
- codeforce 893B (模拟)
- CodeForce 439C Devu and Partitioning of the Array(模拟)
- codeforce#378C. Epidemic in Monstropolis(模拟+分块+树状数组)
- Codeforce # C. Average Score
- CodeForce 279C
- codeforce 237 C D
- MessageBodyProviderNotFoundException,ModelValidationException
- Tabl栏效果的实现
- Beginning Spring学习笔记——第7章 使用Spring进行测试驱动开发
- 安装jdk后出现bash: ./java: /lib/ld-linux.so.2: bad ELF interpreter
- mybatis与spring的整合
- CodeForce 681C 模拟题
- Java正则表达式实例
- 线程安全的单例模式
- 2017微软中国社招C++研发岗面试经历(共5面)
- 面向服务与微服务架构
- 学习使我快乐
- 线程中实例化spring注入服务
- jq判断一个字符串是否存在一个数组中
- 30 Three.js的相机飞行控件FlyControls