Advertisement
Korotkodul

A_v3

Mar 29th, 2023
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #define pii pair <int, int>
  6. using namespace std;
  7.  
  8. bool sh = 0;
  9.  
  10. int dif(int x) {
  11.     string s = to_string(x);
  12.     int minim = 10, maxim = -1;
  13.     for (char l: s) {
  14.         int k = (int)l - (int)'0';
  15.         if (k < minim) minim = k;
  16.         if (k > maxim) maxim = k;
  17.     }
  18.     return maxim - minim;
  19. }
  20.  
  21. int seek(int l, int r) {
  22.     pii bst = {-1, -1};
  23.     for (int i = l; i <= r; ++i) {
  24.         int d = dif(i);
  25.         if (d > bst.first) {
  26.             bst = {d, i};
  27.         }
  28.     }
  29.     return bst.second;
  30. }
  31.  
  32. int get(int x) {
  33.     string s = to_string(x);
  34.  
  35.     while (1) {
  36.         if (sh) {
  37.             cout << "x = " << x << "\n";
  38.         }
  39.         s = to_string(x);
  40.         int minim=10, maxim=-1;
  41.         for (char l: s) {
  42.             int k = (int)l - (int)'0';
  43.  
  44.             if (k < minim) minim = k;
  45.             if (k > maxim) maxim = k;
  46.         }
  47.         if (sh) {
  48.             //cout << "k = " << k <<"\n";
  49.             cout << "minim maxim = " << minim << ' ' << maxim << "\n";
  50.         }
  51.         if (sh) {
  52.             cout << "\n";
  53.         }
  54.         if (maxim == 9 && minim == 0) {
  55.             return x;
  56.         }
  57.         x++;
  58.     }
  59. }
  60.  
  61. int main()
  62. {
  63.     ios::sync_with_stdio(0);
  64.     cin.tie(0);
  65.     cout.tie(0);
  66.  
  67.     int t=1;
  68.     cin >> t;
  69.     for (int go = 0; go < t; ++go) {
  70.         int l, r; cin >> l >> r;
  71.         if (sh) {
  72.             cout << "l r = " << l << ' ' << r << "\n";
  73.         }
  74.         int ans = -1;
  75.         ans = get(l);
  76.         if (sh) {
  77.             cout << "ans = " << ans << "\n";
  78.         }
  79.         if (ans > r) {
  80.             ans = seek(l, r);
  81.             if (sh) {
  82.                 cout << "delta ans = " << ans << "\n";
  83.             }
  84.         }
  85.         cout << ans << "\n";
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement