CF1304D Shortest and Longest LIS (最长单调序列构造)

Description

有一个 1 ∼ n 1\sim n 1n 的排列,给定 n − 1 n-1 n1 个大于号或小于号描述相邻两个数的大小关系。请你构造两个符合要求的排列,分别满足 LIS 最短和 LIS 最长。LIS : 最长上升子序列。

1 ≤ n ≤ 2 × 1 0 5 1 \leq n \leq 2 \times 10^5 1n2×105

Solution

考虑最短的 LIS。因为小于号产生的上升是不可避免的,所以让大于号产生的尽可能短。所以先让排列为 n , n − 1 , ⋯   , 2 , 1 n,n-1, \cdots ,2,1 n,n1,,2,1。对于一段连续的小于号翻转对应的区间。最长的 LIS 同理,让排列为 1 , 2 , ⋯   , n − 1 , n 1,2,\cdots,n-1,n 1,2,,n1,n,对于一段连续的大于号翻转对应的区间。

时空复杂度为 O ( n ) O(n) O(n)

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
inline int read() {
	int x = 0, f = 0; char ch = 0;
	while (!isdigit(ch)) f |= ch == '-', ch = getchar();
	while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
char s[N];
int a[N];
int main() {
	int T = read();
	while (T--) {
		int n = read(); scanf("%s", s + 1);
		for (int i = 1; i <= n; i++) a[i] = n - i + 1;
		for (int i = 1; i < n; i++) {
			int l = i;
			while (s[i] == '<') i++;
			reverse(a + l, a + i + 1);
		}
		for (int i = 1; i <= n; i++) printf("%d ", a[i]); puts("");
		for (int i = 1; i <= n; i++) a[i] = i;
		for (int i = 1; i < n; i++) {
			int l = i;
			while (s[i] == '>') i++;
			reverse(a + l, a + i + 1);
		}
		for (int i = 1; i <= n; i++) printf("%d ", a[i]); puts("");
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值