Submission #388755

#TimeUsernameProblemLanguageResultExecution timeMemory
388755CaroLindaGroup Photo (JOI21_ho_t3)C++14
100 / 100
84 ms528 KiB
#include <bits/stdc++.h>

#define lp(i,a,b) for(int i =a ; i < b ; i++)
#define ll long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x.size())
#define pii pair<int,int>
#define mk make_pair
#define mkt make_tuple
#define tiii tuple<int,int,int>

const int MAXN = 5010 ;

using namespace std ;

int N ;
int H[MAXN] , pos[MAXN] , art_pos[MAXN] ;
int maior_esq[MAXN] ;
int dp[MAXN] ;

int main()
{
	scanf("%d", &N ) ;
	lp(i,1,N+1) 
	{
		scanf("%d", &H[i] ) ;
		pos[H[i]] = i ;
	}

	vector<int> v = {1} ;

	for(int i = 2 ; i <= N ; i++ )
	{
		int p = -1 ;

		for(int j = 0 ; j < sz(v) ; j++ )
			if( pos[ v[j] ] > pos[i] )
			{
				p = j ;
				break ;
			}

		if(p == -1) v.pb(i) , p = sz(v)-1 ;
		else v.insert( v.begin()+p , i ) ;

		for(int j = p+1 ; j < sz(v) ; j++ ) maior_esq[ v[j] ]++ ;
		for(int j = 0 ;j < sz(v) ; j++ ) art_pos[ v[j] ] = j ;

		dp[i]= N*N ;

		int cost = 0 ;
		for(int g = i ; g > 0 ; g-- )
		{
			cost += i-1-art_pos[g]-maior_esq[g] ;
			dp[i]=min(dp[i] , dp[g-1]+cost) ;
		}

	}

	printf("%d\n" , dp[N] ) ;

}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   26 |  scanf("%d", &N ) ;
      |  ~~~~~^~~~~~~~~~~
Main.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |   scanf("%d", &H[i] ) ;
      |   ~~~~~^~~~~~~~~~~~~~
#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...