hdu 6095 Rikka with Competition---思维题贪心

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=6095

题目大意:

任意两个人相比,相差大于K,分低的淘汰,否则两人都有可能赢,剩下的继续比,问有最多多少人可能赢?

思路:

排序,如果Ai - Ai-1的值大于k,那从Ai-1开始的人都不可能有机会赢了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<set>
 6 #include<cmath>
 7 using namespace std;
 8 const int maxn = 1e5 + 10;
 9 typedef long long ll;
10 int T, n, m, k;
11 int a[maxn];
12 int main()
13 {
14     cin >> T;
15     while(T--)
16     {
17         cin >> n >> k;
18         for(int i = 1; i <= n; i++)cin >> a[i];
19         sort(a + 1, a + n + 1);
20         int ans = 0;
21         for(int i = n; i >= 2; i--)
22         {
23             if(a[i] - a[i - 1] > k)
24             {
25                 ans = i - 1;//从这里开始就不可能赢了
26                 break;
27             }
28         }
29         ans = n - ans;
30         cout<<ans<<endl;
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/fzl194/p/8696640.html