codeforces 447C. DZY Loves Sequences 解题报告(446A)
题目链接:http://codeforces.com/problemset/problem/447/C
题目意思:给出 一个 包含 n 个数的序列你,从中需要找出这个序列的最长子串,满足在里面只修改其中一个元素,使得这个子串的元素严格递增,求出这个长度是多少。
以为是DP题(它的分类确实是DP题),无从下手。看了下别人写的,有些部分不太明白;根据自己的理解再结合一些别人的,加了些特判,总算过了^_^,我好像觉得我的做法不像是用了DP思想咯........
总体思路就是:设两个数组,分别为inc_left[] 和 inc_right[],顾名思义,对于第 i 个元素,inc_left[i]就是从左到右扫描序列,到达这个inc_left[i]元素的时候,它的递增长度是多少。举个例子,对于test 1 中的 7 2 3 1 5 6
inc_left[i]为: 1 1 2 1 2 3
对于6这个数来说,inc_left[6] = 3 是因为 1 5 6 严格递增。而为什么有些inc_left[i] = 1,是因为前面加上自己,不构成递增序列。inc_right[]依次类推。
这两个数组结合起来就可以求出 2 3 1 5 6 的情况了,即对于某个a[i],只改变a[i]这个数,使得以这个a[i] 为原点,左右扩散起来,能够构成的最长递增序列。
要注意一些特判,例如,当n = 1 时直接输出1;如果输入序列本来已经严格递增,直接输出n。最后一种情况就是,1 1 这种,只改变其中一个元素,答案为2,代码中的
ans = max(ans, max(inc_left[i]+1, inc_right[i]+1)); 就是为了应对这种情况的。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int N = 1e5 + ;
int a[N], inc_left[N], inc_right[N]; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
memset(a, , sizeof(a));
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
if (n == )
printf("1\n");
else
{
int ans = ;
inc_left[] = ;
for (int i = ; i <= n; i++)
{
inc_left[i] = (a[i] > a[i-] ? inc_left[i-]+ : );
ans = max(ans, inc_left[i]);
}
inc_right[n] = ;
for (int i = n-; i >= ; i--)
{
inc_right[i] = (a[i+] > a[i] ? inc_right[i+]+ : );
ans = max(ans, inc_right[i]);
}
if (ans == n) // 处理序列本来就是递增情况
printf("%d\n", n);
else
{
for (int i = ; i <= n; i++)
{
if (a[i+] > a[i-]+) // 这两种情况需排除:1 ? 2 或 1 ? 1,可以更改的至少需要满足1 ? 3
ans = max(ans, inc_right[i+]+inc_left[i-]+);
else
ans = max(ans, max(inc_left[i]+, inc_right[i]+)); // 处理 1 1的情况,只能更改其中一个
}
printf("%d\n", ans);
}
}
}
return ;
}
codeforces 447C. DZY Loves Sequences 解题报告(446A)的更多相关文章
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- codeforces 445B. DZY Loves Chemistry 解题报告
题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...
- CodeForces - 445A - DZY Loves Chessboard解题报告
对于这题本人刚开始的时候觉得应该用DFS来解决实现这个问题,但由于本人对于DFS并不是太熟,所以就放弃了这个想法: 但又想了想要按照这个要求实现问题则必须是黑白相间,然后把是字符是'B'或'W'改为' ...
- CodeForces 447C DZY Loves Sequences DP
题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来. #inc ...
- 【BZOJ3309】DZY Loves Math 解题报告
[BZOJ3309]DZY Loves Math Description 对于正整数\(n\),定义\(f(n)\)为\(n\)所含质因子的最大幂指数.例如\(f(1960)=f(2^3×5^1×7^ ...
- codeforces C. DZY Loves Sequences
http://codeforces.com/contest/447/problem/C 题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值. 思路:先从左边开始求出连续递增 ...
- codeforces 450B. Jzzhu and Sequences 解题报告
题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- [CodeForces - 447C] C - DZY Loves Sequences
C - DZY Loves Sequences DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai ...
随机推荐
- Laravel 5 Form 和 HTML 的使用
最近在用 laravel 5 做例子,在做到表单的时候,习惯性的使用 Form::open() 结果发现提示错误,没有这个类, 好吧,找了找,发现 在laravel 5 中,把 from 和 html ...
- 图片裁剪上传插件——jquery.photoClip.js
想要裁剪图片上传: 需要依赖的的插件为: [jquery.photoClip.js] 插件[iscroll-zoom.js] 插件[hammer.js] 插件 [lrz.all.bundle.js] ...
- android 完美退出应用程序。
Android 程序在点击回退键时,如果只有一个activity,调用finish()方法就能退出界面,如果有多个界面,在调用该方法时,只会销毁当前的activity,显示栈顶的其它activity, ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- FireDac心得
usesFireDAC.Phys.MySQL, FireDAC.Stan.Def, FireDAC.DApt, FireDAC.Comp.Client, FireDAC.Comp.UI, FireDA ...
- Unity3D 异步加载 在 场景加载 中的使用
异步加载 我们想一想玩过的一些游戏,基本都会有加载界面——因为游戏场景数据较大,所以需要加载一小段时间.那为什么一些2D游戏也会有加载界面呢?按理说2D游戏场景会很小,这样做是为了让游戏跑在低端设备上 ...
- HDU - 5974 A Simple Math Problem (数论 GCD)
题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...
- 洛谷——P1057 传球游戏
P1057 传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹 ...
- Answer's Question about pointer
When you create a new pointer, this will be in heap until you delete it. So what you said is sort o ...
- POJ 1260 Pearls (动规)
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7210 Accepted: 3543 Description In ...