zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    将一个float型转化为内存存储格式的步骤
    判断是否是中文、英文字符串
    打开文件对话框
    线性表之四,受限的线性表,队列
    线性表之三,受限的线性表,栈
    数据结构
    List
    SamplesHashtable
    Exception
    maven指定本地仓库
  • 原文地址:https://www.cnblogs.com/lhsghhqgmzy/p/14499421.html
Copyright © 2011-2022 走看看