• Early Orders


    Early Orders

    题意:

    n个数 范围 1-k, 保证都存在, 找到 大小为k的 k个数都存在的字典序 最小的序列

    题解:

    我们 需要 尽可能地让答案正确

    则:

    我们给定一个 栈 ans[] 保存可能出现在答案中的 数值 把不可能存在的全都删掉

    一个数的值为v, 位置为 vis

    则一定满足:

    ans[]中存在v, 忽略当前操作

    ans[] 中不存在v, 则把 之前的所有大于 该值的 且在 vis后 出现过的 数 删掉 。

    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    int n,k;
    const int N = 2e5+5;
    int arr[N];
    stack<int>p1;
    int last[N];
    
    void output(stack<int> P){
    	stack<int>y;
    	while(!P.empty()){
    		y.push(P.top());
    		P.pop();
    	}
    	while(!y.empty()){
    		cout<<y.top()<<" ";y.pop();
    	}cout<<endl;
    }
    signed main(){
    	cin >> n >> k;
    	for(int i=1;i<=n;i++){
    		cin >> arr[i];
    		last[arr[i]] = i;
    	}
    	set<int>st;
    	for(int i=1;i<=n;i++){
    		if(p1.empty()){
    			p1.push(arr[i]);st.insert(arr[i]);continue;
    		}
    		while(1){
    			int tp = p1.top();
    			if(tp > arr[i] && last[tp] > i && st.count(arr[i])==0){
    				st.erase(tp);p1.pop();
    			}
    			else{
    				break;
    			}
    			if(p1.empty())break;
    		}
    		if(!st.count(arr[i])){
    			p1.push(arr[i]);
    		}
    		st.insert(arr[i]);
    	}
    	output(p1);
    }
    
  • 相关阅读:
    4A安全体系
    Linux 安装 Nginx
    Linux 安装 Mysql
    Web容器对比
    Nginx 学习记录
    SO_REUSEADDR选项的平台差异(对于TCP)
    centos下搭建yum服务器
    IOCP 下行为投递的关键点
    网络包解包错误 与 标准io(linux)
    关闭ssh的dns反向解析
  • 原文地址:https://www.cnblogs.com/lhsghhqgmzy/p/14499421.html
Copyright © 2020-2023  润新知