[CC-MCO16306]Fluffy and Alternating Subsequence
[CC-MCO16306]Fluffy and Alternating Subsequence
题目大意:
给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\)。
对于一个序列\(b\),如果以下两个条件之一成立,则称\(b_i\)是一个跳跃序列:
- \(b_{2k}<b_{2k-1}\)对所有的\(2k\le n\)都成立,且\(b_{2k}<b_{2k+1}\)对所有的\(2k+1\le n\)都成立。
- \(b_{2k}>b_{2k-1}\)对所有的\(2k\le n\)都成立,且\(b_{2k}>b_{2k+1}\)对所有的\(2k+1\le n\)都成立。
求\(a\)的最长跳跃子序列的长度\(l\),并求出有多少个长度为\(l\)的跳跃子序列,模\(10^9+7\)。
思路:
\(f[i][0/1][0/1]\)表示考虑到第\(i\)位,当前位是峰/谷,当前序列符合条件1/2时,长度的最大值。\(g[i][0/1][0/1]\)表示相同状态下的方案数。
线段树优化后做做到\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=3e5+1,mod=1e9+7;
int a[N],f[N][2][2],g[N][2][2];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int f[N<<2],g[N<<2];
void push_up(const int &p) {
f[p]=std::max(f[p _left],f[p _right]);
g[p]=0;
if(f[p _left]==f[p]) (g[p]+=g[p _left])%=mod;
if(f[p _right]==f[p]) (g[p]+=g[p _right])%=mod;
}
public:
void modify(const int &p,const int &b,const int &e,const int &x,const int &y,const int &z) {
if(b==e) {
if(y>f[p]) {
f[p]=y;
g[p]=0;
}
if(y==f[p]) {
(g[p]+=z)%=mod;
}
return;
}
if(x<=mid) modify(p _left,b,mid,x,y,z);
if(x>mid) modify(p _right,mid+1,e,x,y,z);
push_up(p);
}
std::pair<int,int> query(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(b==l&&e==r) {
return std::make_pair(f[p],g[p]);
}
std::pair<int,int> pl,pr,ret;
pl=std::make_pair(0,0);
pr=std::make_pair(0,0);
ret=std::make_pair(0,0);
if(l<=mid) pl=query(p _left,b,mid,l,std::min(mid,r));
if(r>mid) pr=query(p _right,mid+1,e,std::max(mid+1,l),r);
if(pl.first>ret.first) {
ret=std::make_pair(pl.first,0);
}
if(pl.first==ret.first) {
(ret.second+=pl.second)%=mod;
}
if(pr.first>ret.first) {
ret=std::make_pair(pr.first,0);
}
if(pr.first==ret.first) {
(ret.second+=pr.second)%=mod;
}
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree sgt[2][2];
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) {
a[i]=getint();
}
for(register int i=1;i<=n;i++) {
f[i][0][1]=f[i][1][1]=1;
g[i][0][1]=g[i][1][1]=1;
for(register int j=0;j<2;j++) {
for(register int k=0;k<2;k++) {
std::pair<int,int> p;
if(j) {
p=sgt[!j][!k].query(1,1,n,a[i],n);
} else {
p=sgt[!j][!k].query(1,1,n,1,a[i]);
}
if(p.first+1>f[i][j][k]) {
f[i][j][k]=p.first+1;
g[i][j][k]=0;
}
if(p.first+1==f[i][j][k]) {
(g[i][j][k]+=p.second)%=mod;
}
}
}
for(register int j=0;j<2;j++) {
for(register int k=0;k<2;k++) {
sgt[j][k].modify(1,1,n,a[i],f[i][j][k],g[i][j][k]);
}
}
}
int ans=0,cnt=0;
for(register int i=1;i<=n;i++) {
for(register int j=0;j<2;j++) {
for(register int k=0;k<2;k++) {
ans=std::max(ans,f[i][j][k]);
}
}
}
for(register int i=1;i<=n;i++) {
for(register int j=0;j<2;j++) {
for(register int k=0;k<2;k++) {
if(f[i][j][k]==ans) {
(cnt+=g[i][j][k])%=mod;
}
}
}
}
printf("%d %d\n",ans,cnt);
return 0;
}
[CC-MCO16306]Fluffy and Alternating Subsequence的更多相关文章
- CF# 334 Alternative Thinking
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心
C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...
- Alternative Thinking 找规律
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
- CodeForces - 603A-Alternative Thinking (思维题)
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF #636 (Div. 3) 对应题号CF1343
unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...
- Codeforces Round #636 (Div. 3)
比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...
- codeforc 603-A. Alternative Thinking(规律)
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes Kevin ha ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
随机推荐
- mac下Fiddler的安装-启动
使用教程参考:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#request 环境安装 Mono安装 首先,Mac下需要 ...
- C/C++中二进制与文本方式打开文件的区别
二进制与文本文件主要有两个大的区别: 1.换行符的区别: Windows平台下 对于Windows文本文件,它们使用回车和换行来表示换行符:如果以“文本”方式打开文件,当读取文件的时候,系统会将所有 ...
- python 内置数据类型之字符串
1.3 字符串 字符串本身就是一个有序(从左至右)的字符的集合.是序列这种类型的一种,后面还要学习列表与元组. 在这一节中,需要了解字符串的定义,特殊字符,转义与抑制转义:字符串基本操作.格式化等. ...
- js 压缩上传的图片方法(默认上传的是file文件)
//压缩图片方法 function compressImg(file,callback){ var src; var fileSize = parseFloat(parseInt(file['size ...
- Python(列表操作应用实战)
# 输入一个数据,删除一个列表中的所有指定元素# 给定的列表数据data = [1,2,3,4,5,6,7,8,9,0,5,4,3,5,"b","a",&quo ...
- mybatis 遍历map;
mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...
- JavaScript编程语言
JavaScript编程语言 JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应 ...
- MySql-8.0.12 安装教程
MySql-8.0.12 安装教程随笔https://www.cnblogs.com/CrazyDemo/p/9409995.html MySQL 安装https://m.runoob.com/mys ...
- [转] 组件库按需加载 借助babel-plugin-import实现
前段时间一直在基于webpack进行前端资源包的瘦身.在项目中基于路由进行代码分离,http://www.cnblogs.com/legu/p/7251562.html.对于公司内部的组件库,所有内容 ...
- PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://xxxx.wsdl'
libxml_disable_entity_loader(false); $client = new \SoapClient($wsdl); 完美解决办法加上 php的soap扩展是否安装 open ...