bzoj4361 isn(树状数组优化dp+容斥)
4361: isn
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 938 Solved: 485
[Submit][Status][Discuss]
Description
Input
Output
一行一个整数,描述答案。
Sample Input
1 7 5 3
Sample Output
HINT
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 2005
const ll P=1e9+;
int n,m,A[N],B[N],p[N];
ll fac[N],s[N][N],f[N][N],g[N],ans;
inline ll Md(ll a){return a<P?a:a-P;}
void Add(int id,int x,ll v){for(;x<=n;x+=x&-x)s[id][x]=Md(s[id][x]+v);}
ll Sum(int id,int x){ll re=; for(;x;x-=x&-x)re=Md(re+s[id][x]); return re;}
void prep(){
fac[]=;
for(ll i=;i<=n;++i) scanf("%d",&A[i]),B[i]=A[i],fac[i]=fac[i-]*i%P;
sort(B+,B+n+); m=unique(B+,B+n+)-B-;
for(int i=;i<=n;++i) p[i]=lower_bound(B+,B+m+,A[i])-B;//离散化
}
int main(){
scanf("%d",&n); prep(); Add(,,);
for(int i=;i<=n;++i)
for(int j=i;j;--j)
f[i][j]=Md(f[i][j]+Sum(j-,p[i])),Add(j,p[i],f[i][j]);
for(int i=;i<=n;++i)
for(int j=i;j<=n;++j)
g[i]=Md(g[i]+f[j][i]);
for(ll i=;i<=n;++i)
ans=Md(Md(ans+g[i]*fac[n-i]%P)-g[i+]*fac[n-i-]%P*(i+)%P+P);
printf("%lld",ans);
return ;
}
bzoj4361 isn(树状数组优化dp+容斥)的更多相关文章
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- LUOGU P2344 奶牛抗议 (树状数组优化dp)
传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...
- 【题解】Music Festival(树状数组优化dp)
[题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...
- 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
[题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...
- BZOJ4361 isn 树状数组、DP、容斥
传送门 不考虑成为非降序列后停止的限制,那么答案显然是\(\sum\limits_{i=1}^N cnt_i \times (N-i)!\),其中\(cnt_i\)表示长度为\(i\)的非降序列数量 ...
- 4.9 省选模拟赛 划分序列 二分 结论 树状数组优化dp
显然发现可以二分. 对于n<=100暴力dp f[i][j]表示前i个数分成j段对于当前的答案是否可行. 可以发现这个dp是可以被优化的 sum[i]-sum[j]<=mid sum[i] ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】
Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...
随机推荐
- SVN常用命令说明
本文转载至 http://www.blogjava.net/jasmine214--love/archive/2011/01/12/342839.html :1.5 及更新版本 名词说明: WC:W ...
- 安装 RabbitMQ
Ubuntu 16.04 安装 RabbitMQ #1 更新 $ sudo apt-get update $ sudo apt-get upgrade #2 安装Erlang $ cd /tmp $ ...
- Golang etcd服务注册与发现
//sevice.go package discovery import ( "context" "errors" "sync" " ...
- Go etcd初探
1.etcd集群的配置 SET IP1_2380=http://127.0.0.1:2380 SET IP1_2379=http://127.0.0.1:2379 SET IP2_2380=http: ...
- iOS - Harpy版本更新工具兼容版本第三方库
Harpy(兼容版) git地址:https://github.com/yangchao0033/Harpy ###(iOS5-9适配版本,基于ArtSabintsev/Harpy v3.4.5) 提 ...
- vmare 往 virtualbox迁移
vmare实在太卡了.抓狂. 于是想迁移到virtualbox观察下. 谷歌了下方案,发现众说纷纭. 有操作超级复杂的,比如:http://stackoverflow.com/questions/69 ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- Spring-Boot自动装载servlet
Spring-Boot自动装载servlet 本人spring-boot相关博客均自己手动编写,但技术均从简书 恒宇少年 处学习,该大佬一直是我的偶像,鉴于能充分理解,所以已做笔记的方式留下这些文档, ...
- @staticmethod和classmethod
之前一直搞不清楚这两个类方法有什么区别,今天着重学习了一下 @staticmethod是静态方法,不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样. class C(object): ...
- Caused by: java.lang.ClassNotFoundException: Could not load requested class :XXX.XXX.XXX 异常处理
在ssh整合中:出现的异常 Exception sending context initialized event to listener instance of class org.springfr ...