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
//  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;
}
/*

*/

方法二(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;
}
/*

*/

时间: 2024-10-09 00:39:06

STL or Force --- 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

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

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 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 continuous subsequence of the given sequence and its m

[ACM] CSU 1553 Good subsequence(尺取法)

题目地址:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 给定n的数的序列,求最长连续区间满足区间内的数最大值与最小值的差<=k (尺取法) const int maxn=10010; int num[maxn]; int n,k; int MIN,MAX; int main() { while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) sc

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

csu 1553(RMQ+尺取法)

1553: Good subsequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 794  Solved: 287[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 c

2018年省赛热身赛第4场

A:CSU 1547: Rectangle (思维题加一点01背包) B:1548: Design road (思维题 做法:三分找极值) C:1549: Navigition Problem (几何计算+模拟 细节较多) D:1550: Simple String (做得少的思维题,两个字符串能否组成另外一个字符串问题) G:1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力) H:1554: SG Value (巧妙的模拟题,也属于思维题) I:1555:

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

Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一个序列a[n],要你按照要求去构造一个序列b. 序列a[i]表示序列b中的i前面有a[i]个数比i大. 转换一下就是: 已知一个连续的序列(1,2,3,4,...),然后告诉了我们这个序列中每个数前面比本身大的个数,根据这些条件将这个序列调整顺序,找到满足条件的序列. analyse: STL大法好

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

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci(Ci<=109),每条边有一个耗时,如果乘Ci号线地铁到达一个节点换乘Cj号线地铁离开,还需要花费|Ci-Cj|时间. 求1到n的最小花费时间. 题目思路: [最短路][STL] d[u][Ci]表示从1到u,最后一条地铁是Ci号线的最小耗时.按照边做,每条边枚举上一个是从哪一条地铁坐过来的,更新答案

Brute Force &amp; 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: 求出可重排列的下一个排列. analyse: 直接用STL来实现就可.自己手动写了一个,并不复杂.