8593 最大覆盖问题 two pointer
8593 最大覆盖问题
时间限制:50MS 内存限制:1000K
提交次数:193 通过次数:88
题型: 编程题 语言: G++;GCC;VC
Description


输入格式
第1行是正整数n,(n<=10000)
第2行是整数序列 a1 a2 ... an
输出格式
计算出的最大覆盖区间长度
输入样例
10
1 6 2 1 -2 3 5 2 -4 3
输出样例
5
提示
若依次去求出每个数的最大覆盖长度,则必须有两个嵌套的循环,时间复杂度为O(n^2)。
但此处求所有数的一个最大覆盖长度,倒没有必要每个数的最大覆盖长度都求出来。 初始时,用两个指针i和j指向串末,当ai和aj的关系满足不等式时,j不动,i往左
走,……,直到不等式不满足,记录下长度。
下一步j往左移一个,i不回退,继续上面的比较,若找到更长的覆盖长度,更新。
每循环一次要么i要么j少1;最后i=-1,j=0;共进行了2(n-1)次。所以时间复杂度为O(n)。 我的思路是dp。dp[i]表示以第i个为结尾的最大覆盖长度。然后枚举第i + 1个时,如果其abs还比a[i]小,那么dp[i + 1] = 1,就是自己一个了。否则,因为它比a[i]大了,而a[i]之前也算好了dp[i],就是[i - dp[i] + 1, dp[i] ]这段区间是比abs(a[i])小的了,所以可以不比较这段区间,直接和i - dp[i]比较即可。然后递归下去。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int a[maxn];
int dp[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
a[] = inf;
dp[] = ;
for (int i = ; i <= n; ++i) {
int t = abs(a[i]);
if (t < a[i - ]) {
dp[i] = ;
} else {
int pos = i - - dp[i - ];
while (t >= a[pos]) {
pos = pos - dp[pos];
}
dp[i] = i - pos;
}
}
int ans = ;
for (int i = ; i <= n; ++i) {
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
题解是用了two pointer
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int a[maxn];
int dp[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
int ans = ;
int R = n, L = n;
while (L >= ) {
while (L >= && a[L] <= abs(a[R])) {
--L;
}
ans = max(ans, R - L);
R--;
}
printf("%d\n", ans);
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
fuck 5
1 7 2 -100000 3
ans = 4
8593 最大覆盖问题 two pointer的更多相关文章
- JQUERY 拖拽 draggable droppable resizable selectable sortable
今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下: <script language="JavaScript" type="text/ ...
- JQuery UI - selectable
·概述 Selectable插件允许用户对指定的元素进行选中的动作.此外还支持按住Ctrl键单击或拖拽选择多个元素. 官方示例地址:http://jqueryui.com/demos/selectab ...
- jQuery UI-Draggable 参数集合
·概述 在任何DOM元素启用拖动功能.通过单击鼠标并拖动对象在窗口内的任何地方移动. 官方示例地址:http://jqueryui.com/demos/draggable/ 所有 ...
- 父元素相对定位后,子元素在ie下被覆盖的问题!
<div id="append_parent" style="position: relative;"> <div id="zoom ...
- CSS之边框覆盖
今天想做一个淘宝导航来练练手,遇到了边框覆盖的问题.如下图: li的红色边框盖不住该灰色边框.后来问经验人士告诉我,这种边框覆盖是会出现无法100%保证正常的情况,遂得到如下3中解决方案: 1.以后遇 ...
- c++虚函数,纯虚函数,抽象类,覆盖,重载,隐藏
C++虚函数表解析(转) ——写的真不错,忍不住转了 http://blog.csdn.net/hairetz/article/details/4137000 浅谈C++多态性 http://bl ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
- c++中的隐藏、重载、覆盖(重写)
转自c++中的隐藏.重载.覆盖(重写) 1 重载与覆盖 成员函数被重载的特征: (1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual关键字可有可无. 覆盖是 ...
- 【转】c++重载、覆盖、隐藏——理不清的区别
原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...
随机推荐
- 优化js的执行
避免使用setTimeout和setInterval进行视觉更新操作;使用 requestAnimationFrame. 将长时间运行的JavaScript 从主线程转移到 Web Workers. ...
- 基于官方驱动封装mongodb
还是一如既往先把结构图放出来,上上个版本添加了redis的缓存,但是不满足我的需求,因为公司有项目要求是分布式所以呢,这里我就增加了mongoDb进行缓存分布式,好了先看结构图(1). 总的来说比较蛋 ...
- object_test.py
#方法,属性,私有化加双下划线 ''' __a 从外部无法访问,但是类的内部可以访问.实际上还是能在类外访问这些私有方法,尽管不应该这么做:s._A__a 如果不需要使用这种方法但是又不行让其他对象不 ...
- Js常见的六种报错
EvalError: raised when an error occurs executing code in eval() EvalError:当一个错误发生在()执行的代码RangeError: ...
- ab-如何提交post请求
ab -n 1 -c 1 -p post.txt http://***********/ 文件post.txt中存放要post的数据
- C结构体、C++结构体、C++类的区别
先来说说C和C++中结构体的不同 a) C语言中的结构体不能为空,否则会报错 1>d:\myproject\visual studio 2013\projects\myc++\main.c(71 ...
- MVC 登录后重定向回最初请求的 URL FormsAuthentication.RedirectFromLoginPage
在传统的Asp.net webForm 中如果使用 Form身份验证.登录后重定向到最初请求的页面只需使用 FormsAuthentication.RedirectFromLoginPage 但在MV ...
- ARM之工作模式
前言 以下内容是个人学习之后的感悟,转载请注明出处~ ARM工作模式 ARM工作模式根据功能不同,可分为7类: User Mode:用户模式.操作系统的Task一般以这种模式执行 ...
- 《Android学习笔记》Android Studio如何去除顶部标题栏教程
序] 在初步开发Android应用中,我们会遇到一个问题,顶部标题栏的名字是项目名字(app名字)并且不可编辑.非常的不方便,那么我们有什么办法把他去掉呢? [相关文章] [开发工具] Android ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...