zoukankan      html  css  js  c++  java
  • K-th K

    题目描述

    You are given an integer sequence x of length N. Determine if there exists an integer sequence a that satisfies all of the following conditions, and if it exists, construct an instance of a.

    a is N2 in length, containing N copies of each of the integers 1, 2, …, N.
    For each 1≤i≤N, the i-th occurrence of the integer i from the left in a is the xi-th element of a from the left.
    Constraints
    1≤N≤500
    1≤xi≤N2
    All xi are distinct.

    输入

    The input is given from Standard Input in the following format:

    N
    x1 x2 … xN

    输出

    If there does not exist an integer sequence a that satisfies all the conditions, print 'No'. If there does exist such an sequence a, print 'Yes'.

    样例输入

    3
    1 5 9
    

    样例输出

    Yes
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    int a[255000];
    struct node
    {
        int num;
        int x;
    }s[2550];
    bool cmp(node a,node b)
    {
        return a.x<b.x;
    }
    int main()
    {
        int i,j,n;
        cin>>n;
        for(i=1;i<=n;i++){
            cin>>s[i].x;
            s[i].num=i;
        }
        sort(s+1,s+n+1,cmp);
        int now=1;
        for(i=1;i<=n;i++){
            a[s[i].x]=s[i].num;
            for(j=1;j<s[i].num;j++){
                while(a[now]) now++;
                a[now]=s[i].num;
            }
            if(now>s[i].x) {cout<<"No"<<endl;return 0;}
        }
        for(i=1;i<=n;i++){
            for(j=1;j<=n-s[i].num;j++){
                while(a[now]) now++;
                if(now<s[i].x) {cout<<"No"<<endl;return 0;}
                a[now]=s[i].num;
            }
        }
        cout<<"Yes"<<endl;
        return 0;
    }
    View Code

    For each 1≤i≤N, the i-th occurrence of the integer i from the left in a is the xi-th element of a from the left.

    就这句话。。。仔细读

    第i个i在下标为xi的地方。嗯

    这是贪心,先放上这些数字应在的位置

    再从小到大把i弄上去

    排除不可能的解

    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    枚举8项素数和环
    登录过滤器
    线程调度
    回溯素数环
    centos 6.5 samba简单配置
    区间k大数查询
    Centos安装arm-linux-gcc等交叉工具链
    centos7安装tftp服务器
    八皇后问题
    输出1——n的排列(深度优先搜索)
  • 原文地址:https://www.cnblogs.com/smallocean/p/8886703.html
Copyright © 2011-2022 走看看