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

 

 

 

目录
相关文章
|
C++
Brute Force & STL --- UVA 146 ID Codes
 ID Codes    Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=82&mosmsg=Submission+received+with+ID+14418598  Mean:   求出可重排列的下一个排列。
890 0
|
Java Linux
BackTrack5 Note01
1、默认用户名:root,默认密码:toor 2、进入图形界面:startx 3、卸载自带的 openjdk:apt-get remove openjdk* 4、安装.
821 0
[LeetCode]--263. Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ug
843 0
|
C++
[LeetCode] Ugly Number II
Stefan is a hero! Personally, I love his first C++ solution. 1 class Solution { 2 public: 3 int nthUglyNumber(int n) { 4 vector ...
836 0
|
C++
[LeetCode] Ugly Number
No matter what language you use on the OJ, you may refer to Stefan's post for a solution. The C++ is rewritten below, just really concise.
576 0
LeetCode 264. Ugly Number II
编写一个程序,找出第 n 个丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
80 0
LeetCode 264. Ugly Number II
LeetCode 263. Ugly Number
编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
103 0
LeetCode 263. Ugly Number
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
128 0
|
C++
STL --- UVA 123 Searching Quickly
UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19296   Mean:  有一个字符串集合Ignore,还有一个文本集合TXT,在TXT中除了Ignore中的单词外其他的都是关键字,现在要你根据这些关键字来给TXT文本排序(根据关键字的字典)。
1111 0
poj 3468 A Simple Problem with Integers
点击打开链接poj 3468 思路:线段树成段更新 分析: 1 最基础的线段树的成段更新的题目,我们只要建好线段树然后进行更新即可 2 注意由于输入的数最大为10^9,因此我们应该使用long long,区间的和已经区间的延时标记都要...
982 0

热门文章

最新文章