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
  • 相关阅读:
    JSP第二次作业
    软件测试课堂练习
    内容提供者读取短信信息
    购物车
    第六周jsp
    第四周jsp
    第一周 软件测试
    第八次安卓
    安卓第七次作业
    安卓第六次作业
  • 原文地址:https://www.cnblogs.com/smallocean/p/8886703.html
Copyright © 2011-2022 走看看