POJ - 1065 Wooden Sticks(DP 子序列问题)
来源:互联网 发布:笔记本软件限制策略 编辑:程序博客网 时间:2024/05/29 18:05
Description
(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l <= l' and w <= w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
Input
Output
Sample Input
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1
Sample Output
213
这题可以用贪心做也可以用DP做
DP:最少上升子序列数等于最长下降子序列长度,,注意过程中保留最大值,,
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 50100;
int dp[N];
struct node
{
int x, y;
}p[N];
int cmp(node a,node b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
for(int i=0;i<n;i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
}
sort(p,p+n,cmp);
int ans=0;
for(int i=0;i<n;i++)
{
dp[i]=1;
int tmp=0;
for(int j=0;j<i;j++)
{
if(p[j].y>p[i].y)
{
tmp=max(tmp,dp[j]);
}
}
dp[i]+=tmp;
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
- POJ - 1065 Wooden Sticks(DP 子序列问题)
- POJ 1065 Wooden Sticks (dp)
- 【最长下降子序列】北大 POJ 1065 Wooden Sticks
- POJ 1065 Wooden Sticks 最长上升子序列
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
- zoj 1025 || poj 1065 Wooden Sticks(DP做法)
- poj 1065 Wooden Sticks dp LIS
- zoj 1025 Wooden Sticks (DP子序列)(第100,纪念一下)
- POJ 1065-Wooden Sticks 贪心问题
- POJ 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- POJ 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- POJ-1065-Wooden Sticks
- POJ 1065 Wooden Sticks
- iOS基础之网络编程问题集
- 第三周项目4-顺序表应用(1)
- Arraylist gossip
- Animator与Animation之间的互转
- 大数 A*B
- POJ - 1065 Wooden Sticks(DP 子序列问题)
- 定制ListView的几个步骤
- html5定位
- 【Boost】Boost使用几条简单笔记
- CentOS 7.2 Mitaka云平台计算节点部署脚本(VLAN+VXLAN私有网络)
- KMP算法
- 顺序栈&链栈
- CodeForce 711B
- 80行代码搞定菜单展开动画