HDU-1051 wooden sticks

Source: Internet
Author: User
Wooden sticks

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3902 accepted submission (s): 1621

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
Machine. The setup times of the woodworking machine are given
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 shoshould 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
Magnbench at most 10000, where Li and WI are the length and weight
The I th wooden stick, respectively. The 2n integers are delimited
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

2
1
3

Question: given some (1 <= n <= 5000) groups of two-dimensional coordinate points, 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.

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 ),
).

Save the information of all vertices first, and then select X or Y as the object for sorting. Note that if the X of the two vertices is the same, keep y in order at this time. In this way, all sets are displayed linearly. It can be understood that after such a sorting, a point set with the most vertices satisfying the meanings of the question can be found each time from the beginning to the end. The correctly separated set does not have elements in this arrangement in reverse order.

The Code is as follows:

#include <stdio.h>#include <stdlib.h>#include <string.h>int T;struct E{int len, wi;} e[5010];char hash[5010];int cmp( const void *a, const void *b ){if( ( ( struct E * )a )-> len!= ( ( struct E * )b )-> len ){   return ( ( struct E * )a )-> len- ( ( struct E * )b )-> len;}else{    return ( ( struct E * )a )-> wi- ( ( struct E * )b )-> wi;}}int find( int N ){for( int i= 0; i< N; ++i  ){if( !hash[i] ){return i;}}return -1;}bool ok( int i, int baselen, int basewi ){if( e[i].wi>= basewi ){return true;}else{return false;}}int main(  ){scanf( "%d", &T );while( T-- ){        memset( hash, 0, sizeof( hash ) );int N, time= 0;scanf( "%d", &N );for( int i= 0; i< N; ++i ){scanf( "%d %d", &e[i].len, &e[i].wi );} qsort( e, N, sizeof( e[0] ), cmp ); int sta;while( 1 ){sta= find( N );//printf( "sta=%d\n", sta );if( sta== -1 ){break;}int baselen= e[sta].len, basewi= e[sta].wi;//printf( "blen= %d, bwi= %d\n", baselen, basewi );for( int i= sta; i< N; ++i ){//printf( "blen= %d, bwi= %d\n", baselen, basewi );if( !hash[i]&& ok(i, baselen, basewi ) ){hash[i]= 1;//printf( "..%d  ..%d\n", e[i].len, e[i].wi );baselen= e[i].len;basewi= e[i].wi;}}++time; }printf( "%d\n", time );}}/*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*/

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: info-contact@alibabacloud.com 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.