正文

Wooden Sticks (STL use)2007-09-03 19:13:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/29092.html

分享到:

Wooden Sticks
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
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

/// 很简单的一道题,思路先一 length 或者 weight 排序 sticks, 然后以另一个变量(length or weight)  以“ 最大”消逆序,如果不能消则 times 增 1,并将该变量插入链表中(表里面保存的就是逆序的东西) 以

5
4 9 5 2 2 1 3 5 1 4
为例:按 length 排序后为(1,4),(2,1),(3,5),(4,9),(5,2) 首先 4 进入链表 times=1, 下一个 1,因为链表里没有小于等于 1的数所以 1,插入表头,times++. 接下来为, 5,9, 每次我们用表中较大的数来比较,所以链表的变化为 1,5,  ==== >  1,9  , 最后 一个 2, 因为表里有1,1<=2成立所以最后一根也不需要发时间,链表变为 2,9 , times=2.

//// MY CODE ,  NOTE HOW TO USE STL

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int const MAX = 5000;
pair<int,int> stick[MAX];
struct myCmp{
    bool operator()(const pair<int,int>& a,const pair<int,int>& b){
        return a.first<b.first;
    }
};
int main(){
    int nTest;
    cin>>nTest;
    for(int i=0; i<nTest; i++){
        int nStks,j;
        cin>>nStks;
        for(j=0; j<nStks; j++)
            cin>>stick[j].first>>stick[j].second;
        sort(&stick[0],&stick[j],myCmp());
        list<int> lst;
        list<int>::reverse_iterator it;
        int times=1;
        lst.push_back(stick[0].second);
        for(int j=1; j<nStks; j++){
            for(it=lst.rbegin(); it!=lst.rend(); it++){
                if(stick[j].second>=*it){
                    *it = stick[j].second;
                    break;
                }
            }
            if(it==lst.rend()){
                lst.push_front(stick[j].second);
                times++;
            }
        }
        cout<<times<<endl;
    }
    return 0;
}

 

阅读(3517) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册