//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define vi vector<int>
#define vl vector<ll>
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;
const int MAX = 5007, MOD = 1e9 + 7;
ll a[MAX], dp[MAX][MAX], ans[MAX], pos[MAX], add[MAX][MAX], ori[MAX];
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
ans[i] = 2e18;
pos[a[i]] = i;
}
for(int i = 1;i <= n;i++){
for(int j = i - 1;j >= 1;j--){
add[i][j] = add[i][j + 1];
if(pos[j] < pos[i])
add[i][j]++;
}
for(int j = i + 1;j <= n;j++){
add[i][j] = add[i][j - 1];
if(pos[i] > pos[j])
add[i][j]++;
}
}
for(int i = 1;i <= n;i++){
int now = 0;
ll res = 0;
for(int j = n;j >= 1;j--){
if(a[j] <= i){
now++;
ori[a[j]] = 0;
}else{
ori[a[j]] = now;
res += now;
}
}
for(int j = 1;j <= i;j++){
res += add[j][1];
}
for(int j = i + 1;j <= n + 1;j++){
dp[j - i][j - 1] = res;
res += add[j][j - i + 1] + add[j][n];
res -= i - 1 - add[j - i][j - 1] + add[j][j - i];
}
}
for(int i = 1;i <= n;i++){
for(int j = i;j <= n;j++){
cout << dp[i][j] << " ";
}
cout << '\n';
}
for(int i = 0;i < n;i++){
for(int j = 1;j <= n;j++)
ans[j] = min(ans[j], ans[i] + dp[i + 1][j]);
}
cout << ans[n] << '\n';
}
int main(){
IOS
// CASE
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |