Nyoj 236 eager C Xiaojia

Source: Internet
Author: User

This can be done by sorting, sorting the weight first, and then sorting the length, and finally counting and dividing the chain, that is, the number of times processed by the machine. below is the implementation of the Code.

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 typedef struct stick {/* where L is length, W is weight, and visit indicates whether the current group has been sorted */5 Int L, W; 6 int visit; 7} Stick; 8 int CMP (const void * a, const void * B) // defines the comparison function 9 {10 stick * c = (stick *); 11 stick * D = (stick *) B; 12 if (c-> W = D-> W) 13 {14 return C-> L-D-> L; // sort by weight from small to large. If the weight is the same, 15} 16 return C-> W-D-> W are arranged by length; 17} 18 int main () 19 {20 // freopen ("1.txt"," r ", stdin); 21 // freopen (" 2.txt", "W", stdout ); 22 int t; 23 scanf ("% d", & T); 24 while (t --) 25 {26 int N, CNT = 0, Len; // Len is used to save the following temporary length, CNT count, total time spent 27 stick arr [5010]; 28 scanf ("% d", & N ); 29 for (INT I = 0; I <n; I ++) 30 {31 scanf ("% d", & arr [I]. l, & arr [I]. w); 32 arr [I]. visit = 1; // initialize to 133} 34 qsort (ARR, N, sizeof (stick), CMP); // sort, of course, you can also sort 35 for (INT I = 0; I <n; I ++) 36 {37 If (ARR [I]. visit) // if it is not listed, sort it in another column 38 {39 Len = arr [I]. l;/* Save the current length. Because it is arranged by weight, the weight must increase progressively, that is, the last one must be greater than or equal to the previous one, therefore, you do not need to compare the weight */40 for (Int J = I + 1; j <n; j ++) 41 {42 if (ARR [J]. visit & Len <= arr [J]. l) // if the requirements are met, keep searching for 43 {44 arr [J]. visit = 0; // mark it as zero after it is found, so that it will not be retrieved from the other column next time 45 Len = arr [J]. l; 46} 47} 48 CNT ++; 49} 50} 51 printf ("% d \ n", CNT); 52} 53 return 0; 54}

 

Nyoj 236 eager C Xiaojia

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.