[ZJOJ] 5772【NOIP2008模拟】今天你AK了吗
Description
AK:All kill
“你为什么没背书?”
“没有为什么,我就是没背书。”
“……我去年买了个表,G—U—N!”
头铁王InFleaKing把背书的时间都拿去列排列了......
n=3的排列一共有六个(顺序按字典序从小到大):
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
气不打一处来的InFleaKing把n的排列打乱了。
他想知道字典序第k小的n的排列是什么?
由于InFleaKing被捉去背书了,所以这个问题只能交给被万人顶礼膜拜的dalao您来解决了。
Input
一行,两个数,分别是n,k。
n,k的含义见题目描述。
Output
一行,n个数。
代表字典序第k小的n的排列。
注意两两数之间要用空格隔开。
Sample Input
Sample Input1:
1 1
Sample Input2:
2 2
Sample Output
Sample Output1:
1
Sample Output2:
2 1
Data Constraint
【数据约定】
对于10%的数据:1<=n<=3
对于20%的数据,1<=n<=9
对于30%的数据:1<=n<=18,1<=k<=10^6
对于60%的数据:1<=n<=18
对于80%的数据:1<=n<=100,1<=k<=10^100
对于90%的数据:1<=n<=1000,1<=k<=10^1000
对于100%的数据:1<=n<=100000,1<=k<=min(10^20000,n!)
思路解析
无力吐槽。
如果想恶心我的话成功了。
根据惯例,我们面向数据编程。
第一档
对于10%的数据:1<=n<=3
手算 + 打表
预期分数:10
第二档
对于30%的数据:1<=n<=18,1<=k<=10^6手算 + 打表还是可以
直接暴力递归求解。
预期分数:20 ~ 30
第三档
对于60%的数据:1<=n<=18
要不要听一个看上去很高级的洋玩意叫康托展开?
这个题听说是逆康托展开。没兴趣讲,请自行baidu
说一下我的想法
因为是输出字典序,我姑且列出所有情况看一下吧
以4 6这组数据为例
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
........
可以找出一个规律:
第一位数是当前1~4中没用过的第k/6大的数
第二位是当前1~4中没用过的第k/2大的数
6 = 3!
2 = 2!
好了,写代码吧
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const long long sum[] = {,,,,,,,,,,,,,,,,,,};
long long n,k;
bool vis[];
inline void find_kth(long long x) {
long long res = ;
long long i;
for(i = ;i <= n;i++) {
if(!vis[i]) res++;
if(res == x) break;
}
printf("%lld ",i);
vis[i] = ;
}
int main() {
freopen("array.in","r",stdin);
freopen("array.out","w",stdout);
scanf("%lld%lld",&n,&k);
long long now,tmp,cnt,x;
now = k;
cnt = n;
while(cnt--) {
tmp = now / sum[cnt];
x = now;
now = now % sum[cnt];
if(!now) now = sum[cnt];
if(tmp * sum[cnt] < x) tmp++;
find_kth(tmp);
}
return ;
}
第四档
对于90%的数据:1<=n<=1000,1<=k<=10^1000
高精不解释
每次的商是小于n的,所以可以二分商涉及到高精度减法,高精度乘法,还算好打。
一次的复杂度是O(n log n)
总复杂度O(n^2 log n)
预期分数:70~90
第五档
对于100%的数据:1<=n<=100000,1<=k<=min(10^20000,n!)
以下引自作者的题解》》
k<=10^20000
我可以很负责任的告诉你:6100!>10^20000,但是我们的n却有10^5那么大。
这说明什么?
这说明前100000-6100=93900个数一定是1、2、3、…、93900(你发现了
吗?)
因此只需要O(6100^2)的复杂度来完成这个部分。
即使你不会数据结构或者分块也可以解决这道题!
假数据范围
预计分数:90~100(需要常数优化)
总结
考场不要写高精,除非你时间充裕
[ZJOJ] 5772【NOIP2008模拟】今天你AK了吗的更多相关文章
- JZOJ 5777. 【NOIP2008模拟】小x玩游戏
5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms Memory Limits ...
- JZOJ 5809. 【NOIP2008模拟】数羊
5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms Memory Limits: ...
- JZOJ 5793. 【NOIP2008模拟】小S练跑步
5793. [NOIP2008模拟]小S练跑步 (File IO): input:run.in output:run.out Time Limits: 2000 ms Memory Limits: ...
- JZOJ 5791. 【NOIP2008模拟】阶乘
5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms Memory ...
- JZOJ 5776. 【NOIP2008模拟】小x游世界树
5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms Me ...
- JZOJ 5775. 【NOIP2008模拟】农夫约的假期
5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms Memory Lim ...
- JZOJ 5773. 【NOIP2008模拟】简单数学题
5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms Memory Limits ...
- JZOJ 5771. 【NOIP2008模拟】遨游
5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms Memory Limits: 2 ...
- JZOJ5776. 【NOIP2008模拟】小x游世界树
题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...
随机推荐
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
- 【干货】Kafka 事务特性分析
特性背景 消息事务是指一系列的生产.消费操作可以要么都完成,要么都失败,类似数据库的事务.这个特性在0.10.2的版本是不支持的,从0.11版本开始才支持.华为云DMS率先提供Kafka 1.1.0的 ...
- 自己定义 View 基础和原理
课程背景: 在 Android 提供的系统控件不能满足需求的情况下,往往须要自己开发自己定义 View 来满足需求,可是该怎样下手呢.本课程将带你进入自己定义 View 的开发过程,来了解它的一些原理 ...
- poj2011
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17608 Accepted: 765 ...
- [POI 2018] Prawnicy
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5102 [算法] 首先,n条线段的交集一定是[Lmax,Rmin] , 其中,Lmax ...
- PCB MS SQL 存储过程(CLR) 实现Json转DataTable表的方法
一.准备需转为DataTable的json字符串 原json字符串数据 [{"TechName":"ECN","TechNo":" ...
- 【USACO2002 Feb】奶牛自行车队
[USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...
- 题解报告:hdu 1272 小希的迷宫
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...
- 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
- 【转】Linux GCC常用命令
转自:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Comp ...