Wooden sticks
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11605 accepted submission (s): 4792
Problem descriptionthere 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 (), and ), then the minimum setup time shocould be 2 minutes since there is a sequence of pairs ).
Inputthe 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 magnloud 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.
Outputthe output shoshould 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
213
Simple and greedy ..
The question can be viewed as: given a number of two-dimensional coordinate points (1 <= n <= 5000, if "X1 <= X2 & Y1 <= Y2" is met, we acknowledge that these two coordinates belong to the same set. The question requires us to find the coordinate points that can represent at least a few sets.
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <queue> using namespace STD; const int maxn = 5000 + 50; int CAS; // case int N; // Number of wood bars int vis [maxn]; // mark the array int ans_minute; // shortest time struct point {int L; // length int W; // weight}; point S [maxn]; bool CMP (point a, point B) {if (. L = B. l) return. W <B. w; else return. L <B. l;} int main () {scanf ("% d", & CAS); While (CAS --) {scanf ("% d", & N); ans_minute = 0; memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) scanf ("% d", & S [I]. l, & S [I]. w); sort (S + 1, S + n + 1, CMP); // For (INT I = 1; I <= N; I ++) printf ("% d __% D", s [I]. l, s [I]. w); int judge = 1; for (INT I = 1; I <= N; I ++) {If (vis [I]) continue; ans_minute ++; int temp = s [I]. w; vis [I] = 1; for (Int J = I + 1; j <= N; j ++) {If (temp <= s [J]. W &&! Vis [J]) {temp = s [J]. w; Judge ++; vis [J] = 1; Continue ;}} if (Judge >=n) break;} printf ("% d \ n ", ans_minute );}}
HDU 1051: wooden sticks