Greedy algorithm WOODEN STICKS instance code

  • 2020-04-02 00:47:44
  • OfStack

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. Called the 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 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 < And w = l ' < =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).

The 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 2 n positive integers l1, w1, w2, l2,... , ln, wn, each of the points 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.

The Output
The output should contain The minimum setup time in minutes, one per line.

The Sample Input
3, 5, 4, 9, 5, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 2, 3, 3, 1

The Sample Output
1 2 3


#include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #define N 5000;

 struct node
 {
     int l;
     int w;
     int flag;
 }sticks[5000];
 int cmp(const void *p,const void *q)
 {
     struct node *a = (struct node *)p;
     struct node *b = (struct node *)q;
     if(a->l > b->l) return 1;
     else if(a->l < b->l) return -1;
     else return a->w > b->w ? 1 : -1;
 }
 int main()
 {
     int t,n,cnt,cl,cw;
     int i,j;
     scanf("%d",&t);
     while(t--)
     {
         memset(sticks,0,sizeof(sticks));
         scanf("%d",&n);
         for( i = 0; i < n; i++)
             scanf("%d %d",&sticks[i].l,&sticks[i].w);
         qsort(sticks,n,sizeof(sticks[0]),cmp);
         sticks[0].flag = 1;
         cl = sticks[0].l;
         cw = sticks[0].w;
         cnt = 1;
         for( j = 1; j < n; j++)
         {
             for( i = j; i < n; i++)
             {
                 if(!sticks[i].flag&&sticks[i].l>=cl&&sticks[i].w>=cw)
                 {
                     cl = sticks[i].l;
                     cw = sticks[i].w;
                     sticks[i].flag = 1;
                 }
             }
             i = 1;
             while(sticks[i].flag)
                i++;
             j = i;
             if(j == n) break;
             cl = sticks[j].l;
             cw = sticks[j].w;
             sticks[j].flag = 1;
             cnt++;
         }
         printf("%dn",cnt);

     }
     return 0;
 }

The question:

We're going to deal with some sticks, the first stick is going to take 1 minute, and the other stick, the one behind the length l, the weight w, is going to be l', the weight w', as long as l < And w = l ' < If = w', no time is needed, otherwise, 1 minute is needed. Figure out how to arrange the order of handling sticks so as to minimize the time spent.

Ideas:

Greedy algorithm. First arrange the sticks in order of length and weight from small to large. Cl and cw are the length and weight of the first root. Is the latter one larger than the current cl and cw? Yes, we set the flag as 1 and follow the new cl and cw. After the comparison, scan from front to back to find the first flag bit is 0, as the smallest of the second batch, counter increment one. Let's take its length and its weight as the current cl, cw, and recycle it and compare it later. Until everything is done.


Related articles: