先将火柴按照长度(或重量)优先排序,在不断遍历数组,找出其中重量(长度)递增子序列,并标记


Problem Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(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 (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup
time should be 2 minutes since there is a sequence of pairs (1,4),
(3,5), (4,9), (2,1), (5,2).

 
Input
The
input consists of T test cases. The number of test cases (T) is given
in the first line of the input file. Each test case consists of two
lines: The first line has an integer n , 1<=n<=5000, that
represents the number of wooden sticks in the test case, and the second
line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of
magnitude at most 10000 , where li and wi are the length and weight of
the i th wooden stick, respectively. The 2n integers are delimited by
one or more spaces.
 
Output
The output should contain the minimum setup time in minutes, one per line.
 
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
2
1
3
#include<iostream>
#include<algorithm>
using namespace std;
struct wood{
int l;
int w;
int flag;
}W[];
bool cmp(wood w1, wood w2){
if(w1.l<w2.l )
return true;
else if(w1.l==w2.l && w1.w<=w2.w)
return true;
return false;
}
int main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i=;i<n;i++){
cin>>W[i].l>>W[i].w;
W[i].flag=;
}
sort(W,W+n,cmp);
//for(int i=0;i<n;i++)
//cout<<W[i].l<<" "<<W[i].w<<endl;
int tw;
int result=;
for(int i=;i<n;i++){
if(W[i].flag)
continue;
tw=W[i].w;
W[i].flag=;
for(int j=;j<n;j++){
if(W[j].flag)
continue;
if(W[j].w>=tw){
tw=W[j].w;
W[j].flag=;
}
}
result++;
}
cout<<result<<endl;
}
return ;
}
 

最新文章

  1. Java 技能树
  2. asp.net identity 3.0.0 在MVC下的基本使用 序言
  3. command line
  4. 使用PouchDB来实现React离线应用
  5. LPC4370 ACDHS speed and DMA
  6. Remove Duplicates from Sorted List II leetcode java
  7. HTML5,添加图片
  8. 【每日scrum】NO.4
  9. c常用字符串函数
  10. MAC——laravel环境
  11. javascript获得浏览器工作区域的大小
  12. Java 5 的新标准语法和用法详解集锦
  13. Linux开发环境的搭建和使用——Linux本必备软件SSH
  14. log4net发布时assembly引用错误的问题
  15. 二,ESP8266 GPIO和SPI和定时器和串口
  16. Vsftp的PASV mode和Port模式配置文件的设置
  17. Guava新增集合类型-Bimap
  18. git pull的理解 以及 git conflict的解决
  19. spring aop 之xml
  20. 图解Eclipse中配置Maven并创建Maven的Web工程

热门文章

  1. nginx stream module on mt7621(newifi3 d2) with openwrt 18.06.2
  2. IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 Industry Track Call for Papers
  3. jenkins 多版本 jdk
  4. 5. Go函数
  5. 2019南昌网络赛-I(单调栈+线段树)
  6. tensorflow实现二分类
  7. 15. 3Sum (JAVA)
  8. select2的设置选中
  9. WEB-INF目录下的jsp怎么引用外部文件:js,css等
  10. NoteBook学习(一)-------- Zeppelin VS Jupyter