D. Salary Changing----------思维/二分(稍难)
作者:互联网
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from li to ri dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
the median of the sequence [5,1,10,17,6] is 6,
the median of the sequence [1,2,1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l1+l2+⋯+ln≤s.
Note that you don’t have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1≤t≤2⋅105) — the number of test cases.
The first line of each query contains two integers n and s (1≤n<2⋅105, 1≤s≤2⋅1014) — the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers li and ri (1≤li≤ri≤109).
It is guaranteed that the sum of all n over all queries does not exceed 2⋅105.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. ∑i=1nli≤s.
Output
For each test case print one integer — the maximum median salary that you can obtain.
Example
inputCopy
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
outputCopy
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal1=12,sal2=2,sal3=11 (sali is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal1=4,sal2=3,sal3=6,sal4=6,sal5=7. Then the median salary is 6.
题意:给n个人发工资,总钱数为s。每个人有一个工资范围。要求一个发工资方案,使得工资中位数最大,求这个中位数
解析:
1.区间按照左端点排序
2.我们的目标是取出n/2个点<=m,一个点等于m,再取出n/2点大于m。
3.我们要把区间分为三类:区间上限<m ,区间下限>m ,m位于区间上下限之间
4.前两类根据贪心思想取最小的,就取每个区间的下限。对于第三类是取m还是取区间下限。取左端点表示放在m的左边,取m表示在m的右边。所以第三类取m
5.从后往前遍历,能否找到n/2+1个点>=m,以及取出来的总和小于总的钱数。
思路来自于:https://blog.csdn.net/m0_37809890/article/details/102742396
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10000;
int t,n;
ll s;
struct node
{
int l,r;
}a[N];
bool cmp(const node &a,const node &b)
{
return a.l<b.l;
}
int check(ll x)
{
int cnt=n/2+1;
ll sum=0;
for(int i=n;i>=1;i--)
{
if(cnt&&a[i].l<=x&&a[i].r>=x) //对于m位于区间上下限之间的,贪心策略 取m
{
sum+=x;
cnt--;
}
else
{
sum+=a[i].l; //对于<m的 贪心取最小的
if(a[i].l>=x&&cnt) cnt--; //如果下限>x
}
}
if(cnt) return 0;//不满足条件,x不能是中位数
return sum<=s;//判断钱够不够
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d %lld",&n,&s);
for(int i=1;i<=n;i++) scanf("%d %d",&a[i].l,&a[i].r);
sort(a+1,a+1+n,cmp);
ll l=0,r=s;
while(l<=r)
{
ll mid=l+r>>1;
if(check(mid)) l=mid+1;
else r=mid-1;
}
cout<<l-1<<endl;
}
}
AKone123456
发布了328 篇原创文章 · 获赞 6 · 访问量 5767
私信
关注
标签:Salary,salary,cnt,distribute,median,salaries,test,Changing,稍难 来源: https://blog.csdn.net/qq_43690454/article/details/104107326