简单题
Description
给出一个长度为n的数列,求最少删除几个数可以让剩下的数中 最大值-最小值<=m
Input
第一行输入两个数n,m(1≤n≤100,0≤m≤100)
第二行有n个数(0≤xi≤100)
ps:本题为多组输入
Output
输出所求答案
Sample Input 1
3 1 2 1 4
Sample Output 1
1
Sample Input 2
3 0 7 7 7
Sample Output 2
0
Sample Input 3
6 3 1 3 4 6 9 10
Sample Output 3
3
Hint
解释:
样例1可以删除4
样例2不用删除
样例3删除1,9,10,此时剩下3,4,6满足题意
让每个数字都先取余m,那么符合这样的关系的数字一定是相等的,
然后在这n个数字中,能否找到找k个相同的数字,找到为Yes,反之No
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<stack>
using namespace std;
#define inf 0x3fffffff
#define LL long long
map<int,int>ma;
int main()
{
int n,k,m,a;
while(~scanf("%d%d%d",&n,&k,&m))
{
ma.clear();
int flag=0;
while(n--)
{
scanf("%d",&a);
if(flag) continue;
if(++ma[a%m]>=k) flag=1;
}
if(flag)printf("Yes\n");
else printf("No\n");
}
}

本文介绍了一种算法,用于解决从给定数列中通过删除部分元素使剩余元素的最大值与最小值之差不超过指定阈值的问题。算法首先利用取余操作简化数值关系,接着采用计数方法统计相同余数出现的次数,最终确定需要删除的元素数量。
802

被折叠的 条评论
为什么被折叠?



