qmh Test 1

Title: Portal

 

 

First, input a n, then the number of inputs n a (1 <= a <= 1e7), after this sort number n, all you need to find a continuous length thereof. These discrete lengths after sorting output

 

Entry

 

Input:

8

1 5 2 7 4 5 7 1

 

Export

 

Output:

1 2 2

Sample Analysis:

The number was above Sort:

1 1 2 4 5 5 7 7

Go after re-:

1 2 4 5 7

Continuous length:

2 2 1

Because less between 124 to a connector 3, so to disconnect between 4 and 12. And because less between 45 7 and a connector 6, so that between 5 and 7 to disconnect linear

Sort results:

1 2 2

 

answer:

Because the maximum n Yes 1e7, and the program must get an answer within 1s. So here we certainly have to use the bucket sort (n) complexity O

For: 15,274,571 this set of data

If the bucket then sorted array to open to at least v [9], since the data used for sorting the barrel itself when the array subscript v

    v Array: 0123456789 // array subscript

Deal with what v array: 0110110100 // where 1 represents the index appears in the data input before, there have been 0 for no. Example, v [2] = 1, it means that 2 data number input in our

Code:

#include <stdio.h>
#include<string.h>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e7+5;
int v[maxn],w[maxn],p[maxn];
int main()
{
        int n,a,maxx=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a);
        v[a]=1;
        maxx=max(maxx,a);
    }
}

 

After we found so treated to complete the re-ordering and

for(int i=1;i<=maxx;++i)
    {
        if(v[i]>0)
        {
            printf("%d ",i);
        }
    }
    printf("\n");   

You can use this code to print out an array v the result will be 12457

 

Then we ask it of continuous length, this time on the new definition of an array w and p

int ANS = 0 , I;
     for (I = . 1 ; I <= Maxx; ++ I) {
         IF (V [I]) { 
            W [I] = W [I- . 1 ] + . 1 ; 
        } 
        the else { 
            P [ W [I - . 1 ]] ++ ; 
            ANS = max (ANS, W [I- . 1 ]);   // we 
        } 
    } 
 subscript: 0  . 1  2  . 3  . 4  . 5  . 6  . 7  . 8  . 9 
V in the values: 0  . 1  1  0  1 . 1  0  . 1  0  0  
W Initial value: 0  0  0  0  0  0  0  0  0  0 
: W after treatment 0  . 1  2  0  . 1  2  0  . 1  0  0 
put W [ . 1 , . 7 until all 0 in] the subscripts values are kept up, because it is that a continuous length
 2  2  1   it is not a continuous length 1 0 before this, we have to take it out of 
the first 2 { 1 , 2 two numbers} these two numbers a continuous length. 
2 is a second { 4 , 5 } lengths of the two consecutive numbers 
third number 1 is { 7 } This is a continuous length number

 

In this case, we'll get 221 this result, but we have to sort it and then output. Here again the sort used casks

But I've put them inside the barrel p

int ANS = 0 , I;
     for (I = . 1 ; I <= Maxx; ++ I) {
         IF (V [I]) { 
            W [I] = W [I- . 1 ] + . 1 ; 
        } 
        the else {   // else break occurred is 0 
            P [W [I- . 1 ]] ++;   // this number with the bucket kept up 
            ANS = max (ANS, W [I- . 1 ]);   // find the right end of the barrel point, so that the next time we go to the output of the enumeration 
        }   // because the sort is barrel this number as a subscript, so finding the right point just depends on your need to use the maximum index is number 
    }

Index: 01234

p Initial value: 00,000 

After treatment p: 0 1 2 0 0 

 

After it is printed out on it, pay attention to the format:

P [W [I- 1 ]] ++ ;   
ANS = max (ANS, W [I- 1 ]);   // not add two lines of code, then the sample 1, it will not put the barrel p is 
    
    for ( int I = . 1 ; I <= ANS; ++ I) {
         the while (p [I]) 
        { 
            IF (I == ANS && p [I] == . 1 ) 
                the printf ( " % D \ n- " , I);
             the else  
                the printf ( " % D " , I); 
            P [I] - ; 
        } 
    }
 

 

Total code: Submit with c ++ format

 1 #include <stdio.h>
 2 #include<string.h>
 3 #include <iostream>
 4 #define INF 0x3f3f3f3f
 5 using namespace std;
 6 const int maxn=1e7+5;
 7 int v[maxn],w[maxn],p[maxn];
 8 int main(){
 9     //while(1){
10     //    memset(w,0,sizeof(w));
11     //    memset(v,0,sizeof(v));
12     //    memset(p,0,sizeof(p));
13     int n,a,maxx=0;
14     scanf("%d",&n);
15     for(int i=1;i<=n;++i)
16     {
17         scanf("%d",&a);
18         v[a]=1;
19         maxx=max(maxx,a);
20     }
21     int ans=0,i;
22     for(i=1;i<=maxx;++i){
23         if(v[i]){
24             w[i]=w[i-1]+1;
25         }
26         else {
27             //printf("%d**\n",w[i-1]);
28             p[w[i-1]]++;
29             ans=max(ans,w[i-1]);
30         }
31     }
32     p[w[i-1]]++;
33     ans=max(ans,w[i-1]);
34     
35     for(int i=1;i<=ans;++i){
36         while(p[i])
37         {
38             if(i==ans && p[i]==1)
39                 printf("%d\n",i);
40             else 
41                 printf("%d ",i);
42             p[i]--;
43         }
44     }
45 //    }
46     
47     return 0;
48 } 
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11628424.html