Advertisement
audreych

12883 - Patrick and Spongebob in SAO

Dec 23rd, 2020 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int cnt[1000] = {0};
  5. int arr[1000] = {0};
  6.  
  7. int main(){
  8.     int n;
  9.     scanf("%d", &n);
  10.     for(int i = 1; i <= n; i++){
  11.         scanf("%d", &arr[i]);
  12.     }
  13.     int q;
  14.     scanf("%d", &q);
  15.     for(int m = 1; m <= q; m++){
  16.         int l, r;
  17.         int maxCnt = 0;
  18.         int best = 0;
  19.         int value = 100000;
  20.         scanf("%d %d", &l, &r);
  21.         for(int i = l; i <= r; i++){
  22.             cnt[arr[i]]++;
  23.             if(maxCnt < cnt[arr[i]]){
  24.                 maxCnt = cnt[arr[i]];
  25.                 value = arr[i];
  26.             } else if(cnt[arr[i]] == maxCnt) {
  27.                 if(arr[i] < value) value = arr[i];
  28.             }
  29.         }
  30.         printf("%lld\n", value);
  31.         memset(cnt, 0, sizeof(cnt)); //reset the value of the array cnt to 0
  32.     }
  33.     return 0;
  34. }
  35. //notes
  36. // cnt : a new array that counts the total amount of elements scanned
  37. // 1 1 3
  38. //e.g cnt[arr[1]] = cnt[1]++ = 0 + 1;
  39. //    cnt[arr[2]] = cnt[1]++ = 1 + 1;
  40. //update the max amount of count everytime to maxCnt
  41. //this is to compare the maxCnt right now with the other elements maxCnt
  42. //if the cnt of element atm is still < maxCnt that means it does nothing and use previous value
  43. //update value everytime maxCnt is updated
  44. //else if turns out the cnt[arr[i]] during the scanning are the same as the maxCnt
  45. //always take the smaller value (according to question)
  46. //e.g value  = 3 and arr[i] = 2, then value will become 2 if they have the same amount of cnt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement