QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#34570 | #163. Change a Password | arvindr9 | RE | 4ms | 6720kb | C++ | 3.7kb | 2022-06-11 05:24:07 | 2022-06-11 05:24:08 |
Judging History
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 = stoi(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 = stoi(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";
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5900kb
input:
201
output:
701
result:
ok single line: '701'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5948kb
input:
512
output:
012
result:
ok single line: '012'
Test #3:
score: 0
Accepted
time: 1ms
memory: 6032kb
input:
99999
output:
49876
result:
ok single line: '49876'
Test #4:
score: 0
Accepted
time: 3ms
memory: 6028kb
input:
765876346
output:
265874931
result:
ok single line: '265874931'
Test #5:
score: 0
Accepted
time: 1ms
memory: 6412kb
input:
15526126
output:
65498732
result:
ok single line: '65498732'
Test #6:
score: 0
Accepted
time: 3ms
memory: 6720kb
input:
596140804
output:
096142357
result:
ok single line: '096142357'
Test #7:
score: 0
Accepted
time: 3ms
memory: 5972kb
input:
12159688
output:
62159703
result:
ok single line: '62159703'
Test #8:
score: 0
Accepted
time: 3ms
memory: 5892kb
input:
259516
output:
759486
result:
ok single line: '759486'
Test #9:
score: 0
Accepted
time: 0ms
memory: 5896kb
input:
646368
output:
146370
result:
ok single line: '146370'
Test #10:
score: 0
Accepted
time: 4ms
memory: 5896kb
input:
901017
output:
401235
result:
ok single line: '401235'
Test #11:
score: 0
Accepted
time: 4ms
memory: 6040kb
input:
390529
output:
890527
result:
ok single line: '890527'
Test #12:
score: 0
Accepted
time: 1ms
memory: 6424kb
input:
5964700
output:
0964712
result:
ok single line: '0964712'
Test #13:
score: 0
Accepted
time: 2ms
memory: 5904kb
input:
32352460
output:
82351976
result:
ok single line: '82351976'
Test #14:
score: -100
Dangerous Syscalls
input:
4903140382