Codeforces 513G1 or 513G2 Inversions problem DP
来源:互联网 发布:无人机单片机 编辑:程序博客网 时间:2024/05/24 02:31
题目大意:
就是现在初始给定一个n个数的排列, 每次随机地选取任意的一个段的数进行反转, 问k次随机翻转之后逆序对的数量的期望
G1难度题目链接:http://codeforces.com/contest/513/problem/G1 (n <= 6, k <= 4)
G2难度题目链接:http://codeforces.com/contest/513/problem/G2 (n <= 30, k <= 200)
大致思路:
还是过不了G3难度, 只弄懂了G1和G2难度的解法...
代码如下:
G1难度 Result : Accepted Memory : 28420 KB Time : 15 ms
G2难度 Result : Accepted Memory : 28420 KB Time : 234 ms
/* * Author: Gatevin * Created Time: 2015/2/25 20:04:17 * File Name: poi~.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;/* * 用dp[i][j][k]表示在k次交换之后位置i和位置j的元素满足a[i] > a[j]的概率 * (考虑两两的位置之间的独立性), 由于位置i, j要么出现逆序对贡献1, 要么没有就贡献0 * 所以期望就是其满足逆序对的概率 * 状态转移的dp就是按照官方的题解讨论的, 这里就是讨论要反转的区间(x, y)与位置i, j的关系 * 复杂度是O(n^4 * k)就算利用x + y预处理也只能降到 O(n^3 * k)还是过不了G3难度...不知道怎么n^3优化到n^2 */double dp[110][110][300];int main(){ memset(dp, 0, sizeof(0)); int n, K; cin>>n>>K; int a[110]; for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(a[i] > a[j]) dp[i][j][0] = 1; double p = 2./n/(n + 1); for(int k = 0; k < K; k++) for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) for(int x = 1; x <= n; x++) for(int y = x; y <= n; y++) if(i < x && y < j) { dp[i][j][k + 1] += dp[i][j][k]*p; dp[j][i][k + 1] += (1 - dp[i][j][k])*p; } else if(x <= i && i <= y && y < j) { dp[x + y - i][j][k + 1] += dp[i][j][k]*p; dp[j][x + y - i][k + 1] += (1 - dp[i][j][k])*p; } else if(i < x && x <= j && j <= y) { dp[i][x + y - j][k + 1] += dp[i][j][k]*p; dp[x + y - j][i][k + 1] += (1 - dp[i][j][k])*p; } else if(x <= i && i <= j && j <= y) { dp[x + y - i][x + y - j][k + 1] += dp[i][j][k]*p; dp[x + y - j][x + y - i][k + 1] += (1 - dp[i][j][k])*p; } else { dp[i][j][k + 1] += dp[i][j][k]*p; dp[j][i][k + 1] += (1 - dp[i][j][k])*p; } double ans = 0; for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++) ans += dp[i][j][K]; printf("%.9f\n", ans); return 0;}
0 0
- Codeforces 513G1 or 513G2 Inversions problem DP
- Codeforces 513G1 513G2 Inversions problem 概率dp
- Codeforces 513G2 Inversions problem (dp)
- rockethon2015 G2题 Inversions problem 概率dp
- CodeForces 513G1 CodeForces 513A
- google G1,G2,G3比较(YY)
- 到底什么是 G1 G2 G3 的 ROM
- Infinite Inversions CodeForces
- Codeforces 528B Clique Problem dp+线段树(or 树状数组)
- G1->I7500->G2,手机原来可以这么有趣
- Codeforces 540 E. Infinite Inversions
- Problem-236A-Codeforces-Boy or Girl
- Inversions
- CodeForces #174.div2.problem E-----(DP)
- Codeforces 706C Hard problem(dp+字符串)
- CodeForces 367C Hard problem DP
- Codeforces 706C Hard problem (DP)
- Codeforces Problem 711C Coloring Trees(DP)
- 第一天开通博客
- 开发出了海思HI3516C的38模组,挺有意思的,高清200W像素
- 《coredump问题原理探究》Linux x86版7.1节vector对象
- poj 1830 高斯消元模版题
- C++多态技术
- Codeforces 513G1 or 513G2 Inversions problem DP
- ps学习(一)
- Impala简明调优手册
- 在Mini2440上移植RT3070驱动+wpa_supplicant调通
- PHPcms --资料
- virtualbox 虚拟机上网配置
- Android Xposed 参考
- 黑马程序员_Java_GUI
- 2014年总结——回眸过去