湖南多校对抗赛(2015.03.28) G Good subsequence

题意:找到一个序列中极值<=k的最长字串的长度。

解题思路:set容器双递推。

解题代码:

 1 // File Name: g.cpp
 2 // Author: darkdream
 3 // Created Time: 2015年03月28日 星期六 12时04分39秒
 4 
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 #define maxn 100005
26 using namespace std;
27 multiset<int>  st;
28 int n , k ; 
29 int a[maxn]; 
30 int main(){
31    while( scanf("%d %d",&n,&k) != EOF)
32    {
33     int mx = 1;
34     st.clear();
35     for(int i = 1 ;i <= n;i ++)
36     {
37        scanf("%d",&a[i]);
38     }
39     for(int i = 1,j = 1; i <= n;i ++)
40     {
41        st.insert(a[i]);
42        for(;*st.rbegin()-*st.begin() > k ; j ++)
43        {
44           st.erase(st.find(a[j]));
45        }
46        if(i - j +1 >= mx)
47            mx = i-j+1;
48     }
49     printf("%d\n",mx);
50    }
51 return 0;
52 }
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4375240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值