【ACM】2019杭电多校第十场1005 HDU 6695 Welcome Party 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6695

Welcome Party

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

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个人,对于每一个人,选择唱歌的贡献是x,选择说相声的贡献是y,每个人都要做选择,每个项目至少有一个人,

求abs(max x-max y)的最小值

思路:

参考博客:https://blog.csdn.net/Nothing_but_Fight/article/details/99994381

放上代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
set <long long> li,ll;
set <long long>::iterator iter1,iter2;
struct node
{
    long long x,y;
    int pos;
} pe[maxn];
long long maxy[maxn];
bool cmp(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        fill(maxy,maxy+n,0);
        li.clear();
        ll.clear();
        for(int i=0; i<n; i++)
        {
            scanf("%lld%lld",&pe[i].x,&pe[i].y);
            pe[i].pos=i;
        }
        sort(pe,pe+n,cmp);
//        for(int i=0;i<n;i++)
//            printf("*%lld %lld\n",pe[i].x,pe[i].y);
        //说相声的最大值
        maxy[n-2]=pe[n-1].y;
        for(int i=n-3; i>=0; i--)
        {
            maxy[i]=max(maxy[i+1],pe[i+1].y);
            maxy[n-1]=max(maxy[n-1],pe[i].y);
        }
        maxy[n-1]=max(maxy[n-1],pe[n-2].y);
//        for(int i=0;i<=n-1;i++)
//            printf("*%lld\n",maxy[i]);
        long long ans=1e18+5;
        //单独处理0
        ans=min(ans,abs(maxy[0]-pe[0].x));
        li.insert(pe[0].y);
        ll.insert(-pe[0].y);
        for(int i=1; i<n-1; i++)
        {
            if(pe[i].x<=maxy[i])
                ans=min(ans,abs(maxy[i]-pe[i].x));
            else
            {
                iter1=li.lower_bound(pe[i].x);
                iter2=ll.lower_bound(-pe[i].x);
                ans=min(ans,abs(maxy[i]-pe[i].x));
                if(iter1!=li.end())
                    ans=min(ans,abs((*iter1)-pe[i].x));
                if(iter2!=ll.end())
                    ans=min(ans,abs((-*iter2)-pe[i].x));
            }
            li.insert(pe[i].y);
            ll.insert(-pe[i].y);
        }
        //单独处理n-1
        iter1=li.lower_bound(pe[n-1].x);
        iter2=ll.lower_bound(-pe[n-1].x);
        if(iter1!=li.end())
            ans=min(ans,abs((*iter1)-pe[n-1].x));
        if(iter2!=ll.end())
            ans=min(ans,abs((-*iter2)-pe[n-1].x));
        printf("%lld\n",ans);
    }
    return 0;
}
/*
10
10
11 11
4 17
10 10
4 2
3 13
16 9
15 16
19 10
14 13
1 15

5
7 7
12 19
18 4
11 9
8 4

2
7 15
2 11

8
15 6
18 5
12 0
2 6
14 11
16 6
4 15
13 11

3
13 17
6 12
12 16

2
6 2
7 6

2
10 6
5 16

7
4 1
0 17
6 12
5 9
14 13
17 11
3 3

9
9 7
5 5
10 17
2 5
10 19
17 14
11 19
1 9
10 9

5
18 7
4 16
9 17
19 13
2 1


10
10
11 11
4 17
10 10
4 2
3 13
16 9
15 16
19 10
14 13
1 15

5
7 7
12 19
18 4
11 9
8 4

2
7 15
2 11

8
15 6
18 5
12 0
2 6
14 11
16 6
4 15
13 11

3
13 17
6 12
12 16

2
6 2
7 6

2
10 6
5 16

7
4 1
0 17
6 12
5 9
14 13
17 11
3 3

9
9 7
5 5
10 17
2 5
10 19
17 14
11 19
1 9
10 9

5
18 7
4 16
9 17
19 13
2 1

0
1
4
0
1
0
1
0
0
1
*/

猜你喜欢

转载自blog.csdn.net/qq_41279172/article/details/100031951