[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. Java+selenium之WebDriver定位页面元素(二)

    Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法: 单个对象的定位方法,多个对象的定位方法和层级定位 1. 定位单个元素 // 对于元素的属性包含 id 的情况适用,推 ...

  2. HTTP 599: SSL certificate problem: unable to get local issuer certificate错误

    自己在用 PySpider 框架爬虫运行代码后时出现 HTTP 599: SSL certificate problem: unable to get local issuer certificate ...

  3. 在 Python 中使用 JSON

    在 Python 中使用 JSON 本教程将会教我们如何使用 Python 编程语言编码和解码 JSON.让我们先来准备环境以便针对 JSON 进行 Python 编程. 环境 在我们使用 Pytho ...

  4. python no module named builtins

    使用python2时出现此错误. 解决办法: pip install future

  5. vector的 []

    摘自<C++编程剖析> #include <iostream> #include <vector> using namespace std; int main() ...

  6. Neo4j导入本地csv问题

    把要导入的文件放到D盘,LOAD CSV WITH HEADERS FROM "file:///D:/xx.csv" AS line create (:node); 总提示输入错误

  7. 请推荐几个asp.net下做网站的好的开源框架

    1.We7 CMS We7 CMS是由西部动力开发的一款充分发掘互联网Web2.0(如博客.RSS等)的信息组织优势,将其理念利用到政府企事业网站的构建.组织.管理中的网站建设和管理方面的产品. 系统 ...

  8. Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)

    Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...

  9. BZOJ2141 排队 树状数组 分块

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2141.html 题目传送门 - BZOJ2141 题意 给定一个序列 $a$ ,先输出原先的逆序对数. ...

  10. docker下搭建zipkin for mysql

    docker pull openzipkin/zipkin 新建docker-compose.yml加入以下内容,自行修改. version: ' services: # The zipkin pro ...