CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP
考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\)
故设\(dp_{i,j}\)表示\(a_i>a_j\)的概率,每一次一个交换操作时\(O(n)\)地更新即可。
AGC030D就在模意义下运算,最后就乘上\(2^Q\)就行了
看着好简单啊就是想不到
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<vector>
#include<cmath>
#define ld long double
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
}
ld dp[1010][1010];
int num[1010] , N , M;
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read();
M = read();
for(int i = 1 ; i <= N ; ++i)
num[i] = read();
for(int i = 1 ; i <= N ; ++i)
for(int j = i - 1 ; j ; --j){
dp[i][j] = num[i] > num[j];
dp[j][i] = num[j] > num[i];
}
for(int i = 1 ; i <= M ; ++i){
int a = read() , b = read();
for(int j = 1 ; j <= N ; ++j)
if(j != a && j != b){
dp[j][a] = dp[j][b] = (dp[j][a] + dp[j][b]) * 0.5;
dp[a][j] = dp[b][j] = (dp[a][j] + dp[b][j]) * 0.5;
}
dp[a][b] = dp[b][a] = (dp[a][b] + dp[b][a]) * 0.5;
}
ld sum = 0;
for(int i = 1 ; i <= N ; ++i)
for(int j = i - 1 ; j ; --j)
sum += dp[j][i];
cout << fixed << setprecision(8) << sum;
return 0;
}
CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP的更多相关文章
- CF258D Little Elephant and Broken Sorting (带技巧的DP)
题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...
- CF 258 D. Little Elephant and Broken Sorting
D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...
- CodeForces - 258D Little Elephant and Broken Sorting
Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...
- CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)
题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望 .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...
- 「AGC030D」Inversion Sum
「AGC030D」Inversion Sum 传送门 妙啊. 由于逆序对的个数最多只有 \(O(n^2)\) 对,而对于每一个询问与其相关的逆序对数也最多只有 \(O(n)\) 对,我们可以对于每一对 ...
- 【AGC030D】Inversion Sum DP
题目大意 有一个序列 \(a_1,a_2,\ldots,a_n\),有 \(q\) 次操作,每次操作给你两个数 \(x,y\),你可以交换 \(a_x,a_y\),或者什么都不做. 问你所有 \(2^ ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
随机推荐
- 【读书笔记】iOS-Game Kit
名字虽然叫Game Kit,但是Game Kit并不是仅仅开发游戏用的,它为开发者提供了两个非常实用的功能:使用Bonjour通过蓝牙进行点对点的网络传输功能,以及应用内语音聊天功能.有意思的是,语音 ...
- 理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
ajax的4种方法:$.get.$.post.$getJSON.$ajax. 1.$.get $.get()方法使用GET方式来进行异步请求,它的语法结构为: $.get( url [, data] ...
- Jmeter进阶篇之保存测试结果
Jmeter现在真的是一款越来越流行的接口测试工具. 但是通过和老大哥LR相比较,可能有部分同学觉得,LR的图表功能,报告功能不要太强大. 但是小弟jmeter在这方面其实并不差... 今天我们就来学 ...
- WEB服务器----Apache 安装配置
1.官网下载需要的安装包(包括主安装包和依赖包的下载)可以直接使用Linux的wget进行下载: httpd安装包下载地址:http://mirrors.hust.edu.cn/apache//htt ...
- JMeter 参数化之利用CSV Data Set Config从文件读取参数并关联变量
参数化之利用CSV Data Set Config从文件读取参数并关联变量 by:授客 QQ:1033553122 1. 添加CSV Data Set Config 右键线程组->配置元 ...
- Angular基础(四) 创建Angular应用
应用(Application)是由组件构成的树.树的根部是最顶层的组件即应用本身,启动的时候,浏览器会最先渲染顶层组件,然后根据树形结构,迭代渲染子组件.组件是可装配的,可以互相组合以构成更大的组件. ...
- python之with语句的原理
首发时间:2018-02-23 15:28 之前看到一篇博客说博主python面试时遇到面试官提问with的原理,而那位博主的博文没有提及with原理,故有此文. 关于with语句,官方文档中是这样描 ...
- 设计模式--Proxy
转自:http://blog.csdn.net/dan_xp/article/details/1820852 最近一直在看java的设计模式 ,感觉印象最深刻的就是"面向接口编程" ...
- Hadoop2.7.6_05_mapreduce-Yarn
1. MAPREDUCE原理 Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带默认 ...
- MATLAB矩阵的LU分解及在解线性方程组中的应用
作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 三.实验程序 五.解答(按如下顺序提交电子版) 1.(程序) (1)LU分解源程序: function [ ...