尺取法 — 详解 + 例题模板(全)_lxt_Lucia的博客-CSDN博客_尺取法
2020CCPC秦皇岛
Professor Alex is preparing an exam for his students now.
There will be nn students participating in this exam. If student ii has a good mindset, he/she will perform well and get a_iai points. Otherwise, he/she will get b_ibi points. So it is impossible to predict the exam results. Assume the highest score of these students is xx points. The students with a score of no less than x \cdot p\%x⋅p% will pass the exam.
Alex needs to know what is the maximum number of students who can pass the exam in all situations. Can you answer his question?
Input
The first line of the input gives the number of test cases, T (1≤T≤5×10^3). TT test cases follow.
For each test case, the first line contains two integers n (1≤n≤2×10^5) andp (1≤p≤100), where nn is the number of students and p% is the ratio.
Each of the following nn lines contains two integers ai,bi (1≤bi≤ai≤10^9), representing the scores of student i.
The sum of n in all test cases doesn't exceed 5×10^5.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1), and y is the maximum number of students.
Input
2 2 50 2 1 5 1 5 60 8 5 9 3 14 2 10 8 7 6
Output
Case #1: 2 Case #2: 4
AC代码:2020CCPC秦皇岛 Exam Results(尺取)_林苏泽-CSDN博客
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ull;
const ull Max=1e6+5;
struct node{
ull val,id;
};
bool cmp(node a,node b){
return a.val<b.val;
}
node pos[Max];
ull vis[Max];
ull res,ant,num,now;
int main(){
ull T;scanf("%lld",&T);ull u=1;
while(T--){
res=1;
ull n,p;scanf("%lld%lld",&n,&p);
for(int i=1;i<=n;i++){
ull x,y;scanf("%lld%lld",&x,&y);
pos[res].val=x;
pos[res].id=i;res++;
pos[res].val=y;
pos[res].id=i;res++;
vis[i]=0;
}
res--;
sort(pos+1,pos+1+res,cmp);
now=0;num=0;
while(now!=n){
num++;
if(vis[pos[num].id]==0){
now++;
}
vis[pos[num].id]++;
}
ant=1;ull ans=0;
vis[pos[num].id]--;num--;now--;
while(num<res){
num++;
if(vis[pos[num].id]==0) now++;
vis[pos[num].id]++;
while(pos[ant].val*100<pos[num].val*p){
if(vis[pos[ant].id]==1){
now--;
}
vis[pos[ant].id]--;
ant++;
}
ans=max(ans,now);
}
printf("Case #%lld: %lld\n",u,ans);u++;
}
}