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的值>n>>k) { ve.

 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
//  1347K     0MS
//   by : crazyacking
//   2015-03-30-16.02
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
int n,k;
vector<int> ve;
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
//      freopen("C:\\Users\\Devin\\Desktop\\cin.cpp","r",stdin);
//      freopen("C:\\Users\\Devin\\Desktop\\cout.cpp","w",stdout);
        while(cin>>n>>k)
        {
                ve.clear();
                for(int i=0;i<n;++i)
                {
                        int tmp;
                        cin>>tmp;
                        ve.push_back(tmp);
                }
                int ans=1;
                for(int i=0;i<n;++i)
                {

                        int cnt=1;
                        int maxx=ve[i];
                        int minn=ve[i];
                        for(int j=i+1;j<n;++j)
                        {
                                if(ve[j]>=maxx)
                                {
                                        maxx=ve[j];
                                }
                                if(ve[j]<=minn)
                                {
                                        minn=ve[j];
                                }
                                if(maxx-minn>k) break;
                                cnt++;
                        }
                        if(cnt>ans) ans=cnt;
                }
                cout<<ans<<endl;
        }
        return 0;
}
/*

*/
View Code

方法二(STL):

做法很巧妙,用一个multiset来维护:加入当前这个数后满足条件的连续子序列,也就是说每一轮循环set中的元素都是满足条件的。

//  Memory   Time
//  1347K     0MS
//   by : crazyacking
//   2015-03-30-15.53
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
int n,k;
multiset<int> se;
vector<int> ve;
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
//      freopen("C:\\Users\\Devin\\Desktop\\cin.cpp","r",stdin);
//      freopen("C:\\Users\\Devin\\Desktop\\cout.cpp","w",stdout);
        while(cin>>n>>k)
        {
                se.clear();
                ve.clear();
                for(int i=0;i<n;++i)
                {
                        int tmp;
                        cin>>tmp;
                        ve.push_back(tmp);
                }
                int ans=1;
                for(int i=0,j=0;i<n;++i)
                {
                        se.insert(ve[i]);
                        for(;*se.rbegin()-*se.begin()>k;j++)
                        {
                                se.erase(ve[j]);
                        }
                        if(i-j+1>ans) ans=i-j+1;
                }
                cout<<ans<<endl;
        }
        return 0;
}
/*

*/
View Code

 

 

 

目录
相关文章
|
7月前
杭电2095(find your present (2))
杭电2095(find your present (2))
35 0
|
Linux iOS开发 MacOS
报错 sed: 1: extra characters at the end of d command
报错 sed: 1: extra characters at the end of d command
541 0
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
83 0
LeetCode 241. Different Ways to Add Parentheses
|
Perl
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
122 0
|
人工智能 vr&ar
Atcoder--Candy Distribution II--前缀和+map
题目描述 There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l,r) that satisfy the following:
101 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
127 0
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
104 0
HDOJ 1096 A+B for Input-Output Practice (VIII)
HDOJ 1096 A+B for Input-Output Practice (VIII)
108 0
|
算法 C# 索引
算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
1170 0