题意:给定一个1-n的排列,它的一个子序列(不一定连续)设为S,令这个子序列的逆序对为R(S),长度为L(S)。请找到一个S,使得R/S
最大
。
题解:裸的最大密度子图
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define V G[p].v
#define eps 1e-5
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline int dcmp(double a,double b){
if (fabs(a-b)<eps) return 0;
if (a<b) return -1; return 1;
}
namespace DINIC{
const int N=100005;
const int M=500005;
struct edge{
int u,v; double f;
int next;
}G[M];
int head[N],inum=1;
inline void add(int u,int v,double f,int p){
G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
}
inline void link(int u,int v,double f){
add(u,v,f,++inum); add(v,u,0,++inum);
}
int S,T;
int dis[N];
int Q[N],l,r;
inline bool bfs(){
memset(dis,-1,sizeof(dis)); l=r=-1;
Q[++r]=S; dis[S]=1;
while (l<r){
int u=Q[++l];
for (int p=head[u];p;p=G[p].next)
if (dcmp(G[p].f,0)>0 && dis[V]==-1){
dis[V]=dis[u]+1; Q[++r]=V;
if (V==T) return 1;
}
}
return 0;
}
int cur[N];
double dfs(int u,double flow){
if (u==T) return flow;
double used=0,now;
for (int p=cur[u];p;p=G[p].next)
{
cur[u]=p;
if (dcmp(G[p].f,0)>0 && dis[V]==dis[u]+1)
{
now=dfs(V,min(flow-used,G[p].f));
G[p].f-=now,G[p^1].f+=now;
used+=now; if (dcmp(flow,used)==0) break;
}
}
if (dcmp(used,0)==0) dis[u]=-1;
return used;
}
double Dinic(){
double ret=0;
while (bfs())
memcpy(cur,head,sizeof(cur)),ret+=dfs(S,1e40);
return ret;
}
void clear(){
cl(head); inum=1;
}
}
const int N=10005;
int n,m;
int a[N];
int u[N],v[N],deg[N];
inline bool check(double mid)
{
using namespace DINIC;
double U=m;
clear(); S=n+1; T=n+2;
for (int i=1;i<=m;i++) link(u[i],v[i],1),link(v[i],u[i],1);
for (int i=1;i<=n;i++) link(S,i,U);
for (int i=1;i<=n;i++) link(i,T,U+2*mid-deg[i]);
double ret=Dinic();
return dcmp(U*n-ret,0)>0;
}
inline void Solve()
{
using namespace DINIC;
double L=0,R=m,MID;
while (R-L>=eps)
if (check(MID=(L+R)/2))
L=MID;
else
R=MID;
printf("%.5lf\n",L);
}
int main()
{
freopen("per.in","r",stdin);
freopen("per.out","w",stdout);
read(n);
for (int i=1;i<=n;i++) read(a[i]);
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
if (a[i]>a[j])
u[++m]=i,v[m]=j,deg[i]++,deg[j]++;
Solve();
return 0;
}