QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#704145#163. Change a PasswordTheZoneAC ✓1ms3892kbC++204.6kb2024-11-02 19:24:172024-11-02 19:24:18

Judging History

This is the latest submission verdict.

  • [2024-11-02 19:24:18]
  • Judged
  • Verdict: AC
  • Time: 1ms
  • Memory: 3892kb
  • [2024-11-02 19:24:17]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

typedef int int2;
#define int long long
#define pi pair<int, int>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
const int inf = 1e18;

int t;

const int maxn = 1e5 + 5;
vector<int> divs[maxn];
int dp[maxn];

#define EPS 1e-4

void gg() {
    cout << "NO\n";
    exit(0);
}

typedef double D;

D dis(D a, D b, D c, D d) {
    D dx = c - a;
    D dy = d - b;
    return sqrt(dx * dx + dy * dy);
}

bool check(string A, string B, string S) {
    // start with A first
    int n = S.size();
    int ia = 0;
    int ib = 0;
    bool possible = true;
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0) {
            while (ia < (int)A.size() and A[ia] != S[i]) {
                ia++;
            }
            if (ia == (int)A.size()) possible = false;
            ia++;
        } else {
            while (ib < (int)B.size() and B[ib] != S[i]) {
                ib++;
            }
            if (ib == (int)B.size()) possible = false;
            ib++;
        }
    }
    return possible;
}
vector<int> ten_pows;
int n, val;
string S;
string res;
int ans = 0;

void go(string T) {
    vector<bool> taken(10);
    for (int i = 0; i < n; i++) {
        if (taken[T[i]-'0']) return;
        taken[T[i]-'0'] = true;
    }
    int x = stoll(T);
    int cand = abs(x - val);
    cand = min(cand, ten_pows.back() * 10LL - cand);
    // cout << "x: " << x << ", cand: " << cand << "\n";
    if (cand > ans) {
        ans = cand;
        res = T;
    } else if (cand == ans) {
        res = min(res, T);
    }
}

void inc(string T, int i, int dist) {
    vector<bool> taken(10);
    for (int j = 0; j < i; j++) {
        if (taken[T[j]-'0']) return;
        taken[T[j] - '0'] = true;
    }
    T[i]+=dist;
    if (taken[T[i]-'0']) {
        return;
    }
    taken[T[i]-'0'] = true;
    int cur = 0;
    for (int j = i + 1; j < n; j++) {
        while (taken[cur]) cur++;
        T[j] = (char)('0' + cur);
        taken[cur] = true;
    }
    go(T);
}

void dec(string T, int i, int dist) {
    vector<bool> taken(10);
    for (int j = 0; j < i; j++) {
        if (taken[T[j]-'0']) return;
        taken[T[j] - '0'] = true;
    }
    T[i]-=dist;
    if (taken[T[i]-'0']) {
        return;
    }
    taken[T[i]-'0'] = true;
    int cur = 9;
    for (int j = i + 1; j < n; j++) {
        while (taken[cur]) cur--;
        T[j] = (char)('0' + cur);
        taken[cur] = true;
    }
    go(T);
}

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> S;
    n = S.size();
    val = stoll(S);
    ten_pows.pb(1);
    for (int i = 1; i <= n-1; i++) {
        int val = ten_pows.back() * 10LL;
        ten_pows.pb(val);
    }
    int target = (val + 5LL * ten_pows.back()) % (10LL * ten_pows.back());
    // cout << "target: " << target << "\n";
    string T = to_string(target);
    while ((int)T.size() < n) T = '0' + T;
    string large, small;
    for (int i = 0; i < n; i++) {
        large += (char)('9' - i);
        small += (char)('0' + i);
    }
    go(large);
    go(small);
    go(T);
    for (int i = 0; i < n; i++) {
        if (T[i] >= '1') {
            int rem = T[i] - '0';
            for (int j = 1; j <= rem; j++) dec(T, i, j);
        }
        if (T[i] <= '8') {
            int rem = '9' - T[i];
            for (int j = 1; j <= rem; j++) inc(T, i, j);
        }
    }
    cout << res << "\n";
} 



























































































































































































































































































































































































































































































































































Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3648kb

input:

201

output:

701

result:

ok single line: '701'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3644kb

input:

512

output:

012

result:

ok single line: '012'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

99999

output:

49876

result:

ok single line: '49876'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

765876346

output:

265874931

result:

ok single line: '265874931'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

15526126

output:

65498732

result:

ok single line: '65498732'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3536kb

input:

596140804

output:

096142357

result:

ok single line: '096142357'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

12159688

output:

62159703

result:

ok single line: '62159703'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3656kb

input:

259516

output:

759486

result:

ok single line: '759486'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

646368

output:

146370

result:

ok single line: '146370'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3876kb

input:

901017

output:

401235

result:

ok single line: '401235'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3848kb

