codeforces #312C 558C C. Amr and Chemistry(位运算)
来源:互联网 发布:网络犯罪罪名 编辑:程序博客网 时间:2024/05/01 05:11
题目链接:
点击打开链接
题目大意:
给出一些数,他们能做的操作是左移和右移,问最少做多少次操作能把他们都变成相同的数
题目分析:
首先要知道,左移的话的只是在二进制数末尾填0,而右移则要根据末尾是0还是1,如果是1的话,那么再左移的话会得到完全不同的结果,如果是0的话,那么再左移只是还原没有意义。所以做法就是对初始的数左移得到的数全是最短距离,然后右移得到的数去全是最短操作数,然后每次右移吞掉1的时候,再左移得到所有可能得到的结果,用dp[i]记录所有数到达i的最小操作数的总数,因为每次得到这个数时的操作数一定是最小的(没有做多余的操作),然后另开一个数组记录对于i是不是所有数都能到达,然后最后比较能够全部到达的数的总操作数最小的那个就是最终结果
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX 100007using namespace std;int n;int a;int dp[MAX];int vis[MAX];int main ( ){ while ( ~scanf ( "%d" , &n ) ) { memset ( vis , 0 , sizeof ( vis )); memset ( dp , 0 , sizeof (dp)); int judge = n; while ( n-- ) { scanf ( "%d" , &a ); int num = 0; int temp = a; while ( temp <= 1e5 ) { vis[temp]++; dp[temp] += num; temp <<= 1; num++; } num = 0; while ( a ) { if ( a&1 && a != 1 ) { int temp = (a>>1)<<1; int sum = num+2; while ( temp <= 1e5 ) { vis[temp]++; dp[temp] += sum; temp<<=1; sum++; } } num++; dp[a>>=1] += num; vis[a]++; } } /*cout << "dp : " << endl; for ( int i = 1 ; i <= 10 ; i++ ) printf ( "%d " , dp[i] ); puts("");*/ /*cout << "vis : " << endl; for ( int i = 1 ; i <= 10 ; i++ ) printf ( "%d " , vis[i] ); puts("");*/ int ans = 1e9; for ( int i = 0 ; i <= 1e5 ;i++ ) if (vis[i] == judge ) ans = min ( ans , dp[i] ); printf ( "%d\n" , ans ); }}
0 0
- Codeforces 558C Amr and Chemistry(数论+位运算)
- codeforces #312C 558C C. Amr and Chemistry(位运算)
- Codeforces 558C Amr and Chemistry 位操作
- codeforces 558C Amr and Chemistry(位操作)
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- !codeforces 558C Amr and Chemistry-yy题-(位运算相关)
- 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 (枚举)
- Codeforces 558C Amr and Chemistry
- 【23.39%】【codeforces 558C】Amr and Chemistry
- 558C Amr and Chemistry
- C. Amr and Chemistry(Codeforces Round #312(div2))
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry
- Codeforces Round #312 (Div. 2) C Amr and Chemistry
- Secure Delivery Center快速入门指南(五):本地更改
- Board level的启动流程分析
- GitHub 优秀的 Android 开源项目
- Facebook测试在品牌专页内开展电商销售
- POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数
- codeforces #312C 558C C. Amr and Chemistry(位运算)
- 通过CookieManager给WebView设置Cookie
- SpringMvc+Spring同时扫描出现问题。
- 搜索进阶 Fire!
- 字符串的获取
- 一键解决ScrollView嵌套ListView只显示一行的问题
- [MFC] 获得客户区在对话框上的位置
- 抓包使用说明
- 写一些去第一家公司面试的一些题目及答案