CF234G Practice

来源:互联网 发布:网络共享管理软件 编辑:程序博客网 时间:2024/06/15 03:34

G.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

#include <stdio.h>#include <stdlib.h>#include <string.h>int ans[1000][1001];int quebg[1000000], head, tail;int queed[1000000];int imax(int a, int b){return a > b ? a : b;}void solve(int n){int i, j, k, m, bg, ed;int p, q, t;head = tail = 0;quebg[tail] = 1;queed[tail] = n;tail++;m = 0;while(head < tail){t = tail;k = 0;while(head < t){bg = quebg[head];ed = queed[head++];p = (bg + ed) / 2;//printf("m = %d, [%d, %d] | %d\n", m, bg, ed, p);for (i = bg; i <= p; i++)ans[m][++k] = i;if (p - bg + 1 > 1){quebg[tail] = bg;queed[tail++] = p;}if (ed - p > 1){quebg[tail] = p + 1;queed[tail++] = ed;}}ans[m][0] = k;m++;}printf("%d\n", m);for (i = 0; i < m; i++){printf("%d ", ans[i][0]);for (j = 1; j < ans[i][0]; j++)printf("%d ", ans[i][j]);printf("%d\n", ans[i][j]);}}int main(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);int i, j, k, n, m;scanf("%d", &n);solve(n);return 0;}/*每次把在同一组中的人平均分成两队来比赛总是最优的,即log(n)次就可以比完。问题是构造解,比如n=7时,括号里表示同一组,即没有相互比过赛的(1 2 3 4 5 6 7)(1 2 3 4) : (5 6 7)(1 2) (5 6) : (3 4) 71 3 5 : 2 4 6 7大致就是每次把同一组的,取一半出来组成一队。还有种方法,波哥说的,看1..n这n数的二进制,第i场,把第i-1位为0的分成一队,为1的分成一队。由于各个数都不同,所有任两数至少有1位不同,即至少比过一次赛。啧啧,真漂亮= =*/


原创粉丝点击