Firstly, each person is divided into 0 writing scales, 1 writing scales, 2 writing scales, and the time is taken out to arrange the order. Then consider a time interval [l,r], which requires everyone to complete. So the number of scales written by each person must be within an interval [Li,Ri]. In fact, we just need to scan the twopointer, and then write down the values of Li and Ri for everyone, because this is the interval, so it's well maintained. Firstly, each person is divided into 0 writing scales, 1 writing scales, 2 writing scales... The time is taken out to arrange the order.
Then consider a time interval [l,r], which requires everyone to complete.
So the number of scales written by each person must be within an interval [Li,Ri].
In fact, we just need to scan the two pointer s and record the values of each person's L_i and R_i, because this is the interval, so it's well maintained. Firstly, each person is divided into 0 writing scales, 1 writing scales, 2 writing scales, and the time is taken out to arrange the order. Then consider a time interval [l,r], which requires everyone to complete. So the number of scales written by each person must be within an interval [Li,Ri]. In fact, we just need to scan the twopointer and record the values of everyone's Li and Ri, because this is the interval, so it's well maintained.
Double pointer is the second-level pointer, and the second-level pointer is relative to the first-level pointer. Secondary pointers are generally used for function parameter transfer.
At first I didn't think how greedy I should be, and then I got cold.
#include<bits/stdc++.h> #define N 300000 #define ll long long using namespace std; ll read() { ll x=0,f=1; char c=getchar(); while(c>'9'||c<'0') { if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } return f*x; } int suml,sumr,X,Y,n,num,top,now; ll ans=1e18; struct nodeP { int l,r; bool flag; }a[N]; struct mts { int bel; ll x; }st[N]; bool cmp(mts x,mts y) { return x.x<y.x; } void add(int x) { int tmp=st[x].bel; if(a[tmp].flag) { a[tmp].l--; suml--; } else { a[tmp].r++; sumr++; } if(a[tmp].l==a[tmp].r) now++; } void del(int x) { int tmp=st[x].bel; if(a[tmp].flag) { a[tmp].r--; sumr--; } else { a[tmp].l++; suml++; } if(a[tmp].l==a[tmp].r+1) now--; } bool ck() { return now==n&&suml<=X&&X<=sumr; } int main() { X=read(),Y=read(),n=read(); num=(X+Y)/n; for(int i=1;i<=n;++i) { ll x=read(),y=read(); // a[i].vx=x,a[i].vy=y; a[i].flag=x<y; if(a[i].flag) a[i].l=num+1,a[i].r=num; else a[i].l=0,a[i].r=-1; sumr+=a[i].r,suml+=a[i].l; for(int j=0;j<=num;++j) st[++top]=(mts){i,x*j+y*(num-j)}; } sort(st+1,st+1+top,cmp); int r=0; for(int i=1;i<=top;++i) { while(!ck()) { ++r; if(r>top) break; add(r); } if(r>top) break; ans=min(ans,st[r].x-st[i].x); // cout<<i<<" "<<r<<'\n'; del(i); } cout<<ans<<'\n'; return 0; }
Source: zr