题目
MianKing chose a course in this semester. There are nn students in this course, and everyone needs to write a final paper. Let
w
i
w_i
widenote the word count of the i-th student’s final paper.The i-th student has a lower bound
L
i
L_i
Liand an upper bound
R
i
R_i
Ri
on the number of words in his final paper so that
L
i
≤
w
i
L_i\leq w_i
Li≤wiThe grade rule of this course is very amazing. The grade of the i-th student
g
i
g_i
giis
n
−
K
,
K
i
n-K,Ki
n−K,Ki is the number of
j
∈
[
1
,
n
]
j \in [1,n]
j∈[1,n] satisfies that
w
j
>
w
i
w_j>w_i
wj>wiEvery student wants to achieve the highest possible grade, so under the optimal decision
w
i
w_i
wi will equal to
R
i
R_i
Rifor the i-th student.
But MianKing found an interesting thing: let’s assume that ∀ i ∈ [ 1 , n ] \forall i \in [1,n] ∀i∈[1,n], L i = 1000 , R i = 10000 ∀ i ∈ [ 1 , n ] L_i=1000,R_i=10000∀i∈[1,n] Li=1000,Ri=10000∀i∈[1,n] Under the optimal decision w i w_i wiare all equal to 10000 and the grades of the students are all nn. But if everyone only writes 1000 words in their final papers, their grades are still all nn and they can use the time they save to play games.
Now to fight against involution, MianKing wants to decide
w
i
w_i
wi for each student, and his plan has to satisfy the following conditions:
For each student, his grade cannot be less than that in the original plan.
Minimize the sum of
w
i
w_i
wi
You need help MianKing calculate the minimum value of
∑
i
=
1
n
w
i
\sum_{i=1}^{n}w_i
∑i=1nwi
The first line has one integer nn.
Then there are nn lines, the i-th line has two integers L i , R i L_i,R_i Li,Ri
1 ≤ n ≤ 1 0 5 1≤n≤10^5 1≤n≤105
1
≤
L
i
≤
R
i
≤
1
0
9
1\leq L_i\leq R_i\leq 10^9
1≤Li≤Ri≤109
题意
期末写论文,每个人写论文的字数在 [ L , R ] [L,R] [L,R]之间,每个人的得分是 n − K n-K n−K, K K K是比当前这个人的作业字数多的人数,每个人都想写尽可能多的字,这个现象被称为“内卷”。为了对抗内卷,我们需要最小化 W i W_i Wi的值。
思路
尽可能地让每个学生的 W i W_i Wi值相同,考虑贪心,先按照 R R R从小到大排序,如果 R R R相同再按照 L L L从大到小排序,这样就可以保证上一次选择的最大的 L L L尽可能多的在下面的区间内,这样我们就能选择最多的相同的 W i W_i Wi使得每个学生的得分最平均且最节省时间。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
class Stu
{
public:
ll l,r;
}t[maxn];
bool cmp(Stu a,Stu b)
{
return a.r==b.r?a.l>b.l:a.r<b.r;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld%lld",&t[i].l,&t[i].r);
sort(t+1,t+1+n,cmp);
ll ans = 0,L = 0;
for(int i=1;i<=n;i++)
{
ans += max(L,t[i].l);
L = max(L,t[i].l);
}
cout<<ans;
}