Codeforces 312C
来源:互联网 发布:国家生态系统研究网络 编辑:程序博客网 时间:2024/05/17 15:05
直接用暴力解法,把小于等于最大数的数字都试一遍。
// 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
- Codeforces 312C
- Codeforces Round #312 (Div. 2) C
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- codeforces 148 div2 C
- codeforces 242C
- Codeforces-234C Weather
- codeforces 106C Buns
- codeforces 158C
- Codeforces 243C
- CodeForces 13C Sequence
- C#高级编程五十七天----位数组
- 最小二乘法
- EALayout 第五节 自动计算容器高度
- Http状态消息
- 从文件结构认识HLS
- Codeforces 312C
- JAVA/IOS下emjoi表情的截取
- 合唱队
- hdu1087
- 叫我猿猿
- centos 安装jdk7
- Android Volley的基本用法--StringRequest以及JsonReques
- Android学习笔记(四十八):注册注销登录MVC模式案例
- excel转换成pdf的常用方法