码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

时间:2019-11-02 11:47:14      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:ORC   hat   you   tee   href   enter   clu   mon   sort   

链接:

https://codeforces.com/contest/1251/problem/D

题意:

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.

思路:

二分, 判断赛的时候贪心判断。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
 
const int MAXN = 2e5+10;
 
struct Node
{
    int l, r;
    bool operator < (const Node& rhs) const
    {
        if (this->l != rhs.l)
            return this->l > rhs.l;
        return this->r > rhs.r;
    }
}node[MAXN];
int n;
LL s;
 
bool Check(LL val)
{
    int p = n/2+1;
    LL sum = 0;
    for (int i = 1;i <= n;i++)
    {
        if (node[i].r >= val && p > 0)
        {
            sum += max(val, (LL)node[i].l);
            p--;
        }
        else
        {
            sum += node[i].l;
        }
    }
    return (sum <= s && p == 0);
}
 
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> s;
        for (int i = 1;i <= n;i++)
            cin >> node[i].l >> node[i].r;
        sort(node+1, node+1+n);
        LL l = 1, r = s;
        LL res = 0;
        while(l <= r)
        {
            LL mid = (l+r)/2;
            //cout << mid << endl;
            if (Check(mid))
            {
                res = max(res, mid);
                l = mid+1;
            }
            else
                r = mid-1;
        }
        cout << res << endl;
    }
 
    return 0;
}

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

标签:ORC   hat   you   tee   href   enter   clu   mon   sort   

原文地址:https://www.cnblogs.com/YDDDD/p/11781085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!