A - Exposition CodeForces - 6E
题目链接:https://vjudge.net/contest/202699#problem/A
题意
给一个n个元素的序列,从中挑出最长的子序列,要求子序列中元素差的最大值不超过k。问有几个最长子序列,子序列长度,以及这几个子序列的起始、终止位置。
n<=10e5,k<=10e6
题解
很显然是水题。。
方法1.rmq+二分查找
很暴力很暴力的做法。若用st表维护时间复杂度为(nlogn),线段树(nlognlogn)
即维护出每一段区间的最小最大值
每次查找时确定头结点,二分尾节点
方法2.滑动窗口+单调队列
时间复杂度(n)
很容易发现这是个滑动窗口问题,即固定左端点,向右延伸右端点
若无法延伸,则移动左端点,而右端点维持原状
其中当前区域内的最小值和最大值显然用单调队列维护
代码
方法1:
#include<iostream>
#include<cstdio>
typedef struct re {int x,h,t;};
int a[],f[];
re p1[],p2[];
using namespace std;
int n,k;
void build1(int x,int h,int t)
{
p1[x].h=h; p1[x].t=t;
if (h==t)
{
p1[x].x=a[h]; return;
}
int mid=(h+t)/;
build1(x*,h,mid); build1(x*+,mid+,t);
p1[x].x=max(p1[x*].x,p1[x*+].x);
}
void build2(int x,int h,int t)
{
p2[x].h=h; p2[x].t=t;
if (h==t)
{
p2[x].x=a[h]; return;
}
int mid=(h+t)/;
build2(x*,h,mid); build2(x*+,mid+,t);
p2[x].x=min(p2[x*].x,p2[x*+].x);
}
int query1(int x,int h,int t)
{
if (p1[x].h>t || p1[x].t<h) return();
if (h<=p1[x].h && p1[x].t<=t) return(p1[x].x);
return(max(query1(x*,h,t),query1(x*+,h,t)));
}
int query2(int x,int h,int t)
{
if (p2[x].h>t || p2[x].t<h) return();
if (h<=p2[x].h && p2[x].t<=t) return(p2[x].x);
return(min(query2(x*,h,t),query2(x*+,h,t)));
}
bool check(int x,int y)
{
if (query1(,x,y)-query2(,x,y)<=k) return(true);
else return(false);
}
main(){
scanf("%d%d",&n,&k);
for (int i=; i<=n; i++)
scanf("%d",&a[i]);
build1(,,n);
build2(,,n);
int maxn=;
for (int i=; i<=n; i++)
{
int h=,t=n-i;
while (h<t)
{
int mid=(h+t)/+;
if (check(i,i+mid)) h=mid; else t=mid-;
}
f[i]=h+; maxn=max(maxn,f[i]);
}
int cnt=;
for (int i=; i<=n; i++)
if (f[i]==maxn) cnt++;
printf("%d %d\n",maxn,cnt);
for (int i=; i<=n; i++)
if (f[i]==maxn) printf("%d %d\n",i,f[i]+i-);
return();
}
A - Exposition CodeForces - 6E的更多相关文章
- Codeforces - 6E - Exposition - 尺取
https://codeforc.es/problemset/problem/6/E 既然可以多个log,那就直接map伺候.尺取之后要查询区间里面的最大值和最小值的差.众所周知尺取的时候要是不是有序 ...
- codeforces 6E (非原创)
E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...
- Codeforces Beta Round #6 (Div. 2 Only) E. Exposition multiset
E. Exposition Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/ ...
- [Codeforces Education Round 6E] New Year Tree
[题目链接] https://codeforces.com/contest/620/problem/E [算法] 显然 , 一棵子树的DFS序必然为连续的一段 用线段树维护颜色数即可 [代码] #in ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴
题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- 获取本地计算机名和IP地址
WSADATA wsadata; != WSAStartup(MAKEWORD(, ), &wsadata)) { AfxMessageBox("初始化网络环境失败!"); ...
- MYSQL添加远程用户或允许远程访问
1.用root用户登陆 格式:grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码"; @ 后面是访问M ...
- Alpha 冲刺 (1/10)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...
- python操作三大主流数据库(13)python操作redis之新闻项目实战①新闻数据的导入
1.新闻处理页面redis_news.py #coding:utf-8 import math import redis class RedisNews(object): def __init__(s ...
- VUE (vue-cli)脚手架项目说明
1. 概述 1.1 说明 使用vue-cli快速创建的vue项目目录如下: build -- webpack相关配置以及服务启动文件,配置多依赖于下边的config文件夹中内容 config -- ...
- 整理六百篇web前端知识混总
9个有用的和免费的工具来支持动态网页开发 8个基本的引导工具的网页设计师 11款CSS3动画工具的开发 2016年某前端群题目答案参考 9最好的JavaScript压缩工具 创建响应式布局的10款优秀 ...
- swift 实践- 07 -- UISwitch 开关
import UIKit class ViewController: UIViewController { var uiswitch: UISwitch? override func viewDidL ...
- Confluence 6 配置管理员会话安全的备注
禁用密码确定. Confluence 安装使用自定义授权机制有可能会在密码校验的时候遇到问题.如果必要的话,你可以设置 password.confirmation.disabled 系统属性来禁用密码 ...
- Confluence 6 log4j 日志级别
日志级别 DEBUG - 被设计为用来获得最多的信息和事件,在对应用程序进行调试的时候,这个日志级别通常能够提供最多的有效信息(查看应用程序怎么了) INFO - 有关系统正常运行-计划任务运行,服务 ...
- Confluence 6 从站点首页集中访问面板
如果你选择设置一个页面为你的站点主页面,但是你还是希望你的用户能够访问 Confluence 的主面板,你可以将主面板的连接添加到应用导航(Application Navigator)中. 希望添加 ...