我是靠谱客的博主 时尚太阳,最近开发中收集的这篇文章主要介绍【水排序】#32 A. Reconnaissance,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

A. Reconnaissance
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights area1, a2, ..., an centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment.

Ways (1, 2) and (2, 1) should be regarded as different.

Input

The first line contains two integers n and d (1 ≤ n ≤ 1000, 1 ≤ d ≤ 109) — amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers — heights of all the soldiers in Bob's detachment. These numbers don't exceed 109.

Output

Output one number — amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d.

Sample test(s)
input
5 10
10 20 50 60 65
output
6
input
5 1
55 30 29 31 55
output
6

排序后遍历,每个元素向后找m以内差距的元素个数乘二求和

#include <cmath>   
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <iostream>  
#include <algorithm>
using namespace std;
int hgt[1001];
int main()
{
	int n,m,cnt,ans=0;
	cin>>n>>m;
	memset(hgt,0,sizeof hgt);
	for(int i=0;i<n;i++)
	{
		cin>>hgt[i];
	}
	sort(hgt,hgt+n);
	for(int i=0;i<n;i++)
	{
		cnt=0;
		for(int j=i+1; hgt[j]<=hgt[i]+m && j<n; j++ )
		{
			cnt++;
		}
		ans+= 2*cnt;
	}
	printf("%d",ans);
	return 0;
}


最后

以上就是时尚太阳为你收集整理的【水排序】#32 A. Reconnaissance的全部内容,希望文章能够帮你解决【水排序】#32 A. Reconnaissance所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部