CF1430C Numbers on Whiteboard

题目描述

Numbers 1, 2, 3, \dots n1,2,3,…n (each integer from 11 to nn once) are written on a board. In one operation you can erase any two numbers aa and bb from the board and write one integer \frac{a + b}{2}2a+b​ rounded up instead.

You should perform the given operation n - 1n−1 times and make the resulting number that will be left on the board as small as possible.

For example, if n = 4n=4 , the following course of action is optimal:

  1. choose a = 4a=4 and b = 2b=2 , so the new number is 33 , and the whiteboard contains [1, 3, 3][1,3,3] ;
  2. choose a = 3a=3 and b = 3b=3 , so the new number is 33 , and the whiteboard contains [1, 3][1,3] ;
  3. choose a = 1a=1 and b = 3b=3 , so the new number is 22 , and the whiteboard contains [2][2] .

It's easy to see that after n - 1n−1 operations, there will be left only one number. Your goal is to minimize it.

输入格式

The first line contains one integer tt ( 1 \le t \le 10001≤t≤1000 ) — the number of test cases.

The only line of each test case contains one integer nn ( 2 \le n \le 2 \cdot 10^52≤n≤2⋅105 ) — the number of integers written on the board initially.

It's guaranteed that the total sum of nn over test cases doesn't exceed 2 \cdot 10^52⋅105 .

输出格式

For each test case, in the first line, print the minimum possible number left on the board after n - 1n−1 operations. Each of the next n - 1n−1 lines should contain two integers — numbers aa and bb chosen and erased in each operation.

题意翻译

TT 次询问,对于每一次询问:

给定 nn 个数,为 1,2,3,...,n1,2,3,...,n,每次可以选择两个数 a,ba,b 并删除,然后把 \left\lceil\frac{a+b}{2}\right\rceil⌈2a+b​⌉ 加入到这些数中。最小化最后剩下的一个数,输出这个数并且输出构造方案。

2\leq n\leq 2\cdot10^5,\sum n \leq 2\cdot10^52≤n≤2⋅105,∑n≤2⋅105

输入输出样例

输入 #1复制

1
4

输出 #1复制

2
2 4
3 3
3 1

题目类型:推理

题目目的:找出方案

解题思路:1)n>= 2, 且向上取整;

                2)当 n ==2时,方案: {1 2}

                  当 n=3时, 方案:{1 3}

                  当 n=4时, 方案:{(2 4)  (3 3 ), (3, 1)}

                   当n = 5时, 方案:{(3, 5) (4 4) (4 2) (3 1)}

                  当n = 6时, 方案:{(4 6) (5 5) (5 3) (4 2) (3 1)}

                        ……

                  当 n = k时,方案:{(n-2, n)(n-1 n-1) (n-1 n-1-2)……(3 1)}

                        不难发现 当 n >= 4 ,第一组数据为(n-2 n),第二组数据一定为(n-1 n-1),从第三组数据开始遵从(k k-2);

AC代码:

#include  <bits/stdc++.h>
#define rep(x, a, b) for(int x=a; x<=b; x++)
using namespace std;
const int N= 2e5+10;
int a[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n, cur;
		scanf("%d", &n);
		printf("2\n");
		if(n == 2)
			printf("1 2\n");
		else
		{
			printf("%d %d\n", n-2, n);
			printf("%d %d\n",n-1, n-1 );
			for(int i = n-3; i>=1; i--)
				printf("%d %d\n", i+2, i);
		}
			
	}
	return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值