Advertisement
StoneHaos

simon

Nov 13th, 2021
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <exception>
  4. #include <limits.h>
  5. #include <algorithm>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. const int MOD = 100;
  13.  
  14. class sdmax {
  15.  
  16. private:
  17.     int counte;
  18.     int * elements;
  19.     int countp;
  20.     int * premax;
  21.     int cnt;
  22.  
  23. public:
  24.     sdmax(int count) {
  25.         if (count < 0) throw exception();
  26.         counte = count;
  27.         countp = (int)sqrt((double)count);
  28.         elements = new int[counte];
  29.         premax = new int[countp];
  30.         for (int i = 0; i < counte; ++ i)
  31.             elements[i] = 0;
  32.         for (int i = 0; i < countp; ++ i)
  33.             premax[i] = 0;
  34.         cnt = round(1.0 * counte / countp);
  35.     }
  36.  
  37.     int size() const {
  38.         return counte;
  39.     }
  40.  
  41.     const int& operator[](const int index) const {
  42.         if (index < 0 || index >= counte) throw exception();
  43.         return elements[index];
  44.     }
  45.  
  46.     void set(int index, int value) {
  47.         if (index < 0 || index >= counte) throw exception();
  48.         elements[index] = value;
  49.         if (elements[index] > premax[min(index / cnt, countp - 1)])
  50.             premax[min(index / cnt, countp - 1)] = elements[index];
  51.     }
  52.  
  53.     int max() {
  54.         return *max_element(premax, premax + countp);
  55.     }
  56.  
  57.     ~sdmax() {
  58.         delete[] elements;
  59.         delete[] premax;
  60.     }
  61. };
  62.  
  63. void print(const sdmax &a) {
  64.     for (int i = 0; i < a.size(); ++ i) {
  65.         printf("%d ", a[i]);
  66.     }
  67.     printf("\n");
  68. }
  69.  
  70. int main(void) {
  71.     time_t t = time(NULL);
  72.     printf("time = %lld\n", t);
  73.     srand(t);
  74.  
  75.     printf("Input size>");
  76.     int n;
  77.     scanf("%d", &n);
  78.     sdmax a = sdmax(n);
  79.     for (int i = 0; i < n; ++ i) {
  80.         a.set(i, rand() % MOD);
  81.     }
  82.     print(a);
  83.     printf("max = %d\n", a.max());
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement