This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 還要更強
#include<iostream>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<math.h>
#include<cstring>
#include<stack>
#include<string.h>
#include<random>
#include<algorithm>
#include<vector>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define INF (ll)(1e9)
#define pb push_back
#define AC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int bit[5005],a[5005],b[5005],n,pos[5005];
void add(int x){
for(int i=x;i>0;i-=i&(-i)) bit[i]++;
}
int ask(int x){
int r=0;
for(int i=x;i<=n;i+=i&(-i)){
r+=bit[i];
}
return r;
}
int cnt(){
int ans=0;
FOR(i,1,n+1) bit[i]=0;
FOR(i,1,n+1){
ans+=ask(b[i]);
add(b[i]);
}
return ans;
}
bool check(vector<int> v){
FOR(i,1,n)if(v[i]+2<=v[i-1]) return 0;
return 1;
}
signed main(){
AC;
int res;
cin>>n;
vector<int> v;
FOR(i,1,n+1){
v.pb(i);
cin>>a[i]; pos[a[i]]=i;
}
FOR(i,1,n+1){
b[i]=a[i];
}
res=cnt();
do{
if(!check(v)) continue;
//FOR(i,0,v.size()) cout<<v[i]<<" ";
//cout<<endl;
FOR(i,0,v.size()) b[pos[v[i]]]=i+1;
res=min(res,cnt());
}while(next_permutation(v.begin(),v.end()));
cout<<res<<endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:16:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | #define FOR(i,a,b) for(int i=a;i<b;i++)
......
70 | FOR(i,0,v.size()) b[pos[v[i]]]=i+1;
| ~~~~~~~~~~~~
Main.cpp:70:9: note: in expansion of macro 'FOR'
70 | FOR(i,0,v.size()) b[pos[v[i]]]=i+1;
| ^~~
# | 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... |