您的位置:首页 > 其它

Gym - 100548C The Problem Needs 3D Arrays

2015-04-14 13:33 281 查看

Problem C. The Problem Needs 3D Arrays

  Time Limit: 6000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

A permutation is a sequence of integers p1,p2,...,pn, consisting of n distinct positive integers and each of them does not exceed n. Assume that r(S) of sequence S denotes the number of inversions in sequence S (if i < j and Si > Sj, then the pair of (i,j) is called an inversion of S), l(S) of sequence S denotes the length of sequence S. Given a permutation P of length n, it’s your task to find a subsequence S of P with maximum. A subsequence of P is a sequence (pi1,pi2,...,pit) which satisfies that 0 < i1 < i2 < ... < it ≤ n.

Input

The first line of the input gives the number of test cases, T. T test cases follow.

For each test case, the first line contains an integer n (1 ≤ n ≤ 100), the length of the permutation P. The second line contains n integers p1,p2,...,pn, which represents the permutation P.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum.

Your answer will be considered correct if it is within an absolute error of 10−6 of the correct answer.

Samples

Sample Input

Sample Output

1

5

3 4 2 5 1

Case #1: 1.250000000000

解题:最大密度子图转为最大权闭合图

#include <bits/stdc++.h>
using namespace std;
const int maxn = 12000;
const int INF = 0x3f3f3f3f;
int n,m,tot,S,T,x[maxn],y[maxn],A[102];
struct arc {
int to,next;
double flow;
arc(int x = 0,double y = 0,int z = -1) {
to = x;
flow = y;
next = z;
}
} e[maxn<<4];
int head[maxn],cur[maxn],d[maxn];
void add(int u,int v,double flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,0,head[v]);
head[v] = tot++;
}
queue<int>q;
bool bfs() {
memset(d,-1,sizeof d);
while(!q.empty()) q.pop();
q.push(S);
d[S] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow > 0 && d[e[i].to] == -1) {
d[e[i].to] = d[u] + 1;
q.push(e[i].to);
}
}
}
return d[T] > -1;
}
double dfs(int u,double low) {
if(u == T) return low;
double tmp = 0,a;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow > 0 && d[e[i].to] == d[u] + 1 &&(a=dfs(e[i].to,min(e[i].flow,low)))>0) {
e[i].flow -= a;
e[i^1].flow += a;
low -= a;
tmp += a;
if(low <= 0) break;
}
}
if(tmp <= 0) d[u] = -1;
return tmp;
}
bool dinic() {
double ans = m;
while(bfs()) {
memcpy(cur,head,sizeof head);
ans -= dfs(S,INF);
}
return ans <= 0;
}
void build(double delta) {
memset(head,-1,sizeof head);
for(int i = tot = 0; i < m; ++i) {
add(S,i + n + 1,1.0);
add(i + n + 1,x[i],INF);
add(i + n + 1,y[i],INF);
}
for(int i = 1; i <= n; ++i) add(i,T,delta);
}
int main() {
int cm = 1,cs;
scanf("%d",&cs);
while(cs--) {
scanf("%d",&n);
m = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d",A+i);
for(int j = i-1; j > 0; --j)
if(A[j] > A[i]) {
x[m] = j;
y[m++] = i;
}
}
S = 0;
T = n + m + 1;
double low = 0,high = m,ans = 0;
if(m == 0) {printf("Case #%d: %.7f\n",cm++,ans);continue;}
while(high - low > 1e-7){
double mid = (low + high)/2.0;
build(mid);
if(dinic()) ans = high = mid;
else low = mid;
}
printf("Case #%d: %.7f\n",cm++,ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: