Gym101158G Placing Medals on a Binary Tree(二進制模擬)
阿新 • • 發佈:2018-09-13
EDA eps sin als == const 路徑 map n)
Gym101158G-Placing Medals on a Binary Tree
題意
一顆完全二叉樹,給出n個點,xi的值表示深度為xi的點。問能否在當前狀態下使得從根節點到該點的路徑中不會遇到其他點。
思路
其實本題的意思就是 1/2,1/4,1/8等等等等,看剩下的點能不能減。然而由於k值並不小,所以long double也無法處理。其實完全二叉樹給了啟發,也就是如果當前深度的點占據了,相當於之後的子樹都不能進行選擇了。所以運算方式上類似二進制。相當於利用map對二進制進行模擬。
代碼
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; int T; map<int,int>mp; int n; void init(){ mp.clear(); } int main(){ scanf("%d",&n); int fa = 0,flag = 0,mmax = 0; init(); for(int i=0;i<n;i++){ int x; scanf("%d",&x); if(flag==1) printf("No\n"); else if(x>fa){ printf("Yes\n"); mp[x]++; int cur=x; while(mp[cur]>1&&cur>fa){ mp[cur]=0; cur--; mp[cur]++; } while(mp[fa+1]>=1) fa++; }else if(x==fa){ int f = 0; for(int i=x;i>=0;i--){ if(mp[i]<2) f = 1; } if(mmax>fa&&f) printf("No\n"); else{ printf("Yes\n"); flag=1; } }else printf("No\n"); mmax = max(mmax,x); } return 0; }
Gym101158G Placing Medals on a Binary Tree(二進制模擬)