哈尔滨理工大学软件与微电子学院程序设计竞赛 Defeat the monster 排序+滑动窗口

链接:https://ac.nowcoder.com/acm/contest/5929/L
来源:牛客网

题目描述

多多最近迷上了网游,游戏中有一只大Boss非常厉害,多多想要组个团打败他。

游戏中有个奇怪的设定,就是打怪的团队人数越多,越容易战胜Boss。

但是如果队伍中的选手能力值差距超过 5 的话,那么Boss将会得到强化。

现在多多作为队长,想要在可选的玩家中挑选尽可能多的队员,但是他又不想让Boss得到强化。多多难住了,不知道怎么办了,你能帮帮他吗?
输入描述:

第一行输入一个整数 N (1 ≤ N ≤ 2·105),代表多多可选的玩家数量。

第二行输入 N 个整数,代表每位玩家的能力值。

输出描述:

输出一个整数,表示多多可选的最大玩家的数量。

示例1
输入
复制

6
1 10 17 12 15 2

输出
复制

3

说明

选 2,4,5 号玩家
选 3,4,5 号玩家

示例2
输入
复制

10
1337 1337 1337 1337 1337 1337 1337 1337 1337 1337

输出
复制

10

题意 : 给定一个数组,要求选出尽可能多的数字,每两个数字之间的差值不能大于 5 5 5
思路 :

  • 排序
  • 每次窗口左右差值大于5就吐掉左边
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)2e5+7)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
	do { \
		cout << "\033[31;1m " << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, a[MAXN];

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	read(n);
	for(int i=1; i<=n; i++) read(a[i]);
	sort(a+1, a+1+n);
	int tmax = 0/*记录答案*/, lef = 1, rig = 0;
	while(lef <= n) {
		while(rig+1 <= n && a[rig+1] <= a[lef]+5) rig ++;
		tmax = max(tmax, rig-lef+1);
		lef ++;
	}
	printf("%d\n", tmax);




#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值