2019HDU多校赛第十场E、Welcome Party

Welcome Party

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1479    Accepted Submission(s): 387


 

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≤T≤70), the number of test cases.

Each test case starts with a line of a single integer n (2≤n≤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≤x,y≤1018), 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

 

 

 

题意

有n个人,每个人有两个权值xi,yi,把这些人分为两组,一组取xi的最大值,一组取yi的最大值

求这两个最大值的差的最小值

 

题解

先按x从大到小排序,这样我们就可以枚举x的最大值

显然,当我们枚举到一个xi时,x值比它大的人都应该分到y组

所以我们只需要把1~i-1的y值的前缀最大值存一下,与当前的xi来相减,更新答案

我们还需要一个set来维护后面的y值与当前的xi最小的差(为什么后面可以直接取最接近的y值?因为其他y值不优的人就可以把他分到x组,而他的x值又比当前的xi小,所以不会影响答案)

边做边删除set里面的元素就可以啦

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
#define N 100005
#define LL long long
inline LL gi()
{
    char c;LL num=0ll;
    while((c=getchar())<'0'||c>'9');
    while(c>='0'&&c<='9'){num=num*10ll+1ll*c-48ll;c=getchar();}
    return num;
}
struct node{
    LL x,y;
    int i;
    node(){}
    node(LL a,LL b,int c){x=a;y=b;i=c;}
    bool operator < (const node &t)const{
        return y<t.y||(y==t.y&&x<t.x)||(y==t.y&&x==t.x&&i<t.i);
    }
}a[N];
set<node> s;
set<node>::iterator it;
bool cmp(node q,node w){return q.x>w.x||(q.x==w.x&&q.y<w.y);}
inline LL ab(LL x){return x<0?-x:x;}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        s.clear();
        int n,i;
        long long ans=0,mx=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            a[i].x=gi();a[i].y=gi();a[i].i=i;
            s.insert(a[i]);
            ans=max(ans,max(a[i].x,a[i].y));
        }
        sort(a+1,a+n+1,cmp);
        for(i=1;i<=n;i++){
            mx=max(mx,a[i-1].y);
            if(i>1)//注意这种情况
                ans=min(ab(mx-a[i].x),ans);
            s.erase(a[i]);
            it=s.lower_bound(node(0,a[i].x,0));
            if(it!=s.end())
                ans=min(ab(max((*it).y,mx)-a[i].x),ans);
            if(it!=s.begin()){
                it--;ans=min(ab(max((*it).y,mx)-a[i].x),ans);
            }
        }
        printf("%lld\n",ans);
    }
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值