Nordic Collegiate Programming Contest 2015? B. Bell Ringing

Method ringing is used to ring bells in churches, particularly in England. Suppose there are 6 bells that have 6 different pitches. We assign the number 1 to the bell highest in pitch, 2 to the second highest, and so on. When the 6 bells are rung in some order—each of them exactly once—it is called a row. For example, 1, 2, 3, 4, 5, 6 and 6, 3, 2, 4, 1, 5 are two different rows.

An ideal performance contains all possible rows, each played exactly once. Unfortunately, the laws of physics place a limitation on any two consecutive rows; when a bell is rung, it has considerable inertia and the ringer has only a limited ability to accelerate or retard its cycle. Therefore, the position of each bell can change by at most one between two consecutive rows.

In Figure ??, you can see the pattern of a non-ideal performance, where bells only change position by at most one.

Given nn, the number of bells, output an ideal performance. All possible rows must be present exactly once, and the first row should be 1, 2, \cdots , n1,2,?,n.

Input Format

The first and only line of input contains an integer nn such that 1 \le n \le 81≤n≤8.

Output Format

Output an ideal sequence of rows, each on a separate line. The first line should contain the row 1, 2, \cdots, n1,2,?,n and each two consecutive lines should be at most 11 step away from each other. Each row should occur exactly once in the output.(No extra space at the end of each line)

本题答案不唯一,符合要求的答案均正确

样例输入

2

样例输出

1 2
2 1

题目来源

Nordic Collegiate Programming Contest 2015?

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <cmath>
11 #include <cstdlib>
12 #include <ctime>
13 using namespace std;
14 typedef long long ll;
15 int n;
16 const int N=5e5+3;
17 int a[N][10];
18 int x,y;
19 void solve()
20 {
21     a[0][0]=1;
22     int s=0,end=0;
23     for(int i=2;i<=8;i++)
24     {
25         int len=end;
26         for(int j=s;j<=end;j++)
27         {
28             if((j-s)%2)//从左往右
29             {
30                 for(int k=0;k<i;k++)
31                 {
32                     len+=1;
33                     for(int l=0;l<k;l++)//前面的直接复制下来
34                     {
35                         a[len][l]=a[j][l];
36                     }
37                     a[len][k]=i;//插到a[len][k]
38                     for(int m=k+1;m<i;m++)//后面的与J行错一复制下来
39                     {
40                         a[len][m]=a[j][m-1];
41                     }
42                 }
43             }
44             else//从右到左
45             {
46                 for(int k=i-1;k>=0;k--)
47                 {
48                     len+=1;
49                     for(int l=0;l<k;l++)
50                     {
51                         a[len][l]=a[j][l];
52                     }
53                     a[len][k]=i;
54                     for(int m=k+1;m<i;m++)
55                     {
56                         a[len][m]=a[j][m-1];
57                     }
58                 }
59
60             }
61         }
62         s=end+1;//更新s,end.
63         end=len;
64     }
65 }
66 int jie(int n)
67 {
68     int ans=1;
69     for(int i=1;i<=n;i++){
70         ans*=i;
71     }
72     return ans;
73 }
74 int main()
75 {
76     solve();
77     scanf("%d",&n);
78     for(int i=1;i<=n-1;i++)
79     {
80         x+=jie(i);
81     }
82     y=x+jie(n)-1;
83     for(int i=x;i<=y;i++)
84     {
85         for(int j=0;j<n;j++)
86         {
87             printf("%d%c",a[i][j],j==n-1?‘\n‘:‘ ‘);
88         }
89     }
90     return 0;
91 }

原文地址:https://www.cnblogs.com/tingtin/p/9393647.html

时间: 2024-09-30 14:32:54

Nordic Collegiate Programming Contest 2015? B. Bell Ringing的相关文章

(寒假GYM开黑)2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

layout: post title: 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! B.Baby Bites (签到模拟) 按照题意模拟就行了 int a[maxn]; string s; int main() { std::ios::syn

【离散化树状数组】Nordic Collegiate Programming Contest G.Galactic Collegiate Programming Contest

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,m; 5 const int maxn=1e5+3; 6 struct node 7 { 8 int team; 9 int num; 10 int time; 11 int id; 12 }a[maxn]; 13 int ans[maxn]; 14 int b[maxn]; 15 int c[maxn]; 16 int s[maxn

2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017)

D.Distinctive Character 看到样例,第一个反应贪心.先写了个按这一位1和0的数目多少,确定0还是1的东西.感觉不够真,又写了个尽量加到相似的比较小的串上的贪心.在和前边的那个组合一下,换了换顺序...好吧就过了13组样例...正解如下:考虑如何求出,所有2^k个状态与这n个串的最大相似度.起初的n个串的答案显然为k,那改变一个位置,相似度就改变为k-1,对于一个状态,越早算出来的相似度,越大,那么就可以直接bfs求出所有状态的最大相似度了.答案就是取最小值的状态. #inc

Gym .101933 Nordic Collegiate Programming Contest (NCPC 2018) (寒假gym自训第四场)

(本套题算是比较温和吧,就是罚时有点高. B .Baby Bites 题意:给出一个婴儿给出的数组,有一些数字听不清楚,让你还原,问它是否是一个从1开始的一次增加的数组. 思路:从左往右依次固定,看是否有矛盾即可. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep2(i,a,b) for(int i=a;i>=b;i--) using n

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018 Problem A. Can Shahhoud Solve it? Problem B. Defeat the Monsters Problem C. UCL Game Night Problem

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018)

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018) B. New Assignment 有n个人(1?≤?n?≤?104),有男有女,每个人都有一个id,现在这n个人分成学习互助小组,有三种组队模式,一个男人一组,一个女人一组,一男一女一组,如果要一男一女一组,那么这两人id的gcd要>1.保证任意三个人的gcd=1.求小组的组数最少是多少? 看起来是一个很裸的二

2018 German Collegiate Programming Contest (GCPC 18)

2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bit

2018 ACM-ICPC, Syrian Collegiate Programming Contest

2018 ACM-ICPC, Syrian Collegiate Programming Contest A Hello SCPC 2018! 水题 B Binary Hamming 水题 C Portals 思路:并查集维护连通性 代码: //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error &quo

(寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队博客 C.Coolest Ski Route (记忆化搜索) 题意 给出一个有向图,求出一个权值最长的链, 题解 暴力dfs会超时,所以直接储存每个起点能走到的最远距离 #include<