CF258D Little Elephant and Broken Sorting 概率dp

28 篇文章 0 订阅
11 篇文章 0 订阅

Description


有一个 1 ∼ n 1 \sim n 1n的排列,会进行m次操作,操作为交换a,b。每次操作都有 50 % 50\% 50%的概率进行。

求进行m次操作以后的期望逆序对个数。
n , m ≤ 1000 n,m \le 1000 n,m1000

Translated by Youngsc

Solution


bzoj权限号到期了gg,心态崩了.jpg

记f[i,j]为a[i]>a[j]的概率,那么答案实际上就是 ∑ i = 1 n ∑ j = i + 1 n f [ i , j ] \sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^{n}f[i,j] i=1nj=i+1nf[i,j]
初始的f全都是1,然后每次改的时候直接算就可以了

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef double db;

const int N=2005;

db f[N][N],r[N];

int a[N];

int main(void) {
	int n,m; scanf("%d%d",&n,&m);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,1,n) rep(j,1,n) f[i][j]=(a[i]>a[j]);
	for (;m--;) {
		int x,y; scanf("%d%d",&x,&y);
		if (x==y) continue;
		rep(i,1,n) if (i!=x&&i!=y) {
			r[i]=f[x][i];
			f[x][i]=(f[x][i]+f[y][i])*0.5;
			f[i][x]=1-f[x][i];
		}
		rep(i,1,n) if (i!=x&&i!=y) {
			f[y][i]=(r[i]+f[y][i])*0.5;
			f[i][y]=1-f[y][i];
		}
		f[x][y]=f[y][x]=0.5;
	}
	db ans=0;
	rep(i,2,n) rep(j,1,i-1) ans+=f[j][i];
	printf("%.8lf\n", ans);
	return 0;
}
Sure, here is a sorting function template in C++: ```c++ #include <iostream> #include <vector> #include <string> #include <algorithm> template <typename T> void sortVector(std::vector<T>& vec) { std::sort(vec.begin(), vec.end()); } int main() { std::vector<int> intVec = {3, 1, 4, 2, 5}; std::vector<float> floatVec = {3.5, 1.2, 4.1, 2.0, 5.7}; std::vector<std::string> stringVec = {"apple", "banana", "car", "dog", "elephant"}; std::cout << "Before sorting:" << std::endl; for (auto i : intVec) std::cout << i << " "; std::cout << std::endl; for (auto f : floatVec) std::cout << f << " "; std::cout << std::endl; for (auto s : stringVec) std::cout << s << " "; std::cout << std::endl; sortVector(intVec); sortVector(floatVec); sortVector(stringVec); std::cout << "After sorting:" << std::endl; for (auto i : intVec) std::cout << i << " "; std::cout << std::endl; for (auto f : floatVec) std::cout << f << " "; std::cout << std::endl; for (auto s : stringVec) std::cout << s << " "; std::cout << std::endl; return 0; } ``` This function template takes a vector of any type `T` and sorts it using the `std::sort` function from the `<algorithm>` header. The main function creates three vectors of different types (integer, floating point, and string), fills them with some values, prints them before sorting, sorts them using the `sortVector` function template, and then prints them again after sorting. You can compile and run this program to see the sorting in action. The output should look something like this: ``` Before sorting: 3 1 4 2 5 3.5 1.2 4.1 2 5.7 apple banana car dog elephant After sorting: 1 2 3 4 5 1.2 2 3.5 4.1 5.7 apple banana car dog elephant ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值