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): 11244 accepted submission (s): 4627

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
 
Question:
Length and weight of N wooden sticks. Obtain the shortest time for making a wooden stick as required. It takes one minute to create the first wooden stick. If the weight and length of the wooden stick to be created are longer than those of the wooden stick, it does not need to be established. If not, it will take another time to establish the wooden stick. Evaluate the minimum time.
Solution:
Sort the length and weight of a wooden stick, with the length as the primary consideration. After sorting, the weight and length of the next wooden stick are not necessarily greater than those of the previous one. As a result, we perform multiple scans on the sorted array to mark the sorted array within a creation time, know all the sticks (set an external variable to count the number of scanned elements ).

Example:
5
4 9 5 2 2 1 3 5 1 4
After sorting:
1 4 2 1 3 5 4 9 5 2
For the first scan: mark with the mark [] array. MARK [] is initialized to 0, and red is the first scan.
Stiks: (1 4) (2 1) (3 5) (4 9) (5 2)
MARK: 1 0 1 1 0
The setuptime is the time required to create the first wooden stick, that is, 1. At this time, the scan count is 3.
Next, perform the second scan. Blue indicates the result of the second scan.
Stiks: (1 4) (2 1) (3 5) (4 9) (5 2)
MARK: 1 0 1 1 0
This indicates that setuptime is 1, and the scan count is 5.

The Code is as follows:
#include<stdio.h>#include<iostream>#include<algorithm>#define M 10000using namespace std;struct node{int x,y,z;}p[M];int cmp(node a,node b){if(a.x<b.x) return 1;else {if(a.x==b.x){if(a.y<b.y) return 1;}else return 0;}}int main (){ int n,m;int i,j;scanf("%d",&n);while(n--){scanf("%d",&m);for(i=0;i<m;i++){scanf("%d%d",&p[i].x,&p[i].y);p[i].z=0;}sort(p,p+m,cmp);int sum=0;for(i=0;i<m;i++){if(p[i].z==0){p[i].z=1;sum++;int k=p[i].y;for(j=i+1;j<m;j++){if(p[j].z==0 && p[j].y>=k){p[j].z=1;k=p[j].y;}}}}printf("%d\n",sum);}return 0;}


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.