传送门
文章目录
- 题意:
- 思路:
题意:
n≤5000n\le5000n≤5000
思路:
从dpdpdp题集中看到的,一看n≤5000n\le5000n≤5000,这不就乱搞就行了,哪有什么dpdpdp🦆。
看到冒泡排序,一般跟逆序对脱不了关系了,所以定义change[i][j]change[i][j]change[i][j]表示将iii放到jjj位置的时候逆序对变化数量,这个可以O(n2)O(n^2)O(n2)预处理出来,让后n2n^2n2枚举i,ji,ji,j计算答案就行了。
// Problem: C. Insertion Sort
// Contest: Codeforces - Codeforces Round #212 (Div. 2)
// URL: https://codeforces.com/problemset/problem/362/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
#define lowbit(x) ((x)&(-x))
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=5010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N];
LL tr[N];
int change[N][N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cin>>n;for(int i=1;i<=n;i++) {scanf("%d",&a[i]);int cnt1,cnt2; cnt1=cnt2=0;for(int j=i-1;j>=1;j--) {change[i][j]=cnt1-cnt2;if(a[j]<a[i]) cnt1++;else cnt2++;}}for(int i=n;i>=1;i--) {int cnt1,cnt2; cnt1=cnt2=0;for(int j=i+1;j<=n;j++) {change[i][j]=cnt2-cnt1;if(a[j]<a[i]) cnt1++;else cnt2++;}}int sum=0;for(int i=1;i<=n;i++) {for(int j=i+1;j<=n;j++)sum+=a[i]>a[j];}int ans=INF,cnt=0;for(int i=1;i<=n;i++) {for(int j=i+1;j<=n;j++) {int add=0;if(a[i]<a[j]) add++;else add--;add+=change[i][j]+change[j][i];if(add<ans) ans=add,cnt=1;else if(add==ans) cnt++; }}cout<<sum+ans<<' '<<cnt<<endl;return 0;
}
/**/