HDU 6695 Welcome Party (贪心)

2019 杭电多校 10 1005

题目链接:HDU 6695

比赛链接:2019 Multi-University Training Contest 10

Problem Description

The annual welcome party of the Department of Computer Science and Technology is coming soon! Many students have been applying to show up at the welcome party, and every one of them can choose to sing a song or play crosstalk. This troubles the chief director a lot: how to arrange the program list, such that every student can have a chance to show up on the stage, and the satisfactory value of audiences is maximized?

To cope with this problem, the director proposes a model. In this model, every student has two attributes: the singing ability and crosstalking ability. The satisfactory value of audiences to singings is the maximum singing ability among all students that choose to sing a song; similarly, the satisfactory value to crosstalks is the maximum crosstalking ability among all students that choose play crosstalk. The strange thing is, the overall satisfactory value to the whole party is negatively related to the absolute difference between the satisfactory values to singings and crosstalks. The problem is, what is the minimum possible absolute difference between the satisfactory values of the two types of programs?

Note that:

  • every student should choose exactly one type of programs to play;
  • at least one student should sing a song, and at least one student should play crosstalk.

Input

The first line of input consists of a single integer \(T (1\le T\le 70)\), the number of test cases.

Each test case starts with a line of a single integer \(n (2\le n\le 100000)\), denoting the number of students applying to show up on the stage. Then follow \(n\) lines, each containing two integers \(x\) and \(y (0\le x,y\le 10^{18})\), denoting the singing ability and crosstalking ability of a student.

It is guaranteed that the sum of \(n\) over all test cases never exceeds \(1000000\).

Output

For each test case, output a single integer, denoting the minimum possible absolute difference between the satisfactory values of the two types of programs.

Sample Input

2
5
27 46
89 13
55 8
71 86
22 35
3
3 5
4 7
6 2

Sample Output

3
1

Solution

题意

有 \(n\) 个人,\(2\) 种节目,每个人要表演其中的一种节目,每种节目至少有一人表演。用 \(x_i\) 和 \(y_i\) 表示第 \(i\ (1\le i\le n)\) 个人表演两种节目的能力值。现在要使表演第一种节目的人中的能力最大值与表演第二种节目的人中的能力最大值之差最小,求这个最小值。

题解

贪心

如下图,维护两个集合 \(s_1\) 和 \(s_2\)。

按 \(x\) 从大到小枚举。假设 \(x_i\) 为 \(x\) 中的最大值 (下图中的 \(4\)),则比 \(x_i\) 大的都选择 \(y\),也就是取 \(s_1\) 中的最大值 (下图中的 \(8\))。比 \(x_i\) 小的取与 \(x_i\) 最接近的 \(y\) (下图中的 \(3\)),因为更大的 \(y\) 可以选择 \(x\) (下图中的 \(7\) 可以用 \(2\) 替换)。然后取两个的较大值更新到 \(ans\) (下图中 \(|3 - 4| < |8 - 4|\) 取 \(8 - 4\)),维护最小值 \(ans\) 即可。

比赛中队友用线段树过的。赛后我用 \(multiset\) 写了一下。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 5;
const ll inf = 2e18;

struct STU {
    ll x, y;
} s[maxn];
ll maxy[maxn];

int cmp(STU s1, STU s2) {
    return s1.x > s2.x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        multiset<ll> s1, s2;
        for(int i = 0; i < n; ++i) {
            cin >> s[i].x >> s[i].y;
            s2.insert(s[i].y);
        }
        sort(s, s + n, cmp);
        ll ans = inf;
        for(int i = 0; i < n; ++i) {
            s2.erase(s2.find(s[i].y));  // 一个人只能选择一种表演
            if(!s1.empty()) {
                ans = min(ans, abs(*s1.rbegin() - s[i].x));
            }
            if(!s2.empty()) {
                multiset<ll>::iterator it = s2.lower_bound(s[i].x); // 找到第一个大于等于 s[i].x 的 y
                if(it == s2.end()) {
                    --it;
                }
                ll tmp = abs(*it - s[i].x);
                if(tmp < ans && (s1.empty() || *it > *s1.rbegin())) {
                    ans = tmp;
                }
                if(it != s2.begin()) {  // 找到最后一个小于 s[i].x 的 y
                    --it;
                    tmp = abs(*it - s[i].x);
                    if(tmp < ans && (s1.empty() || *it > *s1.rbegin())) {
                        ans = tmp;
                    }
                }
                s1.insert(s[i].y);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wulitaotao/p/11403208.html

时间: 2024-11-07 12:50:52

HDU 6695 Welcome Party (贪心)的相关文章

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 1045 Fire Net 贪心

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh

HDU 1338 Game Prediction 贪心

Problem Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. Du

hdu 1009 FatMouse&#39; Trade(贪心)

题目来源:hdu 1009 FatMouse' Trade FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54581 Accepted Submission(s): 18299 Problem Description FatMouse prepared M pounds of cat food, ready

hdu 4925 Apple Tree(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4925 尽量让每棵苹果树周围都施肥,每次找到一个空地种上苹果树之后,使其周围的空地施肥,不再种苹果树. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector>

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上