# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1122826 | cpdreamer | Giraffes (JOI22_giraffes) | C++20 | 4 ms | 328 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair
#define S second
#define F first
#define all(v) v.begin(),v.end()
#define V vector
#define pb push_back
const ll MOD=998244353;
void file() {
freopen("input.txt.txt", "r", stdin);
freopen("output.txt.txt", "w", stdout);
}
void setIO(string s = "") {
if (!s.empty()){
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
ios::sync_with_stdio(false);
cin.tie(0);
}
bool check(int n,V<int>A){
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
int mind=INT_MAX;
int maxd=INT_MIN;
for(int g=i;g<=j;g++){
mind=min(mind,A[g]);
maxd=max(maxd,A[g]);
}
if(mind<min(A[i],A[j]) && maxd>max(A[i],A[j]))
return false;
}
}
return true;
}
void solve() {
int n;
cin>>n;
V<int>A(n);
for(int i=0;i<n;i++){
cin>>A[i];
}
if(check(n,A)){
cout<<0<<endl;
return;
}
int ans=INT_MAX;
for(int mask=0;mask<(1<<(n+1));mask++){
V<int>B=A;
V<int>C;
V<int>pos;
for(int j=0;j<n;j++){
if(((1<<j)&mask)!=0){
pos.pb(j);
C.pb(A[j]);
}
}
sort(all(C));
for(int i=0;i<(int)pos.size();i++){
B[pos[i]]=C[i];
}
if(check(n,B)){
ans=min(ans,(int)pos.size());
}
reverse(all(C));
for(int i=0;i<(int)pos.size();i++){
B[pos[i]]=C[i];
}
if(check(n,B)){
ans=min(ans,(int)pos.size());
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//file();
solve();
}
Compilation message (stderr)
# | 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... |