zoukankan      html  css  js  c++  java
  • 2019福建省队集训day2T3排序sort

    题目描述:

     

    思路:

    把i不变的时候叫做一轮,则前i轮操作后,cnt=sigma(n-1,n-2...n-i)。所以我们可以很快知道题目要求达到的cnt的值是第几轮得到的结果。

    之后我们考虑对于一个一轮操作,我们会把第i个位置变成i同时从第i位的单调下降序列往后移动一格下降序列的最后一个数就是i移到i这个位置。

    所以当某个位置前有一个比我小的数,则我在这轮这个位置上的数不会移动,当我前面每个数都比我大,我会变成在我之前最小的数字

    所以k轮之后,如果前面比我小的数字超过k个则我是我自己,否则这个位置的数字为在我之前第k小的数字。用一个栈维护即可。

    还有另一种思路,

    考虑每个数移到哪个位置,

    我们按照数字大小排序,前k小的数的位置很好确定。

    对于第i小的数,如果比我小的k个数的位置都小于我,则我的位置不会发生移动,否则每次出现一个比我小在我之后的数字时构成,因为会构成单调下降序列,所以我会往比我小的位置第k靠后的位置移动。

    也可以有栈维护,不过栈内维护的是位置。

    我写的时第二种思路

    以下代码:

    #include<bits/stdc++.h>
    #define il inline
    #define LL long long
    #define _(d) while(d(isdigit(ch=getchar())))
    using namespace std;
    const int N=1e6+5;
    int n,b[N];LL cnt;
    struct node{
        int x,id;
    }a[N];
    priority_queue<int> q;
    il LL read(){
        LL x,f=1;char ch;
        _(!)ch=='-'?f=-1:f;x=ch^48;
        _()x=(x<<1)+(x<<3)+(ch^48);
        return f*x;
    }
    bool cmp(node t1,node t2){
        return t1.x<t2.x;
    }
    int main()
    {
        freopen("sort.in","r",stdin);
        freopen("sort.out","w",stdout);
        n=read();cnt=read();
        int k=0,x=n-1;
        while(cnt-x>=0){
            cnt-=x;x--;k++;
        }
        for(int i=1;i<=n;i++){
            a[i].x=b[i]=read();a[i].id=i;
        }
        sort(a+1,a+1+n,cmp);
        for(int i=1;i<=k;i++){
            b[i]=i;q.push(a[i].id);
        }
        if(k)for(int i=k+1;i<=n;i++){
            int x=q.top();
            if(x<a[i].id)continue;
            b[x]=a[i].x;q.pop();q.push(a[i].id);
        }
        for(int i=k+2;;i++){
            if(cnt==0)break;
            cnt--;if(b[k+1]>=b[i])swap(b[k+1],b[i]);
        }
        for(int i=1;i<=n;i++)printf("%d ",b[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    【24点游戏】cocos2dx 源码
    『Python题库
    『Python题库
    【python安装】Windows上安装和创建python开发环境
    『Linux基础
    『Linux基础
    『Linux基础
    『Linux基础
    『Linux基础
    『Python基础-14』匿名函数 `lambda`
  • 原文地址:https://www.cnblogs.com/Jessie-/p/10382235.html
Copyright © 2011-2022 走看看