2018计蒜之道 第二场 A

 题目链接

题意:给个数组,求一个最长子序列长度,子序列相邻两数之间的差值不超过d

思路:因为d<=100,所以暴力一下就行了,一开始我用的map标记,想了一下pi才 1e5,所以干脆用数组来记录了,比较简单的题,直接贴代码

#include<algorithm>
#include<typeinfo>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define pi acos(-1)
ll gcd(ll x, ll y) { return x ? gcd(y%x, x) : y; }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int t, n, d, val, mp[200005];

int main() {
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(6);

	while (cin >> t)
		while (t--) {
			memset(mp, 0, sizeof(mp));
			int ans = 0;
			cin >> n >> d;
			for (int a = 0; a < n; a++){
				cin >> val;
				int maxn = mp[val];
				for (int b = 1; b <= d; b++) {
					if (val - b >= 0)
						maxn = max(maxn, mp[val - b]);
					maxn = max(maxn, mp[val + b]);
				}
				mp[val] = maxn + 1;
				ans = max(ans, mp[val]);
			}
			cout << ans << endl;
		}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/icliuli/article/details/80307923
今日推荐