http://codeforces.com/problemset/problem/990/B


题意:
有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且ai<=aj+k,那么细菌i就可以吃掉细菌j,问最后可以剩于多少个细菌。
做法:
用map记录相同尺寸细菌的个数,然后用set进行去重排序,从次小的细胞开始比较比该细胞小一点的细胞是否符合要求,如果符合要求,减去被比较的细胞的个数
1 #include <stdio.h>
2 #include <string.h>
3 #include <iostream>
4 #include <string>
5 #include <math.h>
6 #include <algorithm>
7 #include <vector>
8 #include <stack>
9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 #define Bug cout<<"---------------------"<<endl
16 const int mod=1e9+7;
17 const int maxn=2e5+10;
18 using namespace std;
19
20 map<int,int> mp;
21 set<int> st;
22
23 int main()
24 {
25 int n,k;
26 scanf("%d %d",&n,&k);
27 for(int i=0;i<n;i++)
28 {
29 int t;
30 scanf("%d",&t);
31 mp[t]++;
32 st.insert(t);
33 }
34 int num=n;
35 int pre;
36 for(set<int>::iterator it=st.begin();it!=st.end();it++)
37 {
38 if(it==st.begin())
39 {
40 pre=*it;
41 continue;
42 }
43 if(*it>pre&&*it<=pre+k)
44 {
45 num-=mp[pre];
46 }
47 pre=*it;
48 }
49 printf("%d\n",num);
50 return 0;
51 }