Codeforces Round #404 (Div. 2) DE
昨晚玩游戏竟然不小心错过了CF。。我是有多浪啊。 今天总算趁着下课时间补了,感觉最后两题还是挺有意思的,写个题解。
D:
题目大意:
给出一个括号序列,问有多少个子序列 是k个'(' + k个')' 这样的形式。 n<=200000
解法:
对于每个'('的位置,计算以它为最右边的'('的合法子序列数。 假设它左边(包括它)有$l$个'(', 右边有 $r$个')' , 如果子序列的长度2k, 那么
方案数有$\binom{l-1}{k-1} * \binom{r}{k}= \binom{l-1}{l-k} * \binom{r}{k}$
这个式子对$k$求和是 $\binom{l+r-1}{l}$ 可以预处理阶乘O(1)计算出。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <set>
#include <bitset>
using namespace std; typedef long long ll;
typedef pair<int,int> pii; #define N 400010
#define X first
#define Y second const int INF=<<;
const int Mod=;
int ans;
char s[N];
int fac[N],fac_inv[N]; int Power(int x,int p)
{
int res=;
for (;p;p>>=)
{
if (p&) res=1ll*res*x%Mod;
x=1ll*x*x%Mod;
}
return res;
} int C(int n,int m)
{
if (m==) return ;
if (n==) return ;
int res=1ll*fac[n]*fac_inv[m]%Mod;
return 1ll*res*fac_inv[n-m]%Mod;
} int l[N],r[N]; int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); fac[]=fac_inv[]=;
for (int i=;i<N;i++)
{
fac[i]=1ll*fac[i-]*i%Mod;
fac_inv[i]=Power(fac[i],Mod-);
}
int n; scanf("%s",s+);
n=strlen(s+); for (int i=;i<=n;i++) l[i]=l[i-]+(s[i]=='(');
for (int i=n;i>=;i--) r[i]=r[i+]+(s[i]==')'); for (int i=;i<=n;i++)
{
if (s[i]!='(') continue;
if (!r[i]) continue;
ans+=C(l[i]+r[i]-,l[i]);
if (ans>=Mod) ans-=Mod;
} printf("%d\n",ans);
return ;
}
E:
题目大意:
一开始有一个1-n的排列,然后每次操作交换两个数,每次询问逆序对数。 n<=200000,Q<=50000
解法:
假设我们交换了a[l],a[r], 那么逆序对数如何改变呢?
首先逆序对数会减少[l+1,r-1]这个区间里 比a[r]大的数 和 比a[l]小的数。
然后会增加[l+1,r-1]这个区间里 比a[r]小的数 和 比a[l]大的数。
因此我们需要快速计算某个区间里有多少个数比x大的。
我的做法是分块,每个块维护一个树状数组即可。 复杂度比较悬,换了几次块的大小,极限数据本地跑要10s,但是CF机子2s就跑完了?
代码:
By lzw4896s, contest: Codeforces Round # (Div. ), problem: (E) Anton and Permutation, Accepted, #
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath> using namespace std; typedef long long ll;
#define N 200010 int n,m,block;
int v[N],id[N]; inline int Lowbit(int x){return x&-x;} struct BIT
{
int c[N];
void Insert(int x,int v)
{
while (x<=n)
{
c[x]+=v;
x+=Lowbit(x);
}
}
int Sum(int l,int r)
{
if (l>r) return ;
int res=,x=r;
while (x)
{
res+=c[x];
x-=Lowbit(x);
}
x=l-;
while (x)
{
res-=c[x];
x-=Lowbit(x);
}
return res;
}
}tree[]; int Query(int l,int r,int low,int high)
{
int res=;
if (id[r]-id[l]<=)
{
for (int i=l;i<=r;i++) res+=(v[i]>=low && v[i]<=high);
return res;
}
for (int i=l;i<=block*id[l];i++) res+=(v[i]>=low && v[i]<=high);
for (int i=block*(id[r]-)+;i<=r;i++) res+=(v[i]>=low && v[i]<=high);
for (int i=id[l]+;i<=id[r]-;i++) res+=tree[i].Sum(low,high);
return res;
} int main()
{
///freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); int Q,l,r; ll ans=;
scanf("%d%d",&n,&Q);
if (n>) block=;
else block=;
for (int i=;i<=n;i++) v[i]=i;
//block=4; for (int i=;i<=n;i+=block)
{
int j=min(n,i+block-); m++;
for (int k=i;k<=j;k++)
{
id[k]=m;
tree[m].Insert(k,);
}
}
while (Q--)
{
scanf("%d%d",&l,&r);
if (l==r)
{
printf("%I64d\n",ans);
continue;
}
if (l>r) swap(l,r);
if (v[l]>v[r]) ans--;
else ans++;
tree[id[l]].Insert(v[l],-);
tree[id[r]].Insert(v[r],-);
if (r-l>)
{
ans-=Query(l+,r-,v[r]+,n);
ans-=Query(l+,r-,,v[l]-);
ans+=Query(l+,r-,,v[r]-);
ans+=Query(l+,r-,v[l]+,n);
}
swap(v[l],v[r]);
tree[id[l]].Insert(v[l],);
tree[id[r]].Insert(v[r],);
//cout<<"eee\n";
printf("%I64d\n",ans);
//for (int i=1;i<=n;i++) cout<<v[i]<<" ";
//cout<<endl;
/*for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++) cout<<tree[i].Sum(j,j)<<" ";
cout<<endl;
}*/
} return ;
}
Codeforces Round #404 (Div. 2) DE的更多相关文章
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
- Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...
- Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)
A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...
- Codeforces Round #404 (Div. 2)A,B,C
A. Anton and Polyhedrons 题目链接:http://codeforces.com/contest/785/problem/A 智障水题 实现代码: #include<bit ...
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分
C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...
- Codeforces Round #404 (Div. 2) B. Anton and Classes 水题
B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...
- Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题
A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...
- 【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2
http://codeforces.com/blog/entry/50996 官方题解讲得很明白,在这里我复述一下. 枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号, ...
随机推荐
- Restful Web Service部署到weblogic 12c
介绍一下环境: 首先需要下载一个jaxrs-ri-2.22.2.zip的包 采用Jdeveloper 12c版本,jdk1.8 WebLogic Server 12.2.1版本 Restful项目建立 ...
- CSS的伪元素和伪类
css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类—— :hover, :active, :visited, :focus. 常见伪元素——::first-letter,::first-l ...
- final修饰符的三种使用场景
final有三种使用场景,各自是修饰变量.方法和类.不管哪种修饰.一旦声明为final类型.你将不能改变这个引用了,编译器会检查代码,假设你试图再次初始化,编译器会报错.以下我来详细说说每一种修饰场景 ...
- NTFS数据流和web安全
NTFS流简单介绍: NTFS因为它的稳定性 强大的功能 以及它所提供的安全性而成为一种更优越的文件系统,NTFS交换数据流(ADSs)是为了和Macintosh的HFS文件系统兼容而设计的,它使用资 ...
- 【AS3 Coder】任务四:噪音的魅力(下)
在之前的两篇文章中我们介绍了PerlinNoise的两种用途:创建云雾等自然效果以及用作随机数提供源.那么在这一章中,贫道将隆重介绍一位perlinNoise的好基友:DisplacementMapF ...
- scrapy-splash抓取动态数据例子九
一.介绍 本例子用scrapy-splash抓取众视网网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...
- Rails generate的时候不生成assets和test
我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...
- Centos6.2上做nginx和tomcat的集成及负载均衡(已实践)
Centos6.2上做nginx和tomcat的集成及负载均衡 ---------------------------------------------------------Jdk-------- ...
- Android实现蓝牙耳机连接
代码地址如下:http://www.demodashi.com/demo/13259.html 前言 讲讲android对于蓝牙耳机连接技术的实现 今天涉及的内容有: 流程讲解 新建广播Bluetoo ...
- ios图片轮播效果
代码地址如下:http://www.demodashi.com/demo/11959.html ImageCarousel 简单封装的图片轮播器 内存过大由于我加载的图片分辨率较高(4k) 文件目录 ...