当前位置: 代码迷 >> 综合 >> Codeforces C. Numbers on Whiteboard (Round 96 Rated for Div.2) (思维 / 贪心)
  详细解决方案

Codeforces C. Numbers on Whiteboard (Round 96 Rated for Div.2) (思维 / 贪心)

热度:59   发布时间:2023-12-22 13:15:03.0

传送门

题意: 现有1到n的n个数字,需要进行N-1次操作,每次选出两个数a与b删除,并把 (a+b)/2新加入集合中。求最后只剩一个数时的最小值。
在这里插入图片描述
思路: 用一个优先队列,每次取出最大的两个数,并把他们和的1/2上取整新加入集合。真没想到这样能行(#.#),神奇鸭。

代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
    {
    1, 0}, {
    -1, 0}, {
    0, 1}, {
    0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;int t, n;signed main()
{
    IOS;cin >> t;while(t --){
    cin>> n;cout << 2 << endl;priority_queue<int> q;for(int i = 1; i <= n; i ++) q.push(i);while(q.size() > 1){
    int top1 = q.top(); q.pop();int top2 = q.top(); q.pop();cout << top1 << " " << top2 << endl;q.push(ceil((top1*1.0+top2*1.0)/2));}}return 0;
}
  相关解决方案