您的位置:首页 > 其它

Amr and Chemistry---cf558C(暴力,加技巧)

2016-01-27 10:26 344 查看
题目链接:http://codeforces.com/problemset/problem/558/C

题意:有n个数,每个数都可以进行两个操作 *2 或者 /2,求最小的操作次数和,使得所有的数都相等;

计算一下时间复杂度可以知道每个数所能达到数的时间复杂度是log(1e5)最终的时间复杂度也就是 nlog(1e5), 所以暴力能过,

刚开始看到的时候一看数据范围就根本不敢暴力了。。。

我们可以把每个数所有变化情况都记录下来,并记录出现的次数,因为只有用1

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 100100
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
using namespace std;

int  num
, cnt
;

void F(int x)
{
int step = 0, t = x;

cnt[x] ++;

while(t*2 < N)///往上*2得到的都是偶数,直接记录;
{
t = t*2;
step ++;
num[t] += step;
cnt[t] ++;
}

t = x, step = 0;

while(t > 1)
{
if(t!=1 && t%2==1)///往下/2得到的可能是奇数, 如果是奇数的话继续往上*2;
{
int y = t/2;
int step0 = step+1;
while(y*2 < N)
{
y = y*2;
step0 ++;
num[y] += step0;
cnt[y] ++;
}
}
t = t/2;
step ++;
num[t] += step;
cnt[t] ++;
}
}

int main()
{
int n, x;
while(scanf("%d", &n)!=EOF)
{
met(cnt, 0);
met(num, 0);
for(int i=0; i<n; i++)
{
scanf("%d", &x);
F( x );
}
int ans = INF;
for(int i=0; i<N; i++)
{
if(n == cnt[i])
ans = min(ans, num[i]);
}
printf("%d\n", ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: