Submission #1209738

#TimeUsernameProblemLanguageResultExecution timeMemory
1209738nguynGroup Photo (JOI21_ho_t3)C++20
100 / 100
272 ms98444 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define F first
#define S second
#define pb push_back 
#define pii pair<int,int>

const int N = 5e3 + 5;

int n, h[N], pos[N], rev[N][N], f[N];

int cost(int l, int r) {
	return rev[r][r] - rev[l - 1][r] - rev[r][l - 1] + rev[l - 1][l - 1];
}

signed main(){
    ios_base::sync_with_stdio(false) ; 
    cin.tie(0) ; cout.tie(0) ; 
    if (fopen("INP.INP" ,"r")) {
        freopen("INP.INP" ,"r" , stdin) ;
        freopen("OUT.OUT" , "w" , stdout) ;
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	cin >> h[i];
    	pos[h[i]] = i;
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
    	for (int j = i + 1; j <= n; j++) {
    		if (pos[i] > pos[j]) {
    			rev[i][j] = -1;
    			res++;
    		}
    		else {
    			rev[i][j] = 1;
    		}
    	}
    }
    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= n; j++) {
    		rev[i][j] += rev[i - 1][j] + rev[i][j - 1] - rev[i - 1][j - 1];
    	}
    }
    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= i; j++) {
    		f[i] = min(f[i], f[j - 1] + cost(j, i)); 
    	} 
    }
    cout << res + f[n]; 
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen("INP.INP" ,"r" , stdin) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen("OUT.OUT" , "w" , stdout) ;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...