第K大区间:二分后滑动窗口扫一下;
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100005;
typedef long long ll;
int a[maxn],n,vis[maxn],b[maxn],top=1,val[maxn],c[maxn],cnt[maxn],mx=0;
ll k,ans;
void ins(int x){cnt[c[x]]--;c[x]++;cnt[c[x]]++;mx=max(mx,c[x]);
}
void del(int x){cnt[c[x]]--;c[x]--;cnt[c[x]]++;if(!cnt[mx])mx--;
}
bool ok(int v){memset(c,0,sizeof(c));memset(cnt,0,sizeof(cnt));mx=0;
int l=1,r;ll ans=0;
for(r=1;r<=n;r++){ins(val[r]);
for(;;){
del(val[l]);
if(mx<v){ins(val[l]);break;}
l++;
}
if(mx>=v)ans+=l;
}
return ans>=k;
}
bool cmp(int x,int y){return a[x]<a[y];}
int main(){//freopen("in.in","r",stdin);
scanf("%d%lld",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);b[i]=i;}
sort(b+1,b+1+n,cmp);
val[b[1]]=1;
for(int i=2;i<=n;i++){
if(a[b[i]]==a[b[i-1]])val[b[i]]=top;else val[b[i]]=++top;
}
int l=0,r=maxn;
while(l<r){
int mid=(l+r+1)>>1;
if(ok(mid))l=mid;
else r=mid-1;
}
printf("%d",l);
return 0;
}
比赛时我把k大转成了n*(n-1)-k+1小,但就是调不好…
逛街
很容易想到贪心算法,关键是维护数据的数据结构的应用;
这里使用堆与权值线段树维护;
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
using namespace std;
const int maxn=100005;
const int maxnode=4000000;
typedef long long ll;
int read(){char c=getchar();while(c<'0'||c>'9')c=getchar();int ans=0;
for(;c>='0' && c<='9';c=getchar())ans=ans*10+c-'0';
return ans;
}
const int inf=2e9;
int a[maxn],b[maxn],c[maxn],n,k,ch[maxnode][2],sz[maxnode],tot=0,rt=0,ans=0,ok=0;
ll sum[maxnode],T;
priority_queue<int> Q;
void insert(int& o,int l,int r,int p,int val){
if(!o)o=++tot;
sz[o]+=val;sum[o]+=p*val;
if(l==r)return;
int mid=(l+r)>>1;
if(p<=mid)insert(ch[o][0],l,mid,p,val);
else insert(ch[o][1],mid+1,r,p,val);
}
int query(int& o,int l,int r,int k){
if(!o)return 0;
if(l==r){return min(sz[o],k/l);}
int mid=(l+r)>>1;
if(k<=sum[ch[o][0]])return query(ch[o][0],l,mid,k);
else return sz[ch[o][0]]+query(ch[o][1],mid+1,r,k-sum[ch[o][0]]);
}
int main(){//freopen("in.in","r",stdin);
scanf("%d%lld%d",&n,&T,&k);
rep(i,1,n)a[i]=read();
rep(i,1,n)b[i]=read();
rep(i,1,n)c[i]=read();
ll need=(ll)k*inf;
rep(i,1,k)Q.push(inf);
rep(i,1,n){
if(c[i]==1){
if(k && b[i]<Q.top()){
need=need-Q.top()+b[i];
if(Q.top()<inf)insert(rt,1,1e9,Q.top(),1);
Q.pop();Q.push(b[i]);
}
else insert(rt,1,1e9,b[i],1);
}
else insert(rt,1,1e9,b[i],1);
if(need<=(T-a[i])){ok=1;
ans=max(ans,k+query(rt,1,1e9,T-a[i]-need));
}
}
printf("%d\n",ok ? ans : -1);
}