题解:AT_arc206_d [ARC206D] LIS ∩ LDS

· · 题解

赛时拼了老命,WA 了 5 发,终于临结束把这题给踩出来了!

分类讨论:

以上规律均为打表总结。

代码(在做不出来就掉很多 rating 的危急情况下写的,所以比较乱,还和讲得有点不一样,见谅):

// Copyright (C) 2012-2025 zyx2012.
#define debug(...) nullptr
#define stdlib(...) nullptr
#define streamdebug(...) nullptr
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef __int128 i128;
typedef unsigned __int128 u128;
typedef float f32;
typedef double f64;
typedef long double f80;
typedef __float128 f128;
constexpr int m99 = 998244353;
constexpr int m17 = 1e9 + 7;
mt19937 mrand(random_device{}());
inline int rnd(int l,int r){return uniform_int_distribution<int>(l,r)(mrand);}
#define rndshuffle(bg,en) shuffle(bg,en,mrand)
int main (){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T;
    cin >> T;
    for (;T--;)
    [&](){
    int n,k;
    cin >> n >> k;
    if (n == k){
        for (int i = n;i;i--)
            cout << i << ' ';
        return 0;
    }
    if (n & 1&&k == 1&&n > 3){
        cout << n - 1 << ' ';
        for (int i = 1;i <= n;i += 2)
            cout << i << ' ';
        for (int i = n - 3;i;i -= 2)
            cout << i << ' ';
        return 0;
    }
    if (n == 8&&!k){
        cout << "3 4 8 7 2 1 5 6";
        return 0;
    }
    if (!k&&n > 8){
        for (int i = 1;i <= n - 8;i++)
            cout << i << ' ';
        cout << n - 5 << ' ' << n - 4 << ' ' << n << ' ' << n - 1 << ' ' << n - 6 << ' ' << n - 7 << ' ' << n - 3 << ' ' << n - 2 << '\n';
        return 0;
    }
    if (k == 1&&n > 4){
        cout << n << ' ' << n - 2 << ' ';
        for (int i = 1;i < n;i += 2)
            cout << i << ' ';
        for (int i = n - 4;i;i -= 2)
            cout << i << ' ';
        return 0;
    }
    if (k < 2)
        return cout << "-1",0;
    vector<int> a;
    for (int i = 1;i <= n;i++)
        a.push_back(i);
    reverse(a.end() - k,a.end());
    for (auto i:a)
        cout << i << ' ';
    return 0;
    }(),cout << '\n';
}