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 ...
随机推荐
- “strcmp()” Anyone?
“strcmp()” Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two ...
- HDU<1372>/bfs
题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...
- apache php 配置 CI 框架
声明:配置域名需要用到 httpd.conf httpd_vhosts.conf (apache) 中两个文件 和 hosts (C:\Windows\System32\drivers\et ...
- UICollectionView 浅析
什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是UICollectionView的最最 ...
- Android中在activity中弹出一个popwindow
//-----在onCreate方法--中------创建popwindow布局 --pop_item-------------------------- View view=Lay ...
- 两个byte[]拼接
//两个byte[]拼接 public byte[] copybyte(byte[] a, byte[] b, byte[] c, byte[] d, byte[] e)///,byte[] f,by ...
- gulp相关知识(2)
将之前的东西的理论部分整理了一下—— gulp 前端构建工具 它可以帮助我们完成一些自动化操作 它需要一些插件的支持 var gulp = require('gulp'); --> gulp命令 ...
- 使用Qt报错error while building deploying project
方法一:点击左侧的“项目”栏,看“构建目录”栏的路径,一定要注意,在路径中一定不要出现汉字,否则一定会报“error while building deploying project”的错误. 方法二 ...
- 5--OC--构造方法
// Created by Stephen on 16/3/2.// Copyright © 2016年 Stephen. All rights reserved.//// 回顾上一章节Perso ...
- Ubuntu + Django + Nginx + uwsgi
环境 Ubuntu 14.04 Python 2.7 Django 1.8.4 1 安装Nginx sudo apt-get install nginx 测试 sudo /etc/init. ...