zoukankan      html  css  js  c++  java
  • Codeforces Round #357 (Div. 2)

    WA * 3 + TLE *1  啊啊啊!说好的考虑问题要仔细呢?!

     

    题意:


    简单说就是,有一串操作,丢失了一部分,只有 n 个操作了, 补全操作,使得每次 getMin 得到对应的值。
    输出所有操作的个数和操作序列。

    解题:

    用优先队列直接模拟过来的,标记一下某些位置 表示它之前还要进行哪些操作 才能做到,最后直接输出;
    对于每个情况,
    如果是 removeMin , 考虑队列是否为空,如果空,就要先插入一个;;
    如果是 getMin ,
    考虑 当前队列最小的元素 比 给出的数 大还是小,
    如果小的话要把小的都出队,注意可能它们不止一个(WA了一次= =),记录一下步数;
    如果大的话, 直接入队即可。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<queue>
    using namespace std;
    
    priority_queue<int,vector<int>,greater<int> > q;
    
    const int maxn = 100010;
    int ins[maxn],re[maxn],op[maxn];
    char str[maxn][20];
    int main()
    {
        int n;
        while(!q.empty()) q.pop();
        scanf("%d",&n);
        int cnt = n,cr=0;
        for(int i=0;i<n;i++){
            scanf("%s",str[i]);
            if( strcmp(str[i],"removeMin") != 0 )
                scanf("%d",&op[i]);             
            if(strcmp(str[i],"insert") == 0){
                q.push(op[i]);
            }
            if(strcmp(str[i],"removeMin")==0){
                if(q.empty()){                
                    op[i] = 0;
                    ins[i] = 1; cnt++;
                }
                else q.pop();
            }
            if(strcmp(str[i], "getMin") == 0){
                if(!q.empty()){
                    if(q.top() < op[i]){
                        while((q.top() < op[i]) && (!q.empty()) ){                
                            re[i] ++; cnt ++;
                            q.pop(); 
                        }
                        if(q.top() != op[i] ) {
                            q.push(op[i]); cnt++;
                            ins[i] = 1;
                        }                 
                    }                
                    if(q.top() > op[i]){
                        q.push(op[i]);
                        ins[i] = 1; cnt ++;
                    }
                }
                else {
                    q.push(op[i]);
                    ins[i] = 1; cnt ++;
                }            
            }        
        }
        cout<<cnt<<endl;
        for(int i=0;i<n;i++){
            if(ins[i]) printf("insert %d
    ",op[i]);
            if(re[i])
                for(int j=0;j<re[i];j++) printf("removeMin
    ");
            if(strcmp(str[i], "removeMin") != 0)
                printf("%s %d
    ",str[i],op[i]);
            else printf("%s
    ",str[i]);
        }
        return 0; 
    }
  • 相关阅读:
    Python_报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
    Python_报错:TypeError: file must have 'read' and 'readline' attributes
    Maven不扫描java文件夹下的配置文件解决办法
    Web中的相对路径和绝对路径
    sqlyog报错2058
    base标签的作用
    相对路径和绝对路径的解释
    自定义Tomcat部署目录
    常用正则表达式
    接口的结构定义
  • 原文地址:https://www.cnblogs.com/ember/p/5724099.html
Copyright © 2011-2022 走看看