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 ...
随机推荐
- [二十三]SpringBoot 之 redis
本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...
- 《Linux Shell 脚本攻略》读书笔记
本书主要讲解一些linux shell命令的用法,讲解一些shell的奇技淫巧. 第一章 小试牛刀 主要介绍一些基本shell指令 终端打印:echo.printf 别名:alias 终端处理工具:t ...
- Ulipad和有道词典冲突解决方法
问题现象 Ulipad和目前版本的有道词典有冲突,表现为先开有道词典,Ulipad就无法运行. 解决方法 找到Ulipad安装目录下的config.ini,添加以下两行: [server] port= ...
- Spring Batch @SpringBatchTest 注解
Spring Batch 提供了一些非常有用的工具类(例如 JobLauncherTestUtils 和 JobRepositoryTestUtils)和测试执行监听器(StepScopeTestEx ...
- BZOJ3262:陌上花开 & 洛谷3810:三维偏序——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3262 https://www.luogu.org/problemnew/show/3810 Desc ...
- BZOJ 2959: 长跑 解题报告
2959: 长跑 Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能"为祖国健康工作五十年",同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑 ...
- Calculating and saving space in PostgreSQL
Q: I have a table in pg like so: CREATE TABLE t ( a BIGSERIAL NOT NULL, -- 8 b b SMALLINT, -- 2 b c ...
- Codeforces Round #305 (Div. 2) D 维护单调栈
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- python学习(十三)进程和线程
python多进程 from multiprocessing import Process import os def processFunc(name): print("child pro ...
- 11.UiAutomator 相关JAVA知识
一.封装方法与模块化用例 1.方法: 在JAVA中,方法就好比日常生活中的一个动作,由动作组合成一系列完整的操作. 方法结构: 方法修饰符 方法返回值类型 方法名 { 方法体 } 比如: public ...