ZOJ 3931 Exact Compression
题目看了半小时才看懂的。
题意:首先根据给出的序列,构造出哈夫曼树,构造出来的是一棵二叉树,每个节点都有一个权值,每个节点的两个儿子只能取一个,问能否使取出来的节点权值之和刚好等于e。
这样一分析就很容易看出是01背包。但是,背包容量特别大!不能纯粹的用循环做背包肯定会超时。可以使用两个队列存能够凑出来的数字(相当于滚动数组的作用),还有一个优化,看当前要压入队列的数字是否已经在队列中,这个可以用map存一下。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std; const int maxn=;
int s;
long long e;
long long f[maxn];
struct X
{
long long a,b;
} u[maxn];
int Size,cnt;
map<long long,bool>flag; struct cmp
{
bool operator ()(long long &a,long long &b)
{
return a>b;
}
}; int main()
{
int T; scanf("%d",&T);
while(T--)
{
flag.clear();
scanf("%d",&s);
cnt=;
priority_queue<long long,vector<long long>,cmp>q;
for(int i=; i<=s; i++)
{
scanf("%lld",&f[i]);
q.push(f[i]);
}
scanf("%lld",&e);
Size=s;
while(Size!=)
{
u[cnt].a=q.top();
q.pop();
Size--;
u[cnt].b=q.top();
q.pop();
Size--;
q.push(u[cnt].a+u[cnt].b);
cnt++;
Size++;
} queue<long long>Q1;
queue<long long>Q2;
int f=;
Q1.push(u[].a);
Q1.push(u[].b); for(int i=; i<cnt; i++)
{
flag.clear();
if(f==)
{
while(!Q1.empty())
{
if(Q1.front()+u[i].a<=e&&flag[Q1.front()+u[i].a]==){
Q2.push(Q1.front()+u[i].a);
flag[Q1.front()+u[i].a]=;
}
if(Q1.front()+u[i].b<=e&&flag[Q1.front()+u[i].b]==){
Q2.push(Q1.front()+u[i].b);
flag[Q1.front()+u[i].b]=;
}
Q1.pop();
}
f=;
}
else
{
while(!Q2.empty())
{
if(Q2.front()+u[i].a<=e&&flag[Q2.front()+u[i].a]==){
Q1.push(Q2.front()+u[i].a);
flag[Q2.front()+u[i].a]=;
}
if(Q2.front()+u[i].b<=e&&flag[Q2.front()+u[i].b]==){
flag[Q2.front()+u[i].b]=;
Q1.push(Q2.front()+u[i].b);
}
Q2.pop();
}
f=;
}
} bool ans=; if(f==)
{
while(!Q1.empty())
{
if(Q1.front()==e) ans=;
Q1.pop();
}
}
else
{
while(!Q2.empty())
{
if(Q2.front()==e) ans=;
Q2.pop();
}
} if(ans) printf("Yes\n");
else printf("No\n");
}
return ;
}
ZOJ 3931 Exact Compression的更多相关文章
- 01背包 ZOJ 3931 Exact Compression
题目连接 题意:n个数字构建哈夫曼树,问是否存在这样一棵树使得:(Fi数字大小,Ci哈夫曼表示下,'0'的数量) 分析:每次从优先队列取出两个数字可以互换位置,这样可以01互换.设a[i] <= ...
- Types of compression algorithms
http://www.html5rocks.com/en/tutorials/speed/img-compression/ Types of compression algorithms There ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ZOJ 2477 Magic Cube(魔方)
ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds Memory Limit: 65536 KB This is a very popular gam ...
- zoj 2081 BFS 延迟标记 读入问题
Mission Impossible Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- ZOJ 3349 Special Subsequence
Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Ori ...
- Dancing Links and Exact Cover
1. Exact Cover Problem DLX是用来解决精确覆盖问题行之有效的算法. 在讲解DLX之前,我们先了解一下什么是精确覆盖问题(Exact Cover Problem)? 1.1 Po ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- BZOJ 3931: [CQOI2015]网络吞吐量
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1555 Solved: 637[Submit][Stat ...
随机推荐
- C++ concepts: Compare
The concept Compare is a set of requirements expected by some of the standard library facilities fro ...
- ZendStudio格式化HTML代码方法及格式化后错位问题修正
ZendStudio中格式化HTML快捷键:Ctrl+Shift+F 为什么html文件里面的内容格式化完都乱套了? 选 择window菜单->Preferences->Web->H ...
- 锤子便签的 monkeyrunner 测试脚本(转)
https://testerhome.com/topics/878 MonkeyRunner可能大家已经听过无数次了,大家在网上也看过了各种关于的它的资料了,我这里就不再过多的啰嗦它的用途了,它可以对 ...
- USER-AGENT是什么
USER-AGENT是什么? USER-AGENT:记录请求所来自的浏览器. User-Agent分析网站 http://www.useragentstring.com/ 通过解析User-Agent ...
- 使用maven开发过程中,pom报的一些错的解决方法
1. maven-resources-plugin prior to 2.4 is not supported by m2e. Use maven- resources-plugin versio ...
- 转:移植SlidingMenu Android library,和安装example出现的问题解决
很多项目都用到类似左侧滑动菜单的效果,比如facebook,evernote,VLC for android等等,这很酷 源代码可以从GitHub的https://github.com/jfeinst ...
- C++ Builder string相互转换(转)
源:http://www.cnblogs.com/zhcncn/archive/2013/05/20/3089084.html 1. char*->string (1)直接转换 const ch ...
- 一个基于jQuery的简单树形菜单
在工作中的项目使用的是一个前端基于 jQuery easyui 的一个系统,其中左侧的主菜单使用的是 easyui 中的 tree 组件,不是太熟悉,不过感觉不是太好用. 比如 easyui 中的 t ...
- POJ 1511 Invitation Cards 正反SPFA
题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少. 思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点 ...
- 转:web_submit_data函数
web_submit_data函数处理无状态或者上下文无关的表单提交.它用来生成表单的GET或POST请求,这些请求与Form自动生成的请求是一样的.发送这些请求时不需要表单上下文. 函数语法:Int ...