This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std ;
// o ideal eh começar decrescente
// o ideal eh o começo ter os certos
// se quiser mudar
const int maxn = 1e5 + 5 ;
int n, k, v[maxn], ct[maxn], used[maxn], seq[maxn] ;
// pga o menorr q satisfaz smp pros verdadeiros
// pros falos coloca os maiores mais a esq
int main(){
cin >> n >> k ;
for(int i = 0 ; i < n ; i++) cin >> v[i] ;
bool ans = 0 ;
for(int mask = 0 ; mask < (1<<n) ; mask++){ // 0 = falso
int ctr = 0, on = 0 ;
for(int i = n - 1 ; i >= 0 ; i--){
ct[i] = ctr ;
if(!(mask&(1<<i))) ctr++, on++ ;
}
for(int i = 0 ; i < n ; i++) used[i] = 0, seq[i] = -1 ;
for(int i = 0 ; i < n ; i++){ // colocar os vdds
if(!(mask&(1<<i))) continue ;
int x = ct[i] ; //preciso do 1o valor menor ou igual a isso q n tenha sido usado
int mn = maxn, id = -1 ;
for(int j = 0 ; j < n ; j++){
if(!used[j]){
if(mn > v[j] && v[j] <= x) mn = v[j], id = j ;
}
}
if(id != -1) seq[i] = v[id] ; used[id] = 1 ;
}
// colocar os falos - os maiores 1o
for(int i = 0 ; i < n ; i++){
if(mask&(1<<i)) continue ;
int x = ct[i] ;
int mx = -1, id = -1 ;
for(int j = 0 ; j < n ; j++){
if(used[j] || v[j] <= x) continue ;
if(mx < v[j]) mx = v[j], id = j ;
}
if(id != -1) used[id] = 1, seq[i] = v[id] ;
}
bool ok = 1 ;
if(on != k) ok = 0 ;
for(int i = 0 ; i < n ; i++) if(seq[i] == -1) ok = 0 ;
if(ok){
for(int i = 0 ; i < n ; i++) cout << seq[i] << " " ;
cout << "\n" ;
exit(0) ;
}
}
cout << "-1\n" ;
}
Compilation message (stderr)
karte.cpp: In function 'int main()':
karte.cpp:37:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
37 | if(id != -1) seq[i] = v[id] ; used[id] = 1 ;
| ^~
karte.cpp:37:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
37 | if(id != -1) seq[i] = v[id] ; used[id] = 1 ;
| ^~~~
karte.cpp:19:10: warning: unused variable 'ans' [-Wunused-variable]
19 | bool ans = 0 ;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |