我是靠谱客的博主 时尚太阳,这篇文章主要介绍【水排序】#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
复制代码
1
2
5 10 10 20 50 60 65
output
复制代码
1
6
input
复制代码
1
2
5 1 55 30 29 31 55
output
复制代码
1
6

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部