位压缩
http://ac.jobdu.com/problem.php?pid=1402?特殊的數
#include<iostream> #include<cstdio> #include<cstring> using namespace std;#define MAX 1000001 unsigned char hash[MAX/4]; //用兩位來保存一個數字,所以這里是MAX/4,而不是MAX/8,如果是用一位來保存一個數字,那么數組的大小只要達到MAX/8就夠用了int main(void) {int sum,n,value,min,max,i,index;while(scanf("%d",&n) != EOF){memset(hash,0,sizeof(hash));min = 1000001;max = -1;while(n--){scanf("%d",&value);if(value > max)max = value;if(value < min)min = value;value <<= 1;index = value >> 3; //確定這個數字保存在某個字符類型上value %= 8; //確定這個數字保存在某個字符的某個字節位置上if(hash[index] & (1 << value)) //這個數字已經出現了兩次及以上continue ;if(hash[index] & (1 << (value+1))) //這個數字已經出現了一次hash[index] |= (1 << value);elsehash[index] |= (1 << (value+1)); //這個數字第一次出現}sum = 0;for(i = min;i <= max; i++){index = (i << 1) >> 3;value = (i << 1) % 8;if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1)))){sum++;}}if(!sum){puts("0");continue;}printf("%d\n",sum);for(i = min;i <= max; i++){index = (i << 1) >> 3;value = (i << 1) % 8;if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1)))){printf("%d",i);break;}}for(i++;i <= max;i++){index = (i << 1) >> 3;value = (i << 1) % 8;if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1))))printf(" %d",i);}puts("");} }與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
- 上一篇: POJ1753 棋盘翻转(位压缩+广度优
- 下一篇: 汽车加油行驶问题