hdu Save the dwarfs 4314
来源:互联网 发布:数组未赋值,默认为什么 编辑:程序博客网 时间:2024/06/12 08:11
转自:http://blog.csdn.net/longshuai0821/article/details/7794699
题意: 在井中有n个矮子,每个矮子都有ai(脚到肩膀的高度)和bi(手臂长度), 当存在A1 + A2 + ... + Ak-1 + Ak + Bk >= H,k可以从井里面逃出去。问最多能逃出去几个人。
解题思路:按ai+bi和的值从小到大排序后dp,dp[i][j]表示前i个矮子跑出去j个时,需要之前井中剩下的人的最小A高度之和。有如下转移方程dp[i][j] = min(dp[i-1][j] - s[i-1].a, max(dp[i-1][j-1], H - sumA[i-1] - s[i-1].b ))。最后找到最大的j满足dp[n][j]<=0即为所求。
粗略证明:当逃出去的组合(逃出去的人编号的组合)是一个的时候,如果某个组合的人可以都逃出去,先跑ai+bi小的肯定可以跑出去。
#include<iostream>#include<stdio.h>#include<algorithm>const int N=3000;const int inf=1e9;using namespace std;struct node{ int a,b; void read(){ scanf("%d%d",&a,&b); } bool operator <(const node tmp)const { return a+b<tmp.a+tmp.b; }}no[N];int opt[N][N],n,h;int solve(){ int sum=0; for(int i=1;i<=n;i++) sum+=no[i].a; for(int j=1;j<=n;j++) opt[0][j]=inf; // opt[i][j]表示前i个人要出去j个的最小要减去的的高度值即A opt[0][0]=0; for(int i=1;i<=n;i++) { opt[i][0]=0; for(int j=1;j<=n;j++){ opt[i][j]=opt[i-1][j]; // 如果第i个不出去的话 if(sum-opt[i-1][j-1]+no[i].b>=h) //如果能够出去的话 opt[i][j]=min(opt[i][j],opt[i-1][j-1]+no[i].a);// cout<<i<<' '<<j<<' '<<opt[i][j]<<' '<<opt[i-1][j]<<endl; } } for(int j=n;j>=0;j--) if(opt[n][j]!=inf) return j;}int main(){ while(scanf("%d",&n)!=-1) { for(int i=1;i<=n;i++) no[i].read(); scanf("%d",&h); sort(no+1,no+n+1); cout<<solve()<<endl; } return 0;}
- hdu Save the dwarfs 4314
- hdu 4314 Save the dwarfs
- HDU-4314-Save the dwarfs
- hdu - 4314 - Save the dwarfs - dp
- hdu 4314 Save the dwarfs(dp,4级,多校)
- HDU 4315 Save the dwarfs 动态规划
- Save the dwarfs
- HDU4314-Save the dwarfs
- MUTC 2 E - Save the dwarfs DP?
- hdu4314Save the dwarfs【dp】
- hdu4314Save the dwarfs 【dp】
- Snow White and the Seven Dwarfs
- URAL 1243. Divorce of the Seven Dwarfs
- URAL 1243 Divorce of the Seven Dwarfs
- Hdu 2284 Solve the puzzle, Save the world! (组合数)
- POJ 1912 A highway and the seven dwarfs 已翻译
- poj1912:A highway and the seven dwarfs(凸包)
- poj1912 A highway and the seven dwarfs【凸包+二分】
- Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】
- 报错expected specifier-qualifier-list before ‘uint32_t‘
- Maven与Ant比较
- 【原创】详谈内核三步走Inline Hook实现
- 批量bulk insert 原理不明白之处 求解答
- hdu Save the dwarfs 4314
- API创建对话框
- apache 配置文件 httpd-vhosts.conf 添加一个站点NameVirtualHost *:80
- 黑马程序员--HEML基础
- VC++快捷键
- javascript 中的preventDefault和stopPropagation
- BFS——NYOJ 21 三个水杯
- spring xmlns 配置
- 堆和栈