Numbers on Whiteboard (codeforces1430)(数学分析)

Numbers 1,2,3,…𝑛 (each integer from 1 to 𝑛 once) are written on a board. In one operation you can erase any two numbers 𝑎 and 𝑏 from the board and write one integer 𝑎+𝑏2 rounded up instead.

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

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

choose 𝑎=4 and 𝑏=2, so the new number is 3, and the whiteboard contains [1,3,3];
choose 𝑎=3 and 𝑏=3, so the new number is 3, and the whiteboard contains [1,3];
choose 𝑎=1 and 𝑏=3, so the new number is 2, and the whiteboard contains [2].
It’s easy to see that after 𝑛−1 operations, there will be left only one number. Your goal is to minimize it.

Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases.

The only line of each test case contains one integer 𝑛 (2≤𝑛≤2⋅105) — the number of integers written on the board initially.

It’s guaranteed that the total sum of 𝑛 over test cases doesn’t exceed 2⋅105.

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

Example
inputCopy
1
4
outputCopy
2
2 4
3 3
3 1

题意翻译

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

给定 n 个数,为 1,2,3,...,n每次可以选择两个数 a,b 并删除,然后把[(a+b)​/2⌉ 加入到这些数中。最小化最后剩下的一个数,输出这个数并且输出构造方案。

额,还是一个水题简单模拟就行;
从后往前依次合并,可以保证最大数每次减少1或者不变,最优结果为2,注意从第2位开始。

额,一到水题,写了2份ac代码,可选择看;

 我的头像

 有事你就q我;QQ2917366383

学习算法

#include <iostream>
#include <cstring>
#include <ctime>
#include <algorithm>
using namespace std;
int t,n;
void solve(){
    printf("2\n");
    int _t;
    _t = n;
    if(n == 2){
        printf("1 2\n");
        return;
    }//打印2段是为了让你们看得清楚一点。
    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,i + 2);
    }
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        solve();
    }
    return 0;//听懂掌声
}

#include<bits/stdc++.h>
using namespace std;
int n,t;
void pk()
{
	cout<<"2"<<endl;
	if(n==2)
	{
			cout<<"1"<<' '<<"2"<<endl;
			return;
	}
cout<<n-2<<' '<<n<<endl;
cout<<n-1<<' '<<n-1<<endl;
for(int i=n-3;i>=1;i--)
cout<<i<<' '<<i+2<<endl;
	
}
int main()
{
	cin>>t;
	while(t--)
	{
	
		cin>>n;
		pk(); 
	}
return 0;	
 } 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值