14西安区域赛C - The Problem Needs 3D Arrays
最大密度子图裸题,详情请见胡博涛论文:
https://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html
不加当前弧优化t到死= =
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a, b) ((a)>(b)?(a):(b))
#define Min(a, b) ((a)<(b)?(a):(b))
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const int N=20000+10,maxn=50000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
struct edge{
int to,Next;
double c;
}e[maxn];
int s,t,cnt,head[N],cur[N];
void init()
{
cnt=0;
memset(head,-1,sizeof head);
}
void add(int u,int v,double c)
{
e[cnt].to=v;
e[cnt].c=c;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].to=u;
e[cnt].c=0;
e[cnt].Next=head[v];
head[v]=cnt++;
}
inline bool zero(double x){return fabs(x)<=eps;}
int dis[N];
bool bfs()
{
queue<int>q;
memset(dis,-1,sizeof dis);
dis[s]=1;
q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];~i;i=e[i].Next)
{
int y=e[i].to;
if(dis[y]==-1&&!zero(e[i].c))
{
dis[y]=dis[x]+1;
q.push(y);
}
}
}
return dis[t]!=-1;
}
double dfs(int u,double mx)
{
if(u==t)return mx;
double flow=0,f;
for(int &i=cur[u];~i;i=e[i].Next)
{
int x=e[i].to;
if(dis[x]==dis[u]+1&&!zero(e[i].c)&&!zero(f=dfs(x,min(mx-flow,e[i].c))))
{
e[i].c-=f;
e[i^1].c+=f;
return f;
}
}
return 0;
}
double maxflow()
{
double ans=0,f;
while(bfs())
{
for(int i=0;i<=t;i++)cur[i]=head[i];
while(!zero(f=dfs(s,inf)))ans+=f;
}
return ans;
}
int a[N],deg[N],n,m;
pii p[maxn];
bool ok(double x)
{
init();
s=n+m+1,t=n+m+2;
for(int i=1;i<=n;i++)add(i,t,x);
for(int i=1;i<=m;i++)
{
add(s,n+i,1);
add(n+i,p[i].fi,inf);
add(n+i,p[i].se,inf);
}
return !zero(m-maxflow());
}
double solve()
{
double l=0,r=m;
while(r-l>=1e-8)
{
double mid=(l+r)/2;
if(ok(mid))l=mid;
else r=mid;
}
return r+eps;
}
int main()
{
int T;scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
memset(deg,0,sizeof deg);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
m = 0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(a[j]<a[i])
{
p[++m]=mp(i,j);
deg[i]++,deg[j]++;
}
}
}
printf("Case #%d: %.8f\n",cas,solve());
}
return 0;
}
/********************
2
5
3 4 2 5 1
5
3 4 2 5 1
********************/