[USACO11FEB] Cow Line
https://www.luogu.org/problem/show?pid=3014
题目描述
The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line number is. In return, Farmer John can give them a line number and the cows must rearrange themselves into that line.
A line number is assigned by numbering all the permutations of the line in lexicographic order.
Consider this example:
Farmer John has 5 cows and gives them the line number of 3.
The permutations of the line in ascending lexicographic order: 1st: 1 2 3 4 5
2nd: 1 2 3 5 4
3rd: 1 2 4 3 5
Therefore, the cows will line themselves in the cow line 1 2 4 3 5.
The cows, in return, line themselves in the configuration '1 2 5 3 4' and ask Farmer John what their line number is.
Continuing with the list:
4th : 1 2 4 5 3
5th : 1 2 5 3 4
Farmer John can see the answer here is 5
Farmer John and the cows would like your help to play their game. They have K (1 <= K <= 10,000) queries that they need help with. Query i has two parts: C_i will be the command, which is either 'P' or 'Q'.
If C_i is 'P', then the second part of the query will be one integer A_i (1 <= A_i <= N!), which is a line number. This is Farmer John challenging the cows to line up in the correct cow line.
If C_i is 'Q', then the second part of the query will be N distinct integers B_ij (1 <= B_ij <= N). This will denote a cow line. These are the cows challenging Farmer John to find their line number.
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and K
- Lines 2..2*K+1: Line 2*i and 2*i+1 will contain a single query.
Line 2*i will contain just one character: 'Q' if the cows are lining up and asking Farmer John for their line number or 'P' if Farmer John gives the cows a line number.
If the line 2*i is 'Q', then line 2*i+1 will contain N space-separated integers B_ij which represent the cow line. If the line 2*i is 'P', then line 2*i+1 will contain a single integer A_i which is the line number to solve for.
输出格式:
- Lines 1..K: Line i will contain the answer to query i.
If line 2*i of the input was 'Q', then this line will contain a single integer, which is the line number of the cow line in line 2*i+1.
If line 2*i of the input was 'P', then this line will contain N space separated integers giving the cow line of the number in line 2*i+1.
输入输出样例
5 2
P
3
Q
1 2 5 3 4
1 2 4 3 5
5 题意:
询问这是第几个全排列,询问第k个全排列是什么
#include<cstdio>
#include<cstring>
using namespace std;
long long jc[];
bool v[];
int p[];
int main()
{
int n,q,cnt,now,sum;
long long x,tot;
char c[];
scanf("%d%d",&n,&q);
jc[]=;
for(int i=;i<=n;i++) jc[i]=jc[i-]*i;
while(q--)
{
scanf("%s",c);
if(c[]=='P')
{
scanf("%lld",&x);
memset(v,,sizeof(v));
for(int i=;i<n;i++)
{
cnt=now=;
while(x>jc[n-i]) x-=jc[n-i],cnt++;
for(int i=;i<=;i++)
if(!v[i])
{
now++;
if(now==cnt+)
{
printf("%d ",i);
v[i]=true;
continue;
}
}
}
for(int i=;i<=;i++)
if(!v[i])
{
printf("%d\n",i);
break;
}
}
else
{
tot=,sum;
for(int i=;i<=n;i++) scanf("%d",&p[i]);
memset(v,,sizeof(v));
for(int i=;i<n;i++)
{
sum=;
for(now=;now<=n;now++)
if(!v[now] && now<p[i]) sum++;
v[p[i]]=true;
tot+=sum*jc[n-i];
}
printf("%lld\n",tot+);
}
}
}
[USACO11FEB] Cow Line的更多相关文章
- P3014 [USACO11FEB]牛线Cow Line && 康托展开
康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托 ...
- 洛谷 P3014 [USACO11FEB]牛线Cow Line
P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...
- [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)
如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里: 简陋的博客 百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...
- BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛
3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 48 Solved: 41[S ...
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- BZOJ1640: [Usaco2007 Nov]Best Cow Line 队列变换
1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 Solved: 2 ...
随机推荐
- Some good articles
https://alligator.io/vuejs/introduction-render-functions/ https://alligator.io/vuejs/vue-jwt-pattern ...
- 【转】cpu的核心数与线程数的关系
原文地址:http://www.dn580.com/dnzs/dncs/2013/10/08/172948914.html 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能 ...
- 欢迎来怼--第二十一次Scrum会议
一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/11/2 17:05~17:15,总计10min. 地点: ...
- 往Matlab中添加工具包
使用Matlab过程中,常常会缺少一些函数包导致无法运行,会显示未定义函数. 假如我要用sigshift( ) 这个移位函数,但Matlab中没有,就会提示错误:未定义函数或变量 'sigshift' ...
- caffe2安装教程
相比于网上的安装教程不如直接看官方安装教程:https://caffe2.ai/docs/getting-started.html?platform=windows&configuration ...
- 第45天:2017webstrom下载破解汉化
1.webstrom 11.0.3下载地址1:http://pan.baidu.com/s/1kVQjcwf 密码:uggr 下载地址2:http://pan.baidu.com/s/1kVQjcwf ...
- htm中的 src未指定具体路径的话 默认查找当前文件夹
htm中的 src未指定具体路径的话 默认查找当前文件夹
- 【刷题】SPOJ 705 SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ3498:March of the Penguins——题解
最近的题解的故事背景割. 题目: 描述 在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃, ...
- XXE漏洞攻击与防御整理
一.漏洞原理 1.DTD 文档类型定义(DTD)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档的结构.DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 内部的 DOC ...