Kyoya and Permutation

这题想了好久才写出来,没看题解写出来的感觉真的好爽啊!!!

题目大意:题意我看了好久才懂,就是给你一个序列,比如[4, 1, 6, 2, 5, 3],第一个数字

的值是4,那么我们找下标为4的数( 跟链表差不多意思 ),然后一直找到底,这些数分为一类,

如[4, 1, 6, 2, 5, 3] 就可以分为三类,[4, 2, 1] , [6, 3],[5],这三类,然后每个类里面按从大

往小排,然后类之间按字典序排,[4, 1, 6, 2, 5, 3] 重新组合之后为,[4, 2, 1] [5] [6, 3]=[4, 2, 1, 5, 6, 3]

我们把进行重组之后数字序列保持不变的 序列 按字典序大小从小到大排出来。

然后给你一个长度n和数字k,让你找出长度为n的序列中排第k个的序列是什么。

思路:首先我想的是怎样的序列它重新组合之后还是原序列,我打了一下表,基础序列为

1,2,3,4,……,n,只有相邻的两个数交换之后得到的是满足要求的序列。如果我们从小到大

枚举出所有的序列显然是不可能的复杂度太高,那么我们先求总共的序列数,我们设dp[ i ],

表示从i 到 n 一共有多少种交换方法。dp[n]=1,那么状态转移方程为dp[ i ] = dp[ i + 1 ]+dp[ i + 2 ] ,

为什么呢,因为到i这里的时候,我们可以选择交换i 和 i+1 或者不交换,交换的话种数是dp[ i +2 ],

不交换的话是dp[ i + 1 ]。

我们从n开始往前找,找到第一个大于k的dp[ s ],那么s和s+1是必须要交换的,因为如果不交换

种数为dp[ s + 2 ],又dp[ s + 2] < k 不满足。这样我们的问题就变成了k为k-dp[ s + 2]的相同问题

我们可以用dfs递归求解。

 #include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[],n,k,ans[],cnt[];
void dfs(ll k)
{
if(k==) return;
int item=-;
for(int i=n;i>=;i--)
{
if(dp[i]>=k)
{
item=i;
break;
}
}
swap(ans[item],ans[item+]);
dfs(k-dp[item+]);
}
int main()
{
cin>>n>>k;
dp[n]=; dp[n-]=;
for(int i=n-;i>=;i--) dp[i]=dp[i+]+dp[i+];
for(int i=;i<=n;i++) ans[i]=i;
dfs(k);
printf("%d",ans[]);
for(int i=;i<=n;i++) printf(" %d",ans[i]);
puts("");
return ;
}

Codeforces Round #309 (Div. 2) -D. Kyoya and Permutation的更多相关文章

  1. Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造

    B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  2. 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

    题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...

  3. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  4. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题

    A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  5. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  6. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks【*组合数学】

    A. Kyoya and Photobooks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. 贪心 Codeforces Round #309 (Div. 2) B. Ohana Cleans Up

    题目传送门 /* 题意:某几列的数字翻转,使得某些行全为1,求出最多能有几行 想了好久都没有思路,看了代码才知道不用蠢办法,匹配初始相同的行最多能有几对就好了,不必翻转 */ #include < ...

  8. Codeforces Round #309 (Div. 1)

    A. Kyoya and Colored Balls 大意: 给定$k$种颜色的球, 第$i$种颜色有$c_i$个, 一个合法的排列方案满足最后一个第$i$种球的下一个球为第$i+1$种球, 求合法方 ...

  9. C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))

    C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...

随机推荐

  1. 淘淘商城之springmvc前端控制器

    一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  2. Hbase 集群安装(Hadoop 2.6.0 hbase0.99.2)

    一:说明 该安装是在hadoop集群安装后进行,详情可见上一篇博客虚拟机centos7系统下安装hadoop ha和yarn ha(详细) .其中涉及五台机器,两台master(机器名:master, ...

  3. Java垃圾回收机制复习

    一.如何确定某个对象是“垃圾” 二.典型的垃圾收集算法 三.典型的垃圾收集器 JVM(HotSpot) 7种垃圾收集器的特点及使用场景 https://www.cnblogs.com/chengxuy ...

  4. script标签中type为"text/x-template"或"text/html"

    写过一点前端的都会碰到需要使用JS字符串拼接HTML元素然后append到页面DOM树上的情况,一般的写法都是使用+号以字符串的形式拼接,如果是短点的还好,如果很长很长的话就会拼接到令人崩溃了. 比如 ...

  5. 数据库操作之整合Mybaties和事务讲解 5节课

    1.SpringBoot2.x持久化数据方式介绍          简介:介绍近几年常用的访问数据库的方式和优缺点 1.原始java访问数据库             开发流程麻烦           ...

  6. OGG实现两台Oracle数据库的同步

    今天通过最简单的一个例子,给大家讲解下 goldengate 实现两台Oracle数据库的同步.内容如下:1.配置数据库信息.2.安装golden gate.3.配置golden gate.4.测试同 ...

  7. 【逆向工具】IDA使用4-控制台逆向分析 Reverse004.exe 获取密码

    工具 吾爱破解版本OD.IDA6.8 OD使用-动态分析 OD快捷方式 F2 下断点,也就是指定断点的地址F3加载一个可执行程序,进行调试分析F4程序执行到光标处 F5 缩小.还原当前窗口 F7 单步 ...

  8. 如何交叉编译 linux kernel 内核

    Compilation We first need to move the config file by running cp arch/arm/configs/bcmrpi_cutdown_defc ...

  9. 工作中bug笔记

    1.报Cannot read property  indexOf of undefined 错误的时候!!!报这种错的时候,一般是因为indexOf前面检查的东西是不存在的!!!!!  2.使用< ...

  10. Python3学习笔记11-循环语句

    条件判断使用if,需要加上冒号,当条件判断为True时,执行if下的代码块,为false就什么也不做 只要var1不是0,非空字符串,非空list等,就判断为True.否则为False var1 = ...