Impatient C-Little plus

Source: Internet
Author: User

Describe

C Small plus some sticks, their length and quality have already known, need a machine to handle these sticks, the machine will need to use a unit of time when opening, if the weight and length of the i+1 stick is equal to the first I handle the stick, then it will not be time consuming, otherwise it would take a unit of time. Because anxious to go to date, C small plus want in the shortest time to finish the stick, can you tell him how to do?

Input
The first line is an integer t (1<t<1500), which indicates that the input data has a total T group.
The first line of each set of test data is an integer n (1<=n<=5000), which indicates that there are n sticks. The next line is to enter the L,w (0 < L, W <= 10000) of the n sticks respectively, separated by a space, indicating the length and quality of the stick.
Output
the shortest time to handle these sticks.
Sample input
 
 
Sample output
213
At first I thought that as long as the length and weight ascending, and then find out when Length[i]!=length[i-1], weight[i]<weight[i-1] and then count++, the results found not so, 1 4 2 1 3 5 4 9 5 2 so that count is 3
In fact the order is 1 4 3 5 4 9 2 1 5 2 belongs to greedy strategy, as far as possible to make each processing of the stick more; This problem has a high demand for time, select Sort is not OK, select the fast row sort;
1#include <stdio.h>/*wa*/2typedefstruct 3 {4     intlength;5     intweight;6 }stick;7 intMain ()8 {9     intT;Tenscanf"%d",&t); One      while(t--) A     { -         intN,i,j,count=0, first=1; -scanf"%d",&n); the stick a[n],t; -          for(i=0; i<n;i++) -scanf"%d%d",&a[i].length,&a[i].weight); -          for(i=0; i<n-1; i++) +              for(j=i+1; j<n;j++) -             { +                 if(a[i].length>a[j].length) A                 { att=A[i]; -a[i]=A[j]; -a[j]=T; -                 } -             } -          for(i=0; i<n-1; i++) in              for(j=i+1; j<n;j++) -             { to                 if((a[i].length==a[j].length) && (a[i].weight>a[j].weight)) +                 { -t=A[i]; thea[i]=A[j]; *a[j]=T; $                 }Panax Notoginseng              -             } the      for(i=0; i<n;i++) +printf"%d%d\n", a[i].length,a[i].weight); A      for(i=1; i<n;i++) the     { +         if((a[i].length!=a[i-1].length) && (a[i].weight<a[i-1].weight)) -count++; $     } $     if(first==1) -       { -printf"%d\n", count+1); thefirst=0; -       }Wuyi     Else theprintf"%d\n", count); -      } Wu}

1#include <stdio.h>/*ac*/2#include <algorithm>3#include <string.h>/*the Memset function resets the array to memset (array name, replacement element, size)*///size is usually sizeof (array name) (all permutations), sizeof (int) *n4 using namespacestd;5typedefstruct 6 {7     intlength;8     intweight;9 }stick;Ten BOOLCMP (Stick X,stick y)/*Sort Sorting Method*/ One { A     if(x.length<y.length) -         return true; -     if(x.length==y.length&&x.weight<y.weight) the         return true; -     return false; - } - intMain () + { -     intT; +scanf"%d",&T); A      while(t--) at     { -         intCount=0, i,j,n,t; -scanf"%d",&n); - stick a[n]; -Memset (A,0,sizeof(a)); -          for(i=0; i<n;i++) inscanf"%d%d",&a[i].length,&a[i].weight); -Sort (a,a+n,cmp); to          for(i=0; i<n;i++)/*The first stick is the time it takes to open a machine .*/ +         { -             if(a[i].weight!=0)/*greedy strategy, every time as much as possible to process the wooden stick!! */ the             { *t=A[i].weight; $count++;Panax Notoginseng                  for(j=i+1; j<n;j++) -                 { the                     if(a[j].weight>=t) +                     { At=A[j].weight; thea[j].weight=0;/*post-machined stick quality 0*/ +                     } -                 } $              $             } -  -         } theprintf"%d\n", count); -     }Wuyi}

Impatient C-Little plus

Related Article

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.