QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#752295 | #8107. Permutation | jeroenodb# | AC ✓ | 854ms | 80416kb | C++20 | 3.2kb | 2024-11-16 00:02:55 | 2024-11-16 00:02:56 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
template<typename T> struct fenwick {
int n;
vector<T> fen;
fenwick(){}
fenwick(int nn) {
fen.resize(nn+1);
n = nn;
}
auto sum(int i) {
T ans = 0;
while(i) {
ans+=fen[i];
i&=i-1;
}
return ans;
}
auto query(int l, int r) {
return sum(r+1)-sum(l);
}
void update(int i, T val) {
++i;
while(i<=n) {
fen[i]+=val;
i+= i&(-i);
}
}
int lower_bound(int x) {
int at=0;
for(int pw=1<<20;pw>=1;pw/=2) {
if(at + pw<=n and fen[at+pw]<x) {
x-=fen[at+pw];
at+=pw;
}
}
return at;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; ll k; cin >> n >> k;
map<pair<int,ll>,ll> dp;
int iter=0;
auto rec = [&](auto&& self, int n, ll inv) -> ll {
iter++;
if(n==0) {
return inv==0;
}
ll maxinv = (ll)(n-1)*(n-2)/2;
ll need = max(0LL,inv-maxinv);
if(need>n-1) return 0;
if(dp.count({n,inv})) return dp[{n,inv}];
ll& res = dp[{n,inv}];
for(int take=need;take<=n-1;++take) {
res+=self(self,n-1,inv-take);
if(res>=k) {
res=min(res,k);
return res;
}
}
return res;
};
vi invs;
auto rec2 = [&](auto&& self, int n, ll inv) -> void {
iter++;
if(n==0) {
return;
}
ll maxinv = (ll)(n-1)*(n-2)/2;
ll need = max(0LL,inv-maxinv);
if(need>n-1) return;
for(int take=need;take<=n-1;++take) {
ll tmp = rec(rec,n-1,inv-take);
if(tmp>=k) {
invs.push_back(take);
self(self,n-1,inv-take);
return;
}
k-=tmp;
}
};
auto total = ll(n)*(n-1)/2;
ll half = total/2;
if(half*2==total) {
rec2(rec2,n,half);
cerr << iter << '\n';
if(invs.size()==n) {
cout << "YES\n";
fenwick<int> fen(n);
for(int i=0;i<n;++i) fen.update(i,1);
for(auto i : invs) {
int x = fen.lower_bound(i+1); //
cout << x+1 << ' ';
fen.update(x,-1);
}
cout << '\n';
exit(0);
}
}
cout << "NO\n";
}
/*
250000 1000000000000000000
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
4 3
output:
YES 2 4 1 3
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
4 57
output:
NO
result:
ok single line: 'NO'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
1 1
output:
YES 1
result:
ok 2 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
1 2
output:
NO
result:
ok single line: 'NO'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
1 42
output:
NO
result:
ok single line: 'NO'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
4 1
output:
YES 1 4 3 2
result:
ok 2 lines
Test #7:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
4 6
output:
YES 4 1 2 3
result:
ok 2 lines
Test #8:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
4 7
output:
NO
result:
ok single line: 'NO'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
5 13
output:
YES 3 4 2 1 5
result:
ok 2 lines
Test #10:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
5 21
output:
YES 5 1 3 2 4
result:
ok 2 lines
Test #11:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
5 22
output:
YES 5 2 1 3 4
result:
ok 2 lines
Test #12:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
5 23
output:
NO
result:
ok single line: 'NO'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
8 3836
output:
YES 8 7 2 1 3 4 5 6
result:
ok 2 lines
Test #14:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
8 3837
output:
NO
result:
ok single line: 'NO'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
9 29228
output:
YES 9 8 4 1 2 3 5 6 7
result:
ok 2 lines
Test #16:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
9 29229
output:
NO
result:
ok single line: 'NO'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
9 32768
output:
NO
result:
ok single line: 'NO'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
2 1
output:
NO
result:
ok single line: 'NO'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
7 1
output:
NO
result:
ok single line: 'NO'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
13 296643390
output:
YES 13 12 11 7 1 2 3 4 5 6 8 9 10
result:
ok 2 lines
Test #21:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
13 296643391
output:
NO
result:
ok single line: 'NO'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3888kb
input:
20 62119523114983223
output:
YES 20 19 18 17 16 10 2 1 3 4 5 6 7 8 9 11 12 13 14 15
result:
ok 2 lines
Test #23:
score: 0
Accepted
time: 1ms
memory: 3732kb
input:
20 62119523114983224
output:
YES 20 19 18 17 16 11 1 2 3 4 5 6 7 8 9 10 12 13 14 15
result:
ok 2 lines
Test #24:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
20 62119523114983225
output:
NO
result:
ok single line: 'NO'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
21 1
output:
YES 1 2 3 4 5 6 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
result:
ok 2 lines
Test #26:
score: 0
Accepted
time: 1ms
memory: 3708kb
input:
21 1000000000000000000
output:
YES 17 21 14 6 9 4 3 11 7 2 20 1 18 15 12 16 5 10 8 13 19
result:
ok 2 lines
Test #27:
score: 0
Accepted
time: 1ms
memory: 3848kb
input:
44 333333333333333333
output:
YES 1 2 3 4 5 6 7 8 9 10 11 21 42 44 41 40 43 38 39 35 34 37 36 32 31 33 29 27 28 25 30 26 23 24 15 22 18 16 20 19 17 12 13 14
result:
ok 2 lines
Test #28:
score: 0
Accepted
time: 2ms
memory: 3920kb
input:
49 600000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 37 47 49 44 46 45 48 43 42 41 39 40 35 38 33 31 36 34 30 27 32 25 28 29 26 24 23 17 22 20 19 21 18 16 15 14
result:
ok 2 lines
Test #29:
score: 0
Accepted
time: 0ms
memory: 3912kb
input:
16 738680521142
output:
YES 16 15 14 13 7 1 2 3 4 5 6 8 9 10 11 12
result:
ok 2 lines
Test #30:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
16 738680521143
output:
NO
result:
ok single line: 'NO'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
350 702274833889168257
output:
NO
result:
ok single line: 'NO'
Test #32:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
347 823694565238057857
output:
NO
result:
ok single line: 'NO'
Test #33:
score: 0
Accepted
time: 3ms
memory: 3764kb
input:
348 548514836018174081
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #34:
score: 0
Accepted
time: 3ms
memory: 3804kb
input:
349 1000000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #35:
score: 0
Accepted
time: 1ms
memory: 3656kb
input:
17 11501573822788
output:
YES 17 16 15 14 11 1 2 3 4 5 6 7 8 9 10 12 13
result:
ok 2 lines
Test #36:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
17 11501573822789
output:
NO
result:
ok single line: 'NO'
Test #37:
score: 0
Accepted
time: 7ms
memory: 4244kb
input:
1536 1000000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #38:
score: 0
Accepted
time: 1ms
memory: 3864kb
input:
1533 1
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #39:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
1534 639944301316494081
output:
NO
result:
ok single line: 'NO'
Test #40:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
1535 133749702370803553
output:
NO
result:
ok single line: 'NO'
Test #41:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
12 25598186
output:
YES 12 11 10 4 1 2 3 5 6 7 8 9
result:
ok 2 lines
Test #42:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
12 25598187
output:
NO
result:
ok single line: 'NO'
Test #43:
score: 0
Accepted
time: 2ms
memory: 4528kb
input:
5000 1
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #44:
score: 0
Accepted
time: 16ms
memory: 5580kb
input:
4997 577621398254762881
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #45:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
4998 1000000000000000000
output:
NO
result:
ok single line: 'NO'
Test #46:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
4999 1000000000000000000
output:
NO
result:
ok single line: 'NO'
Test #47:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
8 3836
output:
YES 8 7 2 1 3 4 5 6
result:
ok 2 lines
Test #48:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
8 3837
output:
NO
result:
ok single line: 'NO'
Test #49:
score: 0
Accepted
time: 99ms
memory: 14132kb
input:
30000 15298495309447071
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #50:
score: 0
Accepted
time: 81ms
memory: 14104kb
input:
29997 253859976519879617
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #51:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
29998 1
output:
NO
result:
ok single line: 'NO'
Test #52:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
29999 1000000000000000000
output:
NO
result:
ok single line: 'NO'
Test #53:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
5 10
output:
YES 3 2 4 5 1
result:
ok 2 lines
Test #54:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
5 25
output:
NO
result:
ok single line: 'NO'
Test #55:
score: 0
Accepted
time: 284ms
memory: 29996kb
input:
75000 1000000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #56:
score: 0
Accepted
time: 35ms
memory: 13424kb
input:
74997 1
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #57:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
74998 952975592247715841
output:
NO
result:
ok single line: 'NO'
Test #58:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
74999 563899854863562881
output:
NO
result:
ok single line: 'NO'
Test #59:
score: 0
Accepted
time: 1ms
memory: 3720kb
input:
20 62119523114983182
output:
YES 20 19 18 17 16 9 1 2 4 3 5 7 6 8 10 11 12 13 14 15
result:
ok 2 lines
Test #60:
score: 0
Accepted
time: 1ms
memory: 3960kb
input:
20 62119523114983324
output:
NO
result:
ok single line: 'NO'
Test #61:
score: 0
Accepted
time: 382ms
memory: 42164kb
input:
125000 890645059675041665
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #62:
score: 0
Accepted
time: 66ms
memory: 19832kb
input:
124997 1
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #63:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
124998 873954992112723841
output:
NO
result:
ok single line: 'NO'
Test #64:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
124999 248632969637282529
output:
NO
result:
ok single line: 'NO'
Test #65:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
17 11501573822775
output:
YES 17 16 15 14 9 2 3 1 4 5 6 7 8 10 11 12 13
result:
ok 2 lines
Test #66:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
17 11501573823343
output:
NO
result:
ok single line: 'NO'
Test #67:
score: 0
Accepted
time: 548ms
memory: 57192kb
input:
175000 1000000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #68:
score: 0
Accepted
time: 540ms
memory: 57476kb
input:
174997 328322156846141249
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #69:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
174998 184010129245469601
output:
NO
result:
ok single line: 'NO'
Test #70:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
174999 988653544556724225
output:
NO
result:
ok single line: 'NO'
Test #71:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
16 738680521137
output:
YES 16 15 14 13 6 1 2 3 4 7 5 8 9 10 11 12
result:
ok 2 lines
Test #72:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
16 738680521147
output:
NO
result:
ok single line: 'NO'
Test #73:
score: 0
Accepted
time: 854ms
memory: 80416kb
input:
250000 265999253667994753
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #74:
score: 0
Accepted
time: 818ms
memory: 80112kb
input:
249997 1000000000000000000
output:
YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok 2 lines
Test #75:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
249998 994057636750856961
output:
NO
result:
ok single line: 'NO'
Test #76:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
249999 728689378265228289
output:
NO
result:
ok single line: 'NO'