Wave

T3 Wave
题目描述
给定一个长为n的数列,试求一个最长的不稳定波动子序列满足任意偶数项的值不小于其相邻两项的值,且相邻两项的差不小于k。

输入输出格式
输入格式:
输入第一行两个正整数n,k。

第二行n个非负整数描述这个数列。

输出格式:
输出一个整数即为答案。

输入输出样例
输入样例#1:
10 3
2 6 7 9 0 3 7 6 4 4
输出样例#1:
5
说明
对于20%的数据,n<=10^3

对于70%的数据,n<=10^5

对于100%的数据,n<=2*10^5,数列中的数不超过2^31-1


贪心,让偶项尽可能大,奇项尽可能小,就能使序列最长。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.2
17 using namespace std;
18 int n,k;
19 int a[2000010];
20 int b[2000010];
21 int cnt;
22 
23 void in(int &x)
24 {
25     int y=1;
26     char c=g();x=0;
27     while(c<'0'||c>'9')
28     {
29     if(c=='-')
30     y=-1;
31     c=g();
32     }
33     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
34     x*=y;
35 }
36 void o(int x)
37 {
38     if(x<0)
39     {
40         p('-');
41         x=-x;
42     }
43     if(x>9)o(x/10);
44     p(x%10+'0');
45 }
46 int main()
47 {
48     in(n),in(k);
49     in(a[1]);
50     b[++cnt]=a[1];
51     For(i,2,n)
52      {
53          in(a[i]);
54          if(cnt&1)
55          {
56              if(a[i]-b[cnt]>=k)
57              b[++cnt]=a[i];
58              else
59              if(a[i]<b[cnt])
60              b[cnt]=a[i];
61         }
62         else
63         {
64             if(b[cnt]-a[i]>=k)
65              b[++cnt]=a[i];
66              else
67              if(a[i]>b[cnt])
68              b[cnt]=a[i];
69         }
70      }
71      o(cnt);
72      return 0;
73 }
原文地址:https://www.cnblogs.com/war1111/p/7772289.html