【算法】Processing Management

来源:互联网 发布:淘宝联盟挣钱吗? 编辑:程序博客网 时间:2024/05/17 09:16

问题描述:

Assistant Manager Lee is on a visit to a factory run by a partner company.

A machine is cutting steel plates that are different in length and width.

However, a closer look found that the machine keeps operating only when the length and width of the steel plate it is cutting are equal or larger than those of the previous plate.

Otherwise, he found it takes one minute to prepare the machine working again.
After looking at the length and width of the steel plate ordered, time required for preparing the machine restarting if the order by which steel plate are cut is set could be reduced.

To help the partner company, he immediately starts writing a program that sets the order of cutting steel plates to minimize the time required for the restart of the machine. How did he write?

The steel plate cannot rotate and be mindful of the fact that when cutting a steel plate, it takes one minute to operate for the first time.


Time Limit : 2 sec (Java : 4 sec)


[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T <= 10 )


In each test case, the first line has an integer N(1 ≤ N ≤ 5,000), the number of steel plate ordered.
In the second line, information on each steel plate Li, Wi is given.
Li is the length of i-th steel plate, and Wi is the width of i-th steel plate. (1 ≤ Li, Wi ≤ 10,000)


[Output]

In the each line, output minimum time required for the machine to make all steel plates ordered.


[I/O Example]

Input

2
5
4 9 5 2 2 1 3 5 1 4
3
1 3 2 2 3 1


Output

2

3

分析:

首先根据长度排序,然后再根据宽度剔除符合条件的数据

源码:

#include <iostream>using namespace std;int N;int Li[5001];int Wi[5001];int Answer;struct lengthWidth{int l;int w;};struct lengthWidth input[5001];int compare(const void* a, const void* b){int l1 = ((struct lengthWidth *)a)->l;int l2 = ((struct lengthWidth *)b)->l;if (l1 == l2){return ((struct lengthWidth *)a)->w - ((struct lengthWidth *)b)->w;}else{return l1 - l2;}}int main(int argc, char** argv){int test_case;int T;/*The freopen function below opens input.txt file in read only mode, and afterward,the program will read from input.txt file instead of standard(keyboard) input.To test your program, you may save input data in input.txt file,and use freopen function to read from the file when using cin function.You may remove the comment symbols(//) in the below statement and use it.Use #include<cstdio> or #include<stdio.h> to use the function in your program.But before submission, you must remove the freopen function or rewrite comment symbols(//).*/// freopen("input.txt", "r", stdin);cin >> T;for (test_case = 0; test_case < T; test_case++){cin >> N;for (int i = 1; i <= N; i++){cin >> input[i].l >> input[i].w;}/***********************************  Implement your algorithm here. ************************************/qsort(&input[1], N, sizeof(struct lengthWidth), compare);Answer = 0;int j = 0;for (int i = 1; i <= N; i++){if (input[i].l == 0 && input[i].w == 0)continue;int cur_len = input[i].l;int cur_wid = input[i].w;for (j = i + 1; j <= N; j++){if (cur_len <= input[j].l && cur_wid <= input[j].w){cur_len = input[j].l;cur_wid = input[j].w;input[j].l = input[j].w = 0;}}input[i].l = input[i].w = 0;Answer++;}// Print the answer to standard output(screen).cout << Answer << endl;}return 0;//Your program should return 0 on normal termination.}


0 0