[Algorithm] the biggest lesson clearance issues

Title Description

Linear Time Algorithm Given n real numbers, real numbers n which find the maximum difference between the number of adjacent 2 on the number line, the maximum gap design solutions of the problem (the time complexity is O (n)). 

 

Entry

The first line of a positive integer n (2 <= n <=  2 × 1e7)
a second row of n real numbers, to ensure that these real data only one decimal place. 

 

Export

A real number, a decimal Reserved.

 

Sample input

5
2.3  3.1  7.5  1.5  6.3

 

Sample Output

3.2

 

[Reference] https://blog.csdn.net/llwwlql/article/details/52434280 blog

 

【answer】

  Puzzling, why use the pigeonhole principle, after the teacher explained later found that really is the pigeonhole principle.

 

  The core idea is:

 

  n points, where the two points as the left and right boundaries. The remaining n-2 points in the n-1 must have a blank grid.

  For example: 1,2,3,4.

  Then using the left and right margins as 1,4, and the remaining two numbers, we [1,4] Aliquots of this interval is divided into n-1 intervals.

  Then there will be [1, 2] [2, 3] [3, 4] These three sections, remaining two numbers pitching test, there must be an interval is empty.

  The maximum gap will certainly form the largest gap in the gap after the binding point of the left and right sections.

 

  1, to find the maximum and minimum values ​​of all the scanning points.

  2, is divided into sections which length: len = (Max - Min) / (n - 2). 

  3, each of the processing sections

      a, is obtained in which each point corresponds to a range, (a [i] - Min) / (len) 

      b, to each point corresponding to a good mounting section (Block) inside

      C, in the way statistics section number, the section in the left and right boundaries: i.e., maximum and minimum values ​​in the interval.

  4, and then the process run on the line, only to find out in the course of the maximum range of the left running, the current minimum interval to update the answer, while the right end point of the last update.

  5, special sentenced to two situations:

      a, if the third step occurs when the length is equal to 0, the time interval number Solution: len as the denominator, the answer to this situation is actually directly outputted to 0.

      b, if the situation only two numbers appeared under, if the index is updated from zero, if not handled properly it will not update the value in the first interval.

  


 

 

Code [1]

 1 #include <stdio.h>
 2 #define N 2e7+10
 3 #define mmax 1e5
 4 #define mmin 0
 5 
 6 int n,count[N]={0};
 7 double num[N], maxNum[N],minNum[N];
 8 
 9 
10 int main()
11 {
12     int i,bucket;
13     double maxValue=mmin, minValue=mmax,delta,begin,maxgap=0.,temp;
14 
15     scanf("%d",&n);
16 
17     //the first step, input and compute the max and min value.
18     for(i=0;i<n;i++){
19         scanf("%lf",&num[i]);
20         maxNum[i]=mmin;
21         minNum[i]=mmax;
22 
23         if(num[i]>maxValue)
24             maxValue=num[i];
25 
26         if(num[i]<minValue)
27             minValue=num[i];
28     }
29 
30     //the second step, partion the numbers into buckets
31     delta=(maxValue-minValue)/(n-1);
32 
33 
34     if( delta == 0 ) {
35         printf("0.0\n");
36         return 0 ;
37     }
38 
39     for(i=0;i<n;i++){
40         bucket=(int)((num[i]-minValue)/delta);
41         count[bucket]++;
42         if(maxNum[bucket]<num[i])
43             maxNum[bucket]=num[i];
44         if(minNum[bucket]>num[i])
45             minNum[bucket]=num[i];
46     }
47 
48 
49     /*
50     //deal with the last bucket, containing the max Number
51     if(count[n-2]==0)
52         minNum[n-2]=maxNum[n-2]=maxValue;
53     else
54         maxNum[n-2]=maxValue;
55 
56     count[n-2]++;
57     */
58 
59 
60     //the third step, retrieve the max gap
61     begin=maxNum[0];
62 
63     //把 i < n - 1 改成 i < n
64     for(i=1;i<n;i++){
65         if(count[i]){
66             temp=minNum[i]-begin;
67             if(temp>maxgap)
68                 maxgap=temp;
69             begin=maxNum[i];
70         }
71     }
72 
73     printf("%.1f\n",maxgap);
74 
75     return 0;
76 }
View Code

 

[Code 2]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 2e7 + 10 ;
 5 const double inf = 1e18 ;
 6 double Block[N][2] , a[N] ;
 7 double Max ;
 8 double Min ;
 9 double Len ;
