• 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; 
    }
  • 相关阅读:
    Excel 实用技巧之一
    Windows操作技巧 之二(持续更新)
    ASCII码表
    Excel 函数VLOOKUP初学者使用指南
    Windows 操作小技巧 之一(持续更新)
    Excel 使用宏批量修改单元格内指定文字为红字
    Excel 使用CHIINV函数和GAMMA.DIST函数绘制卡方分布
    新手使用R的注意事项
    如何在R中加载”xlsx”包
    增值税——基础知识
  • 原文地址:https://www.cnblogs.com/ember/p/5724099.html
Copyright © 2020-2023  润新知