题目背景

征求翻译。如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献。

题目描述

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:

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

  1. 1 2 4 3 5
  2. 5
  1. #include<map>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. int n,m,vis[];
  8. long long num[];
  9. int main(){
  10. //freopen("permutation.in","r",stdin);
  11. //freopen("permutation.out","w",stdout);
  12. scanf("%d%d",&n,&m);
  13. for(int i=;i<=;i++) num[i]=;
  14. for(int i=;i<=n;i++) num[n-i+]=i*num[n-i+];
  15. for(int k=;k<=m;k++){
  16. char c;long long x;
  17. cin>>c;
  18. memset(vis,,sizeof(vis));
  19. if(c=='P'){
  20. cin>>x;
  21. long long k=x;
  22. int flag=;
  23. for(int i=;i<=n;i++){
  24. int ans;
  25. if(k>=num[i+]){
  26. long long a=k/num[i+];
  27. long long nn=k%num[i+];
  28. if(nn) flag=;
  29. else flag=;
  30. int bns=;
  31. for(int j=;j<=n;j++){
  32. if(!vis[j]) bns++;
  33. if(bns-flag==a){
  34. ans=j;
  35. vis[j]=;
  36. break;
  37. }
  38. }
  39. k=nn;
  40. }
  41. else{
  42. if(flag==){
  43. for(int j=;j<=n;j++)
  44. if(!vis[j]){
  45. ans=j;
  46. vis[j]=;
  47. break;
  48. }
  49. }
  50. else{
  51. for(int j=n;j>=;j--)
  52. if(!vis[j]){
  53. ans=j;
  54. vis[j]=;
  55. break;
  56. }
  57. }
  58. }
  59. cout<<ans<<" ";
  60. }
  61. cout<<endl;
  62. }
  63. else{
  64. long long ans=;
  65. for(int i=;i<=n;i++){
  66. cin>>x;int bns=;
  67. for(int j=;j<=n;j++){
  68. if(!vis[j]) bns++;
  69. if(j==x) break;
  70. }
  71. bns--;
  72. vis[x]=;
  73. ans+=(long long)bns*num[i+];
  74. }
  75. cout<<ans+<<endl;
  76. }
  77. }
  78. }

洛谷 P3014 [USACO11FEB]牛线Cow Line的更多相关文章

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

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

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

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

  3. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

  4. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  5. 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

    P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...

  6. 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解

    题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...

  7. 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging

    题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...

  8. 洛谷 P2419 [USACO08JAN]牛大赛Cow Contest

    题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a p ...

  9. 洛谷—— P2419 [USACO08JAN]牛大赛Cow Contest

    https://www.luogu.org/problem/show?pid=2419 题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, convenie ...

随机推荐

  1. Tomcat容器 web.xml具体解释

    <init-param> <param-name>debug</param-name> <param-value>0</param-value&g ...

  2. [CQOI 2007] 涂色

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1260 [算法] 区间DP [代码] #include<bits/stdc++. ...

  3. Nginx实战系列之功能篇----后端节点健康检查

    目前,nginx对后端节点健康检查的方式主要有3种,这里列出:   1.ngx_http_proxy_module 模块和ngx_http_upstream_module模块(自带)    官网地址: ...

  4. new一个接口

    首先我们先看看接口的定义: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方 ...

  5. JAVA课设——中药古籍《太平圣惠方》数据处理与分析系统

    一.配置JAVA环境 本次课设是在Windows 10(64bit)平台上实现的,所以首先得配置下JAVA环境. 步骤一:先下载一个JDK(1.7)安装包,安装好JDK: 步骤二:JDK环境配置(由于 ...

  6. IOS-UITextField-改变光标颜色

    方法1: [[UITextField appearance] setTintColor:[UIColor blackColor]]; 这种方法将影响所有TextField. 方法2: textFiel ...

  7. 用命令行在本地创建一个库并上传到Github

    1  如何在本地创建一个仓库并上传到github? 基本步骤: $ mkdir blog //在桌面上创建一个叫"blog"的目录 $ cd blog //"cd blo ...

  8. javascript 将单词首字母大写,其余小写

    // 1 别人写的,我拿来参考了一下 function titleCase(str) { var array = str.toLowerCase().split(" "); for ...

  9. mybatis学习笔记之学习目录(1)

    mybatis学习笔记之学习结构(1) 学习结构: 1.mybatis开发方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发 ...

  10. 体验:Anko + Kotlin

    ● 依赖:compile 'org.jetbrains.anko:anko:0.10.0' ● 界面: import android.view.View import org.jetbrains.an ...