SnackDown Longest Increasing Subsequences 构造题
Longest Increasing Subsequences
题目连接:
https://www.codechef.com/SNCKPA16/problems/MAKELIS
Description
大厨最近了解了经典的最长上升子序列问题。然而,大厨发现,虽然最长上升子序列的长度
是唯一的,但子序列本身却不一定唯一。比如,序列 [1, 3, 2, 4] 的最长上升子序列有两个:[1, 3, 4]
和 [1, 2, 4]。
大厨在这个方向上多做了些研究,然后提出了下面的这个问题:
给定 K,输出一个整数 N 以及一个 1 ∼ N 的排列,使得这一排列包含恰好 K 个最长上升子
序列。大厨要求 1 ≤ N ≤ 100,不然问题就太简单了。
如果有多种可能的答案,输出任意一种即可。
Input
输入的第一行包含一个整数 T,表示测试数据的组数。接下来是 T 组数据。
每组数据仅有一行,包含一个整数 K。
Output
对于每组数据,输出两行。第一行包含一个整数 N,第二行包含 N 个整数,即 1 ∼ N 的一个
排列,以空格分隔。
• 1 ≤ T ≤ 2 · 104
• 1 ≤ K ≤ 105
Sample Input
2
1
2
Sample Output
5
1 2 3 4 5
4
1 3 2 4
Hint
题意
题解:
很有趣的题,一般来说第一想法是分解质因数,变成乘积的形式,我不知道这样搞不搞得出来,很麻烦的样子……
这道题的正确套路是分解进制,考虑你现在是用m进制去处理这个k,你可以得到b[0]b[1]b[2]这个玩意儿,表示这个m进制的每一位是啥
你现在做出来了b0,那么你就在b[0]前面扔m个小的倒叙的,然后再后面扔一个最小的,再倒叙扔b[1]个倒叙的
这样你就得到了mb[0]+b[1]了,然后一直递归下去,你就构造出来了m进制的
经过计算,发现m=6的时候,恰好能够构造出来,所以就做出来了~
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int k;
int pri[maxn];
vector<int>p;
void init()
{
for(int i=2;i<maxn;i++)
{
if(pri[i]==0)
{
for(int j=i+i;j<maxn;j+=i)
pri[j]=1;
}
}
}
int cnt[maxn],tot=0;
int solve()
{
scanf("%d",&k);
if(k==1)
{
cout<<"1"<<endl;
cout<<"1"<<endl;
return 1;
}
if(k==2)
{
cout<<"2"<<endl;
cout<<"2 1"<<endl;
return 2;
}
p.clear();
tot=0;
while(k)
{
cnt[tot++]=k%6;
k/=6;
}
reverse(cnt+0,cnt+tot);
for(int i=cnt[0];i;i--)
p.push_back(i);
for(int i=1;i<tot;i++)
{
if(cnt[i]==0){
for(int j=0;j<p.size();j++)
p[j]+=6;
reverse(p.begin(),p.end());
for(int j=1;j<=6;j++)
p.push_back(j);
reverse(p.begin(),p.end());
continue;
}
for(int j=0;j<p.size();j++)p[j]+=6;
reverse(p.begin(),p.end());
for(int j=1;j<=6;j++)
p.push_back(j);
reverse(p.begin(),p.end());
for(int j=0;j<p.size();j++)p[j]+=(cnt[i]+i);
for(int j=1;j<=i;j++)p.push_back(j);
for(int j=cnt[i]+i;j>i;j--)
p.push_back(j);
}
cout<<p.size()<<endl;
for(int i=0;i<p.size();i++)
cout<<p[i]<<" ";
cout<<endl;
return p.size();
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
solve();
}
SnackDown Longest Increasing Subsequences 构造题的更多相关文章
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences
求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...
- 【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组
题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i ...
- LintCode刷题笔记--Longest Increasing Subsequence
标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
随机推荐
- 使用Scrapy命令行工具【导出JSON文件】时编码设置
Windows 10家庭中文版,Python 3.6.4,virtualenv 16.0.0,Scrapy 1.5.0, 使用scrapy命令行工具建立了爬虫项目(startproject),并使用s ...
- 栈应用之 后缀表达式计算 (python 版)
栈应用之 后缀表达式计算 (python 版) 后缀表达式特别适合计算机处理 1. 中缀表达式.前缀表达式.后缀表达式区别 中缀表达式:(3 - 5) * (6 + 17 * 4) / 3 17 ...
- 缓存数据库-redis介绍
一:Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的 ...
- JavaScript数据检测
前言: 随着编程实践的增加,慢慢发现关于数据类型的检测至关重要.我认为程序就是为了处理数据和展示数据.所以,数据的检测对于编程来说也至关重要.因为只有符合我们预期的输入,才可能产生正确的输出.众所周知 ...
- 序列化 json和pickle
序列化 1. 什么叫序列化 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 2. json dumps loads 一般对字典和列表序列化 dump load 一般对 ...
- 自己动手编译OpenSSL库
因为工作需要,要实现一个基于SSL的通信程序.之前没有接触过SSL协议通讯,这次学习了一下如何自己编译OpenSSL库. 我使用的环境是Windows 10 + VS2015 1.首先打开VS2015 ...
- python开发学习-day16(Django框架初识)
s12-20160507-day16 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- 【AtCoder】AGC023 A-F题解
可以说是第一场AGC了,做了三道题之后还有30min,杠了一下D题发现杠不出来,三题滚粗了 rating起步1300+,感觉还是很菜... 只有三题水平显然以后还会疯狂--啊(CF的惨痛经历) 改题的 ...
- hdoj1203 I NEED A OFFER!(DP,01背包)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1203 思路 求最少能收到一份offer的最大概率,可以先求对立面:一份offer也收不到的最小概率,然 ...
- Tuning Optimization
2017-02-22 在经过Trace的分析并重新优化之后 CPU: Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.6GHz 24core 如上图,CPU使用率平均低于60% ...