CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列。
析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得,最后枚举中间值即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m; }
int f[maxn], g[maxn], a[maxn]; int main(){
while(scanf("%d", &n) == 1){
a[0] = 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
f[0] = 0; f[1] = 1;
for(int i = 2; i <= n; ++i)
if(a[i] > a[i-1]) f[i] = f[i-1] + 1;
else f[i] = 1;
g[n] = 1;
for(int i = n-1; i > 0; --i)
if(a[i] < a[i+1]) g[i] = g[i+1] + 1;
else g[i] = 1;
int ans = 0;
for(int i = 1; i <= n; ++i){
ans = Max(ans, f[i]+g[i]-1);
if(i < n) ans = Max(ans, f[i]+1);
if(i > 1) ans = Max(ans, g[i]+1);
if(i > 1 && a[i] > a[i-1]) ans = Max(ans, f[i-1]+g[i]);
if(i < n && a[i] < a[i+1]) ans = Max(ans, f[i]+g[i+1]);
if(i > 1 && i < n && a[i+1] - a[i-1] > 1) ans = Max(ans, f[i-1]+g[i+1]+1);
}
printf("%d\n", ans);
}
return 0;
}
CodeForces 446A DZY Loves Sequences (DP+暴力)的更多相关文章
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- CodeForces - 446A DZY Loves Sequences(dp)
题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...
- CodeForces 447C DZY Loves Sequences DP
题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来. #inc ...
- 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 447C. DZY Loves Sequences 解题报告(446A)
题目链接:http://codeforces.com/problemset/problem/447/C 题目意思:给出 一个 包含 n 个数的序列你,从中需要找出这个序列的最长子串,满足在里面只修改其 ...
- codeforces C. DZY Loves Sequences
http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
随机推荐
- CSU1160
十进制-十六进制 Time Limit: 1 Sec Memory Limit: 128 MB Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. ...
- java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数
最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...
- [POJ2774][codevs3160]Long Long Message
[POJ2774][codevs3160]Long Long Message 试题描述 The little cat is majoring in physics in the capital of ...
- 解决json_encode中文乱码
在使用json_encode之前把字符用函数urlencode()处理一下,然后再json_encode,输出结果的时候在用函数urldecode()转回来
- ****Call to a member function item() on a non-object
A PHP Error was encountered Severity: Error Message: Call to a member function item() on a non-objec ...
- AOJ 0118 Property Distribution (DFS)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46522 简单DFS,题目翻译参考 http://blog.csdn.net ...
- Java:PPT(X)转图片、PDF和SVG
(一) 简介: 工作中,PowerPoint文档有时需要被转换为PDF/图像文件来存档.因为PDF或图片的页面布局是固定的,很难被修改且能被大多数设备打开,所以PDF或者图片比起PowerPoint格 ...
- 洛谷 P1318 积水面积
P1318 积水面积 题目描述 一组正整数,分别表示由正方体迭起的柱子的高度.若某高度值为x,表示由x个正立方的方块迭起(如下图,0<=x<=5000).找出所有可能积水的地方(图中蓝色部 ...
- 我的arcgis培训照片12
来自:http://www.cioiot.com/successview-381-1.html
- java编程思想-复用类
/* 一个文件中只能有一个public类 并且此public类必须与文件名相同 */ class WaterSource { private String s; WaterSource() { Sys ...