codeforces 446A DZY Loves Sequences

codeforces   446A   DZY Loves Sequences         题目链接:http://codeforces.com/problemset/problem/446/A

题目大意:给出一个定长为n的数列a,问改动当中一个数后。可能出现的最长严格上升子段的长度是多少。

题目分析:先不考虑“改动当中一个数”这个条件,这样问题就简单多了,从前到后遍历计数就可以(定义一个数组inc[]长度同a,初始化全部点为1,遍历假设当前点a[i]>a[i-1]就置inc[i]=inc[i-1]+1)。那么加上改动一个数这个条件,问题变成什么样了呢?原数列里可能会有一些离散的严格上升子段,它们有的能够接续成一段(当两个严格上升子段中间仅仅隔着一个数&&前一段的段末小于后一段的段头-1时,我们能够把隔在中间的那个数改动使其符合要求),或许不存在这种两段,那么答案就是最长的一段的长度+1(假设数列本身就是严格上升的,答案就是n)。

总之就是找最长,其他情况好说,两段接续怎么搞呢?先用刚才提到的inc数组记下全部从前向后找的结果,能够在遍历时直接得到第i-1个点所在子段的最大长度。那么第i+1个点就须要反其道而行,再定义一个dec数组,从后向前遍历计数,两数组配合求得接续情况的最大值(可能说的不够清楚。详參代码)。

code:

#include<stdio.h>
#include<string.h>
int max(int a,int b,int c)
{
	if(c>=a&&c>=b)return c;
	return a>b?a:b;
}
int main()
{
	int i,j,n,a[100020],inc[100020],dec[100020],ans=0;
	scanf("%d",&n);
	if(n==1||n==2)
	{
		printf("%d\n",n);
		return 0;
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",a+i);
	}
	inc[0]=dec[n-1]=1;
	for(i=1;i<n;i++)
	{
		inc[i]=a[i]>a[i-1]?inc[i-1]+1:1;
		dec[n-i-1]=a[n-i-1]<a[n-i]?dec[n-i]+1:1;
	}
	for(i=1;i<n-1;i++)
	{
		int temp=-1;
		if(a[i-1]<a[i+1]-1&&(inc[i]==1||dec[i]==1))temp=inc[i-1]+dec[i+1]+1;
		else if(a[i-1]<a[i])temp=inc[i-1]+2;
		else if(a[i]<a[i+1])temp=dec[i+1]+2;
		else temp=max(inc[i-1]+1,dec[i+1]+1,0);
		//if(temp>ans&&temp>inc[i])printf("temp==%d\n",temp);
		ans=max(temp,ans,inc[i]);
	}
	ans=max(ans,inc[n-2]+1,dec[1]+1);
	//因为上面的循环是从第二个跑到倒数第二个,所以最后须要这样收一下尾
	printf("%d\n",ans);
	return 0;
}
/*
5676345
5676789
5678345
6
7 2 3 1 4 5
inc
1 1 2 1 2 3
dec
1 2 1 3 2 1
*/

PS:题目仅仅要求改一个数。假设变成改m个数该怎么办呢?我想应该在遍历时记下最优改动点,一遍遍历结束后真的去改动数组a、inc、dec的值,然后再去遍历。这样时间复杂度会骤增为原来的m倍,或许须要更优化的数组存储方法…

PSS:我这种方法不是DP,只是题目上打了个标签dp。期待DP做法…

时间: 2024-08-25 12:36:17

codeforces 446A DZY Loves Sequences的相关文章

CodeForces 446A DZY Loves Sequences (DP+暴力)

题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得,最后枚举中间值即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib>

Codeforces 447C - DZY Loves Sequences

447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll long long const int INF=0x3f3f3f3f; const int N=1e5+5; int a[N]; int f[N],g[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; f

CodeForces 447C DZY Loves Sequences DP

题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来.. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define F first 4 #define S second 5 #define pb push_back 6 #define power(a) ((a)*(a)) 7 #define ENT

Codeforces Round #FF(255) C. DZY Loves Sequences (LIS升级)

题目:C. DZY Loves Sequences (LIS升级) 题意: 在n个数中,最多改变一个数字,并求能够达到的最长严格上升子序列(连续)长度 分析: 考虑第i个数,能否改变后拼接前后两个字串,并维护当前最大值 状态: left[i]:  表示以i为终点的最长严格上升子序列长度 right[i]: 表示以i为起点的最长严格上升子序列长度 dp[i]:   表示改变第i个数后,拼接前后字串的长度 转移方程:       dp[i] = max{left[i-1] + right[i+1] 

[2016-04-13][codeforces][447][C][DZY Loves Sequences]

时间:2016-04-13 23:39:47 星期三 题目编号:[2016-04-13][codeforces][447][C][DZY Loves Sequences] 题目大意:给定一串数字,问改变其中一个数字之和,最长能得到多长的严格增加的子串 分析: 维护每个数字往左和往右能延续多长(严格减,增),然后枚举每个点, 如果这个点已经在一个严格增加的序列中,那么ans =min(n, max(ans , l[i] + r[i] + 1)) 即左右两边延伸之后,改变后面非递增的一个数字 注意这

Codeforces Round #FF(255) DIV2 C - DZY Loves Sequences

A - DZY Loves Hash 水题,开辟一个数组即可 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main(){ int p,n; cin >> p >> n; vector<bool> buckets(302,false); bool flag = fal

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] + 1 2. l[i-1] + 1 3. r[i+1] + 1 //修改a[i] */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN =

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

[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?+?1,?...,?aj (1?≤?i?≤?j?≤?n) a subsegment of the sequence a. The value (j?-?i?+?1) denotes the length of the subsegment. Your task is to find the lon