CSU 1757 (greedy or tree-like array)

Source: Internet
Author: User

1757: Train inbound time limit:1 Sec Memory limit:128 MB
submit:209 solved:51
[Submit] [Status] [Web Board] Description

Train station people are always waiting for the train to stop at the platform, a platform has a train stay can not have other trains enter, today there are N trains through, known they come in time Si and the outbound time Ti, the station time to the outbound time between the train must have a platform to stop it, ask all trains can stop on time, At least how many platforms to give these trains

Input

The first line enters a positive integer t, which indicates the number of data groups
Enter a positive integer n for the first row of each group of data, indicating the number of trains (n<=10000)
Next n rows, enter 2 positive integers per line si,ti, indicating the stop time and outbound time of the first train (SI<TI<1E9)

Output

The minimum number of stations to be scheduled for each set of data output

Sample Input
131 33 44 6
Sample Output
2


Because up to 20,000 points, the discretization of the tree array, the maintenance interval, the last to find the maximum value of the point.
#include <iostream>#include<cstring>#include<stdio.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN =100005;intX[n],a[n],b[n];intC[n],n;intLowbit (inti) {    returni& (-i);}voidUpdateintIdxintv) {     for(inti=idx;i<=2*n;i+=lowbit (i)) {C[i]+=v; }}intGetsum (intidx) {    intsum =0;  for(inti=idx;i>=1; i-=lowbit (i)) {Sum+=C[i]; }    returnsum;}intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--) {memset (c,0,sizeof(c)); scanf ("%d",&N); intCNT =1;  for(intI=1; i<=n;i++) {scanf ("%d%d",&a[i],&B[i]); X[cnt++] =A[i]; X[cnt++] =B[i]; }         intK =2; Sort (x+1, x+CNT);  for(intI=2; i<cnt;i++){            if(x[i]==x[i-1])Continue; X[k++] =X[i]; }         for(intI=1; i<=n;i++){            intL = Lower_bound (x+1, X+k,a[i])-(x); intR = Lower_bound (x+1, X+k,b[i])-(x); Update (L,1); Update (R+1,-1); }        intMAX =-1;  for(intI=1; i<=2*n;i++) {MAX=Max (Max,getsum (i)); } printf ("%d\n", MAX); }    return 0;}

Greedy strategy: For the starting point and the end of the order, meet the starting point of a more platform, meet the end of the word less a platform. The maximum value in the middle procedure is the result.

#include <iostream>#include<cstring>#include<stdio.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN =100005;intA[n],b[n],n;intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--) {scanf ("%d",&N);  for(intI=0; i<n; i++) scanf ("%d%d",&a[i],&B[i]); Sort (A,a+N); Sort (b,b+N); intans=0, sum=0; intj=0, i=0;  while(I!=n && j!=N) {if(b[j]<A[i]) {Sum--; J++; }            Else{sum++; I++; } ans=Max (ans,sum); } printf ("%d\n", ans); }    return 0;}

CSU 1757 (greedy or tree-like array)

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.