arc206d - LIS ∩ LDS

Problem

Problem Statement

Among the elements of a permutation P = ( P 1 , ... , P N ) P = (P_1, \dots, P_N) P=(P1,...,PN) of ( 1 , ... , N ) (1, \dots, N) (1,...,N), those that satisfy the following condition are called good elements:

  • The element can be included in both a longest increasing subsequence and a longest decreasing subsequence of P P P.

You are given integers N N N and K K K. Determine whether there exists a permutation P P P of ( 1 , ... , N ) (1, \dots, N) (1,...,N) such that there are exactly K K K good elements, and if it exists, find one.

Answer for T T T test cases.

Constraints

  • All input values are integers
  • 1 ≤ T ≤ 2 × 10 5 1 \le T \le 2 \times 10^5 1≤T≤2×105
  • 1 ≤ N ≤ 2 × 10 5 1 \le N \le 2 \times 10^5 1≤N≤2×105
  • 0 ≤ K ≤ N 0 \le K \le N 0≤K≤N
  • The sum of N N N over all test cases does not exceed 2 × 10 5 2 \times 10^5 2×105.

Translation

题目描述

在 ( 1 , ... , N ) (1, \dots, N) (1,...,N) 的排列 P = ( P 1 , ... , P N ) P = (P_1, \dots, P_N) P=(P1,...,PN) 中,满足以下条件的元素被称为"好元素":

  • 该元素可以同时包含在 P P P 的一个最长上升子序列(LIS)和一个最长下降子序列(LDS)中。

给定整数 N N N 和 K K K。请判断是否存在一个 ( 1 , ... , N ) (1, \dots, N) (1,...,N) 的排列 P P P,使得其中恰好包含 K K K 个好元素。如果存在,请输出该排列。

本题包含 T T T 组测试用例。

约束条件

  • 所有输入值均为整数
  • 1 ≤ T ≤ 2 × 10 5 1 \le T \le 2 \times 10^5 1≤T≤2×105
  • 1 ≤ N ≤ 2 × 10 5 1 \le N \le 2 \times 10^5 1≤N≤2×105
  • 0 ≤ K ≤ N 0 \le K \le N 0≤K≤N
  • 所有测试用例中 N N N 的总和不超过 2 × 10 5 2 \times 10^5 2×105。

Solutions

Approach 1

分类讨论 + 构造。原本还以为是分析性质的大构造,结果半天分析不出来性质。

首先,考虑 N N N 和 K K K 足够大的时候,如果你能在此时看出一个构造方案,那么这题就不难。

显然,可以让 1 \\sim K 按 K , K − 1 , ⋯   , 1 K,K-1,\cdots,1 K,K−1,⋯,1 的顺序排在最前面,然后其余按 K + 1 , K + 2 , ⋯   , N K+1,K+2,\cdots,N K+1,K+2,⋯,N 的顺序排在后面,这就是一个足够的构造。

现在分析有哪些 N N N 和 K K K 取值的边界,这里就是试,反正我做的时候就是试出来的:

  1. 当 K > 1 K > 1 K>1 ,任意 N N N 都可以使用上述。

  2. 当 K = 1 K = 1 K=1 ,此时又分为三种:

    1. N = 1 N = 1 N=1 ,简单。
    2. K ⩾ 5 K \geqslant 5 K⩾5 ,此时的构造形式形如 2 , N , 3 , 1 , 4 , ⋯   , N − 1 2,N,3,1,4,\cdots,N-1 2,N,3,1,4,⋯,N−1 。
    3. 其余不合法。
  3. 当 K = 0 K = 0 K=0 ,此时又分为两种:

    1. N ⩾ 8 N \geqslant 8 N⩾8 ,此时构造形式形如 3 , 4 , N , N − 1 , 2 , 1 , 5 , 6 , ⋯   , N − 2 3,4,N,N-1,2,1,5,6,\cdots,N-2 3,4,N,N−1,2,1,5,6,⋯,N−2 。
    2. 其余不合法。

确认了一下题解,基本完全一样的形式 - Editorial - AtCoder Regular Contest 206 (Div. 2)

本身以为是分析一个点同时处于递增和递降有什么性质,结果就是基本的大分类讨论和试。

Code
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

std::mt19937 rng(std::random_device{}());
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int mod = 998244353;
const double ept = 1e-9;

#define db(x...) { do { cerr << #x << " -> "; err(x); } while (0); }
#define dbv(vec) { cout << #vec << ": "; for(auto &v: vec) { cout << v << ' '; } cout << endl; }
void err() { cerr << endl; } template<class T, class... Ts> void err(const T& arg, const Ts&... args) { cerr << arg << ""; err(args...); }

int n, k;

void solve(int T) {
  cin >> n >> k;

  vector<int> ans;
  if(k == 0) {
    if(n >= 8) {
      ans.push_back(3);
      ans.push_back(4);
      ans.push_back(n);
      ans.push_back(n-1);
      ans.push_back(2);
      ans.push_back(1);
      for(int i=5; i<n-1; i++) ans.push_back(i);
    }
  }
  else if(k == 1) {
    if(n == 1) ans.push_back(1);
    else if(n >= 5) {
      ans.push_back(2);
      ans.push_back(n);
      ans.push_back(3);
      ans.push_back(1);
      for(int i=4; i<n; i++) ans.push_back(i);
    }
  }
  else {
    for(int i=k; i>=1; i--) ans.push_back(i);
    for(int i=k+1; i<=n; i++) ans.push_back(i);
  }

  if(ans.size() < n) cout << -1;
  else for(auto &u: ans) cout << u << ' ';
  cout << endl;
}

void init() {}

signed main() {
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    //cout.flags(ios::fixed); cout.precision(8);
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    init();
    int T_=1;
    std::cin >> T_;
    for(int _T=1; _T<=T_; _T++) { solve(_T); }
    return 0;
}
相关推荐
Stardep1 天前
算法入门19——二分查找算法——X的平方根
算法·leetcode·二分查找算法
We་ct1 天前
LeetCode 135. 分发糖果:双向约束下的最小糖果分配方案
前端·算法·leetcode·typescript
宇钶宇夕1 天前
CoDeSys入门实战一起学习(十三):函数(FUN)深度解析:自定义、属性与实操案例
运维·算法·自动化·软件工程
l1t1 天前
对clickhouse给出的二分法求解Advent of Code 2025第10题 电子工厂 第二部分的算法理解
数据库·算法·clickhouse
Tisfy1 天前
LeetCode 3315.构造最小位运算数组 II:位运算
算法·leetcode·题解·位运算
YuTaoShao1 天前
【LeetCode 每日一题】1292. 元素和小于等于阈值的正方形的最大边长
算法·leetcode·职场和发展
Remember_9931 天前
【数据结构】深入理解Map和Set:从搜索树到哈希表的完整解析
java·开发语言·数据结构·算法·leetcode·哈希算法·散列表
浅念-1 天前
C++第一课
开发语言·c++·经验分享·笔记·学习·算法
charlie1145141911 天前
现代嵌入式C++教程:对象池(Object Pool)模式
开发语言·c++·学习·算法·嵌入式·现代c++·工程实践
燃于AC之乐1 天前
我的算法修炼之路--8——预处理、滑窗优化、前缀和哈希同余,线性dp,图+并查集与逆向图
算法·哈希算法·图论·滑动窗口·哈希表·线性dp