首页 > 其他分享 >D. Buying gifts

D. Buying gifts

时间:2023-03-09 23:57:30浏览次数:37  
标签:int max gifts second test Buying first

D. Buying gifts

Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city.

There are $n$ departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from $1$ to $n$. It is known that gifts in the first store of the $i$ department cost $a_i$ rubles, and in the second store of the $i$ department — $b_i$ rubles.

Entering the mall, Sasha will visit each of the $n$ departments of the mall, and in each department, he will enter exactly one store. When Sasha gets into the $i$-th department, he will perform exactly one of two actions:

Buy a gift for the first friend, spending $a_i$ rubles on it.
Buy a gift for the second friend, spending $b_i$ rubles on it.
Sasha is going to buy at least one gift for each friend. Moreover, he wants to pick up gifts in such a way that the price difference of the most expensive gifts bought for friends is as small as possible so that no one is offended.

More formally: let $m_1$ be the maximum price of a gift bought to the first friend, and $m_2$ be the maximum price of a gift bought to the second friend. Sasha wants to choose gifts in such a way as to minimize the value of $\lvert m_1 - m_2 \rvert$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1\,000$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 500\,000$) — the number of departments in the mall.

Each of the following $n$ lines of each test case contains two integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$) — the prices of gifts in the first and second store of the $i$ department, respectively.

It is guaranteed that the sum of $n$ over all test cases does not exceed $500\,000$.

Output

Print one integer — the minimum price difference of the most expensive gifts bought to friends.

Example

input

2
2
1 2
2 1
5
1 5
2 7
3 3
4 10
2 5

output

0
1

Note

In the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend — in the second department, or vice versa. In the first case, $m_1 = m_2 = 1$, and in the second case — $m_1 = m_2 = 2$. In both cases, the answer is $0$. In the second test case, you can buy gifts for the first friend in the $2$, $4$ and $5$ departments, and for the second friend — in the $1$ and $3$ departments.So $m_1 = \max(2, 4, 2) = 4$, $m_2 = \max(5, 3) = 5$. The answer is $\lvert 4 - 5 \rvert = 1$.

 

解题思路

  现在是根据自己的理解记录的题解,如果存在错误后面会再根据官方的题解进行改正。

  比赛时被C题搞到心态爆炸,导致后面看D题时很浮躁。其实D题比C题简单多了,比赛最后$10$多分钟想到怎么做,但被边界卡住,比赛结束几分钟后才做出来。

  反正我一开始是随便猜个做法,发现对$a$从大到小排序,然后枚举第一个人收到礼物的最大值,假设为$a_i$,由于$a$是从大到小排序,因此第一个人绝对不会收到$a[0 \sim i-1]$中的礼物,且必定会收到礼物$a_i$。与之相对的,第二个人必定会收到$b[0 \sim i-1]$中的所有礼物(因为题目规定每个商店都必定要买一个礼物,既然不能买给第一个人,那么必定是买给第二个人的),且必定不会收到$b_i$。

  那排序后的第$i+1 \sim n-1$家商店的礼物呢?在这些商店中既可以买个第一个人,也可以买个第二个人,都不会影响第一个人收到礼物的最大值$a_i$。因此我们贪心地想能否构造一组购买方案使得第二个人拥有礼物的最大值与$a_i$相差尽可能小呢?

  为此我们可以在$b[i+1 \sim n-1]$中找到满足$x \leq a_i$的最大的数$x$,以及满足$y \geq a_i$的最小的数$y$。记$m = \max\limits_{0 \leq j \leq i-1}{b_j}$,如果选择购买$x$给第二个人,那么两人最大值的差值就是$|a_i - \max \{ m, x \}|$;如果选择购买$y$给第二个人,那么两人最大值的差值就是$|a_i - \max \{ m, y \}|$。因此对这两种情况取个最小值$\min \left\{ |a_i - \max \{ m, x \}|, \ |a_i - \max \{ m, y \}| \right\}$,就是当第一个人收到礼物的最大值为$a_i$时,两人收到礼物最大值的差值的最小值。

  因此可以枚举$0 \sim n-1$来找到所有情况的最小值。其中开个平衡树来维护$b[i+1 \sim n-1]$,开个变量来维护$b[0 \sim i-1]$这个前缀最大值。

  边界处理见代码注释。

  AC代码如下,时间复杂度为$O(n \log n)$:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 5e5 + 10, INF = 0x3f3f3f3f;
 7 
 8 int a[N], b[N], p[N];
 9 
10 void solve() {
11     int n;
12     scanf("%d", &n);
13     for (int i = 0; i < n; i++) {
14         scanf("%d %d", a + i, b + i);
15         p[i] = i;
16     }
17     sort(p, p + n, [&](int i, int j) {    // 根据a[i]从大到小排序 
18         return a[i] > a[j];
19     });
20     multiset<int> st({-INF, INF});    // 插入哨兵,保证每次在平衡树中查找x和y时一定能找到值 
21     for (int i = 0; i < n; i++) {    // 一开始先把b[0~n-1]插入平衡树中 
22         st.insert(b[i]);
23     }
24     int ret = 2e9, maxv = -INF;    // maxv是维护数组b的前缀最大值,一开始先设为-INF。当i=0,很明显第二个人只能收到第p[1]~p[n-1]个商店的礼物,设为-INF不影响答案的求值 
25     for (int i = 0; i < n; i++) {
26         st.erase(st.find(b[p[i]]));    // 把a[p[i]]买给第一个人,从平衡树中删除b[p[i]] 
27         ret = min({ret, abs(a[p[i]] - max(*st.lower_bound(a[p[i]]), maxv)), abs(a[p[i]] - max(*prev(st.upper_bound(a[p[i]])), maxv))});
28         maxv = max(maxv, b[p[i]]);    // 第二个人收到b[0~i]中的所有礼物 
29     }
30     printf("%d\n", ret);
31 }
32 
33 int main() {
34     int t;
35     scanf("%d", &t);
36     while (t--) {
37         solve();
38     }
39     
40     return 0;
41 }

 

参考资料

标签:int,max,gifts,second,test,Buying,first
From: https://www.cnblogs.com/onlyblues/p/17201939.html

相关文章

  • 爱奇迹 Heaven Gifts-电子雾化行业领军品牌
    深圳市爱奇迹科技有限公司(简称​​爱奇迹​​,HeavenGifts)成立于2007年,是一家集设计、制造、销售营运于一体的电子雾化生产企业。爱奇迹坚持践行“以科技的力量为人类健康社......
  • 2558. Take Gifts From the Richest Pile
    packageLeetCode_2558importjava.util.*/***2558.TakeGiftsFromtheRichestPile*https://leetcode.com/problems/take-gifts-from-the-richest-pile/*......
  • Pros and Cons of buying Premium Tech Tool
    Ifyou’veworkedonaVolvoorMackbefore,chancesareyou’veusedPremiumTechToolatsomepointinthepast.Iftheprogramactuallyworks,it’sagreat......
  • Codeforces 1360 D. Buying Shovels
    题意:要买个铲子,商店中有中不同的卖法,依次每一次卖到个铲子,现在只能选择其中的一种买法,问最少买几次同一种的买法,使得刚好买到直接选择小于的AC代码:intn,m,k;......
  • CF103E Buying Sets
    这个世界上怎么有这么巧妙的建模啊。。首先,题目保证了任意\(k\)个子集并的大小\(\gek\)。这说明我们选的数字的数量永远大于等于集合数量如果不考虑数字数量等于集......