做法①:RMQ(预处理NLOGN+后续跳跃蜜汁复杂度)

满足题意的区间的条件转换:

1.长度为R-L+1则最大值也为R-L+1

2.区间内的数不重复

当RMQ(L,R)!=R-L+1时 因为已经保证了 i~r[i] 之间的数不重复 则RMQ(L,R)必定大于当前的R-L+1 所以我们需要至少跳到 i+RMQ(L,R)-1

#include<bits/stdc++.h>
using namespace std;
int a[], r[];
int where[];
int dp[][];
int n,ans;
void rmqinit() {
for (int i = ; i <= n; i++) {
dp[i][] = a[i];
}
for (int j = ; ( << j) <= n; j++) {
for (int i = ; i + ( << j) - <= n; i++) {
dp[i][j] = max(dp[i][j - ], dp[i + ( << (j - ))][j - ]);
}
}
}
int rmq(int l, int r) {
int x = ;
while ( << (x + ) <= r - l + ) {
x++;
}
return max(dp[l][x], dp[r - ( << x) + ][x]);
}
int main() {
cin >> n;
r[n] = n;
for (int i = ; i <= n; i++) {
cin >> a[i];
}
rmqinit();
where[a[n]] = n;
for (int i = n - ; i >= ; i--) {
if (where[a[i]]) {
r[i] = min(r[i + ], where[a[i]] - );
} else {
r[i] = r[i + ];
}
where[a[i]] = i;
}
for(int i=;i<=n;i++)
{
int j=i;
while(j<=r[i])
{
if(rmq(i,j)==j-i+)
{
ans++;
j++;
}
else
{
j=i+rmq(i,j)-;
}
}
}
cout<<ans<<endl;
}

做法②:哈希(O(N))

给每个数一个哈希值 从左往右扫一次 从右往左扫一次 如果扫到1就停下检查答案直到数组末尾或碰到下一个1  注意单个的1每次只能算一次

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int MAXN = ;
int n, ans, T;
ll num[MAXN];
p hsh[MAXN];//每个数的哈希值
p prehsh[MAXN];//数组前缀的哈希值
p sumhsh[MAXN];//1~X数的哈希值
p update(p x, p y) { //哈希异或计算
x.first ^= y.first;
x.second ^= y.second;
return x;
}
int getans(int x) {
ll maxn = ; //从第一个1到下一个1或末端所经过的所有数最大值
ll r = x; //现在遍历到哪个数
int now = ; //目前符合条件的答案
while (r < n && num[r + ] != ) { //如果还没有到头且下一个不是1
r++; //到达下一个
maxn = max(maxn, num[r]); //维护最大值
if (r - maxn >= ) {
p xx;
xx = update(prehsh[r], prehsh[r - maxn]);
if (xx == sumhsh[maxn]) {
now++; //如果最大值不超过遍历过的数的数量且子串的哈希值与sumhsh[maxn]对应则答案++
}
}
}
return now;
}
int main() {
ll x = ;
cin >> n;
for (int i = ; i <= n; i++) {
cin >> num[i];
x ^= num[i];
}
for (int i = ; i <= n; i++) { //分配1~n对应每个值的哈希值
hsh[i].first = x ^ rand();
hsh[i].second = x ^ rand();
sumhsh[i] = hsh[i];
if (i != ) {
sumhsh[i] = update(sumhsh[i], sumhsh[i - ]); //计算数字1~i的哈希前缀
}
}
for (T = ; T <= ; T++) {
for (int i = ; i <= n; i++) {
prehsh[i] = hsh[num[i]];
if (i != ) {
prehsh[i] = update(prehsh[i], prehsh[i - ]); //计算数组前i个数的哈希前缀
}
}
for (int i = ; i <= n; i++) {
if (num[i] == ) {
if (T == ) { //单个1只能从单个方向计算一次
ans++;
}
ans += getans(i);
}
}
reverse(num + , num + + n);
}
cout << ans << endl;
return ;
}

Codeforces 1175F The Number of Subpermutations的更多相关文章

  1. Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)

    Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...

  2. Codeforces 1175F The Number of Subpermutations (思维+rmq)

    题意: 求区间[l, r]是一个1~r-l+1的排列的区间个数 n<=3e5 思路: 如果[l,r]是一个排列,首先这里面的数应该各不相同,然后max(l,r)应该等于r-l+1,这就能唯一确定 ...

  3. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  4. Codeforces 40 E. Number Table

    题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...

  5. Codeforces 124A - The number of positions

    题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...

  6. codeforces Soldier and Number Game(dp+素数筛选)

    D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...

  7. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

  8. Codeforces 980E The Number Games 贪心 倍增表

    原文链接https://www.cnblogs.com/zhouzhendong/p/9074226.html 题目传送门 - Codeforces 980E 题意 $\rm Codeforces$ ...

  9. Codeforces 980E The Number Games - 贪心 - 树状数组

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...

随机推荐

  1. 刀塔OMG塔防1.26b单机版使用方法

    使用方法 1.确保魔兽的版本为1.26以上(低版本会报错),如果版本不对用附件中的版本转换器转一下 2.把 omg.w3x 复制到 魔兽的MAPS文件夹 WarcraftIII 游戏根目录\Maps\ ...

  2. http与https的主要区别

    HTTP与HTTPS的主要区别如下: 1.https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用. 2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输 ...

  3. C#操作Memcached帮助类

    在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...

  4. 澎湃新闻速览版UWP 隐私策略

    ThePaper UWP 此为 澎湃新闻速览版 的隐私策略,本隐私策略内容会不定期更新,以最新内容为主. 若您已经阅读并了解以下内容后,并继续使用该软件,即表示您已同意该协议. 内容: 这是澎湃新闻的 ...

  5. 【转帖】Samba AD DC(域控制器)的配置

    Samba AD DC(域控制器)的配置 http://lihaitao.cn/?p=299 山东前辈写的blog 挺好的.. 改天做实验. 发表于2017年3月7日由李海涛 ■Samba AD DC ...

  6. 常见的几种web攻击

    1. SQL注入 2. OS命令注入 3. 跨站脚本攻击(XSS) 4. HTTP首部注入 5. 会话劫持 6. 跨站点请求伪造(CSRF) 7. 点击劫持 8. DoS

  7. 20190728-Python爬取视频&切割视频&视频加水印

    1.视频爬取 1.下载视频的源码如下: import os import requests from bs4 import BeautifulSoup import threading from bj ...

  8. Pycharm 配置houdini

    一.houdini开发环境配置 1.添加Python可执行文件 2.设置代码自动补全 刚刚添加的Python.exe,右侧点击加号,依次添加以上长方形中的文件,路径会根据个人安装路径有所变化,后面的目 ...

  9. 【leetcode】【二分 | 牛顿迭代法】69_Sqrt(x)

    题目链接:传送门 题目描述: 求Sqrt(x),返回整数值即可. [代码]: #include<bits/stdc++.h> using namespace std; ; /* int m ...

  10. Mysql定时备份[Windows]

    基于mysql5.6.39版本 一.备份脚本 1.windows环境创建批处理文件 @echo off rem ******MySQL backup start****** set mysqlHome ...