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 & %I64uDescription
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
相关文章推荐
- CF GYM 100548 The Problem Needs 3D Arrays(2014ACM西安现场赛Problem C)
- 2014年西安区域赛C The Problem Needs 3D Arrays
- UvaLive 7037 The Problem Needs 3D Arrays 【最大密度子图-最大权闭合子图做法】
- Uvalive 7037 The Problem Needs 3D Arrays (最大密集子图)
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest C – The Problem Needs 3D Arrays(最大密度子图)
- [最大密度子图 最小割] ACM 2014 Xian C The Problem Needs 3D Arrays
- UVALive 7037 The Problem Needs 3D Arrays(最大密度子图)
- Uvalive 7037 The Problem Needs 3D Arrays(最大密度子图)
- UVALive 7037 The Problem Needs 3D Arrays(网络流)
- obj2opengl: convert obj 3D models to arrays compatible with iPhone OpenGL ES
- UVAlive 7037 - The Problem Needs 3D Arrays(网络流‘最大密度子图)
- 3D物体绘制不见
- 3d显示分子作用
- OpenGL ES 绘制 3D 图形
- Arrays.copy()和ArrayList.clone()
- 红青3D立体图像的原理
- 2009年末最强梅麻呂3D动画游戏大作 汉化补丁
- Unity 3d 如何实现点击屏幕,npc自动移动到点击位置
- Gym 101246D Fire in the Country (dfs暴力博弈)
- 分享22个制作3D模型的应用程序