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;
}

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

时间: 2024-09-30 20:59:14

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing的相关文章

Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer

链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n digits (n is between 1 and 3?105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swappi

Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard

链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains 26 buttons (one for each letter of the Latin alph

Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the same backward as forward (formally, t[i]=t[|t|+1?i] for all i∈[1,|t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and

Educational Codeforces Round 75 (Rated for Div. 2)

(体验到了胡出一道题但是写锅的绝望呢) A: 送分题. #include<bits/stdc++.h> #define maxn 100005 #define maxm 500005 #define inf 0x7fffffff #define ll long long using namespace std; bool may[30]; char str[maxn]; inline int read(){ int x=0,f=1; char c=getchar(); for(;!isdigi

Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[200007];int n;long long s;bool check(int x){ int num=0; long long sum=s; for(int i=n;i;--i){ if(a[i].first>=x) ++num; else if(a[i].second>=x&&s

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.