zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

    链接:

    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;
    }
    
  • 相关阅读:
    字符串
    zval结构体
    需要优化代码的leetcode
    删除字符串中的字符
    python 目录
    文件
    awk 复习
    链表和数组的说法
    在linux服务器新添加硬盘,如何识别、挂载。
    Linux 的 date 日期的使用
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11781085.html
Copyright © 2011-2022 走看看