csu 1553: Good subsequence (最长连续子序列)

http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2071&pid=6

题意:有一个由n个数组成的序列

要求出一个满足 max-min<=k 的最长子序列

思路:(听说数据大的情况可以用单调栈解决 但是我只是纯粹暴力了)

首先枚举左界 把max和min都赋值为 a[i]

再枚举右界 每次判断max-min是否小于等于k

求出最大值

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<ctype.h>
using namespace std;
int a[10000+100];
int main()
{
    int n,k;
    int i,j;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int ans=1;
        for(i=0;i<n;i++) scanf("%d",&a[i]);
        for(i=0;i<n;i++)
        {
            if(n-i<ans) break;
            int temp=1;
            int maxx=a[i];
            int minn=a[i];
            for(j=i+1;j<n;j++)
            {
                if(a[j]>maxx)
                {
                    maxx=a[j];
                }
                else if(a[j]<minn)
                {
                    minn=a[j];
                }
                if(maxx-minn>k) break;
                temp++;
            }
            if(ans<temp) ans=temp;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-08-10 15:00:59

csu 1553: Good subsequence (最长连续子序列)的相关文章

csu 1553: Good subsequence

1553: Good subsequence Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 549  Solved: 190 [Submit][Status][Web Board] Description Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a

POJ 1458 Common Subsequence(最长公共子序列LCS)

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[

求和为0的最长连续子序列长度

  题意:给定一个数组,数组中元素的值只能是1或者-1,求其和为0的最长连续子序列的长度: 数组为1,-1,1,-1,1,-1,1,-1,其结果为:8 数组为1,1,-1,1,1,-1,-1,其结果为:6 解析: 通过分析可知,要使其和为0,只有当1和-1的个数相等时,才会成立,但题目要求是连续子序列,所以单纯统计其1和-1个数不可取. 由题目中求最长连续子序列,可想到动态规划来求解,动态规划的求解既是寻找其状态转移方程和建立状态转移表的过程 设dp[i]为下标为i及其之前数组中所有元素的和,

674. Longest Continuous Increasing Subsequence 最长连续增长的子序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i

STL or Force --- CSU 1553: Good subsequence

Good subsequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 Mean: 给你一个长度为n的序列和一个值k,让你找出一个子序列,满足在这个子序列中max-min的值<=k,求这个子序列最长的长度. analyse: 这题做法很多,直接暴力枚举每一个数为起点. Time complexity: O(n) Source code:  方法一(暴力): // Memory Time //

LCS(Longest Common Subsequence 最长公共子序列)

问题描述 最长公共子序列,英文缩写为LCS(Longest Com #include <bits/stdc++.h> const int MAX=1010; char x[MAX]; char y[MAX]; int DP[MAX][MAX]; int b[MAX][MAX]; using namespace std; int PRINT_LCS(int b[][MAX],char *x,int i,int j) { if(i==0||j==0) return 1; if(b[i][j]==1

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ...,

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] O