zoukankan      html  css  js  c++  java
  • 2020杭电HDU-6852多校第七场Increasing and Decreasing(构造+思维模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6852
    CSDN食用链接:https://blog.csdn.net/qq_43906000/article/details/107944569

    Notice:Don't output extra spaces at the end of one line.

    Given n,x,y, please construct a permutation of length n, satisfying that:

    • The length of LIS(Longest Increasing Subsequence) is equal to x.
    • The length of LDS(Longest Decreasing Subsequence) is equal to y.

    If there are multiple possible permutations satisfying all the conditions, print the lexicographically minimum one.

    Input
    The first line contains an integer (T(1≤T≤100)), indicating the number of test cases.

    Each test case contains one line, which contains three integers (n,x,y(1≤n≤10^5,1≤x,y≤n)).

    Output
    For each test case, the first line contains "YES'' or "NO'', indicating if the answer exists. If the answer exists, output another line which contains n integers, indicating the permutation.

    Sample Input
    4
    10 1 10
    10 10 1
    10 5 5
    10 8 8

    Sample Output
    YES
    10 9 8 7 6 5 4 3 2 1
    YES
    1 2 3 4 5 6 7 8 9 10
    YES
    1 2 3 5 4 10 9 8 7 6
    NO

    题目大意:给你一个n,你需要构造一个1-n的排列使得其最长上升子序列长度为(x),最长下降子序列长度为(y)

    emmm,首先可以肯定的是(x,y)是有上限的,对于(x+y>n+1)的而言直接输出“NO”了。至于其他的我们可以慢慢来探索,

    首先我们可以构造一个最长下降子序列为(y)(n,n-1,n-2,...,n-y+1)放在最末尾,那么会剩下(n-y)个数,接下来我们需要构造(x-1)个块来构造最长上升子序列,我们要使其字典序最小,那么也就是说我们尽量在前面只放一个数,而如果(x-1)个块每个块放完了还有剩余的数的话我们为了保证字典序最小,那么只能尽量地将大的数往后丢,那么就可以得到:

    sz[x]=y;
    int p=lst-zu;
    for (int i=x-1; i>=1; i--) {
    	if (p>y-1) {p-=y-1;sz[i]=y;} //如果有多余的数尽量使得后面的块填满
    	else {sz[i]=p+1;break;}
    }
    

    最后将每个块中的数全部倒置就可以了。接下来就是对于“NO”的判断了,神仙队友直接报了个(x*y<n)就不行。。。。。我当时是一脸懵逼的,不过确实是正确的。。。

    以下是AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    #define debug printf("#@$@#$
    ")
    const int mac=1e5+10;
    
    vector<int>g[mac];
    int sz[mac];
    
    int main(int argc, char const *argv[])
    {
        int t;
        scanf ("%d",&t);
        while (t--){
            memset(sz,0,sizeof sz);
            int n,x,y;
            scanf ("%d%d%d",&n,&x,&y);
            for (int i=1; i<=n; i++) g[i].clear();
            int lst=n-y;
            int zu=x-1;
            if (!zu && y==n) {
                printf("YES
    ");
                for (int i=n; i>=1; i--)
                    printf("%d%c",i,i==1?'
    ':' ');
                continue;
            }
            if (!zu && y!=n) {printf("NO
    "); continue;}
            if (x+y>n+1) {printf("NO
    "); continue;}
            if (1LL*x*y<n) {printf("NO
    "); continue;}
            sz[x]=y;
            int p=lst-zu; 
            for (int i=x-1; i>=1; i--){
                if (p>y-1) {p-=y-1; sz[i]=y;}
                else {sz[i]=p+1; break;}
            }
            int cnt=1;       
            printf("YES
    ");
            for (int i=1; i<=zu; i++){
                if (!sz[i]) {g[i].push_back(cnt++); continue;}
                for (int j=1; j<=sz[i]; j++)
                    g[i].push_back(cnt++);
            }
            for (int i=1; i<=y; i++){
                g[zu+1].push_back(cnt++);
            }
            for (int i=1; i<=zu+1; i++){
                reverse(g[i].begin(),g[i].end());
                if (i==zu+1){
                    for (int j=0; j<g[i].size()-1; j++)
                        printf("%d ",g[i][j]);
                    printf("%d",g[i][g[i].size()-1]);
                }
                else for (auto v:g[i]) printf("%d ",v);
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    Maven打包时去掉项目版本号
    maven编译的时候排除junit测试类
    Redis与Zookeeper实现分布式锁的区别
    分布式锁(基于redis和zookeeper)详解
    解读阿里巴巴集团的“大中台、小前台”组织战略
    java高并发系列
    JAVA之Unsafe学习笔记
    测试用例之正交排列法
    测试用例之因果图/判定表
    测试用例之边界值法
  • 原文地址:https://www.cnblogs.com/lonely-wind-/p/13485238.html
Copyright © 2011-2022 走看看