Increasing and Decreasing(七,学习总结)

在这里插入图片描述
题意:有三个数n,x,y。按照字典顺序求长度为n,最长上升子序列为x,最长下降子序列为y的最小排列。

题解:将排列分成x块,每块都为递减的,数最多的块个数为y,就是最长递减数列。而递增的就从x各块中个取一个形成。

注意:每块求法;在这里插入图片描述
为不满足时的情况。

#include <bits/stdc++.h>
using namespace std;
int n,x,y;
int tcase,res,tot = 0,ans[100005];
int main(){
	cin>>tcase;
	while(tcase--){
     	//ios::sync_with_stdio(0);
		//cin.tie(0);cout.tie(0);
		
		tot = 0;
		cin>>n>>x>>y;
		//不满足的情况
		if(n<x+y-1||n>1ll*x*y){
			cout<<"NO"<<endl;
			continue;//下一个循环 
		}
		//满足则分x块,每块都是递减,第x块最长为y 
		 cout<<"YES"<<endl;
		 res = 0;
		 while(1ll*y*(x-1)>=n-res-1&&res<n){
		 	res++;
		 	ans[++tot] = res;
		 	x--;
		 }
		 for(int i=res+(n-res)%y;i>res;i--){
		 	ans[++tot] = i;
		 }
		 if((n-res)%y)x--;
         res+=(n-res)%y;
		 for(int i=1;i<=x;i++){
		       for(int j=res+y;j>res;j--)
		       	ans[++tot] = j;
			    res+=y;
		 }
		 for(int i=1;i<=n;i++){
		 	if(i!=n){
		 		cout<<ans[i]<<" ";
			 }
			 else{
			 	cout<<ans[i]<<endl;
			 }
		 } 
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值