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= ...
随机推荐
- 缓存穿透、雪崩、热点与Redis
(拼多多问:Redis雪崩解决办法) 导读:互联网系统中不可避免要大量用到缓存,在缓存的使用过程中,架构师需要注意哪些问题?本文以 Redis 为例,详细探讨了最关键的 3 个问题. 一.缓存穿透预防 ...
- 绕过CDN查看网站真实IP的一些办法
验证是否存在CDN最简单的办法 通过在线的多地ping,通过每个地区ping的结果得到IP. 看这些IP是否一致,如果都是一样的,极大可能不存在cdn,但不绝对. 如果这些IP大多数都不太一样或者规律 ...
- xss漏洞利用
简述 跨站脚本攻击(也称为XSS)指利用网站漏洞从用户那里恶意盗取信息.攻击者通过在链接中插入恶意代码,就能够盗取用户信息.攻击者通常会在有漏洞的程序中插入 JavaScript.VBScript. ...
- luogu 4158 粉刷匠 dp套dp
dp套dp 每个木板是个递推的dp,外部是个分组背包 #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i&l ...
- POJ3233 Matrix Power Series(快速幂求等比矩阵和)
题面 \(solution:\) 首先,如果题目只要我们求\(A^K\) 那这一题我们可以直接模版矩乘快速幂来做,但是它现在让我们求$\sum_{i=1}^{k}{(A^i)} $ 所以我们思考一下这 ...
- C++获取当前所有进程的完整路径
实现代码 #include <stdio.h> #include <windows.h> #include <tlhelp32.h> #include <st ...
- ansible报错Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this
安装和配置好ansible,执行命令时报错如下 [root@test01 ansible-install]# ansible test -m shell -a 'w' >> Using a ...
- route 的标志位
linux下利用route命令查看当前路由信息时,会打印如下信息: root@root:/# route Kernel IP routing tableDestination Gateway ...
- zabbix通过简单shell命令监控elasticsearch集群状态
简单命令监控elasticsearch集群状态 原理: 使用curl命令模拟访问任意一个es节点可以反馈的集群状态,集群的状态需要为green curl -sXGET http://serverip: ...
- RunLoop的应用场景---关于滑动tableView的时候NSTimer 暂停的问题
1.我们经常会在应用中看到tableView 的header 上是一个横向ScrollView,一般我们使用NSTimer,每隔几秒切换一张图片.可是当我们滑动tableView的时候,顶部的scol ...