題目鏈接
題意
給出a和b,讓你構造出來最長的、且最長上升子序列長度爲a,最長下降子序列長度爲b的序列。
這個題我們沒有想出來要怎麼構造 ,還停留在這個最大長度是不是a+b(b等於1的時候是a)的階段
解決
思路是創建b組長度爲a的上升子序列,而且每一組的最大值都比前一組的最小值來的小,則必然滿足題意
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6;
void open()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
}
//思路是創建b組長度爲a的上升子序列,而且每一組的最大值都比前一組的最小值來的小,則必然滿足題意
int main()
{
int a,b;
int num[300000];
while(~scanf("%d%d",&a,&b))
{
int tot=a*b;
printf("%d\n",tot);
int i,j,k;
for(i=1;i<=b;i++) //共創建b組連續上升的子序列,正在創建第i個序列
{
for(j=i*a,k=tot-(i-1)*a;j>(i-1)*a;j--) //a是長度,k爲當前要填充的數字
{
num[j]=k;
k--;
}
}
printf("%d",num[1]);
rep(i,2,tot+1) printf(" %d",num[i]);
puts("");
}
}