2020杭电HDU-6852多校第七场Increasing and Decreasing(构造+思维模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6852
博客园食用链接:https://www.cnblogs.com/lonely-wind-/p/13485238.html

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 ) T(1≤T≤100) T(1T100), indicating the number of test cases.

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

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 x x,最长下降子序列长度为 y y y

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

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

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 x*y<n xy<n就不行。。。。。我当时是一脸懵逼的,不过确实是正确的。。。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

#define debug printf("#@$@#$\n")
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\n");
            for (int i=n; i>=1; i--)
                printf("%d%c",i,i==1?'\n':' ');
            continue;
        }
        if (!zu && y!=n) {printf("NO\n"); continue;}
        if (x+y>n+1) {printf("NO\n"); continue;}
        if (1LL*x*y<n) {printf("NO\n"); 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\n");
        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("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值