#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define vi vector<int>
#define vpii vector<pii>
#define vp3i vector<p3i>
#define vpll vector<pll>
#define vp3l vector<p3l>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() ((rand() << 14)+rand())
#define scan(X) do{while((X=getchar())<'0'); for(X-='0'; '0'<=(_=getchar()); X=(X<<3)+(X<<1)+_-'0');}while(0)
char _;
#define pi 3.14159265358979323846
int n, a[1005], dp[1005][1005], p[1005], psa[1005][1005], psa2[1004][1005];
bool nxt[1005][1005], rem[1005];
vector<pii> v;
int solve(int N, int L){
if (N==n) return 0;
if (dp[N][L]!=0x3f3f3f3f) return dp[N][L];
int P=p[N];
dp[N][L]=min(dp[N][L], solve(N+1, L)+P+1+(n-N)-psa[p[N]][N]+1);
if (!(P>=L)){
int t=solve(N+1, P)+psa2[p[N]][N]-1;
dp[N][L]=min(dp[N][L], t);
if (t==dp[N][L]) nxt[N][L]=1;
}
return dp[N][L];
}
vector<int> v2;
vector<pii> res, ed;
void out(int N, int L){
if (N==n) return;
if (nxt[N][L]==0){
if (p[N]>=L){
//cout << "*";
ed.pb(mp(p[N], N));
} else {
//cout << N << ' '<< L << endl;
//fox(l, v2.size()) cout << v2[l] << ' '; cout << endl;
fox(l, v2.size()){
if (v2[l]==N){
v2.erase(v2.begin()+l);
fox(l2, v2.size()+1){
if (l2==v2.size() || v2[l2]<N){
res.pb(mp(l+1, l2+1));
v2.insert(v2.begin()+l2, N);
break;
}
}
break;
}
}
}
out(N+1, L);
} else {
out(N+1, p[N]);
}
}
vector<int> v3;
void verify(){
fox(l, res.size()){
int t=v3[res[l].x-1];
v3.erase(v3.begin()+res[l].x-1);
v3.insert(v3.begin()+res[l].y-1, t);
}
fox(l, int(v3.size())-1){
if (v3[l]<v3[l+1]) exit(1);
}
}
int32_t run(){
flood(dp);
v3.clear(); v2.clear(); v.clear();
drain(psa); drain(psa2);
res.clear(); ed.clear();
drain(rem); drain(nxt);
fox(l, n){
v3.pb(a[l]);
v.pb(mp(a[l], l));
}
sort(v.begin(), v.end());
fox(l, n){
a[v[l].y]=l;
p[l]=v[l].y;
}
fox(l, n){
v2.pb(a[l]);
psa[l][a[l]]++;
psa2[l][a[l]]++;
}
foxr(l, n){
foxr(l2, n){
psa[l][l2]+=psa[l+1][l2]+psa[l][l2+1]-psa[l+1][l2+1];
}
}
fox(l, n){
fox(l2, n){
if (l>0) psa2[l][l2]+=psa2[l-1][l2];
if (l2>0) psa2[l][l2]+=psa2[l][l2-1];
if (l>0 && l2>0) psa2[l][l2]-=psa2[l-1][l2-1];
//cout<< psa2[l][l2] << ' ';
}
//cout << endl;
}
int x=solve(0, n);
//cout << x << endl;
out(0, n);
//fox(l, v2.size()) cout << v2[l] << ' ';
sort(ed.begin(), ed.end());
foxr(l, v2.size()){
p[v2[l]]=l;
}
fox(l, ed.size()){
//cout << ed[l].x << ' ' << ed[l].y << endl;
rem[ed[l].y]=1;
}
foxr(l, v2.size()){
if (rem[v2[l]]) v2.erase(v2.begin()+l);
}
//fox(l, v2.size()) cout << v2[l] << ' '; cout << endl;
fox(l, ed.size()){
int N=ed[l].y;
for(int l2=0; l2<v2.size(); ++l2){
if ((l2==0 || v2[l2-1]>N) && N>v2[l2]){
v2.insert(v2.begin()+l2, N);
res.pb(mp(p[N]+1, l2+1));
break;
}
}
}
cout << res.size() << endl;
fox(l, res.size()) cout << res[l].x << ' ' << res[l].y << endl;
verify();
return 0;
}
void test(){
int b[10];
fox(l, n) b[l]=l;
do {
fox(l, n){
cout << b[l] << ' ';
a[l]=b[l];
} cout << endl;
run();
} while(next_permutation(b, b+n));
}
int32_t main(){
cin >> n;
//test
//test();
fox(l, n){
scanf("%i", &a[l]);
}
run();
return 0;
}
//10
//0 1 2 3 4 6 8 7 5 9
Compilation message
sorting.cpp: In function 'void out(int, int)':
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:61:17:
fox(l, v2.size()){
~~~~~~~~~~~~
sorting.cpp:61:13: note: in expansion of macro 'fox'
fox(l, v2.size()){
^~~
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:64:25:
fox(l2, v2.size()+1){
~~~~~~~~~~~~~~~
sorting.cpp:64:21: note: in expansion of macro 'fox'
fox(l2, v2.size()+1){
^~~
sorting.cpp:65:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (l2==v2.size() || v2[l2]<N){
~~^~~~~~~~~~~
sorting.cpp: In function 'void verify()':
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:82:9:
fox(l, res.size()){
~~~~~~~~~~~~~
sorting.cpp:82:5: note: in expansion of macro 'fox'
fox(l, res.size()){
^~~
sorting.cpp: In function 'int32_t run()':
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:133:9:
fox(l, ed.size()){
~~~~~~~~~~~~
sorting.cpp:133:5: note: in expansion of macro 'fox'
fox(l, ed.size()){
^~~
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:141:9:
fox(l, ed.size()){
~~~~~~~~~~~~
sorting.cpp:141:5: note: in expansion of macro 'fox'
fox(l, ed.size()){
^~~
sorting.cpp:143:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int l2=0; l2<v2.size(); ++l2){
~~^~~~~~~~~~
sorting.cpp:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define fox(k, x) for (int k=0; k<x; ++k)
sorting.cpp:152:9:
fox(l, res.size()) cout << res[l].x << ' ' << res[l].y << endl;
~~~~~~~~~~~~~
sorting.cpp:152:5: note: in expansion of macro 'fox'
fox(l, res.size()) cout << res[l].x << ' ' << res[l].y << endl;
^~~
sorting.cpp:6:11: warning: unused variable 'first' [-Wunused-variable]
#define x first
^
sorting.cpp:125:9: note: in expansion of macro 'x'
int x=solve(0, n);
^
sorting.cpp: In function 'int32_t main()':
sorting.cpp:173:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i", &a[l]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
13180 KB |
Output is correct |
2 |
Correct |
12 ms |
13176 KB |
Output is correct |
3 |
Correct |
12 ms |
13176 KB |
Output is correct |
4 |
Correct |
12 ms |
13176 KB |
Output is correct |
5 |
Correct |
12 ms |
13176 KB |
Output is correct |
6 |
Correct |
13 ms |
13176 KB |
Output is correct |
7 |
Correct |
18 ms |
13176 KB |
Output is correct |
8 |
Correct |
27 ms |
13304 KB |
Output is correct |
9 |
Correct |
41 ms |
13304 KB |
Output is correct |
10 |
Correct |
40 ms |
13304 KB |
Output is correct |