zoukankan      html  css  js  c++  java
  • 洛谷——P1160 队列安排(链表的基础操作)

    #include<bits/stdc++.h> 
    using namespace std;
    bool vis[1000003];
    list<int> stus;
    list<int>::iterator pos[1000003];//用来存放每一项的迭代器 这样遍历链表的时间能从O(n)变成O(1)
    int main(){
        int n;
        scanf("%d",&n);
        stus.push_front(1);//插入1到头部 
        pos[1] = stus.begin();//放入迭代器的开始  相当于存入的位置在哪里
        for(int i = 2;i <= n;i++){
            int k,p;
            scanf("%d %d",&k,&p);
            if(p==0){//在左边
                pos[i] = stus.insert(pos[k],i);//插入左边返回一个迭代器 
            } 
            else{
                list<int>::iterator it = pos[k];//取出第k个元素迭代器的位置
                it++;
                pos[i] = stus.insert(it,i);    
            }
        }
        int m = 0;
        scanf("%d",&m);
        while(m--){
            int x = 0;
            scanf("%d",&x);
            if(!vis[x]){
               vis[x]=true;//标记他被删过了
               stus.erase(pos[x]);//传入他的迭代器     
            } 
                
        }
        for(list<int>::iterator it = stus.begin();it!=stus.end();it++){
            printf("%d ",*it);
        }
        printf("
    "); 
        
    
        return 0; 
    } 
  • 相关阅读:
    sql 常见错误
    jdbc 回顾
    oracle return code 2112
    cobol COMP-3最后1位
    oracle Lower Upper length substr
    UTF-8 与 BIG-5 转码
    用UltraEdit转换大小写
    oracle substr
    oracle中 char,varchar,varchar2的区别
    oracle 查看16进制
  • 原文地址:https://www.cnblogs.com/xiaonuolen/p/10284087.html
Copyright © 2011-2022 走看看