Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >D. Shortest and Longest LIS

D. Shortest and Longest LIS

作者头像
某些人
发布于 2020-04-09 07:28:18
发布于 2020-04-09 07:28:18
31800
代码可运行
举报
文章被收录于专栏:一Wa哇一天一Wa哇一天
运行总次数:0
代码可运行

题目链接:D. Shortest and Longest LIS

time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output

Gildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn) time for a sequence of length n. He wants to test himself if he can implement it correctly, but he couldn’t find any online judges that would do it (even though there are actually many of them). So instead he’s going to make a quiz for you about making permutations of n distinct integers between 1 and n, inclusive, to test his code with your output.

The quiz is as follows.

Gildong provides a string of length n−1, consisting of characters ‘<’ and ‘>’ only. The i-th (1-indexed) character is the comparison result between the i-th element and the i+1-st element of the sequence. If the i-th character of the string is ‘<’, then the i-th element of the sequence is less than the i+1-st element. If the i-th character of the string is ‘>’, then the i-th element of the sequence is greater than the i+1-st element.

He wants you to find two possible sequences (not necessarily distinct) consisting of n distinct integers between 1 and n, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.

Input Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤104).

Each test case contains exactly one line, consisting of an integer and a string consisting of characters ‘<’ and ‘>’ only. The integer is n (2≤n≤2⋅105), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is n−1.

It is guaranteed that the sum of all n in all test cases doesn’t exceed 2⋅10^5^.

Output For each test case, print two lines with n integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between 1 and n, inclusive, and should satisfy the comparison results.

It can be shown that at least one answer always exists.

Example

input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3
3 <<
7 >><>><
5 >>><

output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1 2 3
1 2 3
5 4 3 7 2 1 6
4 3 1 7 5 2 6
4 3 2 1 5
5 4 2 1 3

Note In the first case, 1 2 3 is the only possible answer. In the second case, the shortest length of the LIS is 2, and the longest length of the LIS is 3. In the example of the maximum LIS sequence, 4 ‘3’ 1 7 ‘5’ 2 ‘6’ can be one of the possible LIS.

题目大意

先给你一个数字 t 便是有T 组,然后给了一个N 表示有N个数,后面跟着N-1个字符,表示一个大小关系,然后让你输出个一构造出的最短和最长的上升子序列。

解题思路

看了大佬们的博客后才知道,远了这个题这么妙,如果是最长的我肯定想尽可能的让所有的 > 可以联合到一起,对于最短的我们一个策略就是让 > 尽可能的不能相连,那么的话我们可以采取一个分块的策略,分别根据 < 和 > 进行分块,对于最短的我们肯定是我们按照 > 将这个序列分成若干块,让大的数子尽可能的放到前面,对于最长的我们按照 < 进行换分让前面的数字尽可能的小,让我们看下代码吧

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<bits/stdc++.h>
using namespace std;

int a[200010],b[200100];
string s;
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n>>s;
        int l=0,num=n;//先处理最短的 
        for(int i=0;i<n;i++)
        {
            if(i==n-1||s[i]=='>')//当走倒最后一个,或者遇到 > 进行划分 
            {                    //将其分成一块进行赋值 
                for(int j=i;j>=l;j--)//因为是最短,所以需要让前面的数字尽可能的大 
                a[j]=num--;// 因为是用 > 进行分割的所以这一块里面都是小于号   
                l=i+1;       //从后向前开始复制,之后移动左边界    
            }
        }
        l=0,num=1;
        //处理最长的 
        for(int i=0;i<n;i++)
        {
            if(i==n-1||s[i]=='<')//这是按照 < 进行划分 
            {
                for(int j=i;j>=l;j--) //将前面的值尽可能的小 
                b[j]=num++;//因为这个区间内都是 > 号所以还是才有从后向前走 
                l=i+1;
            }
        }
        for(int i=0;i<n;i++) cout<<a[i]<<" ";
        cout<<"\n";
        for(int i=0;i<n;i++) cout<<b[i]<<" ";
        cout<<"\n";
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
ACM 省赛E题 最长的递增子序列(动态规划+最长递增子序列)--------C语言—菜鸟级
最长的递增子序列 Bobo学会了如何计算ICPCCamp中O(nlogn)中的最长增加子序列(LIS)。 对于那些没有加入ICPCCamp的人来说,召回LIS(a1,a2,…,an)被定义为f [
Fivecc
2022/11/21
4460
Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in
Angel_Kitty
2018/04/09
9720
Codeforces Round #FF (Div. 2):C. DZY Loves Sequences[通俗易懂]
DZY has a sequence a, consisting of n integers.
全栈程序员站长
2022/07/08
2210
codeforces 1353D(优先队列)
You are given an array a of length n consisting of zeros. You perform n actions with this array: during the i-th action, the following sequence of operations appears:
dejavu1zz
2020/10/23
2720
CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)
ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from 1 to n, inclusive. Let’s denote the i-th (1 ≤ i ≤ n) element of the permutation a as ai, the j-th (1 ≤ j ≤ n) element of the permutation b — as bj.
风骨散人Chiam
2020/10/28
5520
Codeforce 270D Greenhouse Effect
Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.
风骨散人Chiam
2020/10/28
3740
CodeForces - 1245 B - Restricted RPS(贪心)
Let nn be a positive integer. Let a,b,ca,b,c be nonnegative integers such that a+b+c=na+b+c=n.
风骨散人Chiam
2020/10/28
4330
leetcode 300. Longest Increasing Subsequence
找到整数数组中最长的递增子数组。该子数组可以为不连续的。如题目中例子所示,[10, 9, 2, 5, 3, 7, 101, 18]得到的最长子数组为[2,3,7,101]。
眯眯眼的猫头鹰
2018/10/31
4200
Codeforces Round #633 (Div. 2)C Powered Addition (贪心,二进制)
You have an array aa of length nn. For every positive integer xx you are going to perform the following operation during the xx-th second:
glm233
2020/09/28
3270
Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output At regular competition Vladik and Valera won a and b candies respectively. Vladik offered 1 his candy to Valera. After that
Angel_Kitty
2018/04/09
8550
Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
Brackets
Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular br
attack
2018/04/12
5610
Brackets
HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory limit : 32 M Submitted : 188, Accepted : 113 5.1 Description We give the following inductive definition of a “regular brackets” sequence:
ShenduCC
2018/04/26
5210
HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 928    Accepted Submission(s): 312 Problem Description In graph theory, the  of a graph  is a graph  on the same vertices such th
ShenduCC
2018/04/27
6180
第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)
Time Limit: 1 Second Memory Limit: 65536 KB
风骨散人Chiam
2020/10/28
3240
DP专题 | LIS POJ - 2533
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK<= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
ACM算法日常
2019/06/17
4080
Codeforce 1251C. Minimize The Integer
C. Minimize The Integer time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros.
风骨散人Chiam
2020/10/28
3960
Codeforces Round #411 (Div. 2)(A,B,C,D 四水题)
A. Fake NP time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path. You ar
Angel_Kitty
2018/04/09
1.1K0
Codeforces Round #411 (Div. 2)(A,B,C,D 四水题)
POJ 2955 区间DP必看的括号匹配问题,经典例题
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14226 Accepted: 7476 Description
风骨散人Chiam
2020/10/28
5300
CodeForces - 1102B Array K-Coloring
B. Array K-Coloring time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output
风骨散人Chiam
2020/10/28
3610
Codeforces Round #619 (Div. 2)
A 题目 You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is ai, the i-th letter of b is bi, the i-th letter of c is ci.
杨鹏伟
2020/09/11
3540
推荐阅读
相关推荐
ACM 省赛E题 最长的递增子序列(动态规划+最长递增子序列)--------C语言—菜鸟级
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验