이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 int long long
#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;
freopen ( "sum.in", "r", stdin )
*/
int n, a[ 5001 ], t[ 20001 ], tt[ 20001 ], b[ 5001 ];
int dp[ 5001 ], d[ 5001 ][ 5001 ];
void build( int v, int tl, int tr ) {
if ( tl == tr ) {
t[ v ] = 1;
return;
}
int mid = ( tl + tr ) / 2;
build( v + v, tl, mid );
build( v + v + 1, mid + 1, tr );
t[ v ] = t[ v + v ] + t[ v + v + 1 ];
}
void build1( int v, int tl, int tr ) {
tt[ v ] = 0;
if ( tl == tr ) {
return;
}
int mid = ( tl + tr ) / 2;
build1( v + v, tl, mid );
build1( v + v + 1, mid + 1, tr );
}
void upd( int v, int tl, int tr, int pos ) {
if ( tl > pos || tr < pos )
return;
if ( tl == tr ) {
t[ v ] = 0;
return;
}
int mid = ( tl + tr ) / 2;
upd( v + v, tl, mid, pos );
upd( v + v + 1, mid + 1, tr, pos );
t[ v ] = t[ v + v ] + t[ v + v + 1 ];
}
void upd1( int v, int tl, int tr, int pos ) {
if ( tl > pos || tr < pos )
return;
if ( tl == tr ) {
tt[ v ] = 1;
return;
}
int mid = ( tl + tr ) / 2;
upd1( v + v, tl, mid, pos );
upd1( v + v + 1, mid + 1, tr, pos );
tt[ v ] = tt[ v + v ] + tt[ v + v + 1 ];
}
void gett( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
num += t[ v ];
sum += tt[ v ];
return;
}
int mid = ( tl + tr ) / 2;
gett( v + v, tl, mid, l, r );
gett( v + v + 1, mid + 1, tr, l, r );
}
main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for ( int i = 1; i <= n; i ++ ) {
cin >> a[ i ];
b[ a[ i ] ] = i;
dp[ i ] = oo;
}
build( 1, 1, n );
for ( int i = 1; i <= n; i ++ ) {
build1( 1, 1, n );
for ( int j = i; j >= 1; j -- ) {
d[ j ][ i ] = d[ j + 1 ][ i ];
upd( 1, 1, n, b[ j ] );
num = 0;
gett( 1, 1, n, 1, b[ j ] );
d[ j ][ i ] += num;
sum = 0;
gett( 1, 1, n, b[ j ], n );
d[ j ][ i ] += sum;
// cout << j << " " << i << " - " << d[ j ][ i ] << "\n";
upd1( 1, 1, n, b[ j ] );
}
}
for ( int i = 1; i <= n; i ++ ) {
for ( int j = i - 1; j >= 0; j -- ) {
dp[ i ] = min( dp[ i ], dp[ j ] + d[ j + 1 ][ i ] );
}
}
cout << dp[ n ];
}
/*
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
*/
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
108 | main () {
| ^~~~
# | 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... |