poj -1065 Wooden Sticks (贪心or dp)
2024-08-26 12:04:00
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 ;
}
最新文章
- SMA、SMB、SMC封装的二极管
- 我心目中的Asp.net核心对象
- CSS3-基于浮动的布局,响应式WEB设计,定位网页上的元素,设计打印页面的css技术
- 最简单的一个Oracle定时任务
- iOS开发ARC内存管理
- c++ 模板元编程的一点体会
- 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (二)
- SVN与TortoiseSVN实战:属性的奇技淫巧(一)
- XSS的原理分析与解剖[转http://www.freebuf.com/articles/web/40520.html]
- InfluxDB安装
- android webview网页控件
- linux设备驱动----利用mdev(udev)自动创建设备文件节点
- VMware WorkStation安装时提示The MSI failed
- js 正则学习小记之匹配字符串
- jsp之用户自定义标签
- 【斐波那契数列】java探究
- 五、compose 部署 GitLab 应用
- Kibana中的Coordinate Map地图报索引错误的问题
- 去掉ambiguous expansion of macro警告
- load data妙用
热门文章
- EditPlus配置[C++] [Python] [Java] 编译运行环境
- 【BZOJ】【3238】【AHOI2013】diff(差异)
- thinkPHP生成静态分页列表
- Selenium获取input输入框中值的三种方法
- (转)Eclipse平台技术概述
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
- js-jQuery对象与dom对象相互转换
- Java中常用的加密方法(JDK)
- Windows服务创建及安装
- ExtJs布局之BOX