HDU_6695 Welcome Party 【思维】

一、题目

  Welcome Party

二、分析

  最开始的时候分析错了,认为只要找两个类型中的最小差值就可以了,忽略了是求两个类型中最大值的最小差值。

  那么可以对第一个类型进行从大到小排序,枚举这个类型的最大值,那么,枚举过的这个类型的人就不再选这个类型的值,而是选第二类型。

  考虑对第二个类型处理,枚举过的需要维护一个最大值,对于底下还没枚举的,需要维护一个有序的序列,方便查找,如果还没枚举到的需要找到最接近当前枚举的这个值,用$multiset$维护,二分找时可能找不到,也可能找到后是比这个值小的那个更优,这里要处理一下。

  对于取绝对值,$abs$用于整型,$fabs$用于浮点型,一直WA的原因也是这里。

三、AC代码

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define ll long long
 5 #define Min(a,b) ((a)>(b)?(b):(a))
 6 #define Max(a,b) ((a)>(b)?(a):(b))
 7 #define P pair<int, int>
 8 const int MAXN = 1e5 + 13;
 9 struct node {
10     ll x, y;
11     bool operator < (const node &t) {
12         return x > t.x;
13     }
14 }A[MAXN];
15 
16 int main()
17 {
18     //freopen("input.txt", "r", stdin);
19     // freopen("out.txt", "w", stdout);
20     int T, N;
21     scanf("%d", &T);
22     while(T--) {
23         scanf("%d", &N);
24         multiset<ll> ST;
25         for(int i = 1; i <= N; i++) {
26             scanf("%lld%lld", &A[i].x, &A[i].y);
27             ST.insert(A[i].y);
28         }
29         sort(A + 1, A + 1 + N);
30         ll ans = 2e18;
31         ll ym = -1e18;    //记录删掉的y中最大的值
32         for(int i = 1; i <= N; i++) {
33             ST.erase(ST.find(A[i].y));
34             if(ym >= 0) {
35                 ans = Min(ans, abs(A[i].x - ym));
36             }
37             if(!ST.empty()) {
38                 auto itr = ST.lower_bound(A[i].x);
39                 if(itr == ST.end()) {
40                     itr--;  
41                 }
42                 ll res = *itr;
43                 if(res > ym) {
44                     ans = Min(ans, abs(A[i].x - res));
45                 }
46                 if(itr != ST.begin()) {
47                     itr--;
48                     res = *itr;
49                     if(res > ym) {
50                         ans = Min(ans, abs(A[i].x - res));
51                     }
52                 }
53             }
54             ym = Max(ym, A[i].y);
55         }
56         printf("%lld\n", ans);
57     }
58     return 0;
59 }

 

posted @ 2019-08-24 20:01  Dybala21  阅读(345)  评论(0编辑  收藏  举报