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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
int n;
const int N=5e5+;
int a[N][];
int x,y;
void solve()
{
a[][]=;
int s=,end=;
for(int i=;i<=;i++)
{
int len=end;
for(int j=s;j<=end;j++)
{
if((j-s)%)//从左往右
{
for(int k=;k<i;k++)
{
len+=;
for(int l=;l<k;l++)//前面的直接复制下来
{
a[len][l]=a[j][l];
}
a[len][k]=i;//插到a[len][k]
for(int m=k+;m<i;m++)//后面的与J行错一复制下来
{
a[len][m]=a[j][m-];
}
}
}
else//从右到左
{
for(int k=i-;k>=;k--)
{
len+=;
for(int l=;l<k;l++)
{
a[len][l]=a[j][l];
}
a[len][k]=i;
for(int m=k+;m<i;m++)
{
a[len][m]=a[j][m-];
}
} }
}
s=end+;//更新s,end.
end=len;
}
}
int jie(int n)
{
int ans=;
for(int i=;i<=n;i++){
ans*=i;
}
return ans;
}
int main()
{
solve();
scanf("%d",&n);
for(int i=;i<=n-;i++)
{
x+=jie(i);
}
y=x+jie(n)-;
for(int i=x;i<=y;i++)
{
for(int j=;j<n;j++)
{
printf("%d%c",a[i][j],j==n-?'\n':' ');
}
}
return ;
}
Nordic Collegiate Programming Contest 2015 B. Bell Ringing的更多相关文章
- Nordic Collegiate Programming Contest 2015 G. Goblin Garden Guards
In an unprecedented turn of events, goblins recently launched an invasion against the Nedewsian city ...
- Nordic Collegiate Programming Contest 2015 E. Entertainment Box
Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...
- Nordic Collegiate Programming Contest 2015 D. Disastrous Downtime
You're investigating what happened when one of your computer systems recently broke down. So far you ...
- Nordic Collegiate Programming Contest 2015(第七场)
A:Adjoin the Networks One day your boss explains to you that he has a bunch of computer networks tha ...
- (寒假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: &qu ...
- German Collegiate Programming Contest 2015 计蒜课
// Change of Scenery 1 #include <iostream> #include <cstdio> #include <algorithm> ...
- Codeforces Gym101572 B.Best Relay Team (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))
2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) 今日份的训练,题目难度4颗星,心态被打崩了,会的算法太少了,知 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...
随机推荐
- GPIO的翻转操作方法
STM32在进行IO翻转操作的时候可以使用以下方法:以PE.5为例 GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(1-(GPIO_ReadOutputDataB ...
- SQL server 游标,随机数使用
SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...
- Redis string(字符串)
1.getset key newValue //给key设置value,并返回旧的value,如果没有旧的value,返回nil. 示例: getset age //age 的值被设置为 ...
- sql常用操作(三)多表查询
1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...
- The great pleasure in life is doing what people say you cannot do.
The great pleasure in life is doing what people say you cannot do. 人生最大的快乐是做到别人认为你做不到的事情.
- css 居中问题总结
html代码: <div class="box"> <div class="box-item"> 文字 </div> < ...
- java 实现 excel sheet 拷贝到另一个Excel文件中 poi
public class CopyExcelSheetToAnotherExcelSheet { public static void main(String[] args) throws FileN ...
- 织梦dedecms手机版上下篇链接错误的解决方法
打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...
- leetcdoe Valid Anagram
题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description Given two strings s and ...
- Memcache笔记02-telnet操作memcached
telnet操作Memcached 登录到telnet连接到memcached服务: telnet 127.0.0.1 11211 memcached的基本命令: //当telnet登录成功可以看到一 ...