洛谷P3014 [USACO11FEB]牛线Cow Line

---恢复内容开始---

关于康托展开 与 康托逆展开

 1 #include <bits/stdc++.h>
 2 #define For(i, j, k) for(int i=j; i<=k; i++)
 3 #define Dow(i, j, k) for(int i=j; i>=k; i--)
 4 #define LL long long
 5 using namespace std;
 6 inline LL read() {
 7     LL x = 0, f = 1;
 8     char ch = getchar();
 9     while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘) f = -1; ch = getchar(); }
10     while(ch>=‘0‘&&ch<=‘9‘) { x = x*10+ch-48; ch = getchar(); }
11     return x * f;
12 }
13 void write(LL x) {
14     if(x<0) putchar(‘-‘), x = -x;
15     if(x>9) write(x/10);
16     putchar(x%10+48);
17 }
18 inline void writeln(LL x) { write(x); putchar(‘\n‘); }
19
20 const int N = 30;
21 int n,Ques;
22 LL Fact[N];
23 int vis[N];
24
25 inline void calc(int n) {
26     Fact[0]=1;
27     For(i, 1, n) Fact[i]=Fact[i-1]*i;
28 }
29
30 inline int getpre(int n, int x) {
31     vis[x] = 1;
32     int res = 0;
33     For(i, 1, x-1)
34         if(!vis[i]) ++res;
35     return res;
36 }
37
38 inline void getprint(int n, LL x) {
39     int pos = 0;
40     For(i, 1, x+1) {
41         ++pos;
42         while(vis[pos] && pos<=n) ++pos;
43     }
44     vis[pos] = 1;
45     write(pos); putchar(‘ ‘);
46 }
47
48 int main() {
49     n = read(); Ques = read();
50     calc(n);
51     while(Ques--) {
52         char s[10];
53         scanf("%s",s+1);
54         if(s[1]==‘Q‘) {
55             LL ans = 0;
56             For(i, 0, n) vis[i] = 0;
57             Dow(i, n, 1) {
58                 int x = read();
59                 ans += getpre(n, x)*Fact[i-1];   //  这里可以用树状数组来优化
60             }
61             writeln(ans+1);
62         }
63         else {
64             For(i, 0, n) vis[i] = 0;
65             LL x = read(); x--;
66             Dow(i, n, 1) {
67                 getprint(n, x/Fact[i-1]);
68                 x %= Fact[i-1];
69             }
70             puts("");
71         }
72     }
73     return 0;
74 }

---恢复内容结束---

原文地址:https://www.cnblogs.com/third2333/p/8457949.html

时间: 2024-09-30 18:10:02

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

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 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 Far

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

牛线Cow Line

题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 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 numb

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at

P2952 [USACO09OPEN]牛线Cow Line

题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, some number of cows on the left

洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

传送门 题目大意:n头牛在单行道n个位置,开始用不同的速度跑步. 当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度 跑,称为一个小团体.问:ts后有多少个小团体. 题解:模拟 倒着扫一遍,因为某头牛后面的牛对这头牛的速度没影响. 计算出ts后牛的终点,如果能撞上前面最慢的牛,那么小团体数+1 注意开long long 一开始不理解为什么倒着扫, 因为如果正着扫,看第i头牛能否撞上i+1头, 我们不确定第i+1头牛的速度,可能第i+1头牛 速度很快,追上i+2头牛速度减缓,从而被第i头牛追

洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows move from any of the N (1 <= N <= 250)

洛谷 2966 [USACO09DEC]牛收费路径Cow Toll Paths

https://www.luogu.org/problem/show?pid=2966 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The c

洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th