Greedy algorithm wooden Sticks Instance code _c language

Source: Internet
Author: User

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 is processed by a woodworking machine into 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 "a" wooden stick is 1 minute. (b) Right after processing a stick of length L and weight W, the machine'll 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 are are to find the minimum setup time to process a given pile of n wooden sticks. For example, if to 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 was 2 minutes since there is sequence of pairs (1,4), (3,5), (4,9), (2,1).

The input consists of T test cases. The number of test cases (T) is given in the the the input file. Each test case consists of two lines:the A-has an integer n, 1<=n<=5000, that represents the number of Wo Oden sticks in the "test Case" and the second line contains n 2 positive integers L1, W1, L2, W2, ..., LN, WN, each of Magn Itude 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 should contain the minimum setup time in minutes.

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 the 3 1

Sample Output
2 1 3

Copy Code code as follows:

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

struct node
int l;
int W;
int flag;
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", &AMP;STICKS[I].L,&AMP;STICKS[I].W);
Qsort (Sticks,n,sizeof (sticks[0]), CMP);
Sticks[0].flag = 1;
CNT = 1;
for (j = 1; j < N; j + +)
for (i = j; i < n; i++)
Sticks[i].flag = 1;
i = 1;
while (Sticks[i].flag)
j = i;
if (j = = n) break;
Sticks[j].flag = 1;
printf ("%d\n", CNT);

return 0;


We have to deal with some sticks, the first time is 1 minutes, the other, in the length of L, the length of the root behind the stick is L ', weight w ', as long as L <=l ' and W <= W ', do not need time, otherwise need 1 minutes, how to arrange to handle the order of the stick, so that the time to spend the least.


Greedy algorithm. First, these sticks are arranged in the order of length and weight from small to large. CL and CW are the length and weight of the first root, in order to compare the following is not larger than the current CL,CW, is the case of the logo flag set to 1, and with the new CL,CW. After the comparison, and then the previous scan, to find the first sign bit 0, as is the second batch of the smallest one, counter plus one. Compare its length and weight as the current CL,CW, and then cycle back. Until all of them have been dealt with.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.