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

简介: 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; 
 } 
相关文章
|
机器学习/深度学习 人工智能 算法
主流AI服务——大专生新就业之路
主流AI服务——大专生新就业之路
1147 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10265 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1547 5
|
9天前
|
云安全 数据采集 人工智能