Codeforces 312C

直接用暴力解法,把小于等于最大数的数字都试一遍。

// 312C
#include <iostream>
#include <memory.h>
#include <stdio.h>
using namespace std;

struct link{
	int num;
	link* next;

	link(int no, link* n) : num(no), next(n) {}
	link() : next(NULL) {}
};

struct list{
	link *head, *tail;

	list(){
		head = tail = new link();
	}

	~list(){
		link *tmp = head;
		while (tmp) {
			head = head->next;
			delete tmp;
			tmp = head;
		}
	}

	void append(int x){
		tail = tail->next = new link(x, NULL);
	}
};

int chem[100100];
int indi[100100];

int main(){
	int n, i;
	cin >> n;

	int max = 0;
	for (i = 0; i <= n - 1; i++) {
		cin >> chem[i];
		if (chem[i] > max) max = chem[i];
	}

	int t = 2;
	memset(indi, 0, sizeof(indi));
	for (i = 1; t <= 100100; i++, t <<= 1) indi[t] = i;
	
	int tmax = max;
	list ls;
	while (tmax > 0){
		ls.append(tmax);
		tmax--;
		//tmax >>= 1;
	}

	link* cur = ls.head;
	int min_step = -1;
	while (cur->next){
		int cur_step = 0;
		int reachable = 1;

		for (i = 0; i <= n - 1; i++){
			int step = 0;

			if (cur->next->num > chem[i]){
				if (cur->next->num % 2 != 0){
					reachable = 0;
					break;
				}
				int t = chem[i];
				while (t > 0){
					if (cur->next->num % t == 0 && indi[cur->next->num / t]){
						step += indi[cur->next->num / t];
						break;
					}
					t >>= 1;
					step++;
				}
				if (t == 0){
					reachable = 0;
					break;
				}
				cur_step += step;
			}
			else if (cur->next->num < chem[i]){
				int t = chem[i];
				while (t > 0 && t > cur->next->num){
					t >>= 1;
					step++;
				}

				if (t == cur->next->num) cur_step += step;
				else {
					while (t > 0){
						if (cur->next->num % t == 0 && indi[cur->next->num / t]){
							step += indi[cur->next->num / t];
							break;
						}
						t >>= 1;
						step++;
					}
					if (t == 0){
						reachable = 0;
						break;
					}
					else cur_step += step;
				}
			}
		}

		if (reachable){
			if (min_step == -1) min_step = cur_step;
			else if (cur_step < min_step) min_step = cur_step;

			//cout << "Target: " << cur->next->num << endl;
			//cout << "Steps: " << cur_step << endl;

			cur = cur->next;
		}
		else {
			link* tmp = cur->next;
			cur->next = tmp->next;
			delete tmp;
		}
	}

	cout << min_step << endl;
	//system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值