Codeforces1304D Shortest and Longest LIS
前置扯淡
%%@\(wucstido\),思路是在是巧妙---link
Description
给一个长度为\(n\)由 \(<\) 和 \(>\)组成的字符串,表示序列中相邻位置的数的大小关系
求构造两种排列,使得其在满足上述条件的情况下的 \(LIS\) 分别最长\(/\)最短
Solution
直接考虑构造最短的排列,然后最长的同理(真的是把符号改改就可以了)
整个排列\(LIS\)的最小值在最长的一段\("<"\)中取得,然后我们考虑构造
先把整个排列逆序过来:即 \(a_i=n-i+1\)
然后对于每一个\("<"\),翻转相邻的数字
正确性显然(真的是很巧妙!!!!!!)
最长的构造就是初始化正序,大于号翻转!就完事啦!!
别想复杂了,构造题就是构造题
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm{
inline int read()
{
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
const int N=2e5+10;
char s[N]; int ans[N],n;
inline void work()
{
n=read(); scanf("%s",s+1);
for(int i=1;i<=n;++i) ans[i]=n-i+1;
for(int i=1;i<n;++i)
{
int j=i; while(s[j]=='<') ++j; reverse(ans+i,ans+j+1); i=j;
} for(int i=1;i<=n;++i) printf("%lld ",ans[i]); puts("");
for(int i=1;i<=n;++i) ans[i]=i;
for(int i=1;i<n;++i)
{
int j=i; while(s[j]=='>') ++j; reverse(ans+i,ans+j+1); i=j;
} for(int i=1;i<=n;++i) printf("%lld ",ans[i]); puts("");
return ;
}
signed main()
{
int T=read(); while(T--) work();
return 0;
}
}
signed main(){return yspm::main();}
Codeforces1304D Shortest and Longest LIS的更多相关文章
- Shortest and Longest LIS
Codeforces Round #620 (Div. 2) D. Shortest and Longest LIS 题解: 贪心即可,对于最短序列,我们尽可能用可用的最大数字放入序列中,对于最长序列 ...
- [CF1304D] Shortest and Longest LIS - 贪心
看样例,>><>><,要构造 LIS 最短的,我们需要找最小链划分的方案,即包含最少的下降列 很容易想到把连续 < 的看成一段,比如样例就是 .|.|. .| ...
- Codeforces 1304D. Shortest and Longest LIS
根据题目,我们可以找最短的LIS和最长的LIS,找最短LIS时,可以将每一个increase序列分成一组,从左到右将最大的还未选择的数字填写进去,不同组之间一定不会存在s[i]<s[j]的情况, ...
- Codeforces 1304D. Shortest and Longest LIS 代码(构造 贪心)
https://codeforces.com/contest/1304/problem/D #include<bits/stdc++.h> using namespace std; voi ...
- Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)
A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...
- Codeforces Round #620 (Div. 2) 题解
A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
随机推荐
- Python 中异常嵌套
在Python中,异常也可以嵌套,当内层代码出现异常时,指定异常类型与实际类型不符时,则向外传,如果与外面的指定类型符合,则异常被处理,直至最外层,运用默认处理方法进行处理,即停止程序,并抛出异常信息 ...
- java后台读取excel模板数据
/** * 读取EXCEL模板数据 * * @param excelFilePath excel文件路径 * @param dataRowNum 开始读取数据的行数 * @param keyRowNu ...
- GTK入门
环境准备 官网下载 GTK 源码包,因为本机 GLib 版本不够,下载一个非最新版的 GTK3.8.0 先学习用 直接阅读 "/gtk+-3.8.0/docs/reference/gtk/h ...
- mongodb与sql语句对比
左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...
- maven项目从本地向本地仓库导入jar包
方法一(推荐): <dependency> <groupId>guagua-commons</groupId> <artifactId>guagua-c ...
- 再谈记忆化搜索 HDU-1078
最近做DP题目,发现无论是LCS,还是有些题目涉及将动态规划的路径打印出来,而且有时候还要按格式输出,这个时候,记忆化搜索显得尤其重要,确实,记忆化搜索使用优化版本的动态规划,用起来思路清晰,非常方便 ...
- 给adobe acrobat reader 添加图片注释
首先展示一下 我添加注释的结果, 下面是我的做法: 在Adobe Acrobat 中打开Sample.pdf文档,点开文档右边的“工具”-“内容”-选择“编辑对象”,鼠标光标变成实心箭头+右下角小方 ...
- Cf水题B - Combination
地址: https://vjudge.net/problem/27861/origin Ilya plays a card game by the following rules. A player ...
- Django2.0——模板渲染(一)
在前面的介绍中我们都是用简单的 django.http.HttpResponse来把内容显示到网页上,本节将讲解如何使用渲染模板的方法来显示内容,即调用精美的HTML页面.模板的创建既可以在项目下创建 ...
- zip4j 2.0压缩 加密压缩
https://github.com/srikanth-lingala/zip4j ZipParameters zipParameters = new ZipParameters(); zipPara ...