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. spring mvc提交日期类型参数

    如题,spring mvc直接提交Date类型参数会报错,400 bad request的错误.在controller里加上 @InitBinder protected void initBinder ...

  2. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  3. hdu-5666 Segment(俄罗斯乘法or大数乘法取模)

    题目链接: Segment Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) P ...

  4. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  5. HttpClient入门教程

    HttpClient使用详解与实战一:https://www.jianshu.com/p/375be5929bed

  6. Python interview_python

    https://github.com/taizilongxu/interview_python 1 Python的函数参数传递 strings, tuples, 和numbers是不可更改的对象,而l ...

  7. Numbers Exchange

    题意: Eugeny有n张卡片,他希望和Nikolay交换一些卡片使得他拥有的奇数数字和偶数数字的卡片数目一样,且所有数字都不同. Nikolay有m张卡片,分别写着1到m.问最少交换几次,能够满足要 ...

  8. 1.6-1.10 使用Sqoop导入数据到HDFS及一些设置

    一.导数据 1.import和export Sqoop可以在HDFS/Hive和关系型数据库之间进行数据的导入导出,其中主要使用了import和export这两个工具.这两个工具非常强大, 提供了很多 ...

  9. c++重载输入输出运算符

    1 最好打断点看看哦 2例子 #include <iostream> using namespace std; class Complex2 { public: Complex2(, ) ...

  10. Tomcat之the jre_home environment variable is not defined correctly this environment variable is need

    参考https://blog.csdn.net/qq_30507287/article/details/53981851 今天在服务器的tomcat上部署.war文件,双击startup闪退,然后在t ...