您的位置:首页 > 其它

2018CCPC吉林C-JUSTICE(思维模拟)

2019-07-25 20:38 204 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_40534166/article/details/97297630

2018CCPC吉林C-JUSTICE(思维模拟)

题目链接:
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6557

题意:给你n个数,能否分成两组,使每组的2的ki次方分之一的和大于等于二分之一,能的话输出分组方案

思路:不难发现, 例, 也就是2个k能合成一个k/2,
能不能分成两组,只需要看最后合完之后1的个数是不是大于等于2。
利用二进制的原理,优先队列+并查集,先取出权值最小的,进行合并,如果两个值相同,则可以合并成一个k-1,最后判断是否有两个或两个以上的1出现即可

另:优先队列默认的是数据大的优先级高.

AC代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: