UVA10154->区间DP
2016-07-26 11:04
381 查看
题意:给出一些乌龟的重量和力量,求这些乌龟能叠出的龟塔的最大高度
题解:区间DP,dp[i][j]代表前i只乌龟叠j层的最小重量,从层数开始扩展转移,先求从1-n每个区间叠一层的质量,再求叠两层,以此类推
状态转移方程:if(turtle[i].strength >= dp[i-1][j-1])
dp[i][j] = min(dp[i-1][j] , dp[i-1][j-1]+turtle[i].weight) ;
else
dp[i][j] = dp[i-1][j] ;
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std ;
#define MAX 5700
#define INF 0x3f3f3f3f
int dp[MAX][MAX] ;
struct Node
{
int weight ;
int strength ;
}turtle[MAX];
bool comp(const struct Node &x ,const struct Node &y)
{
//if(x.strength == y.strength) return x.weight < y.weight ;
return x.strength < y.strength ;
}
int main()
{
int a , b , cnt = 1;
while(scanf("%d%d" , &a , &b) != EOF )
{
turtle[cnt].weight = a ;
turtle[cnt].strength = b - a ;
cnt ++ ;
}
//cout << cnt << endl ;
sort(turtle+1 , turtle + cnt+1 , comp) ;
memset(dp , INF , sizeof(dp)) ;
dp[0][0] = 0 ;
int ans = 0;
for(int j = 0 ; j <= cnt ; j ++)
{
for(int i = j + 1 ; i <=cnt ; i ++)
{
if(turtle[i].strength >= dp[i-1][j-1])
dp[i][j] = min(dp[i-1][j] , dp[i-1][j-1]+turtle[i].weight) ;
else
{
dp[i][j] = dp[i-1][j] ;
}
if(dp[i][j] != INF)
{
ans = max(ans , j) ;
//cout << ans << ' ' <<i <<' ' << j <<endl ;
}
}
}
printf("%d\n", ans);
return 0 ;
}
题解:区间DP,dp[i][j]代表前i只乌龟叠j层的最小重量,从层数开始扩展转移,先求从1-n每个区间叠一层的质量,再求叠两层,以此类推
状态转移方程:if(turtle[i].strength >= dp[i-1][j-1])
dp[i][j] = min(dp[i-1][j] , dp[i-1][j-1]+turtle[i].weight) ;
else
dp[i][j] = dp[i-1][j] ;
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std ;
#define MAX 5700
#define INF 0x3f3f3f3f
int dp[MAX][MAX] ;
struct Node
{
int weight ;
int strength ;
}turtle[MAX];
bool comp(const struct Node &x ,const struct Node &y)
{
//if(x.strength == y.strength) return x.weight < y.weight ;
return x.strength < y.strength ;
}
int main()
{
int a , b , cnt = 1;
while(scanf("%d%d" , &a , &b) != EOF )
{
turtle[cnt].weight = a ;
turtle[cnt].strength = b - a ;
cnt ++ ;
}
//cout << cnt << endl ;
sort(turtle+1 , turtle + cnt+1 , comp) ;
memset(dp , INF , sizeof(dp)) ;
dp[0][0] = 0 ;
int ans = 0;
for(int j = 0 ; j <= cnt ; j ++)
{
for(int i = j + 1 ; i <=cnt ; i ++)
{
if(turtle[i].strength >= dp[i-1][j-1])
dp[i][j] = min(dp[i-1][j] , dp[i-1][j-1]+turtle[i].weight) ;
else
{
dp[i][j] = dp[i-1][j] ;
}
if(dp[i][j] != INF)
{
ans = max(ans , j) ;
//cout << ans << ' ' <<i <<' ' << j <<endl ;
}
}
}
printf("%d\n", ans);
return 0 ;
}
相关文章推荐
- 63. Unique Paths II
- 如何掌握Facebook自动人脸识别技术?
- LeetCode 19. Remove Nth Node From End of List
- 进程和线程的区别是什么(转自知乎)
- C++类构造函数初始化列表
- centos7 sshd publickey 无密码登录 问题解决
- 写给自己
- 华为上机:Tom的生日礼物
- 【CreateJS】WebStorm+Adobe Animate CC 搭配开发HTML5,入门教程
- 关于在JSP中使用正则表达式限制输入框输入的条件
- Fragment的生命周期和Activity生命周期对比 代码片段
- Jmeter+maven+Jenkins构建云性能测试平台(一)
- 在O(1)时间删除链表节点
- pthread_key泄露问题检测
- 【教程】新手如何制作简单MAD和AMV,学不会那都是时辰
- WPF中实现类智能感知
- 转!!URL和URI区别
- LeetCode[334] Increasing Triplet Subsequence
- 书乐说:互联网“化石”谢幕,雅虎超低价卖身却绝非“弱鸡”
- .net core 全新的微软跨平台框架