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();
// 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=;i<n;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
int ans=;
for(int i=;i<n;++i)
{ int cnt=;
int maxx=ve[i];
int minn=ve[i];
for(int j=i+;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 ;
}
/* */

方法二(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();
// 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=;i<n;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
int ans=;
for(int i=,j=;i<n;++i)
{
se.insert(ve[i]);
for(;*se.rbegin()-*se.begin()>k;j++)
{
se.erase(ve[j]);
}
if(i-j+>ans) ans=i-j+;
}
cout<<ans<<endl;
}
return ;
}
/* */

STL or Force --- CSU 1553: Good subsequence的更多相关文章

  1. CSU 1553 Good subsequence(RMQ问题 + 二分)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n nu ...

  2. 1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)

    1553: Good subsequence Submit Page    Summary    Time Limit: 2 Sec     Memory Limit: 256 Mb     Subm ...

  3. csu 1553(RMQ+尺取法)

    1553: Good subsequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 794  Solved: 287[Submit][Statu ...

  4. STL or 线段树 --- CSU 1555: Inversion Sequence

    Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...

  5. 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...

  6. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

  7. CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...

  8. CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 升级版:Uva 1471 题意: 让你求删除一段连续的子序列之后的LIS. 题 ...

  9. csu 1551: Longest Increasing Subsequence Again BIT + 思维

    预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...

随机推荐

  1. C#-DataTable分页代码

    /// <summary> /// DataTable分页并取出指定页码的数据 /// </summary> /// <param name="dtAll&qu ...

  2. 正则指引-量词demo

    class Program { static void Main(string[] args) { string str = "1\"3"; var re1 = Rege ...

  3. LTE工作过程

    LTE工作过程 一.LTE开机及工作过程如下图所示: 二.小区搜索及同步过程 整个小区搜索及同步过程的示意图及流程图如下: 1)   UE开机,在可能存在LTE小区的几个中心频点上接收信号(PSS), ...

  4. System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE

    I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...

  5. 15.6.8-sql小技巧

    取月头月尾: declare @someDay datetime,@firstDay datetime,@endDay datetime set @someDay='2015.2.2' ,) ,) s ...

  6. 物料分类账 [COML] PART 2 - 总体流程

    核心流程概要: [1]. 分类账在物料主数据的影响 首先描述下SAP中物料价格的 物料主数据相关的几个点: q价格控制(Price Control): 决定物料计价方式. S 标准价格(Standar ...

  7. swift 枚举类型

    1:swift的枚举类型是一系列的值,不同于c语言中枚举类型是整数类型.每个枚举定义了个新的类型 2:switch类型匹配 2.1枚举类型和switch单个匹配 enum PlatType{ case ...

  8. Fabric自动部署太方便了

    之前不知道有Fabric工具,每次发布程序到服务器上的时候,基本流程:本地打包程序 -> Ftp上传 -> 停服务器Apache -> 覆盖文件 -> 启动Apache, 非常 ...

  9. [转]阎宏博士的JAVA与模式

    阎宏,1964年出生于天津市.1987年毕业于中国科技大学近代物理系,1990年于中科院理论物理所获硕士学位,1992年获博士学位,翌年赴日本京都大学进行博士后研究工作. 作者曾于美国花旗银行(Cit ...

  10. 百度地图api根据定位获取附近商家(只获取屏幕内)

    根据中心点坐标计算出屏幕2个点(一个最低经纬度,一个最高经纬度),判断这两个点中间的所有坐标的商家..考虑屏幕分辨率之类 移动地图中心点变动,如何异步刷新,判断商家是否已经存在..等... 百度地图a ...