比赛时间只有两个小时,我没有选做这题,因为当时看样例也看不懂,比较烦恼。

后来发现,该题对输入输出要求很低。远远没有昨天我在做的A题的麻烦,赛后认真看了一下就明白了,写了一下,一次就AC了,没问题,真心有点后悔。

来先看题目:

C. Practice

time limit per test

1 second

memory limit per test

256 megabytes

input

input.txt

output

output.txt

Little time is left before Berland annual football championship. Therefore the coach of team "Losewille Rangers" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are n players in "Losewille
Rangers". Each player on the team has a number — a unique integer from 1 to n. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices.



Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all n players of the team take part in the game. The players are sorted into two teams in
some way. In this case, the teams may have different numbers of players, but each team must have at least one player.



The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices.



Help him to schedule the practices.

Input



A single input line contains integer n (2 ≤ n ≤ 1000).

Output



In the first line print m — the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in m lines.



In the i-th of those lines print fi — the number of players in the first team during the i-th practice (1 ≤ fi < n), and fi numbers from 1 to n — the numbers of players in the first team. The rest of the players will play in the second team during this practice.
Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them.

Sample test(s)

Input



2



Output



1

1 1



Input



3



Output



2

2 1 2

1 1



如果你跟我一样,看不下题目的话,可以听我解释一下:

有一支球队,有n个人,现在教练要分成两队来练习,队伍必须要有人(不然怎么踢~?)。

问最小组织多少场练习,能够让每个队伍里面的人都有与队友对战过至少一次。

输入为人数n

输出第一行为至少练习场数k。

接下来k行是,第一队伍的人数,以及球员号码,如果有多种情况,随意输出一种。

比如 3

那么至少需要2场

                  第一场               第二场

一队          1    2                   1  3

二队          3                          2

这么我们的输出就是

2

2 1 2

2 1 3

一开始看这题目,我以为是组合数学的东西,后来模拟了一下,有点像二分,当n=2^p,这个情况很容易想到,比如

1 2 3 4 5 6 7 8 个球员

我们需要进行3场球赛

1 2 3 4

5 6 7 8



5 6 3 4

1 2 7 8



1 6 7 4

5 2 3 8

做法就是:第一场1 ~4 为一队,剩下的为二队

第二场为一队前2个与二队的前2个交换

第三场为一队每两个人中的第一个人与二队的对应交换。

推广到2^p次方就是

第一场为 1~ 2^(p-1)为一队,剩下的为二队

第二场为一队前2^(p-2)个人与二队的对应交换

第三场为一队每2^(p-2)个人前2^(p-3)个人与二队的对应交换

………

第  i 场为一队每2^(p-i+1)个人前 2^(p-i)个人与二队的对应交换

直到i=p为止



那么这个策略是最优的吗?

我们可以这么想,当n=2时,p=1,显然是最优,当n=4时,p=2,第一场

1 2

3 4

之后需要的是1 2对战, 3 4对战,当1 2对战时候,3 4也会对战,这个操作是对称的。显然此时也是正确的

那么 1 2 3 4  后

         5 6 7 8

可以化成1 2 3 4对战,同时对应的5 6 7 8也会对战(对称性)。那么就化成1 2 3 4的情况了。

即个策略推广下去,也是最优的。

我们之所以选择前面的往下调,是为了写起来方便,效果是一样的,可以想象一下。

但是当n!=2^p时候,怎么办呢?

我的想法是加上“替补",当然这是我的说法啦,就是填补0,使得填补后的 n=2^p,这样,当填补后满足了,正常的也会满足(这是显然的)。但是,会不会有满足“替补”带来的多余的操作次数呢?(细心的人会这么想)

其实仔细一想,无需担心。我们这样考虑,n必然会被有不等式,2^p0<n<2^(p0+1),我们的策略始终有一个位置的人不动的在1~2^(p0-1)个位置中总有一个不是不动的,在我的策略中就是第2^(p0-1)位置是不动的

比如

1 2 3 4

5 6 7 8 9



5 6 3 4

1 2 7 8 9



1 6 7 4

5 2 3 8 9

这样一来,9号始终不与8对战,同理可以推理更多情况,这样看来,需要的场数需要大于p0,而p0-1必然满足。证毕。

所以我们最后的得到的结果就是 ceil(log2(n))场比赛,当n不为2^p数,填补0,之后按照上述策略,在输出的时候忽略0.

以下是我的代码,一次AC,莫大的鼓励啊!(尽管是赛后了~)代码不当之处,欢迎大家的建议、指导!

