[AGC030D] Inversion Sum
Problem Statement
You are given an integer sequence of length $N$: $A_1,A_2,...,A_N$. Let us perform $Q$ operations in order.
The $i$-th operation is described by two integers $X_i$ and $Y_i$. In this operation, we will choose one of the following two actions and perform it:
- Swap the values of $A_{X_i}$ and $A_{Y_i}$
- Do nothing
There are $2^Q$ ways to perform these operations. Find the sum of the inversion numbers of the final sequences for all of these ways to perform operations, modulo $10^9+7$.
Here, the inversion number of a sequence $P_1,P_2,...,P_M$ is the number of pairs of integers $(i,j)$ such that $1\leq i < j\leq M$ and $P_i > P_j$.
Constraints
- $1 \leq N \leq 3000$
- $0 \leq Q \leq 3000$
- $0 \leq A_i \leq 10^9(1\leq i\leq N)$
- $1 \leq X_i,Y_i \leq N(1\leq i\leq Q)$
- $X_i\neq Y_i(1\leq i\leq Q)$
- All values in input are integers.
先定义 \(dp_{i,j}\) 为 \(a_i<a_j\) 的方案数。
然后发现一次交换会改变所有的 \(dp_{i,j}\)。但是大多的 \(dp_{i,j}\) 都是乘上2,除了 \(x_i\) 和 \(y_i\) 相关的 dp 以外。
所以把 \(dp_{i,j}\) 的定义改为 \(a_i>a_j\) 的概率,然后每次只有 \(O(n)\) 个 dp 值会修改。
#include<bits/stdc++.h>
using namespace std;
const int N=3005,P=1e9+7,iv2=P+1>>1;
int n,q,pw[N],dp[N][N],a[N],x,y,ans,g1[N],g2[N];
int main()
{
scanf("%d%d",&n,&q);
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++)
dp[i][j]=a[i]>a[j];
for(int i=pw[0]=1;i<=q;i++)
{
pw[i]=pw[i-1]<<1;
if(pw[i]>=P)
pw[i]-=P;
}
for(int i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
for(int i=1;i<=n;i++)
g1[i]=dp[i][x]&1? dp[i][x]+P>>1:dp[i][x]>>1,g2[i]=dp[x][i]&1? dp[x][i]+P>>1:dp[x][i]>>1;
for(int i=1;i<=n;i++)
{
if(i^x&&i^y)
{
dp[i][x]=(dp[i][x]&1? dp[i][x]+P>>1:dp[i][x]>>1)+(dp[i][y]&1? dp[i][y]+P>>1:dp[i][y]>>1);
if(dp[i][x]>=P)
dp[i][x]-=P;
}
}
for(int i=1;i<=n;i++)
{
if(i^x&&i^y)
{
dp[x][i]=(dp[x][i]&1? dp[x][i]+P>>1:dp[x][i]>>1)+(dp[y][i]&1? dp[y][i]+P>>1:dp[y][i]>>1);
if(dp[x][i]>=P)
dp[x][i]-=P;
}
}
for(int i=1;i<=n;i++)
{
if(i^y&&i^x)
{
dp[i][y]=(dp[i][y]&1? dp[i][y]+P>>1:dp[i][y]>>1)+g1[i];
if(dp[i][y]>=P)
dp[i][y]-=P;
}
}
for(int i=1;i<=n;i++)
{
if(i^y&&i^x)
{
dp[y][i]=(dp[y][i]&1? dp[y][i]+P>>1:dp[y][i]>>1)+g2[i];
if(dp[y][i]>=P)
dp[y][i]-=P;
}
}
dp[x][y]=dp[y][x]=(dp[x][y]+dp[y][x])*1LL*iv2%P;
}
for(int j=1;j<n;j++)
{
for(int k=j+1;k<=n;k++)
{
ans+=dp[j][k];
if(ans>=P)
ans-=P;
}
}
printf("%lld",ans*1LL*pw[q]%P);
}
[AGC030D] Inversion Sum的更多相关文章
- CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP
传送门--Codeforces 传送门--Atcoder 考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\) 故设\(dp_{i,j}\)表示\(a ...
- 「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^ ...
- AGC 030D.Inversion Sum(DP 期望)
题目链接 \(Description\) 给定长为\(n\)的序列\(A_i\)和\(q\)次操作\((x,y)\).对于每次操作\((x,y)\),可以选择交换\(A_x,A_y\)两个数,也可以选 ...
- CSP-S2020 DP专项训练
前言 \(\text{CPS-S2020}\) 已然临近,而 \(\text{DP}\) 作为联赛中的常考内容,是必不可少的复习要点,现根据教练和个人刷题,整理部分好题如下(其实基本上是直接搬--). ...
- AtCoder Grand Contest 030题解
第一次套刷AtCoder 体验良好 传送门 Poisonous Cookies cout<<b+min(c,a+b+); Tree Burning 难度跨度有点大啊 可以证明当第一次转向之 ...
- AGC030 简要题解
A - Poisonous Cookies 题意 有\(A\)个能解毒的普通饼干,\(B\)个能解毒的美味饼干,\(C\)个有毒的美味饼干,求最多能吃多少个美味饼干,每次吃完有毒的饼干后要解毒后才能继 ...
- 【AtCoder】AGC030
A - Poisonous Cookies 有毒还吃,有毒吧 #include <bits/stdc++.h> #define fi first #define se second #de ...
- AtCoder Grand Contest 030 Solution
A - Poisonous Cookies 签到. #include <bits/stdc++.h> using namespace std; #define ll long long l ...
- AtCoder练习
1. 3721 Smuggling Marbles 大意: 给定$n+1$节点树, $0$为根节点, 初始在一些节点放一个石子, 然后按顺序进行如下操作. 若$0$节点有石子, 则移入盒子 所有石子移 ...
随机推荐
- vue 实现 pdf 预览功能
1 技术背景 1.1 Vue.js 简介和特点 Vue.js 是一种用于构建用户界面的渐进式框架.它具有以下特点: 易学易用:Vue.js 的 API 设计简单直观,使得开发者可以快速上手. 响应式数 ...
- SpringBoot 测试实践 - 1:常用的工具
我自己接触到的一些商业或是开源的基于 SpringBoot 项目,它们大部分是没有测试代码的,test 文件夹只有脚手架初始化生成的那个测试类,跟不同的开发聊到这个话题,发现他们中的大部分没有写测试的 ...
- CodeForces 1388C Uncle Bogdan and Country Happiness
题意 给一棵\(n\)节点的树,每个节点有\(a[i]\)个人住,他们从\(1\)号节点回家,回家路上可能从开心的状态变成不开心的状态(但不可以由不开心变为开心),每个节点有个探测器,会探测经过该节点 ...
- ATtiny88初体验(六):SPI
ATtiny88初体验(六):SPI SPI介绍 ATtiny88自带SPI模块,可以实现数据的全双工三线同步传输.它支持主从两种模式,可以配置为LSB或者MSB优先传输,有7种可编程速率,支持从空闲 ...
- 深入理解HTTP的基础知识:请求-响应过程解析
首先,我们从网络协议的最顶层开始讲解,即应用层.在网络通信中,应用层是最接近用户的一层,它负责为特定的网络应用提供服务和功能.应用层协议定义了数据交换的规则和格式,以便不同的应用程序能够相互通信和交换 ...
- 位图(bitmap)原理以及实现
大家好,我是蓝胖子,我一直相信编程是一门实践性的技术,其中算法也不例外,初学者可能往往对它可望而不可及,觉得很难,学了又忘,忘其实是由于没有真正搞懂算法的应用场景,所以我准备出一个系列,囊括我们在日常 ...
- Solution Set -「CF 1485」
「CF 1485A」Add and Divide Link. 贪心.枚举 \([b,b+\log_{2}\text{range}]\) 然后取个 \(\min\). #include<cstdi ...
- SSM-Mybatis笔记
目录 Mybatis-9.28 1.简介 1.1.什么是Mybatis 1.2.持久化 1.3.持久层 1.4 为什么需要Mybatis? 2.第一个Mybatis程序 2.1.搭建环境 2.2.创建 ...
- 什么是DCloud
什么是DCloud1.什么是Dcloud2.主要包括 1. 开发工具 2. 前端框架 3. uniCloud 4. 5+app 5. MUI 6. wap2app1.什么是Dcloud 1. Dclo ...
- orale命令6 rman备份
RMAN:使用ramn进行备份和恢复,rman不依赖系统操作命令,在数据块级别做备份.块级别备份,能只备份变化后的块,实现增量备份.而且不会备份空的块.好处:1.能实现增量备份2.只备份有数据的块,不 ...