10 int n,Cnt[N] ;
11 
12 int main(){
13 
14     /*Input, while updating the maximum and minimum values * / 
15      Scanf ( " % D " , & n-);
 16  
. 17  
18 is      Scanf ( " % LF " , & A [ . 1 ]);
 . 19      Max = A [ . 1 ];
 20 is      Min = A [ . 1 ];
 21 is      Block [ . 1 ] [ 0 ] =   INF;
 22 is      Block [ . 1 ] [ . 1 ] = - INF;
 23 is  
24      for ( int I = 2 ; I <= n-; I ++ ) {
25          Scanf ( " % LF " , & A [I]);
 26 is          Max = max (Max, A [I]);
 27          Min = min (Min, A [I]);
 28          Block [I] [ 0 ] =   INF ;
 29          Block [I] [ . 1 ] = - INF;
 30      }
 31 is  
32      / * processing each gap * / 
33 is      / * n-point n - 1 number range * / 
34 is      Len = (Max - Min) / (n-- . 1 );
 35  
36      IF (n-== 2 ) {
 37 [         the printf ( " % .1f \ n- " , Max- Min);
 38 is          return  0 ;
 39      }
 40      / * scans each point corresponding to the point in which Block * / 
41 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 42 is          int IDX = int ((A [I] - min) / Len) + . 1 ;
 43 is          Cnt [IDX] ++ ;
 44 is          Block [IDX] [ 0 ] = min (Block [IDX] [ 0 ], A [I]);
 45          Block [IDX] [ . 1] = max( Block[idx][1] , a[i] );
46     }
47 
48     double Ans = 0 , Last = Min ;
49     for( int i = 1 ; i <= n-1 ; i++ ){
50         if( Cnt[i] ){
51             if( Ans < Block[i][0] - Last ){
52                 Ans = Block[i][0] - Last ;
53             }
54             Last = Block[i][1];
55         }
56     }
 57 is  
58      // Laid determination of the position of the last point 
59  
60      IF (Ans <Max - Last) {
 61 is          Ans = Max - Last;
 62 is      }
 63 is  
64      the printf ( " % .1f \ n- " , Ans);
 65      return  0 ;
 66 }
View Code

 

Code [3]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Sheryang main
 4 const int maxn=2e7+7;
 5 typedef long long ll;
 6 const int mod=1e9+7;
 7 #define HEAP(...) priority_queue<__VA_ARGS__ >
 8 #define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > > 
 9 template<class T> inline void gmin(T &x,const T &y){x=x>y?y:x;}
10 template<class T> inline void gmax(T &x,const T &y){x=x<y?y:x;}
11 template<class T> inline bool Gmin(T &x,const T &y){return x>y?x=y,1:0;}
12 template<class T> inline bool Gmax(T &x,const T &y){return x<y?x=y,1:0;}
13 const int BufferSize=1<<16;
14 char buffer[BufferSize],*Bufferhead,*Buffertail;
15 bool Terminal;
16 inline char Getchar(){
17     if(Bufferhead==Buffertail){
18         int l=fread(buffer,1,BufferSize,stdin);
19         if(!l){Terminal=1;return 0;}
20         Buffertail=(Bufferhead=buffer)+l;
21     }
22     return *Bufferhead++;
23 }
24 template<class T>inline bool read(T &x){
25     x=0;char c=Getchar(),rev=0;
26     while(c<'0'||c>'9'){rev|=c=='-';c=Getchar();if(Terminal)return 0;}
27     while(c>='0'&&c<='9') x=x*10+c-'0',c=Getchar();
28     if(c=='.'){
29         c=Getchar();double t=0.1;
30         while(c>='0'&&c<='9') x=x+(c-'0')*t,c=Getchar(),t=t/10;
31     }
32     x=rev?-x:x;
33     return 1;
34 }
35 template<class T1,class T2> inline bool read(T1 &x,T2 &y){return read(x)&read(y);}
36 template<class T1,class T2,class T3> inline bool read(T1 &x,T2 &y,T3 &z){return read(x)&read(y)&read(z);}
37 template<class T1,class T2,class T3,class T4> inline bool read(T1 &x,T2 &y,T3 &z,T4 &w){return read(x)&read(y)&read(z)&read(w);}
38 inline bool reads(char *x){
39     char c=Getchar();
40     while(c<33||c>126){c=Getchar();if(Terminal)return 0;}
41     while(c>=33&&c<=126) (*x++)=c,c=Getchar();
42     *x=0;return 1;
43 }
44 /** keep hungry and keep calm! **/
45  
46  
47 double a[maxn],Cage[maxn][2];
48 int cot[maxn];
49 int Sheryang(){
50      
51     //freopen("input.txt","r",stdin);
52     //freopen("out1.txt","w",stdout);
53      
54     int n;read(n);
55     for(int i=1;i<=n;i++){
56         read(a[i]);
57     }
58      
59     double Max = a[1],Min = a[1];
60     for(int i=2;i<=n;i++){
61         gmax(Max,a[i]);
62         gmin(Min,a[i]);
63     }
64     double gap = (Max - Min) / (n-1);
65      
66     if(gap == 0){
67         printf("0.0\n");
68         return 0;
69     }
70      
71     for(int i=1;i<=n;i++){
72         Cage[i][0] = 1e18;
73         Cage[i][1] = -1e18;
74     }
75      
76     for(int i=1;i<=n;i++){
77         int pos = (int)((a[i] - Min)/gap) + 1;
78         cot[pos] ++;
79         gmin(Cage[pos][0],a[i]);
80         gmax(Cage[pos][1],a[i]);
81     }
82      
83     double last = Min, ans = 0;
84     for(int i=1;i<=n;i++){
85         if(cot[i]){
86             double tmp = Cage[i][0] - last;
87             if(tmp > ans){
88                 ans = tmp;
89             }
90             last = Cage[i][1];
91         }
92     }
93     if(Max - last > ans){
94         ans = Max - last;
95     }
96      
97     printf("%.1f\n",ans);
98     return 0;
99 }
View Code

 

 

  

Guess you like

Origin www.cnblogs.com/Osea/p/11456171.html