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的更多相关文章

  1. JQUERY 拖拽 draggable droppable resizable selectable sortable

    今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下:   <script language="JavaScript" type="text/ ...

  2. JQuery UI - selectable

    ·概述 Selectable插件允许用户对指定的元素进行选中的动作.此外还支持按住Ctrl键单击或拖拽选择多个元素. 官方示例地址:http://jqueryui.com/demos/selectab ...

  3. jQuery UI-Draggable 参数集合

    ·概述    在任何DOM元素启用拖动功能.通过单击鼠标并拖动对象在窗口内的任何地方移动.    官方示例地址:http://jqueryui.com/demos/draggable/      所有 ...

  4. 父元素相对定位后,子元素在ie下被覆盖的问题!

    <div id="append_parent" style="position: relative;"> <div id="zoom ...

  5. CSS之边框覆盖

    今天想做一个淘宝导航来练练手,遇到了边框覆盖的问题.如下图: li的红色边框盖不住该灰色边框.后来问经验人士告诉我,这种边框覆盖是会出现无法100%保证正常的情况,遂得到如下3中解决方案: 1.以后遇 ...

  6. c++虚函数,纯虚函数,抽象类,覆盖,重载,隐藏

    C++虚函数表解析(转) ——写的真不错,忍不住转了  http://blog.csdn.net/hairetz/article/details/4137000 浅谈C++多态性  http://bl ...

  7. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  8. c++中的隐藏、重载、覆盖(重写)

    转自c++中的隐藏.重载.覆盖(重写) 1 重载与覆盖 成员函数被重载的特征: (1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual关键字可有可无. 覆盖是 ...

  9. 【转】c++重载、覆盖、隐藏——理不清的区别

    原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...

随机推荐

  1. hdu 1361.Parencodings 解题报告

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-se ...

  2. html5--3.8 input元素(7)

    html5--3.8 input元素(7) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...

  3. python berkeley DB操作——打开btree索引文件中的database

    打开BDB中某个索引中的数据库代码: from bsddb3 import db import bsddb3 as bsddb print db.DB_VERSION_STRING mydb = db ...

  4. Power Strings--KMP

    https://cn.vjudge.net/problem/POJ-2406 上面是比赛链接. 题目意思很明确,问最多是多少个子串连接而成的? 这个需要用到KMP,很好的理解KMP的Next数组.Ne ...

  5. C++之pair与make_pair

    std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型. 例如std::pair<int,float> 或者 std::pair<double,d ...

  6. Building Objective-C static libraries with categories(ObjC、all_load、force_load)

    https://developer.apple.com/library/mac/qa/qa1490/_index.html    之所以使用该标志,和Objective-C的一个重要特性:类别(cat ...

  7. ASP.NET:Global.asax

    ylbtech-.Net-ASP.NET:Global.asax 1.返回顶部 1. 一.定义:Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 A ...

  8. Visual Studio Ultimate 2013 下载地址

    VS2013_RTM_ULT_CHS.iso 文件大小:2.87G 百度网盘下载地址: http://pan.baidu.com/s/1bn4gavX 微软官网下载地址: http://downloa ...

  9. mfc为对话框添加启动画面

    新建CwzdSplash类 CwzdSplash.h #pragma once class CWzdSplash : public CWnd { DECLARE_DYNAMIC(CWzdSplash) ...

  10. 【218】◀▶ IDL 操作符号说明

    参考:Operators —— 运算符 01   Relational_Operators 比较运算符. 02   Mathematical_Operators 数学运算符. 03   Logical ...