poj -1065 Wooden Sticks (贪心or dp)
http://poj.org/problem?id=1065
题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度
都大于前一根木棍,那么这根木棍不需要生产时间,问你最少的生产时间是多少?
首先可以贪心,先按长度 l排序,如果l相同,按宽度w排序。
从i=0开始,每次把接下来的i+1 - n-1 的没有标记并且长度和宽度大于等于i这根木棍的长度和宽度标记起来。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; struct point
{
int l,w;
bool operator < (const point &a) const
{
return l==a.l ? w<a.w : l<a.l;
}
}p[maxn]; int mark[maxn]; int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&p[i].l,&p[i].w);
}
sort(p,p+n);
//for(int i=0;i<n;i++) printf("%d %d\n",p[i].l,p[i].w);
int s=;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
{
int temp=p[i].w;
if(!mark[i])
{
for(int j=i+;j<n;j++)
{
if(p[j].w>=temp&&!mark[j])
{
temp=p[j].w;
mark[j]=;
}
}
s++;
}
}
printf("%d\n",s);
}
return ;
}
dp:排序跟上面一样,然后就是找 w 的最长下降子序列。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; struct point
{
int l,w;
bool operator < (const point &a) const
{
return l==a.l ? w<a.w : l<a.l;
}
}p[maxn]; int dp[maxn]; int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&p[i].l,&p[i].w);
}
sort(p,p+n);
//for(int i=0;i<n;i++) printf("%d %d\n",p[i].l,p[i].w);
int ans=;
for(int i=;i<n;i++) dp[i]=;
for(int i=;i<n;i++)
{
for(int j=;j<i;j++)
if(p[j].w>p[i].w) dp[i]=max(dp[i],dp[j]+);
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}
poj -1065 Wooden Sticks (贪心or dp)的更多相关文章
- POJ 1065 Wooden Sticks#贪心+qsort用法
(- ̄▽ ̄)-* 这道题用到了cstdlib库的qsort()函数: 用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.h ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- POJ 1065 Wooden Sticks (贪心)
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...
- POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- HDU ACM 1051/ POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- poj 1065 Wooden Sticks 【贪心 新思维】
题目地址:http://poj.org/problem?id=1065 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 ...
- POJ 1065 Wooden Sticks Greed,DP
排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- POJ 1065 Wooden Sticks【贪心】
题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 ...
随机推荐
- JVM 崩溃 Failed to write core dump解决办法 WINDOWS
JVM 崩溃 Failed to write core dump解决办法 WINDOWS MIT key words: JVM,崩溃,windows,Failed,core dump,虚拟内存 最近从 ...
- 【BZOJ】【1036】树的统计
嗯这题是一道对树进行动态修改&查询的经典题目,可以拿来练习树链剖分~ 啊对于这种动态修改&查询的题目,我们最喜闻乐见的就是在一个序列上去做了,毕竟可以直接套各种数据结构模版啊,比如线段 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【bzoj1011】[HNOI2008]遥远的行星
1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 3711 Solved ...
- PHP操作数据库类
<?php /** * 功能: 数据库操作类 . * 作者: 赵铭哲 * 日期: 2016-05-23 * 时间: 9:43 */ namespace ZH\DataBase; use \Exc ...
- 监听HTML input输入框值的即时变化onpropertychange、oninput兼容IE,Chrome,FF,Opera等
转自:http://blog.csdn.net/itchiang/article/details/7769337 要达到的效果 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增 ...
- javascript设计模式--单例模式(Singleton)
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Unity上使用Linq To XML
using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; ...
- 【译】使用 Python 编写虚拟机解释器
[译]如何使用 Python 创建一个虚拟机解释器? 原文地址:Making a simple VM interpreter in Python 更新:根据大家的评论我对代码做了轻微的改动.感谢 ro ...
- poj 3903 Stock Exchange(最长上升子序列,模版题)
题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...