STL or Force --- CSU 1553: Good subsequence
来源:互联网 发布:linux写入文件命令 编辑:程序博客网 时间:2024/06/05 02:26
Good subsequenceProblem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553
View CodeView Code
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 longusing 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 longusing 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;}/**/
0 0
- STL or Force --- CSU 1553: Good subsequence
- csu 1553: Good subsequence
- CSU 1553 Good subsequence(暴力求解)
- [csu oj]1553: Good subsequence(水题)
- CSU 1553-Good subsequence(RMQ)
- [ACM] CSU 1553 Good subsequence(尺取法)
- STL or 线段树 --- CSU 1555: Inversion Sequence
- poj 3061 Subsequence 【尺取法 or STL lower_bound】
- G - Good subsequence
- Rogge: Olympics 'a force for good'
- Maven: good or not
- good material for STL
- 湖南多校对抗赛---Good subsequence
- 300. Longest Increasing Subsequence(good!)
- good man or bad man?
- LightOJ 1051 Good or Bad
- LightOJ 1051 Good or Bad
- code force-27C---Unordered Subsequence (模拟+暴力)
- math --- CSU 1554: SG Value
- STL or 线段树 --- CSU 1555: Inversion Sequence
- 快速幂 --- CSU 1556: Jerry's trouble
- 操作系统——进程控制(二)
- 1001. 害死人不偿命的(3n+1)猜想
- STL or Force --- CSU 1553: Good subsequence
- 三分 --- CSU 1548: Design road
- 三分 --- ZOJ 3203 Light Bulb
- 三分 --- POJ 3301 Texas Trip
- 三分套三分 --- HDU 3400 Line belt
- dp --- Codeforces 245H :Queries for Number of Palindromes
- 搜索 --- 数独求解 POJ 2676 Sudoku
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- dp