[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. Python内置模块之random

    random的方法有 random.random # 返回一个随机的小数 ramdom.uniform # 按照一个区间返回一个小数 random.randint # 返回一个整数 random.ra ...

  2. ffmpeg切割视频

    using System.Diagnostics; public static void carveVideo() { var inputpath = @"d:\1.mp4"; v ...

  3. 史上最简单的 SpringCloud 教程

    史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)史上最简单的Spri ...

  4. k3 Bos开发百问百答

              K/3 BOS开发百问百答   (版本:V1.1)           K3产品市场部       目录 一.基础资料篇__ 1 [摘要]bos基础资料的显示问题_ 1 [摘要]单 ...

  5. C#矩阵求逆

    来源:http://zhidao.baidu.com/link?url=DiqAbq9YUYn3z7QjxGGoF0PLZwN-Y9ecqKB7Gy38JWRD1riMIYukVKXKq88pxtWL ...

  6. VMware搭建虚拟机服务器

    一.需求点描述: 1.在有路由器的情况下,能够通过固定的外网IP访问路由器中某台实体机中运行的虚拟机. 2.能够通过外网IP访问该虚拟机中的ftp.远程连接.iis.tomcat等. 二.原理分析: ...

  7. 一脸懵逼学习Java操作Excel之POI(Apache POI)

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1:下面简单的程序来创建一个空白Microsoft ...

  8. 基于STM32单片机光学指纹识别模块(FPM10A)全教程(基于C语言)

    本文转载,其来源在参考中:1,稍加修改,因为近期使用到这个模块,故而加以整理! 1.平台 首先我使用的是 奋斗 STM32 开发板 MINI板 基于STM32单片机光学指纹识别模块(FPM10A)全教 ...

  9. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  10. EF 数据版本号,处理具体使用方法 RowVersion / Timestamp 使用方法。进行自动处理并发修改

    /* * <div class="form-group"> // 原始 * <div class="form-group hidden"> ...