跳到正文

题解

题解 CF1277B 【Make Them Odd】

gyh20约 1 分钟
原文
文章信息与版本

文章信息

发布
2019年12月16日
收录
2026年6月14日
更新
2026年11月17日
篇幅
约 1 分钟
题目
CF1277B

版本

  1. v1当前

贪心,从大的开始修改,这样不会有多余的修改操作。

用优先队列维护当前最大值,如果是奇数就一直弹,否则一次性弹出相同的再插入这个值/2。

代码:

C++
#include<cstdio>
#include<queue>
#define re register
using namespace std;
inline int read(){
	re int t=0;
	re char v=getchar();
	while(v<'0'||v>'9')v=getchar();
	while(v>='0'&&v<='9'){
		t=(t<<3)+(t<<1)+(v^48);
		v=getchar();
	}
	return t;
}
priority_queue <int> q;
int main(){
	int t=read(),n;
	while(t--){
		n=read();
		int ans=0,tmp=0;
		while(n--){
			q.push(read());
		}
		while(!q.empty()){
			if(q.top()&1){
				q.pop();
				continue;
			}
			else{
				if(q.top()==tmp){
					q.push(q.top()/2);
					q.pop();
					continue;
				}
				else{
					++ans;
					q.push(q.top()/2);
					tmp=q.top();
					q.pop();
					}
			}
		}
		printf("%d\n",ans);
	}
}
原文