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
问题描述
题目给出n个木棍的长度和重量,当机器处理完长度为L,重量为W的木棍后再处理长度为L1(L<=L1),重量为W1(W<=W1)的木棍就不需要处理时间,否则就需要一定的处理时间,根据给出的数据求出最短的处理时间。
伪代码
class Node{//代表一根木棍
木棍的长:L
木棍的重量:W
自定义<:L<L1返回true||L==L1&&W<W1返回true否则放回false
}
1.将数据存入到vector<Node> v中
2.对v排序
3.定义变量vector<Node> v_temp用来某一处理序列的最大值
4.将v中的首元素插入到v_temp中
把v中从第二个开始的所有元素和v_temp中的所有元素按顺序从小到达比较(即一个二重循环)如果当前v中的元素比v_temp中的所有元素都要小,则插入成为v_temp中的元素,否则将第一个比v中元素小的v_temp中的元素跟新成为当前的v中的元素。
5.最后v_temp的大小即为最后的结果。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
class Node{
public:
int l,w;
Node(int l1,int w1):l(l1),w(w1){}
bool operator<(const Node &N){
if(l<N.l)return true;
else if(l==N.l&&w<N.w)return true;
else return false;
}
};
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
vector<Node> v;
while(n--){
int l,w;
scanf("%d %d",&l,&w);
v.push_back(Node(l,w));
}
sort(v.begin(),v.end());
vector<Node> v_temp;
v_temp.push_back(v[0]);
for(int i=1;i<v.size();i++)
for(int j=0;j<v_temp.size();){
if(v[i].l>=v_temp[j].l&&v[i].w>=v_temp[j].w){
v_temp[j].l=v[i].l;
v_temp[j].w=v[i].w;
break;
}
else {
j++;
if(j==v_temp.size())
v_temp.push_back(v[i]);
}
}
cout<<v_temp.size()<<endl;
}
return 0;
}