Luo Gu P1296 cow whisper

Title Description

In your cattle, all the cows are kept in a straight row of bullpen. A total of n cows, cows in the i-th position wherein a straight line which may be an integer coordinates pi (0 <= pi <= 10 ^ 8) are represented. In the boring days, cows often share some gossip with other cows in his cowshed. Each cow loudness emitted is the same, and because the energy attenuation of acoustic waves, a sound can be emitted from cows and it does not exceed the distance d (0 <= d <= 10 ^ 4) cows heard, this so called cows can communicate with each other. The maximum distance d now gives all cows can spread position and sound, you compile a program to calculate your cattle in how many of the cows can communicate with each other.

Input Format

Line 1 comprises two integers n, d.

Line 2 contains n integers, each integer coordinates is a pi, describes the location of a cow on a straight line.

Output Format

A number representing the number of cattle in dairy cows can communicate with each other.

Sample input and output

Input # 1
5 10

10 12 16 37 40

Output # 1
4

Description / Tips

Scale data

For 40% of the data, n <= 10 ^ 3;

To 100% of the data, n <= 10 ^ 6.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n=0,d=0;
 6     cin>>n>>d;
 7     int cow[n];
 8     for(int i=0;i<n;i++)
 9     {
10         cin>>cow[i];
11     }
12     for(int i=0;i<n;i++)
13     {
14         if(cow[i]>cow[i+1]){
15             swap(cow[i],cow[i+1]);
16         }
17     }
18     int ans=0;
19     while(n--)
20     {
21         if(cow[n+1]-d<=cow[n]){
22             ans++;
23         }
24     }
25     cout<<ans;
26 }

 

Guess you like

Origin www.cnblogs.com/anbujingying/p/11295163.html