2018CCPC Jilin C-JUSTICE (Thinking Simulation)
Title link:
Link: http://acm.hdu.edu.cn/showproblem.php?pid=6557
Question: Give you n numbers. Can you divide it into two groups so that the sum of one-half of the ki power of 2 in each group is greater than or equal to one-half. If you can, output the grouping scheme
Idea: It is not difficult to find, for example, that 2 k can be combined into a k/2,
can it be divided into two groups, only need to see if the number of 1 is greater than or equal to 2 after the final combination.
Using the principle of binary, priority queue + check set, first take out the smallest weight, merge, if the two values are the same, it can be merged into a k-1, and finally judge whether there are two or more 1 appear Can
Another: The default priority queue is that the data is large and the priority is high.
AC code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int prime = 999983;
const int INF = 0x7FFFFFFF;
const LL INFF =0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL mod = 1e9 + 7;
LL qpow(LL a,LL b)
{
LL s=1;
while(b>0)
{
if(b&1)s=s*a%mod;
a=a*a%mod;
b>>=1;
}
return s;
}
typedef pair<int,int> P;
const int maxn = 1e5+10;
int a[maxn];
int F[maxn];
int Find(int x)
{
return x == F[x]?x:F[x] = Find(F[x]);
}
int main()
{
int T;
cin>>T;
int Case =0;
while(T--)
{
int n;
cin>>n;
for(int i = 1; i <= n; ++i)
F[i] = i;
for(int i = 1; i <= n; ++i)
scanf("%d",&a[i]);
priority_queue<P>Q;
for(int i = 1; i <= n; ++i)
Q.push(P(a[i],i));
while(!Q.empty()&&Q.top().first > 1)
{
P p = Q.top();
Q.pop();
if(p.first != Q.top().first)
{
continue;
}
if(Q.empty())
break;
P p2 = Q.top();
Q.pop();
int x = Find(p.second);
int y = Find(p2.second);
if(x < y)
swap(x,y);
F[x] = y;
Q.push(P(p.first-1,y));
}
printf("Case %d: ",++Case);
if(Q.size() < 2)
puts("NO");
else
{
puts("YES");
for(int i = 1; i <= n; ++i)
{
int x = Find(i);
printf("%c",x == Q.top().second?'1':'0');
}
puts("");
}
}
return 0;
}