POJ 1065 Wooden Sticks【贪心】
题意:
有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列。问至少要分为多少个序列。且要保证排出来的子序列数最少。
思路:
( 9 , 4 ) ,( 2 , 5 ) ,( 1 , 2 ) ,( 5 , 3 ),( 4 , 1 )可以排成这样
( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ); ( 1 , 2 ) , ( 2 , 5 ) .
其中:(4,1)<=(5,3)<=(9,4)为不降序列,(4,1)<(5,3)由于4<5&&1<3
(1,2)<(2,5)为不降序列。即最少的不降子序列为2,输出2
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 5001;
struct wooden{
int l, w, flag;
}wd[MAX];
bool cmp(wooden x, wooden y){
if (x.l != y.l) return x.l < y.l;
return x.w < y.w;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++){
cin >> wd[i].l >> wd[i].w;
wd[i].flag = 0;
}
sort(wd, wd + n, cmp);
int res = 0;
for (int i = 0; i < n; i++){
if (wd[i].flag)continue;
res++;
int cur = wd[i].w;
for (int j = i + 1; j < n; j++){
if (!wd[j].flag && wd[j].w >= cur){
wd[j].flag = 1;
cur = wd[j].w;
}
}
}
cout << res << endl;
}
return 0;
}
POJ 1065 Wooden Sticks【贪心】的更多相关文章
- 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 ...
- 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
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- poj -1065 Wooden Sticks (贪心or dp)
http://poj.org/problem?id=1065 题意比较简单,有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(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
随机推荐
- Pool多进程示例
利用Pool类多进程实现批量主机管理 #!/usr/bin/python # -*- coding: UTF-8 -*- # Author: standby # Time: 2017-03-02 # ...
- 怎样动态地插入不会暴露给用户的JS文件
也是无意间看见的,以前想过这个问题,但是没多想,今天看到这段代码豁然开朗 (function() { var dynamicScript = document.createElement('scrip ...
- 动态规划之Fib数列类问题应用
一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...
- 阿里云服务器安装SQLServer本地无法远程访问
新买的阿里云服务器,安装上sqlserver2012,本机连接测试没有问题,但是回到本地,使用ip远程连接报错. 尝试了网上各种办法,都是失败.最后找到原因,原来在阿里云的控制台上有设置: 首先进入安 ...
- static, const
static 静态的,类的静态成员函数,静态成员变量是和类相关的,但不和具体对象相关.即使没有具体对象,也能调用类的静态成员函数和成员变量.一般类的静态函数就是一个全局函数,只是作用域在包含它的文件中 ...
- C# unicode 转中文
//Unicode 转中文 private void button1_Click(object sender, EventArgs e) { string unicode = @"\U5fa ...
- 属性动画QPropertyAnimation
属性动画QPropertyAnimation 改变大小.颜色或位置是动画中的常见操作,而QPropertyAnimation类可以修改控件的属性值 大小改变动画: import sys from Py ...
- pt-table-checksum检测不出主从差异处理
几个月前写过pt-table-checksum 3.0.4检测不出主从差异数据,当时的解决方案是使用旧版本,另一个挫方法是自行设置binlog_format='STATEMENT'.现在已经发布到3. ...
- sqlite limit offset
limit 0,20 表示从第1条开始取20条数据 limit 20 offset 2 表示从第2条开始取出20条数据
- web前端最全各类资源
链接:http://www.sohu.com/a/157593700_132276