hdu 6557 Justice ###K //K
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6557
题意:给定n个物品 每个物品钟1/2^k 问能否凑出2份 重量至少都大于1/2 的 并输出 01串表示放在那一组
思路:直接暴力dfs进位是最简便的, 因为要进位很多的话 事先也需要放
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =1e5+10; 6 const int mod=1e9+7; 7 8 map<int,int>mp; 9 struct ac 10 { 11 int p,v; 12 bool operator<(ac a) 13 { 14 return v<a.v; 15 } 16 }; 17 int ans[maxn]; 18 ac a[maxn]; 19 20 void dfs(int u) 21 { 22 if(u==0) 23 return; 24 mp[u]-=2; 25 mp[u-1]++; 26 if(mp[u-1]==2) 27 dfs(u-1); 28 } 29 30 int main() 31 { 32 ios::sync_with_stdio(false); 33 cin.tie(0); 34 int t; 35 cin>>t; 36 int cnt=0; 37 while(t--) 38 { 39 int n; 40 cin>>n; 41 cout<<"Case "<<++cnt<<": "; 42 mp.clear(); 43 for(int i=1;i<=n;i++) 44 { 45 cin>>a[i].v; 46 a[i].p=i; 47 ans[i]=0; 48 } 49 sort(a+1,a+1+n); 50 for(int i=1;i<=n;i++) 51 { 52 int v=a[i].v; 53 int p=a[i].p; 54 if(mp[1]||mp[0]) 55 ans[p]=1; 56 mp[v]++; 57 if(mp[v]==2) 58 dfs(v); 59 60 61 } 62 if(mp[0]) 63 { 64 cout<<"YES"<<'\n'; 65 for(int i=1;i<=n;i++) 66 cout<<ans[i]; 67 cout<<'\n'; 68 } 69 else 70 { 71 cout<<"NO"<<'\n'; 72 } 73 74 } 75 76 77 78 79 80 81 82 83 84 }
很多数在每个位置上 所以dfs时间复杂度不会大

浙公网安备 33010602011771号