Codeforces Amr and Chemistry(数学+乱搞)

题意:给n个数,每个数每次可以乘二或除以二(向下取整相当于左移或右移),问最少经过多少次操作可以使这n个数变相等。

思路:首先考虑每个数的可能取值,将一个数表示成s*2^k的形式,s是奇数。

那么这个数的所有可能取值为s'*2^x,(s'=s/2,(s/2)/2,.....)且s'*2^x<=100000

因为这题数据范围不大,而且每个值可能的取值不多最多几百个,所以记录1到100000每个值可能被取到的次数以及总操作数,最后从1遍历到100000取最小的ans即可

ps:个人赛这道题做了一下午无数乱搞写的代码巨恶心......晚上回来优化了一下勉强能看..........

#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack> 
#include<string>
#include<map> 
#include<set>
#define eps 1e-6 
#define LL long long  
using namespace std;  

//const int maxn = 100 + 5;
const int INF = 100000000;
int n, abc;
int vol[100010], ans[100010];  //vol代表每个容量可能被取到的次数,ans表示取到该容量所要的操作总数 
int main() {
//	freopen("input.txt", "r", stdin);
	while(scanf("%d", &n) == 1) {
		memset(vol, 0, sizeof(vol));
		memset(ans, 0, sizeof(ans));
		for(int i = 0; i < n; i++) {
			cin >> abc;
			int t = abc;
			int cnt = 1, cnt1 = 0;
			abc <<= 1;
			while(abc <= 100000) {
				vol[abc]++;
				ans[abc] += cnt;
				cnt++;
				abc <<= 1;
			}
			cnt = 0;
			while(t%2 == 0) {
				vol[t]++;
				ans[t] += cnt;
				cnt++;
				cnt1++;
				t >>= 1;
			}
			vol[t]++;
			ans[t] += cnt;
			t >>= 1;
			cnt1++;
			while(t != 0) {
				int u = t;		
				int cnt2 = cnt1;
				while(u <= 100000) {
					vol[u]++;
					ans[u] += cnt2;
					cnt2++;
					u <<= 1;
				}
				if(t%2 == 0) {
					while(t%2 == 0) {
						t >>= 1; 
						cnt1++;
						vol[t]++;
						ans[t] += cnt1;
					}
				}
				t >>= 1; cnt1++;
			}
		}
		int pans = INF;
		for(int i = 0; i <= 100000; i++) if(vol[i] == n)
			pans = min(pans, ans[i]);
		//cout << vol[2] << endl << vol[1] << endl << vol[8] << endl;
		cout << pans << endl;	
	}
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值