CodeForces - 446A DZY Loves Sequences(dp)
题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增。
分析:
1、因为至多修改一个数字,假设修改a[i],
2、若能使a[i] < a[i + 1] 且 a[i] > a[i - 1],则修改a[i]能得到的最长连续子序列长度为l[i - 1] + r[i + 1] + 1。
3、若不满足条件2,则修改a[i]能得到的最长连续子序列长度应取l[i - 1] + 1(即从a[i-1]能向左延伸的最大长度加上a[i]形成的序列)和r[i + 1] + 1的最大值。
4、枚举a[i]取最大值即可。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int l[MAXN];//从a[i]向左延伸的长度
int r[MAXN];//从a[i]向右延伸的长度
int main(){
int n;
scanf("%d", &n);
l[0] = 1;
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
if(i){
if(a[i] > a[i - 1]){
l[i] = l[i - 1] + 1;
}
else{
l[i] = 1;
}
}
}
if(n == 1){
printf("1\n");
return 0;
}
r[n - 1] = 1;
for(int i = n - 2; i >= 0; --i){
if(a[i] < a[i + 1]){
r[i] = r[i + 1] + 1;
}
else{
r[i] = 1;
}
}
int ans = Max(r[1], l[n - 2]) + 1;//修改a[0]或a[n-1]能得到的最长连续序列长度的最大值
for(int i = 1; i < n - 1; ++i){
if(a[i + 1] - a[i - 1] > 1){
ans = Max(ans, l[i - 1] + r[i + 1] + 1);
}
else{
ans = Max(ans, l[i - 1] + 1);
ans = Max(ans, r[i + 1] + 1);
}
}
printf("%d\n", ans);
return 0;
}
CodeForces - 446A DZY Loves Sequences(dp)的更多相关文章
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- CodeForces 444C. DZY Loves Physics(枚举+水题)
转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
- CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...
随机推荐
- 2-10 就业课(2.0)-oozie:11、hadoop的federation(联邦机制,了解一下)
==================================================== Hadoop Federation 背景概述 单NameNode的架构使得HDFS在集群扩展性 ...
- {$DEFINE WANYI}
var Form5: TForm5; {$DEFINE WANYI}implementation{$R *.dfm}procedure TForm5.Button1Click(Sender: TObj ...
- Windows环境下的32位汇编语言程序设计
一个逆向的告诉我可以尝试学一下8086处理器,再回头看一看自己学过的会有提高学呗,8086处理器怎么学....然后就学了8086的汇编, 好友就分享了琢石成器——Windows环境下的32汇编语言设计 ...
- iOS 安全地在主线程执行一个Block
//主线程同步队列 #define dispatch_main_sync_safe(block)\ if ([NSThread isMainThread]) {\ block();\ } else { ...
- JavaScript深入理解对象方法——Object.entries()
Object.entries()方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环也枚举原型链中的属性) 示例 ...
- imp.load_source的用法
imp.load_source(name,pathname[,file])的作用把源文件pathname导入到name模块中,name可以是自定义的名字或者内置的模块名称. 假设在路径E:/Code/ ...
- 019、MySQL取本季度开始时间和本季度结束时间
SELECT QUARTER ( adddate( dy, ) ) QTR, date_add( dy, INTERVAL MONTH ) Q_start, adddate( dy, ) Q_end ...
- C# MQTT M2MQTT
MQTT 入门介绍 MQTT是基于二进制消息的发布/订阅编程模式的消息协议 实现MQTT协议需要客户端和服务器端通讯完成,在通讯过程中,MQTT协议中有三种身份:发布者(Publish).代理(Bro ...
- 《ES6标准入门》(阮一峰)--5.字符串的新增方法
1.String.fromCodePoint() ES5 提供String.fromCharCode()方法,用于从 Unicode 码点返回对应字符,但是这个方法不能识别码点大于0xFFFF的字符. ...
- Maven插件方式使用Mybatis Generator
Mybatis Generator Mybatis Generator简称MBG,可以根据数据库自动生成实体类.单表查询接口及其映射xml文件(也可以选择以注解方式生成). 下面介绍一下以maven插 ...