[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 ...
随机推荐
- 外部引入的js 判断js脚本加载是否完成,完成后执行 相应的动作(以引入百度地图js为例)
可以使用JQuery的 $.getScript(url,function(){});方法 $.getScript("http://api.map.baidu.com/getscript?v= ...
- UI开发总结
1. bootstrap tooltip 修改内容 <i class="ace-icon fa fa-user" id="test-tooltip" ti ...
- web和servlet一样的用法但是有区别
- jdbc问题:Access denied for user ''@'localhost''是因为没输入账户和密码
Access denied for user ''@'localhost' to database 'bjpowernode'
- 常见的爬虫分析库(1)-Python3中Urllib库基本使用
原文来自:https://www.cnblogs.com/0bug/p/8893677.html 什么是Urllib? Python内置的HTTP请求库 urllib.request ...
- windows下载安装MariaDB10.2.17 绿色版
1.下载 https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.17/winx64-packages/mariadb-10.2.17-w ...
- ERROR 000732:Output Geodatabase:Dataset Database Connections\Connection to localhost.sde\SDE.Dataset does not exist or is not supported
ArcCatalog 10中向SDE 数据集导入要素类时,出错:ERROR 000732:Output Geodatabase:Dataset Database Connections\Connect ...
- BZOJ1009 [HNOI2008]GT考试 矩阵
去博客园看该题解 题目 [bzoj1009][HNOI2008]GT考试 Description 阿申准备报名参加GT考试,准考证号为N位数X1X2….Xn(0<=Xi<=9),他不希望准 ...
- IISExpress运行网站时,Service Fabric报Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format.
打开VS TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS,选中 "Use the 64 bit vers ...
- js根据银行卡号进行判断属于哪个银行并返回银行卡类型
在做绑定银行卡,输入银行卡的时候,产品有这么一个需求,需要用户输入银行卡号的时候,显示对应的银行卡名称及简称.于是苦苦寻觅,终于找到了支付宝的开放API,银行卡校验接口 https://ccdcapi ...