C. Numbers on Whiteboard(模拟+贪心) Educational Codeforces Round 96 (Rated for Div. 2)

原题链接: https://codeforces.com/contest/1430/problem/C

在这里插入图片描述
测试样例

input
1
4
output
2
2 4
3 3
3 1

题意: 给定一个 1 1 1~ n n n的序列,你每次需要选择两个数 a , b a,b a,b删除,再将 ( a + b + 1 ) / 2 (a+b+1)/2 (a+b+1)/2这个数放回序列中。最后使得这个序列变成一个数,且这个数要尽可能小。输出这个最小数,以及你依次进行的操作选择数。

解题思路: 我们要使得最后剩余的数最小,那么我们一定要知道,让选择大数和小数最后中和得到的一定不是最小,因为这样两个都往中间靠了。所以我们想要让这往左靠,就必须选择两个最大的数进行操作,再将合成数放回序列中,再依次进行如上操作,直到剩余最后一个数。 那么我们很容易就会想到利用优先队列来实现,即每次取队头两个数,并记录(最后输出需要用到。),同时删除这两个数,并将合成数放入队列中模拟操作即可。直到队列中只剩一个元素即为答案。OK,具体看AC代码。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t;
int n;
struct node{
    
    
	int x,y;
};
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n;
			priority_queue<int> q;
			rep(i,1,n){
    
    
				q.push(i);
			}
			vector<node> v;
			while(q.size()>1){
    
    
				int temp1=q.top();
				q.pop();
				int temp2=q.top();
				q.pop();
				v.push_back({
    
    temp1,temp2});
				q.push((temp1+temp2+1)/2);
			}
			cout<<q.top()<<endl;
			int len=v.size();
			rep(i,0,len-1){
    
    
				cout<<v[i].x<<" "<<v[i].y<<endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109015612