[CC-MCO16306]Fluffy and Alternating Subsequence

题目大意:

给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\)。

对于一个序列\(b\),如果以下两个条件之一成立,则称\(b_i\)是一个跳跃序列:

  1. \(b_{2k}<b_{2k-1}\)对所有的\(2k\le n\)都成立,且\(b_{2k}<b_{2k+1}\)对所有的\(2k+1\le n\)都成立。
  2. \(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的更多相关文章

  1. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  3. Alternative Thinking 找规律

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  4. CodeForces - 603A-Alternative Thinking (思维题)

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  5. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. CF #636 (Div. 3) 对应题号CF1343

    unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...

  7. Codeforces Round #636 (Div. 3)

    比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...

  8. codeforc 603-A. Alternative Thinking(规律)

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes   Kevin ha ...

  9. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

随机推荐

  1. 论文阅读笔记十六:DeconvNet:Learning Deconvolution Network for Semantic Segmentation(ICCV2015)

    论文源址:https://arxiv.org/abs/1505.04366 tensorflow代码:https://github.com/fabianbormann/Tensorflow-Decon ...

  2. Django的Session存储Redis环境配置

    第一步:在项目目录下的settings.py中MIDDLEWARE中加上中间件: # session中间件Django项目默认启用Session 'django.contrib.sessions.mi ...

  3. JavaMail在Windows平台下正常发送邮件,部署到Linux后则发送失败

    问题: 在本机(Windows)环境下可以成功发送邮件,但部署到Linux服务器上后不能成功发送,前台不提示错误或提示502. linux下日志提示:javamail isssl false.... ...

  4. 学习笔记: IO操作及序列化

    /// <summary> /// 文件夹  文件管理 /// </summary> public class MyIO {     /// <summary>   ...

  5. James Munkres Topology: Sec 37 Exer 1

    Exercise 1. Let \(X\) be a space. Let \(\mathcal{D}\) be a collection of subsets of \(X\) that is ma ...

  6. mysql-数据库管理安装

    第一节 数据库管理系统 相关网址:www.db-engines.com mysql站点:www.mysql.com mariadb.org   mariadb官方站点 数据库分类: 关系型数据库: o ...

  7. Python 类的内置方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- # 作者:Presley # 邮箱:1209989516@qq.com # 时间:2018-11-04 # p ...

  8. gitlab之三: gitlab邮件通知的配置

    参考 :  https://www.cnblogs.com/lovelinux199075/p/9072265.html gitlab 添加新用户后,会自动发送邮件到填写的邮箱. 实验版本:  11. ...

  9. 使用aws中国的s3时,制订bucket poicy时注意注意……

    { "Version": "2012-10-17", "Statement": [ { "Sid": "Pub ...

  10. sentinel-dashboard安装、运行(ubuntu)

    下载页面https://github.com/alibaba/Sentinel/releases wget -P /opt/downloads https://github.com/alibaba/S ...