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 官方题解讲得很明白,在这里我复述一下. 枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号, ...
随机推荐
- SQL CTE 递归分割以逗号分隔的字符串
)) INSERT INTO @t SELECT 'AAA,BBB,CCC' SELECT * FROM @t ;WITH mycte AS ( ,mend,num FROM @t UNION ALL ...
- RHEL7.1 安装openstack juno 一个BUG
错误提示: -- :: ERROR nova.compute.manager [-] [instance: 887e5e40-ebd8--b2f7-afa2a37bdef8] Instance fai ...
- Kubernetes用户指南(四)--应用检查和调试
一.调试 当你的应用开始运行,那么DEBUG是不可避免的问题. 早些时候,我们在描述的是如何通过kubectl get pods来获得Pod的简单状态信息. 但是现在,这里有更多的方式来获得关于你的应 ...
- map-reduce任务的执行流程
(1).客户端提交一个mr的jar包给JobClient(提交方式:hadoop jar ...) (2).JobClient通过RPC和JobTracker进行通信,返回一个存放jar包的地址(HD ...
- C#秘密武器之LINQ to SQL
LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...
- python 页面信息抓取
1. 特点 在python 解析html这篇文章中已经做了初步的介绍,接下来再坐进一步的说明.python抓取页面信息有下面两个特点: 依赖于HTML的架构. 微小的变化可能会导致抓取失败,这取决于你 ...
- Unity3d 嵌入GoogleMap
原地址“http://cl314413.blog.163.com/blog/static/190507976201442371753142/ 新建工程导入Google Maps for Unity包 ...
- Python——@property属性描述符
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的 假设定义了一个类Cls,该类必须继承自object类,有一私 ...
- c++解释--百度百科
c++ C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛:C++支持多种编程范式 --面向对象编程.泛型编程和过程化编程.最新正式标准C++于2014年8月18日公布.[1] 其编程领域 ...
- C#跨平台物联网通讯框架ServerSuperIO(SSIO)正式开源... 1
今天科技类最大的新闻,莫过于微软宣布.NET开发框架开源计划..NET 开源,集成 Clang 和 LLVM 而且自带 Android 模拟器,这意味着 Visual Studio 这个当下最好没有之 ...