明晚第二场!为自己加油!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
vector<int> v,v2; void swap_v(int bg,int len)
{ int i;
for(i=0; i<len/2; i++)
{
v[bg+i]=v[bg+i]^v2[bg+i]^(v2[bg+i]=v[bg+i]); //位运算交换
}
} int main()
{ freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,i,j; while(cin>>n)
{
int cnt=ceil(log(n)/log(2));
;
cout<<cnt<<endl;
int siz =1<<(cnt-1) ;//siz为填补后的人数一半
v.clear();
v2.clear();
v.resize(siz+1);
v2.resize(siz+1);
for(i=1; i<=siz; i++)
{
v[i]=i; //初始化
}
for(i=1; i+siz<=n; i++)
{ v2[i]=i+siz;
} int len=siz; //len为跨距 for(; len!=0; len>>=1)
{
cnt=0; //其实严格来说要用第二个变量来记载一队的人数,但是“废物利用嘛……”
for(j=1; j<=siz; j++)
{
if(v[j])
{
cnt++;
}
}
cout<<cnt; //输出一队人数
for(j=1; j<=siz; j++)
{
if(v[j])
{
cout<<" "<<v[j]; //不是替补0就输出
}
}
cout<<endl; for(j=1; j<=siz&&len!=1; j+=len)
{ swap_v(j,len); //每len个人交换前len/2个人
} } } fclose(stdin);
fclose(stdout); return 0; }

校省选赛第一场C题解Practice的更多相关文章

  1. 校省选赛第一场A题Cinema题解

    今天是学校省选的第一场比赛,0战绩收工,死死啃着A题来做,偏偏一直WA在TES1. 赛后,才发现,原来要freopen("input.txt","r",stdi ...

  2. 校省选赛第一场D题TwoDecks题解

    今天晚上第二场比赛,现在还是赛后刷上次的题目,越刷越伤心,发现我赛后一次AC的功力很强大啊!!!(希望今晚变成是赛中一次AC啊!!) 好啦,回归正题. 看题目 D. Merging Two Decks ...

  3. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  4. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  5. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  6. 牛客网暑期ACM多校训练营(第一场)I Substring

    题意:给你一个只有abc的字符串,求不相同的子串,(不同构算不同,例如aba和bab算同构) 题解:很显然,如果不考虑同构的问题,我们直接上sa/sam即可,但是这里不行,我们考虑到只有abc三种字符 ...

  7. 2019牛客暑期多校训练营(第一场) - H - XOR - 线性基

    https://ac.nowcoder.com/acm/contest/881/H 题意: 给定n个整数,求其中异或和为 \(0\) 的子集的大小的和. 题解思路: 首先转化为每个可以通过异或表示 \ ...

  8. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes

    传送门 题意: 先输入一个n,代表两个数组里面都有n个数,然后让你从中找到一个p<=n,使其满足(1<=l<=r<=p<=n)可以让在(l,r)这个区间内在两个数组中的的 ...

  9. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

随机推荐

  1. 进军es6(1)---初识es6

    es6,全称ECMAScript6(又名es2015).何为ECMAScript?我们常说的Javascript和它又有什么联系呢? 阮一峰老师有一句话描述的比较准确:“ECMAScript是Java ...

  2. ARM学习笔记1——Arm寄存器与模式的关系

    ARM微处理器上有37个32位的寄存器,其中有6个状态寄存器(一个CPSR,5个SPSR),其它31个为通用寄存器.在ARM的不同模式下,可以访问的物理寄存器是不同,如下图所示: 从图中可知,用户模式 ...

  3. 普通用户从非80端口启动tomcat,通过端口转发监听80端口

    linux下小于1024的端口都需要root去绑定. root权限启动tomcat是不明智的,可以使用非root权限启动tomcat监听8080端口,然后利用端口转发实现对80端口的监听. 端口转发: ...

  4. 西安Uber优步司机奖励政策(2月1日~2月7日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. Notepad++强大的代码补全和代码提示功能的方法

    最近写项目,经常要打开一些文件去修改一些代码段.那么我的项目都是使用ied大型编辑器去写的,每次修改文件,哪怕是一个标点都要用一分钟时间去打开软件.当然,后来我也考虑到使用记事本,但总感觉不是很爽. ...

  6. css控制文本框的只读属性的方法

    css 封装整个只读文本框的属性: .TextBoxReadOnly{ border:1px solid #C0C0C0; text-align:left; background-color:#D3D ...

  7. 保存网页MHT

    uses ADODB_TLB, CDO_TLB, ComObj,MSHTML;{$R *.dfm}{能把网页如 WWW.QQ.COM保存为一个单文件 .MHT但不能把一个 A.HTM 保存为一个单文件 ...

  8. Spring框架:Spring容器具体解释

    Spring容器 Spring容器能够帮助你管理所有的Bean对象.专业术语称之为IoC控制反转.在传统的程序中.对象的生成都是由开发人员完毕的.而在控制反转中,对象的生成所有都交给框架完毕.这种优点 ...

  9. OpenGL中的投影使用

    OpenGL中的投影使用 在OpenGL中,投影矩阵指定了可视区域的大小和形状.对于正投影与透视投影这两种不同的投影类型,它们分别有各自的用途. 正投影 它适用于2D图形,如文本.建筑画图等.在它的应 ...

  10. Android-打反编译工具的一种方法

    转载请注明出处:http://blog.csdn.net/goldenfish1919/article/details/41010261 首先我们来看下dex文件的格式: class_defs的结构: ...