题目链接

传送门

题意

给你\(n\)个数,每个数表示\(\frac{1}{2^{a_i}}\),要你把这\(n\)个数分为两堆,使得每堆的和都大于等于\(\frac{1}{2}\)。

思路

首先我们假设第一堆的下标为\(x_1,x_2\dots,x_n\),且\(2^{a_{x_1}}\leq 2^{a_{x_2}}\leq\dots\leq 2^{a_{x_n}}\),则:

\[\begin{aligned}
&\sum\limits_{i=1}^{n}\frac{1}{2^{a_{x_i}}}&\\
=&\sum\limits_{i=1}^{n}\frac{2^{a_{x_n}-a_{x_i}}}{2^{a_{x_n}}}\text{(通分)}&
\end{aligned}
\]

我们知道\(2\)个\(2^x\)构成一个\(2^{x+1}\),因此我们确定每一堆分母的最大值后按照\(2^{a_{x_i}}\)从小到大来凑\(2^{a_{x_n}-1}\)。

注意,一旦相邻两个数的大小差值大于等于\(17\)就直接不用取输出\(NO\),因为\(2^{17}\geq 10^5\),后面不管怎么凑都是不可能凑出我们所需要的数的。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t, n;
int vis[maxn]; struct node {
int val, id;
bool operator < (const node& x) const {
return val < x.val;
}
}a[maxn]; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
scanf("%d", &t);
int icase = 0;
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i].val);
a[i].id = i;
vis[i] = 0;
}
printf("Case %d: ", ++icase);
sort(a + 1, a + n + 1);
LL nw;
if(a[1].val == 1) {
vis[a[1].id] = 1;
} else {
nw = a[1].val - 1;
if(nw >= 17) {
printf("NO\n");
continue;
}
nw = (1<<nw) - 1;
vis[a[1].id] = 1;
for(int i = 2; i <= n; ++i) {
if(nw == 0) break;
if(a[i].val == a[i-1].val) --nw, vis[a[i].id] = 1;
else {
if(a[i].val - a[i-1].val >= 17) {
break;
}
nw = nw * (1<<(a[i].val - a[i-1].val)) - 1;
if(nw > n - i) break;
vis[a[i].id] = 1;
}
}
if(nw != 0) {
printf("NO\n");
continue;
}
}
int idx = -1;
for(int i = 1; i <= n; ++i) {
if(!vis[a[i].id]) {
idx = i;
break;
}
}
if(idx == -1) {
printf("NO\n");
continue;
}
if(a[idx].val == 1) {
printf("YES\n");
for(int i = 1; i <= n; ++i) {
printf("%d", vis[i]);
}
printf("\n");
} else {
nw = a[idx].val - 1;
if(nw >= 17) {
printf("NO\n");
continue;
}
nw = (1<<nw) - 1;
for(int i = idx + 1; i <= n; ++i) {
if(nw == 0) break;
if(vis[a[i].id]) continue;
if(a[i].val == a[i-1].val) --nw;
else {
if(a[i].val - a[i-1].val >= 17) {
break;
}
nw = nw * (1<<(a[i].val - a[i-1].val)) - 1;
if(nw > n - i) break;
}
}
if(nw != 0) {
printf("NO\n");
continue;
}
printf("YES\n");
for(int i = 1; i <= n; ++i) {
printf("%d", vis[i]);
}
printf("\n");
}
}
return 0;
}

最新文章

  1. 【推荐】MySQL Cluster报错及解决方法(不断更新中)
  2. [原创] ubuntu下安装scrapy报错 error: command &#39;x86_64-linux-gnu-gcc&#39; failed with exit status 1
  3. HDU4686 Arc of Dream 矩阵快速幂
  4. Android开发UI之在子线程中更新UI
  5. OC1_银行账户类
  6. 改变VC生成exe图标
  7. hadoop的thriftserver配置
  8. form不提交问题
  9. java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layou
  10. 编写第一个ROS(创建工作空间workspace和功能包package)
  11. 适合前端学习JS的网站
  12. Spring Security(九):2.4.4 Checking out the Source(检查来源)
  13. Django基础(一)
  14. 【第十七章】 springboot + devtools(热部署)
  15. CentOS7 下源代码安装apache2.4
  16. Linux设备驱动剖析之SPI(四)
  17. 关于hp proliant sl210t服务器远程iLO接口的管理配置
  18. 网站微信登录-python 实现
  19. 创建maven web项目无法创建sec目录
  20. Tornado之抽屉实战(3)--注册

热门文章

  1. mysql 更新与查询(排序 分组 链接查询)
  2. 【bat】【windows】win10查看所有wifi密码
  3. CentOS 5 源
  4. Eclipse项目上传和下载到码云上
  5. SQL Server 使用文件组备份降低备份文件占用的存储空间
  6. Python学习之路:列表(List)的append()、extend()与insert()方法
  7. 一个基于tcp的socket简单对话小例子
  8. python--osi七层模型
  9. Python输错4次用户名密码需要输入验证码
  10. elasticsearch安全重启节点