题目链接: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. equals与==区别

    equals与==区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间 ...

  2. 【BZOJ3667】Rabin-Miller算法(Pollard_rho)

    [BZOJ3667]Rabin-Miller算法(Pollard_rho) 题面 呜,权限题,别问我是怎么做的(我肯定没有权限号啊) 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...

  3. 【HDU5730】Shell Necklace(多项式运算,分治FFT)

    [HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...

  4. 第三周——构建一个简单的Linux系统MenuOS

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个 ...

  5. HDU.1495 非常可乐 (BFS)

    题意分析 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多 ...

  6. joomla! 3.X 开发系列教程

    http://www.mengyunzhi.com/members-resource/joomla/87-joomla-menu-study.html 学习地址 http://blog.csdn.ne ...

  7. 【计算机视觉】SIFT中LoG和DoG比较

    <SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 在实际计算时,三种方法计算的金字塔组数noctaves ...

  8. mysql 自动记录数据插入及最后修改时间

    总结: `uptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 原文 应用场景: 1.在数据 ...

  9. [DeeplearningAI笔记]序列模型1.5-1.6不同类型的循环神经网络/语言模型与序列生成

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.5不同类型的循环神经网络 上节中介绍的是 具有相同长度输入序列和输出序列的循环神经网络,但是对于很多应用\(T_{x}和 ...

  10. tomcat启动 报org.apache.catalina.LifecycleException异常

    java 服务器 tomcat启动 报org.apache.catalina.LifecycleException异常 异常代码如下: [2018-05-10 04:45:08,856] Artifa ...