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; 
 } 
相关文章
|
1月前
|
数据采集 供应链 程序员
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
本文详解京东店铺全量商品接口(jd.seller.ware.list.get)实战经验,涵盖权限申请、分页避坑、SKU关联、数据校验等核心难点,附Python代码与反限流策略,助你高效稳定获取完整商品数据,新手可少走两年弯路。
|
IDE Shell 网络安全
Visual Studio 2022 git error Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa
Visual Studio 2022 git error Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa
742 0
Visual Studio 2022 git error Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa
|
云安全 监控 安全
云安全中心和防火墙有什么不同
云安全中心和防火墙有什么不同
634 4
|
前端开发
前端基础(八)_盒子模型(标准盒子模型和怪异盒子模型)
本文介绍了CSS盒子模型的基本概念,包括内容、内边距、边框和外边距的属性,以及标准盒子模型和怪异盒子模型的区别和转换方法。
501 1
前端基础(八)_盒子模型(标准盒子模型和怪异盒子模型)
|
存储 Java
Java中Integer.MAX_VALUE的含义
Java中Integer.MAX_VALUE的含义
738 0
|
Prometheus 监控 Cloud Native
Prometheus 社区与生态发展
【8月更文第29天】Prometheus 是一个开源的监控系统和时间序列数据库,以其简单易用、高性能的特点受到了广泛欢迎。自 2012 年成立以来,Prometheus 社区迅速壮大,形成了一个庞大且活跃的技术生态系统。本文将探讨 Prometheus 社区的发展趋势、相关项目和工具,以及如何参与贡献。
427 1
|
负载均衡 网络协议 Unix
Nginx负载均衡与故障转移实践
Nginx通过ngx_http_upstream_module模块实现负载均衡与故障转移,适用于多服务器环境。利用`upstream`与`server`指令定义后端服务器组,通过`proxy_pass`将请求代理至这些服务器,实现请求分发。Nginx还提供了多种负载均衡策略,如轮询、权重分配、IP哈希等,并支持自定义故障转移逻辑,确保系统稳定性和高可用性。示例配置展示了如何定义负载均衡设备及状态,并应用到具体server配置中。
|
设计模式 前端开发 PHP
【PHP开发专栏】ThinkPHP框架实战开发
【4月更文挑战第29天】ThinkPHP是中国流行的PHP框架,以其轻量级、模块化和高安全性受到开发者欢迎。本文介绍了ThinkPHP的基础,包括MVC设计模式,以及核心组件如路由、数据库操作、表单处理、模板引擎和错误处理。通过一个博客系统示例,展示了如何进行项目开发,包括控制器、模型和视图的创建。使用ThinkPHP能有效提升开发效率,帮助开发者快速构建复杂的Web应用。
320 0
|
机器学习/深度学习 定位技术
一文搞懂:【OpenJ_Bailian
一文搞懂:【OpenJ_Bailian
113 0
|
文字识别 开发工具
文字识别OCR常见问题之行驶证识别最近总是识别错误如何解决
文字识别OCR(Optical Character Recognition)技术能够将图片或者扫描件中的文字转换为电子文本。以下是阿里云OCR技术使用中的一些常见问题以及相应的解答。
261 0