Wooden Sticks
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 21899 |
|
Accepted: 9350 |
Description
There is a pile of n wooden sticks. The length and weight of each stick is known in advance. The sticks is 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 is associated with cleaning operations and changing tools and shapes. The setup times of the woodworking machine is 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 would need no setup time for a stick of length l ' and Weight W ' if L <= l ' and W <= W '. Otherwise, it'll need 1 minute for setup.
You is 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 is (9, 4), (2, 5), (1, 2), (5, 3), and (4, 1), then the minimum setup time should was 2 minutes since there is a sequence of pairs (4, 1), (5, 3), (9, 4), (1, 2), (2, 5).
Input
The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case consists of a lines:the first line have an integer n, 1 <= n <=, which represents the number of wooden sticks in the test case, and the second line contains 2n positive integers L1, W1, L2, W2,..., Ln, WN, EAC H of magnitude at most 10000, where Li and wi is the length and weight of the i th wooden stick, respectively. The 2n integers is delimited by one or more spaces.
Output
The output should contain the minimum setup time in minutes, one per line.
Sample Input
Sample Output
213
Source
Taejon 2001
#include <iostream> #include <algorithm> #include <string.h>using namespace std;struct node{int x; int y;}; int cmp (Node A,node b) {if (a.x==b.x) return a.y<b.y; return a.x<b.x;} int main () {int t; int n; Node node[5005]; BOOL flag[5005]; Whether the flag is already grouped while (cin>>t) {while (t--) {memset (flag,0,sizeof (flag)); cin>>n; int ans=0; for (int i=0;i<n;i++) cin>>node[i].x>>node[i].y; Sort (node,node+n,cmp); for (int i=0;i<n;i++) {if (flag[i]==0) {int p=node[i].y; for (int j=i;j<n;j++) {if (flag[j]==0&&node[j].y> =p) {flag[j]=1;p=node[j].y;} } ans++; }} cout<<ans<<endl; } }}
The overall idea is that, according to the length of the structure of the order, and then the traversal grouping, if meet p<y to meet the grouping conditions, modify the flag bit, so traverse over, there is ans a 0, there is the ANS group, the answer is ans;
POJ 1065 Greedy algorithm