2014-2015 ACM-ICPC, Asia Xian Regional Contest Problem C. The Problem Needs 3D Arrays(网络流之最大密度子图)

题意:

给你一个长度为n(<=100)的序列T,S为T的任意子序列,r(S)表示子序列S(不连续)中的逆序对数,l(S) 表示S的长度,求出 r(S) / l(S) 的最大值。

思路:

将r(S)看成边,l(S)看成点,问题转化为求 E / V 的最大值。经典的最大密度子图问题。

利用类似0/1分数规划的思想,二分答案,设为mid,则有E / V=mid 即E=V*mid。

即使E-V*mid趋近于0。

问题再转化为求最大权闭合图。最大权闭合图参考:https://blog.csdn.net/LSD20164388/article/details/79224422

设源点为st,汇点为ed,建边:

1、对于每个逆序对标号i=1~m,依次建边( st , i , 1.0 )

2、对于每个逆序对标号i=1~m,对应原数组下标为(x,y),依次建边 ( i , x+m , inf ) , ( i , y+m , inf )

3、对于每个点i=1~n,依次建边( i+m , ed , mid )

使用sap模板的代码:

#include
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
#define maxn 200010
#define clear(A, X) memset (A, X, sizeof A)
#define copy(A, B) memcpy (A, B, sizeof A)
using namespace std;
const double eps=1e-9;
template 
inline void read(T &X)
{
    X=0;int w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    if(w) X=-X;
}
pairne[maxn];
const int MAXN = 20010; //点数的最大值
const int MAXM = 40010; //边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge {
  int to, next;double cap, flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
int n,m;
//加边,单向图三个参数,双向图四个参数
void addedge(int u, int v, double w, double rw = 0.0) {
  edge[tol].to = v;
  edge[tol].cap = w;
  edge[tol].next = head[u];
  edge[tol].flow = 0;
  head[u] = tol++;
  edge[tol].to = u;
  edge[tol].cap = rw;
  edge[tol].next = head[v];
  edge[tol].flow = 0;
  head[v] = tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
double sap(int start, int end, int N) {
  memset(gap, 0, sizeof(gap));
  memset(dep, 0, sizeof(dep));
  memcpy(cur, head, sizeof(head));
  int u = start;
  pre[u] = -1;
  gap[0] = N;
  double ans = 0.0;
  while (dep[start] < N) {
    if (u == end) {
      double Min = INF;
      for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
        if (Min > edge[i].cap - edge[i].flow)
          Min = edge[i].cap - edge[i].flow;
      for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to]) {
        edge[i].flow += Min;
        edge[i ^ 1].flow -= Min;
      }
      u = start;
      ans += Min;
      continue;
    }
    bool flag = false;
    int v;
    for (int i = cur[u]; i != -1; i = edge[i].next) {
      v = edge[i].to;
      if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) {
        flag = true;
        cur[u] = pre[v] = i;
        break;
      }
    }
    if (flag) {
      u = v;
      continue;
    }
    int Min = N;
    for (int i = head[u]; i != -1; i = edge[i].next)
      if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
        Min = dep[edge[i].to];
        cur[u] = i;
      }
    gap[dep[u]]--;

    if (!gap[dep[u]])
      return ans;
    dep[u] = Min + 1;
    gap[dep[u]]++;
    if (u != start)
      u = edge[pre[u] ^ 1].to;
  }
  return ans;
}
int ru[MAXN],fa[MAXN];
void init () {//初始化
    tol = 0;
    clear (head, -1);
    clear (fa, -1);
    clear (ru, 0);
}
double build(int st,int ed,double mid){
    init();
    rep(i,1,m) addedge(st,i,1.0);
    rep(i,1,m) {
        addedge(i,ne[i].first+m,(double)inf);
        addedge(i,ne[i].second+m,(double)inf);
    }
    rep(i,1,n) addedge(i+m,ed,mid);
    return m-sap(n+m+1,n+m+2,n+m+2);
}
int a[maxn];
int main(){
    int i,j,k,l,x,y,z,cas=0,t;
    read(t);
    while (t--){
        read(n);m=0;
        for (i=1;i<=n;i++) read(a[i]);
        for (i=1;i<=n;i++)
        for (j=i+1;j<=n;j++){
            if(a[i]>a[j]){
                ne[++m].first=i;
                ne[m].second=j;
            }
        }
        double l=0,r=m;
        while((r-l)>eps){
            double mid=(l+r)/2;
            //cout<0){
                l=mid;
            }
            else r=mid;
        }
        printf("Case #%d: %.12lf\n",++cas,l);
    }
    return 0;
}

 

你可能感兴趣的:(图论:网络流,数据结构:二分查找,图论:图论基础,运维)