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
  • 相关阅读:
    小组自评、互评及反馈
    "一个程序员的生命周期"读后感
    阅读《构建之法》第10-12章
    阅读《构建之法》第8,9,10章
    冒泡算法程序分析树的评论
    1014 C语言文法定义与C程序的推导过程
    0917 实验一词法分析程序
    0909 我与编译原理不得不说的秘密
    复利计算器4
    复利及单利计算
  • 原文地址:https://www.cnblogs.com/smallocean/p/8886703.html
Copyright © 2011-2022 走看看