Codeforces 558C Amr and Chemistry 规律
来源:互联网 发布:深圳行知小学怎么样 编辑:程序博客网 时间:2024/05/22 02:00
题目链接
题意:
给定n长的序列
每次可以选一个数 让其 *=2 或者 /=2
问至少操作多少次使得所有数相等。
思路:
对于每个数,计算出这个数可以变成哪些数,以及变成那个数的最小步数。
cnt[i] 表示序列中有cnt个数可以变成i
step[i] 表示能变成i的 那些数 变成i的花费和是多少。
notice: if a[i] == 7, a[i] also can reach 6. by /=2 then *=2
7->3->1..
3->6->12
1->2->4
只要是奇数(不包括1) 就能花费2步到达 a[i]-1的位置
#include <iostream>#include <string>#include <vector>#include <cstring>#include <cstdio>#include <map>#include <queue>#include <algorithm>#include <stack>#include <cstring>#include <cmath>#include <set>#include <vector>using namespace std;template <class T>inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template <class T>inline void pt(T x) {if (x <0) {putchar('-');x = -x;}if (x>9) pt(x / 10);putchar(x % 10 + '0');}typedef long long ll;typedef pair<ll, ll> pii;const int N = 200005;const int inf = 1e9 + 10;int n;int a[N];int cnt[N], step[N];void up(int y, int s) {//向上枚举while (y < N) {cnt[y]++;step[y] += s;y <<= 1;s++;}}void go(int y) {up(y, 0);int now = 1;while (y) { //使y一直向下减小if ((y > 1) && (y & 1)) {y >>= 1;up(y, now);}else {y >>= 1;cnt[y]++; step[y] += now;}now++;}}int main() {while (cin >> n) {memset(cnt, 0, sizeof cnt); memset(step, 0, sizeof step);for (int i = 1; i <= n; i++) {rd(a[i]);go(a[i]);}int ans = step[1];for (int i = 2; i < N; i++) {if (cnt[i] == n)ans = min(ans, step[i]);}pt(ans);}return 0;}
0 0
- Codeforces 558C Amr and Chemistry 规律
- codeforces 558 C Amr and Chemistry
- Codeforces 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 暴力 - -
- codeforces 558C Amr and Chemistry
- Codeforces 558C - Amr and Chemistry (枚举)
- Codeforces 558C Amr and Chemistry
- 【23.39%】【codeforces 558C】Amr and Chemistry
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 位操作
- Codeforces 558C Amr and Chemistry 全都变相等
- Codeforces 558C Amr and Chemistry(dp)
- Codeforces 558C Amr and Chemistry(数论+位运算)
- codeforces 558C Amr and Chemistry(位操作)
- Codeforces 558C. Amr and Chemistry(暴搞)
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
- CF 558C Amr and Chemistry
- xstream学习,对象和xml之间转换,对象和json之间转换
- 学习使用豆瓣API
- Android中安装asset文件下的apk
- 分享一个比较有意思的字典相等比较扩展方法DictionaryEqual
- MAP
- Codeforces 558C Amr and Chemistry 规律
- 认知实习有感
- 黑马程序员-Java基础--Thread类
- Target runtime Apache Tomcat v6.0 is not defined.错误解决方法
- Android studio 报错 Failed to complete Gradle execution already disposed:module:'***'
- 指向函数的指针数组的用法
- C语言语句篇
- 腾讯电脑管家(QQ电脑管家)劫持IE开始页至 http://guanjia.qq.com/comm-htdocs/quickaccess/ 的处理
- Android 命令