Gym 101081K Pope's work
题目链接:Gym - 101081K
题意:给n个箱子,每个箱子有一个重量W和一个承重R,表示它上面能放最多R-W的重量。问最多能把多少箱子堆到一堆。
思路:发现在一堆箱子里,两个箱子交换位置,对其他所有箱子没有影响。
所以我们先构造偏序关系,考虑两个箱子i和j,假设Ri<Rj。
那么我们发现假如Ri >= Wi + Wj,则Rj >= Wi + Wj。换言之,假如i上面可以放j,则j上面一定可以放j。但反之不一定成立。
所以可以认为i<=j,按照这种偏序关系排序。
按这种关系排序的好处在于,对于某个排在i后面的箱子j,假如我们想把j放在i上面,则这种情况显然不如把i放在j上面的情况好。
换言之,对于每个箱子,我们只需要考虑排在它前面的箱子即可。
接下来,用d[i][j]表示用前i个箱子堆j个箱子的最小重量。d[i][j]为INF时表示用前i个箱子不能堆j个箱子。
那么可以得到转移方程
。
最终答案为使d[n][i]不为INF的最大的i。
代码如下:
#include"cstdio"
#include"iostream"
#include"cstring"
#include"algorithm"
#include"cstdlib"
#include"vector"
#include"set"
#include"map"
#include"cmath"
using namespace std;
typedef long long LL;
const LL MAXN=;
const LL MOD=+;
const LL INF=0x3f3f3f3f; struct Box
{
int w,r;
bool operator < (const Box x)
{
return r<x.r;
}
};
Box s[MAXN];
int d[MAXN][MAXN];
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int t;
scanf("%d",&t);
for(int tt=;tt<=t;tt++)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&s[i].w,&s[i].r);
sort(s+,s++n);
memset(d,INF,sizeof(d));
for(int i=;i<=n;i++)
d[i][]=;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
{
d[i][j]=d[i-][j];
if(d[i-][j-]+s[i].w <= s[i].r && d[i-][j-]+s[i].w < d[i][j])
d[i][j]=d[i-][j-]+s[i].w;
}
for(int i=n;i>=;i--)
if(d[n][i]<INF)
{
printf("%d\n",i);
break;
}
}
return ;
}
Gym 101081K Pope's work的更多相关文章
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- (转)rabbitmq的web管理界面无法使用guest用户登录
转至http://www.cnblogs.com/mingaixin/p/4134920.html 安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的 ...
- MySQL慢查询日志ES索引模板
{ "template": "mysql-slow-log-*", "settings": { "index": { & ...
- Fdisk 分区详解
Fdisk 分区详解 来源 http://blog.itpub.net/20674423/viewspace-722812/ 1. 通过Fdisk查看系统分区详细信息: Fdi ...
- IBatis Map报错10.1
检查 providers.config 把没用的给关闭掉即可
- Docker学习笔记二:Docker常用命令及提升拉取镜像的速度
一.Docker命令: 1.docker images //命令用来查看docker中所包含的镜像信息 2.docker ps -a //命令用来查看docker中所包含所有容器信息(运行状 ...
- Unity3D for VR 学习(9): Unity Shader 光照模型 (illumination model)
关于光照模型 所谓模型,一般是由学术算法发起, 经过大量实际数据验证而成的可靠公式 现在还记得2009年做TD-SCDMA移动通信算法的时候,曾经看过自由空间传播模型(Free space propa ...
- cmakelist 定义字符串,替换到脚本中。
cmake_minimum_required(VERSION 2.6 FATAL_ERROR) cmake_policy(VERSION 2.6) # . Project Name project(s ...
- C/C++ Volatile关键词深度剖析
文章来源:http://hedengcheng.com/?p=725 背景 此微博,引发了朋友们的大量讨论:赞同者有之:批评者有之:当然,更多的朋友,是希望我能更详细的解读C/C++ Volatile ...
- PlantUML类图
PlantUML类图 雨客 2016-04-08 11:38:03 浏览796 评论0 摘要: 类之间的关系 PlantUML用下面的符号来表示类之间的关系: 泛化,Generalization: ...
- [DeeplearningAI笔记]卷积神经网络2.5-2.7 Network in Network/1*1卷积/Inception网络/GoogleNet
4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Inception网络 --Szegedy C, Liu W, Jia Y, et al. Going deepe ...