input:

390529

output:

890527

result:

ok single line: '890527'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3892kb

input:

5964700

output:

0964712

result:

ok single line: '0964712'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

32352460

output:

82351976

result:

ok single line: '82351976'

Test #14:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

4903140382

output:

9876543210

result:

ok single line: '9876543210'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3684kb

input:

23

output:

73

result:

ok single line: '73'

Test #16:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

91

output:

41

result:

ok single line: '41'

Test #17:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

713125

output:

213098

result:

ok single line: '213098'

Test #18:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

521131794

output:

021345678

result:

ok single line: '021345678'

Test #19:

score: 0
Accepted
time: 1ms
memory: 3592kb

input:

974

output:

473

result:

ok single line: '473'

Test #20:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

6941738

output:

1942035

result:

ok single line: '1942035'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

41

output:

91

result:

ok single line: '91'

Test #22:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

56124627

output:

06124598

result:

ok single line: '06124598'

Test #23:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

1488

output:

6487

result:

ok single line: '6487'

Test #24:

score: 0
Accepted
time: 1ms
memory: 3684kb

input:

197136

output:

697135

result:

ok single line: '697135'

Test #25:

score: 0
Accepted
time: 1ms
memory: 3848kb

input:

60

output:

10

result:

ok single line: '10'

Test #26:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

2026

output:

7026

result:

ok single line: '7026'

Test #27:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

9523001179

output:

4523016789

result:

ok single line: '4523016789'

Test #28:

score: 0
Accepted
time: 1ms
memory: 3548kb

input:

7927

output:

2930

result:

ok single line: '2930'

Test #29:

score: 0
Accepted
time: 1ms
memory: 3844kb

input:

89

output:

39

result:

ok single line: '39'

Test #30:

score: 0
Accepted
time: 1ms
memory: 3876kb

input:

39248928

output:

89250134

result:

ok single line: '89250134'

Test #31:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

9448588101

output:

4398765210

result:

ok single line: '4398765210'

Test #32:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

9

output:

4

result:

ok single line: '4'

Test #33:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

68652768

output:

18652749

result:

ok single line: '18652749'

Test #34:

score: 0
Accepted
time: 1ms
memory: 3640kb

input:

8958581316

output:

3958601247

result:

ok single line: '3958601247'

Test #35:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

1757768

output:

6758012

result:

ok single line: '6758012'

Test #36:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

5171863951

output:

0172345689

result:

ok single line: '0172345689'

Test #37:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

86283

output:

36284

result:

ok single line: '36284'

Test #38:

score: 0
Accepted
time: 1ms
memory: 3656kb

input:

054

output:

549

result:

ok single line: '549'

Test #39:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

10134233

output:

60134257

result:

ok single line: '60134257'

Test #40:

score: 0
Accepted
time: 1ms
memory: 3580kb

input:

746

output:

246

result:

ok single line: '246'

Test #41:

score: 0
Accepted
time: 1ms
memory: 3636kb

input:

6599320

output:

1598764

result:

ok single line: '1598764'

Test #42:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

65071

output:

15072

result:

ok single line: '15072'

Test #43:

score: 0
Accepted
time: 1ms
memory: 3580kb

input:

63

output:

13

result:

ok single line: '13'

Test #44:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

1920905

output:

6920875

result:

ok single line: '6920875'

Test #45:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

3177662539

output:

8176954320

result:

ok single line: '8176954320'

Test #46:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

7848458

output:

2847965

result:

ok single line: '2847965'

Test #47:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

072

output:

572

result:

ok single line: '572'

Test #48:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

3484901367

output:

8490123567

result:

ok single line: '8490123567'

Test #49:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

7215285

output:

2198765

result:

ok single line: '2198765'

Test #50:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

36323

output:

86324

result:

ok single line: '86324'

Test #51:

score: 0
Accepted
time: 1ms
memory: 3852kb

input:

5861875

output:

0861923

result:

ok single line: '0861923'

Test #52:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

488771862

output:

987654321

result:

ok single line: '987654321'

Test #53:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

1348

output:

6348

result:

ok single line: '6348'

Test #54:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

76327

output:

26319

result:

ok single line: '26319'

Test #55:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

9999999999

output:

4987653210

result:

ok single line: '4987653210'

Test #56:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

0000000000

output:

5012346789

result:

ok single line: '5012346789'

Test #57:

score: 0
Accepted
time: 1ms
memory: 3852kb

input:

1111111111

output:

6109875432

result:

ok single line: '6109875432'

Test #58:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

1115110610

output:

6109875432

result:

ok single line: '6109875432'

Test #59:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

1115110611

output:

6120345789

result:

ok single line: '6120345789'

Test #60:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

1109875427

output:

6109875423

result:

ok single line: '6109875423'

Test #61:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

1109875428

output:

6109875432

result:

ok single line: '6109875432'