CodeForces - 240 C. Practice 模拟或者二分或者规律

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 240 C. Practice 模拟或者二分或者规律相关的知识,希望对你有一定的参考价值。


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.

Examples

Input


2


Output


1 1 1


Input


3


Output


2 2 1 2 1 1


题意:

给编号为1~n的人,每一训练可以将其分为两队,队与队之间可以相会打仗,问要求每两个人之间都得打一次的最少的训练数?

分析:

以8为例:

3次即可

1234   5678

1256   3478

1458   3276

多模拟几次就可以发现最后的答案为为ans=ceil(

CodeForces

),然后9,10,11,12,13,14,15,16都是4,所以我们在模拟上述过程把9~15都填充为16个不够补0,0不输出即可,模拟一下上述过程即可。

模拟

我们每次以长度为len交换,隔len交换,且i<=(2^ans)/2,i与i+(2^ans)/2交换

第一次:len=4,num=0

1234   5678

第二次:len=2

5634    1278

第三次:len=1

1674     5238

具体模拟过程看代码把

二分

二分不好写得原因是不确定它在哪一层,所以需要一个数组记录。

规律

1234 5678

第一次:1357       2468

第二次:1526       3748

第三次:1234       5678

然后就可以了

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll ;
const int N=200000+7;
int a[N];
int main()

freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
scanf("%d",&n);
int ans=ceil(log(n)/log(2));
for(int i=1; i<=n; ++i)

a[i]=i;

printf("%d\\n",ans);
int len=(1<<ans)/2;
int mid=(1<<ans)/2;
for(int i=1;i<=ans;i++)

int cnt=0;
for(int j=1;j<=mid;j++)

if(a[j]!=0)
cnt++;

printf("%d ",cnt);
for(int j=1;j<=mid;j++)

if(a[j]!=0)
printf("%d ",a[j]);

cout<<endl;

len/=2;
int num=0;
for(int j=1;j<=mid;j+=len)

num++;
for(int t=j;t<j+len;t++)

swap(a[t],a[t+mid]);

j+=len;
if(num>=(1<<(i-1)))
break;







return 0 ;

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll ;
const int N=200000+7;
int a[N];
vector<int> mapp[N];
void two_solve(int deep,int l,int r)

if(l==r) return;
int mid=(l+r)>>1;
for(int i=l;i<=mid;i++)

mapp[deep].push_back(a[i]);


two_solve(deep+1,l,mid);
two_solve(deep+1,mid+1,r);


int main()

// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n;
scanf("%d",&n);
int ans=ceil(log(n)/log(2));
for(int i=1; i<=n; ++i)

a[i]=i;

printf("%d\\n",ans);

two_solve(1,1,n);
for(int i=1;i<=ans;i++)

printf("%d ",mapp[i].size());
for(auto x:mapp[i])
printf("%d ",x);
cout<<endl;

return 0 ;

 

 

 

以上是关于CodeForces - 240 C. Practice 模拟或者二分或者规律的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 240D. Merging Two Decks 贪心+暴力

CodeForces C. Maximal Intersection

[codeforces 241]C. Mirror Box

codeforces 597C C. Subsequences(dp+树状数组)

CodeForces C. Songs Compression

Codeforces 845 C. Two TVs (模拟)