题目链接: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的更多相关文章

  1. 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 ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. Telnet 远程控制

    Telnet 远程控制 一.挂第3张盘,进入RPMS中: 挂盘:mount /dev/cdrom /mnt/cdrom 路径:cd /mnt/cdrom/RedHat/RPMS 二.将rpm文件复制到 ...

  2. [CTSC2012]熟悉的文章 后缀自动机

    题面:洛谷 题解: 观察到L是可二分的,因此我们二分L,然后就只需要想办法判断这个L是否可行即可. 因为要尽量使L可行,因此我们需要求出对于给定L,这个串最多能匹配上多少字符. 如果我们可以对每个位置 ...

  3. 【JavaScript】函数表达式

    一.前言        接着上一篇的内容,继续学习JavaScript. 二.内容       函数的声明 function functionName(arg0,arg1,arg2){ //函数体 } ...

  4. java根据系统生成临时文件

    createTempFile 会根据当前系统,自动找系统的临时文件目录,在此目录下生成临时文件

  5. Overlaying GPS Coordinates for Camera Crosshairs

    Hey Guys! I am working on a project to allow us to implement GPS coordinates for the location of the ...

  6. SFM学习

    摘自李翠http://www.cnblogs.com/serser/p/6598621.html SFM 1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到 ...

  7. Netfilter之连接跟踪实现机制初步分析

    Netfilter之连接跟踪实现机制初步分析 原文: http://blog.chinaunix.net/uid-22227409-id-2656910.html 什么是连接跟踪 连接跟踪(CONNT ...

  8. Linux I/O缓冲

    1:两类I/O函数的缓冲机制 1.1 系统调用(System call) 这类代表就是read/write等系统函数,它们是不带缓冲的,这里的缓冲指的是进程缓冲,在内核到磁盘之间还是有内核缓冲的. 1 ...

  9. mac调教指南

    最近入手了一个mac,  在此记录下调教的过程. http://note.youdao.com/noteshare?id=5d1eedffab5cb47d4f53970c2ec937aa

  10. div 当高度较小时指定高度,当高度较大时自适应

    在该元素或标签的样式中加入:{min-height:500px;height:auto;},其中min-height:是最小高度,auto是自适应内容.