#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define sqrt sqrtl
using namespace std;
const long long oo = 1e9;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int n, a[ 22 ], b[ 22 ], dp[ ( 1 << 20 ) ][ 20 ];
main () {
cin >> n;
for ( int i = 0; i < n; i ++ )
cin >> a[ i ], b[ a[ i ] ] = i + 1;
for ( int mask = 0; mask < ( 1 << n ); mask ++ ) {
for ( int i = 0; i < n; i ++ )
dp[ mask ][ i ] = oo;
}
for ( int i = 0; i < n; i ++ )
dp[ ( 1 << i ) ][ i ] = b[ i + 1 ] - 1;
for ( int mask = 1; mask < ( 1 << n ); mask ++ ) {
num = __builtin_popcount( mask );
for ( int i = 0; i < n; i ++ ) {
if ( mask & ( 1 << i ) ) {
sum = 0;
mx = num;
for ( int j = 0; j < n; j ++ ) {
if ( !( mask & ( 1 << j ) ) && a[ i ] < a[ j ] + 2 ) {
dp[ mask | ( 1 << j ) ][ j ] = min( dp[ mask | ( 1 << j ) ][ j ] + 0ll, dp[ mask ][ i ] + sum + mx );
sum ++;
}
else
mx --;
}
}
}
}
ans = oo;
for ( int i = 0; i < n; i ++ )
ans = min( ans, dp[ ( 1 << n ) - 1 ][ i ] + 0ll );
cout << ans;
}
/*
h[ i ] + i * 2 < h[ i + 1 ] + i * 2 + 4
h[ i ] - h[ i + 1 ] < 4
3 5 2 4 1
5 9 8 14 13
*/
Compilation message
Main.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |