Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #define pii pair <int, int>
- using namespace std;
- bool sh = 0;
- int dif(int x) {
- string s = to_string(x);
- int minim = 10, maxim = -1;
- for (char l: s) {
- int k = (int)l - (int)'0';
- if (k < minim) minim = k;
- if (k > maxim) maxim = k;
- }
- return maxim - minim;
- }
- int seek(int l, int r) {
- pii bst = {-1, -1};
- for (int i = l; i <= r; ++i) {
- int d = dif(i);
- if (d > bst.first) {
- bst = {d, i};
- }
- }
- return bst.second;
- }
- int get(int x) {
- string s = to_string(x);
- while (1) {
- if (sh) {
- cout << "x = " << x << "\n";
- }
- s = to_string(x);
- int minim=10, maxim=-1;
- for (char l: s) {
- int k = (int)l - (int)'0';
- if (k < minim) minim = k;
- if (k > maxim) maxim = k;
- }
- if (sh) {
- //cout << "k = " << k <<"\n";
- cout << "minim maxim = " << minim << ' ' << maxim << "\n";
- }
- if (sh) {
- cout << "\n";
- }
- if (maxim == 9 && minim == 0) {
- return x;
- }
- x++;
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t=1;
- cin >> t;
- for (int go = 0; go < t; ++go) {
- int l, r; cin >> l >> r;
- if (sh) {
- cout << "l r = " << l << ' ' << r << "\n";
- }
- int ans = -1;
- ans = get(l);
- if (sh) {
- cout << "ans = " << ans << "\n";
- }
- if (ans > r) {
- ans = seek(l, r);
- if (sh) {
- cout << "delta ans = " << ans << "\n";
- }
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement