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. lvs+keepalived详解

    常用软件安装及使用目录 资源链接:https://pan.baidu.com/s/15rFjO-EnTOyiTM7YRkbxuA    网盘分享的文件在此 官网:http://www.linuxvir ...

  2. vuejs学习之 项目打包之后的首屏加载优化

    vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...

  3. CentOS Openvpn搭建以及 linux&&windows客户端的连接

    本文参考:http://www.centoscn.com/CentosServer/test/2014/1120/4153.html 一. Server安装准备     (CentOS release ...

  4. 衡量失业:失业率(Unemployment Rate)

    定义 劳动力 = 就业人数 + 失业人数 失业率 = (失业人数 / 劳动力) * % 劳动力参与率 = (劳动力 / 成年人口) * %

  5. popen()与system()

    一.popen() 用途:执行shell命令(并读取其输出或向其发送一些输入) 特点:通过管道来与shell命令进行通信 二.system()

  6. LintCode-204.单例

    单例 单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计> 模式为单例.例如,对于 class Mouse (不是动物的mouse哦), ...

  7. iOS开发UIApplication用法

    1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplication对象,而且是单例的,如果 ...

  8. arcgis api for javascript 各个版本的SDK下载

    1.首先,进入下载网站,需要登录才能下载.下载链接 2.选择需要下载的版本,进行下载.

  9. matplotlib中什么是后端

    在很多网上文档和邮件列表中提到了"后端",并且很多初学者会对这个术语迷惑.matplotlib把不同使用情形和输出格式作为目标.一些人用matplotlib在python shel ...

  10. Unsupported major.minor version 52.

    面试的时候,京东和美团,360的面试官都问到了同一个问题,java1.7与java1.8的区别, 于是想做个小小的例子: 我的eclipse刚开始是1.7的,后来,我把环境改成了1.8的, 方法:右击 ...