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.

输入输出样例

输入样例#1:

5 2
P
3
Q
1 2 5 3 4
输出样例#1:

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的更多相关文章

  1. P3014 [USACO11FEB]牛线Cow Line && 康托展开

    康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托 ...

  2. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

  3. [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)

    如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...

  4. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  5. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  6. 【BZOJ】【3301】【USACO2011 Feb】Cow Line

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  7. BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛

    3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 48  Solved: 41[S ...

  8. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  9. BZOJ1640: [Usaco2007 Nov]Best Cow Line 队列变换

    1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  Solved: 2 ...

随机推荐

  1. ffmpeg实现mjpeg摄像头的采集-预览-拍照

    摄像头输出是mjpeg格式的,需要实现在线预览功能,然后实现拍照功能 1.可以设置采集图像的分辨率,预览分辨率为640*480,可以自定义 2.ctrl+\ 拍照,ctrl+c 退出 void tes ...

  2. jQuery实现checkbox(复选框)选中、全选反选代码

    谁都知道 在html 如果一个复选框被选中 是 checked="checked". 但是我们如果用jquery alert($("#id").attr(&qu ...

  3. php+原生ajax实现图片文件上传功能实例

    html+js 代码 <!DOCTYPE html> <html> <head> <title>Html5 Ajax 上传文件</title> ...

  4. 让我们一起来做最漂亮的Android界面吧!

    让我们一起来做最漂亮的Android界面吧! AndroidiOS产品设计 摘要:如何为Android设备量身定制以打造出最为完美的应用?这是让诸多开发者很是头疼的问题.不同于iOS,Android设 ...

  5. LintCode-41.最大子数组

    最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...

  6. LintCode-376.二叉树的路径和

    二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 5: 返回: [      ...

  7. 3dContactPointAnnotationTool开发日志(一)

      周日毕设开题报告结束后浪了一天,今天又要开始回归正轨了.毕设要做一个人和物体的接触点标注工具,听上去好像没啥难度,其实实现起来还是挺麻烦的.   今天没做啥,就弄了个3d场景做样例.把界面搭了一下 ...

  8. Flink的序列化与flink-hadoop-compatibility

    最近 用户提交了一个问题 说他的jar包里明明包含相关的类型 但是在提交Flink作业的时候 却报出classnotfound的错误 查看之后发现 这里是flink的一个没有说的太明白的地方 用户的代 ...

  9. init只创建一次 只有父类的init创建servletContext的对象

    init只创建一次 只有父类的init创建servletContext的对象  如果重写父类的方法 但不显示调用父类的init 是不会创建servletContext对象的

  10. bzoj4031-小Z的房间

    题目 给一个\(n\*m\)的矩阵,每个点可能为"."或"*",有多少种方法把矩阵中的点全部连接起来,并且每两个点之间只有一条路径. 分析 题目所求的是一个矩阵 ...