HDU 6695 Welcome Party (璐績)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6695 Welcome Party (璐績)相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/--' title='--'>--   using   nim   href   abi   nts   related   line   mes   

2019 鏉數澶氭牎 10 1005

棰樼洰閾炬帴锛?a href="http://acm.hdu.edu.cn/showproblem.php?pid=6695">HDU 6695

姣旇禌閾炬帴锛?a href="http://acm.hdu.edu.cn/search.php?field=problem&key=2019+Multi-University+Training+Contest+10&source=1&searchmode=source">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\) 涓汉锛?span class="math inline">\(2\) 绉嶈妭鐩紝姣忎釜浜鸿琛ㄦ紨鍏朵腑鐨勪竴绉嶈妭鐩紝姣忕鑺傜洰鑷冲皯鏈変竴浜鸿〃婕斻€傜敤 \(x_i\) 鍜?\(y_i\) 琛ㄧず绗?\(i\ (1\le i\le n)\) 涓汉琛ㄦ紨涓ょ鑺傜洰鐨勮兘鍔涘€笺€傜幇鍦ㄨ浣胯〃婕旂涓€绉嶈妭鐩殑浜轰腑鐨勮兘鍔涙渶澶у€间笌琛ㄦ紨绗簩绉嶈妭鐩殑浜轰腑鐨勮兘鍔涙渶澶у€间箣宸渶灏忥紝姹傝繖涓渶灏忓€笺€?/p>

棰樿В

璐績

濡備笅鍥撅紝缁存姢涓や釜闆嗗悎 \(s_1\) 鍜?\(s_2\)銆?/p>

鎸?\(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\) 鍗冲彲銆?/p>

姣旇禌涓槦鍙嬬敤绾挎鏍戣繃鐨勩€傝禌鍚庢垜鐢?\(multiset\) 鍐欎簡涓€涓嬨€?/p>

鎶€鏈浘鐗? src=

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;

以上是关于HDU 6695 Welcome Party (璐績)的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ4109 Welcome Party

浙江省赛-Welcome Party

Welcome Party ZOJ - 4109 (思维+并查集)

ZOJ - 4109 - Welcome Party (并查集 + BFS + 优先队列)

ZOJ4109 Welcome Party (并查集+优先队列)

HDU 4173 Party Location(计算几何,枚举)