Justice(HDU6557+2018年吉林站+二进制)
题目链接
题意
给你\(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}}\),则:
&\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;
}
Justice(HDU6557+2018年吉林站+二进制)的更多相关文章
- 2018 CCPC 吉林站 H Lovers
2018 CCPC 吉林站 H Lovers 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: q次操作 1.将第l~r个数的左边和和右边都加上一个数d, ...
- Strength(HDU6563+2018年吉林站+双指针瞎搞)
题目链接 传送门 题意 你有\(n\)只怪,每只怪的伤害为\(a_i\),对手有\(m\)只怪,每只怪的伤害为\(b_i\),对手的怪有普通状态和防守状态(普通状态:如果你用攻击力为\(a_i(a_i ...
- Lovers(HDU6562+线段树+2018年吉林站)
题目链接 传送门 题意 初始时有\(n\)个空串,然后进行\(q\)次操作,操作分为以下两种: wrap l r x:把\(l,r\)中的每个字符串的首尾都加入\(x\),如\(s_i=121,x=3 ...
- The Tower(HDU6559+2018年吉林站+数学)
题目链接 传送门 题意 告诉你圆锥的底部圆的半径和圆锥的高,再给你一个点的坐标及其运动向量,问你这个点什么时候会与这个圆锥相撞. 思路 比赛场上二分一直没过但是有人二分过了,今天再写这题想再试下二分, ...
- 2018 CCPC 吉林站 H Lovers || HDU 6562 (线段树哦)
http://acm.hdu.edu.cn/showproblem.php?pid=6562 题意: q次操作 1.将第l~r个数的左边和和右边都加上一个数d, 使得这个数变成 dsiddsid的形式 ...
- 2018年9月22日CCPC吉林站参赛总结
发现思维题是硬伤,代码能力是硬伤,对知识点的理解不深刻是硬伤 接下来要做的就是 1.熟悉每一个知识点,把每一个知识点和实现它的代码联系在一起学习 2.多见题,看看他们是怎么考察这些知识点的,等比赛的时 ...
- 2018 CCPC 桂林站(upc复现赛)补题
2018 CCPC 桂林站(upc复现赛)补题 G.Greatest Common Divisor(思维) 求相邻数的差值的gcd,对gcd分解素因子,对所有的素因子做一次遍历,找出最小答案. 几个样 ...
- CCPC吉林站
A.The Fool #include <bits/stdc++.h> using namespace std; inline int read() { ,f=;char ch=getch ...
- 2018 CCPC 桂林站(upc复现赛)总结
比赛一开始盯上了A题和G题,一个小时过去了还没有出题,心里有些乱.这时我看D题很多人过了,于是宝儿去看D题,说D题简单,转化成二进制暴力,于是就去做了.写的时候好像思路有点卡,WA了一发,后来马上发现 ...
随机推荐
- react一些问题
一.死循环 1.问题描述 function handleClick() { this.setState({count: ++this.state.count}); console.log(" ...
- 使用pkg打包node.js项目(egg框架)为可执行包
问题: 公司有个工具型项目使用node.js 开发,需要部署到客户的服务器中,遇到的问题: 1.客户的服务器没有外网.环境配置,依赖安装等都比较麻烦,只能手工上传,最好能一个文件直接搞定: 2.直接包 ...
- 【idea】【sonarlint】指定文件夹扫描
选择文件夹右键->Analyze->Analyze with SonarLint
- 2019广东外语外贸大学CTF新手赛-密码学-RSA题解
题面 n=100000463700003241 e=17 密文: 分析: 题面已明示是RSA加密,已公开n与公钥e,n为1e18内的数字(64位).要爆破RSA,显然是先分析n的值. n的值是由两个素 ...
- java中的内存分配问题
class A{ int i; int j; } clsaa demo{ public static void main(String[] args){ A aa = new A(); A aa; / ...
- centos7查看防火墙状态、关闭防火墙
查看防火墙状态: firewall-cmd --state 关闭防火墙: systemctl stop firewalld.service 禁止firewall开机启动: systemctl disa ...
- SpringBoot+Vue前后端分离项目,maven package自动打包整合
起因:看过Dubbo管控台的都知道,人家是个前后端分离的项目,可是一条打包命令能让两个项目整合在一起,我早想这样玩玩了. 1. 建立个maven父项目 next 这个作为父工程,next Finish ...
- python_封装redis_hash方法
xshell 进入 虚拟环境 安装 redis workon py3env # 进入虚拟环境 pip install redis # 安装redis deactivate # 退出虚拟环境 简单的封装 ...
- Spring-Cloud之Feign声明式调用-4
一.Feign受Retrofit.JAXRS-2.0和WebSocket影响,采用了声明式API 接口的风格,将Java Http 客户端绑定到它的内部. Feign 首要目的是将 Java Http ...
- C# 运行流程
转载 https://www.cnblogs.com/qcloud1001/p/9816956.html 从编译原理说起 一句话介绍编译器:编译器是将用某种程式语言写成的源代码(源语言),转换成另一 ...