1005C Summarize to the Power of Two (貪心 + 煞筆的我寫了一發離散化)
阿新 • • 發佈:2018-12-12
Summarize to the Power of Two
題意:給定一個數組,問陣列中每一個元素 a[i] , 是否存在另一個元素 a[j], 使 a[i] + a[j] 是 2 的次冪, i != j
思路:貪心,先打出足夠的2的次冪,然後對於每一個數判斷 power - a[j] 是否存在就可以了。 由於數很大,用map就可以了。本ZZ煞筆的寫了一發離散化,還好過了。。。
AC程式碼:
#include<bits/stdc++.h> #define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl #define pii pair<int,int> #define clr(a,b) memset((a),b,sizeof(a)) #define rep(i,a,b) for(int i = a;i < b;i ++) #define pb push_back #define MP make_pair #define LL long long #define INT(t) int t; scanf("%d",&t) #define LLI(t) LL t; scanf("%I64d",&t) using namespace std; const int maxn = 120010; int vis[maxn]; LL power2[32]; int a[maxn]; int b[maxn]; int main() { power2[0] = 1; rep(i,1,32) power2[i] = power2[i - 1] * 2; int n; while(~scanf("%d",&n)){ memset(vis,0,sizeof(vis)); rep(i,0,n){ scanf("%d",&a[i]); b[i] = a[i]; } sort(b,b + n); sort(a,a + n); int f = unique(b,b + n) - b; rep(i,0,n){ int x = lower_bound(b,b + f,a[i]) - b; //debug(x); ++ vis[x]; } int ans = 0; rep(i,0,n){ int x = lower_bound(b,b + f,a[i]) - b; int y; int flag = 0; rep(j,0,32){ y = lower_bound(b,b + f,power2[j] - a[i]) - b; if(b[x] + b[y] == power2[j]){ if(x != y || (x == y && vis[x] > 1)){ flag = 1; break; } } } if(!flag){ ++ ans; } } printf("%d\n",ans); } return 0; }