AGC030D - Inversion Sum

AGC030D - Inversion Sum

题目描述

Solution

考虑 d p dp f i , j f_{i,j} 表示第 i i 个位置的数大于第 j j 个位置的数的概率。
对于每一个询问修改贡献即可。
时间复杂度 O ( n q + n 2 ) O(nq+n^2)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int inv2=(mods+1)>>1;
const int MAXN=3005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int f[MAXN][MAXN],a[MAXN];
inline int upd(int x,int y){ return x+y>=mods?x+y-mods:x+y; }
int main()
{
	int n=read(),q=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++) 
		for (int j=1;j<=n;j++)
			if (a[i]>a[j]) f[i][j]=1;
	for (int i=1;i<=q;i++)
	{
		int x=read(),y=read();
		f[x][y]=f[y][x]=1ll*upd(f[x][y],f[y][x])*inv2%mods;
		for (int j=1;j<=n;j++)
		if (x!=j&&y!=j)
			f[x][j]=f[y][j]=1ll*upd(f[x][j],f[y][j])*inv2%mods,
			f[j][x]=f[j][y]=1ll*upd(f[j][x],f[j][y])*inv2%mods;
	}
	int ans=0;
	for (int i=1;i<=n;i++) 
		for (int j=i+1;j<=n;j++) ans=upd(ans,f[i][j]);
	for (int i=1;i<=q;i++) ans=upd(ans,ans);
	printf("%d\n",ans);
	return 0;
}
发布了94 篇原创文章 · 获赞 6 · 访问量 8540

猜你喜欢

转载自blog.csdn.net/xmr_pursue_dreams/article/details/103470861
sum
今日推荐