Simple hash on hrbust1164, 1287 _____ hrbust

Source: Internet
Author: User

Simple hash on hrbust1164, 1287 _____ hrbust
Hrbust1164, 1287 _____ simple hash of hrbust1164Description on hrbust

A computer randomly generates N random integers (N ≤ 910305) between 0 and 910305 (including 0 and 100000000). Only one repeated number is retained, remove other identical numbers. Then sort these numbers in ascending order.

Please complete "de-duplication" and "sorting.

Input

There are two rows in the input, and 1st act as a positive integer, indicating the number of random numbers generated:

N

The first row contains N positive integers separated by spaces, which are the random numbers generated.

Output

The output is also two rows. 1st acts as a positive integer M, indicating the number of random numbers that are not the same. 2nd act M positive integers separated by spaces. They are random numbers in ascending order.

Sample
Sample Input
1020 40 32 67 40 20 89 300 400 15Sample Output815 20 32 40 67 89 300 400
Analysis:
    A very simple question: AC code:
    //author: svtter//#include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          #include #include 
           
            #include 
            
             #define INF 0xffffff#define lln long long#ifdef ONLINE_JUDGE#define FOI(file) 0#define FOW(file) 0#else#define FOI(file) freopen(file,"r",stdin);#define FOW(file) freopen(file,"w",stdout);#endifusing namespace std;#define N 910305 bool num[N];int main(){ //FOI("input"); //FOW("output"); //write your programme here int i, n; int t, sum; int c; while(~scanf("%d", &n)) { memset(num, 0, sizeof(num)); sum = 0; for(i = 0; i < n; i++) { scanf("%d", &t); if(num[t] == 0) { sum++; num[t] = 1; } } printf("%d\n", sum); c = 0; for(i = 0; i < N; i++) { if(num[i]) { if(c != sum-1) { printf("%d ",i); c++; } else { printf("%d\n", i); break; } } } } return 0;}
            
           
         
        
       
      


    Hrbust1287Description

    A computer randomly generates N random integers (N ≤ 1000000000) between 0 and 1000000000 (including 0 and 5000000). Only one repeated number is retained, remove other identical numbers. Then sort these numbers in ascending order. Please complete "de-duplication" and "sorting"

    Input

    There are two rows in the input, and 1st act as one positive integer, indicating the number of random numbers generated: N 2nd rows have N positive integers separated by spaces, which are the random numbers generated.

    Output

    The output is also two rows. 1st acts as a positive integer M, indicating the number of random numbers that are not the same. 2nd act M positive integers separated by spaces. They are random numbers in ascending order.

    Sample
    Sample Input1020 40 32 67 40 20 89 300 400 15Sample Output815 20 32 40 67 89 300 400
    Algorithm analysis:

    This question was directly fixed without thinking about it at the beginning, and it was refreshing. Then I began to write the zipper method and thought of a new algorithm, which is basically equivalent to a brute force solution:

    It is to open two arrays, with a sorted order, and duplicate ones will not be placed in it.

    AC code:

    //author: svtter//#include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          #include #include 
           
            #include 
            
             #define INF 0xffffff#define lln long long#ifdef ONLINE_JUDGE#define FOI(file) 0#define FOW(file) 0#else#define FOI(file) freopen(file,"r",stdin);#define FOW(file) freopen(file,"w",stdout);#endifusing namespace std;#define N 5000003int num[N];int num2[N];int main(){ //FOI("input"); //FOW("output"); //write your programme here int i, j, n; int sum; while(~scanf("%d", &n)) { memset(num2, -1, sizeof(num2)); sum = n; for(i = 0; i < n; i++) { scanf("%d", &num[i]); } sort(num, num+n); num2[0] = num[0]; j = 0; for(i = 1 ; i < n; i++) { if(num[i] != num2[j]) num2[++j] = num[i]; else sum--; } printf("%d\n", sum); for(i = 0; i < sum-1; i++) printf("%d ", num2[i]); printf("%d\n", num2[sum-1]); } return 0;}
            
           
         
        
       
      


    Refreshing AC:

    But obviously, the time space is too large. I have seen a zipper method through the next array before, so I write it like this .. However, all kinds of egresses need to be checked and deleted constantly, and the consumption of Space reaches an astonishing 59000 K =, rather than just a few times, giving up directly.

    Later, I found that the best solution was to use pointers to write but not to dynamically open up new spaces (I can only see them today ):

    //author: svtter//#include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          #include #include 
           
            #include 
            
             #define INF 0xffffff#define lln long long#ifdef ONLINE_JUDGE#define FOI(file) 0#define FOW(file) 0#else#define FOI(file) freopen(file,"r",stdin);#define FOW(file) freopen(file,"w",stdout);#endifusing namespace std;const int MP = 1007;struct Node{ int d; Node *next;};Node *pnd[MP+1];Node nd[MP+1];int n_cnt;int a_cnt;int a[MP+10];int main(){ //FOI("input"); //FOW("output"); //write your programme here int i, d, n; int p; Node *pt; bool found; while(~scanf("%d", &n)) { memset(pnd, 0, sizeof(pnd)); n_cnt = 0; a_cnt = 0; for(i = 0; i < n; i++) { scanf("%d", &d); p = d % MP; found = false; pt = pnd[p]; while(pt) { if(pt->d == d) { found = 1; break; } pt = pt->next; } if(!found) { nd[n_cnt].d = d; nd[n_cnt].next = pnd[p]; pnd[p] = &nd[n_cnt]; n_cnt++; a[a_cnt++] = d; } } sort(a ,a+a_cnt); printf("%d\n", a_cnt); for(i = 0; i < a_cnt-1; i++) printf("%d ", a[i]); printf("%d\n", a[a_cnt-1]); } return 0;}
            
           
         
        
       
      


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.