2014 西安 The Problem Needs 3D Arrays

The Problem Needs 3D Arrays

题意:给你n个数, 然后1-n的数, 然后要求按顺序选出m个数, 求 逆序数/m 个数的 最大值是多少。

题解:裸的最大密度子图。逆序的2个数建边, 跑一下最大密度子图就AC了。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4 #define LL long long
  5 #define ULL unsigned LL
  6 #define fi first
  7 #define se second
  8 #define pb push_back
  9 #define lson l,m,rt<<1
 10 #define rson m+1,r,rt<<1|1
 11 #define max3(a,b,c) max(a,max(b,c))
 12 #define min3(a,b,c) min(a,min(b,c))
 13 typedef pair<int,int> pll;
 14 const LL INF = 0x3f3f3f3f3f3f3f3f;
 15 const int inf = 0x3f3f3f3f;
 16 const LL mod =  (int)1e9+7;
 17 const int N = 120;
 18 const int M = 500000;
 19 double eps = 1e-8;
 20 int head[N], deep[N], cur[N];
 21 int u[M], v[M];
 22 int vis[N], d[N];
 23 double w[M]; int to[M], nx[M];
 24 int n, m, tot;
 25 int a[N];
 26 void add(int u, int v,double val){
 27     w[tot]  = val; to[tot] = v;
 28     nx[tot] = head[u]; head[u] = tot++;
 29     w[tot]  = 0; to[tot] = u;
 30     nx[tot] = head[v]; head[v] = tot++;
 31 }
 32 int bfs(int s, int t){
 33     queue<int> q;
 34     memset(deep, 0, sizeof(int)*(n+3));
 35     q.push(s);
 36     deep[s] = 1;
 37     while(!q.empty()){
 38         int u = q.front();
 39         q.pop();
 40         for(int i = head[u]; ~i; i = nx[i]){
 41             if(w[i] > 0 && deep[to[i]] == 0){
 42                 deep[to[i]] = deep[u] + 1;
 43                 q.push(to[i]);
 44             }
 45         }
 46     }
 47     if(deep[t] > 0) return 1;
 48     return 0;
 49 }
 50 double Dfs(int u, int t, double flow){
 51     if(u == t) return flow;
 52     for(int &i = cur[u]; ~i; i = nx[i]){
 53         if(deep[u] + 1 == deep[to[i]] && w[i] > 0){
 54             double di = Dfs(to[i], t, min(w[i], flow));
 55             if(di > 0){
 56                 w[i] -= di, w[i^1] += di;
 57                 return di;
 58             }
 59         }
 60     }
 61     return 0;
 62 }
 63 
 64 int Dinic(int s, int t){
 65     double ans = 0, tmp;
 66     while(bfs(s, t)){
 67         for(int i = 0; i <= n+1; i++) cur[i] = head[i];
 68         while(tmp = Dfs(s, t, INF)) ans += tmp;
 69     }
 70     return ans;
 71 }
 72 
 73 bool check(double x){
 74     memset(head, -1, sizeof(int) * (n+2));
 75     tot = 0;
 76     int s = 0, t = n + 1;
 77     for(int i = 1; i <= m; i++){
 78         add(u[i], v[i], 1);
 79         add(v[i], u[i], 1);
 80     }
 81     for(int i = 1; i <= n; i++){
 82         add(s, i, m);
 83         add(i, t, m+2*x-d[i]);
 84     }
 85     return (m*n-Dinic(s,t))/2.0 >= 1e-6;
 86 }
 87 
 88 int main(){
 89     int T;
 90     scanf("%d", &T);
 91     for(int _i = 1; _i <= T; _i++){
 92         scanf("%d", &n);
 93         for(int i = 1; i <= n; i++)scanf("%d", &a[i]);
 94         memset(d, 0, sizeof(int)*(n+1));
 95         m = 0;
 96         for(int i = 1; i <= n; i++){
 97             for(int j = 1; j < i; j++){
 98                 if(a[j] > a[i]){
 99                     d[i]++;
100                     d[j]++;
101                     m++;
102                     u[m] = i;
103                     v[m] = j;
104                 }
105             }
106         }
107         double l = 0, r = m,  mid;
108         while(r - l >= eps){
109             mid = (l+r)/2;
110             if(check(mid)) l = mid;
111             else r = mid;
112         }
113         printf("Case #%d: %.12f\n", _i, l);
114         //printf
115     }
116 
117     return 0;
118 }
View Code

 

posted @ 2018-07-19 10:34  Schenker  阅读(150)  评论(0编辑  收藏  举报