UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#193577#2639. 省钱wosile100583ms3592kbC++2.1kb2023-10-14 11:00:012023-10-14 12:52:24

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k,s;
pair<ll,ll>p[50005];
priority_queue<ll,vector<ll>,greater<ll> >pq;
int rt=0,tot=0;
int tr[50005][2],sz[50005],dat[50005];
ll val[50005],su[50005];
int newnode(int x){
	tot++;
	tr[tot][0]=tr[tot][1]=0;
	val[tot]=su[tot]=x;
	sz[tot]=1;
	dat[tot]=rand();
	return tot;
}
void pushup(int x){
	sz[x]=sz[tr[x][0]]+sz[tr[x][1]]+1;
	su[x]=su[tr[x][0]]+su[tr[x][1]]+val[x];
}
void split(int x,ll v,int &tx,int &ty){
	if(x==0){
		tx=ty=0;
		return;
	}
	if(v>=val[x]){
		tx=x;
		split(tr[x][1],v,tr[tx][1],ty);
	}
	else{
		ty=x;
		split(tr[x][0],v,tx,tr[ty][0]);
	}
	pushup(x);
}
int merge(int x,int y){
	if(x==0 || y==0)return x+y;
	if(dat[x]<dat[y]){
		tr[x][1]=merge(tr[x][1],y);
		pushup(x);
		return x;
	} 
	else{
		tr[y][0]=merge(x,tr[y][0]);
		pushup(y);
		return y;
	}
}
int query(int x,ll v){
	if(!x)return 0;
	if(v<su[tr[x][0]])return query(tr[x][0],v);
	if(v<su[tr[x][0]]+val[x])return sz[tr[x][0]];
	return query(tr[x][1],v-su[tr[x][0]]-val[x])+sz[tr[x][0]]+1;
}
int main(){
//	freopen("N2639.in","r",stdin);
//	freopen("N2639.out","w",stdout);
	scanf("%lld%lld%lld",&n,&k,&s);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&p[i].second,&p[i].first);
	ll delta=0,sum=0;
	sort(p+1,p+n+1);
	int ans=0;
	for(int i=1;i<k;i++)pq.push(p[i].second-p[i].first);
	for(int i=1;i<k;i++){
		sum+=p[i].second,delta+=p[i].second-p[i].first;
		if(sum-delta>s){
			printf("%d",i-1);
			return 0;
		}
		ans=i;
	}
	for(int i=k;i<=n;i++){
		int tx,ty;
		split(rt,p[i].second,tx,ty);
		rt=merge(merge(tx,newnode(p[i].second)),ty);
	}
	for(int i=k;i<=n;i++){
		pq.push(p[i].second-p[i].first);
		sum+=p[i].second;
		delta+=p[i].second-p[i].first;
		while(pq.size()>k){
			delta-=pq.top();
			pq.pop();
		}
//		printf("%d %lld %lld\n",i,sum,delta);
		if(sum-delta>s)break;
		int tx,ty,tz;
		split(rt,p[i].second,tx,tz);
		split(tx,p[i].second-1,tx,ty);
		ty=merge(tr[ty][0],tr[ty][1]);
		rt=merge(merge(tx,ty),tz);
		ans=max(ans,i+query(rt,s-(sum-delta)));
	}
	printf("%d",ans);
	return 0;
	//quod erat demonstrandum
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 22ms
memory: 2244kb

input:

50000 30828 852557364841
682084050 257603011
870868024 517458094
732267860 201407488
777566656 55879...

output:

17903

result:

ok single line: '17903'

Test #2:

score: 10
Accepted
time: 92ms
memory: 3432kb

input:

50000 10508 8982273367520
34111224 12372852
549875017 525549262
357107918 219952140
644308048 222008...

output:

37621

result:

ok single line: '37621'

Test #3:

score: 10
Accepted
time: 18ms
memory: 2244kb

input:

50000 23114 535861686266
359271294 298114231
605400720 491693949
755566780 539381575
155586610 92962...

output:

14723

result:

ok single line: '14723'

Test #4:

score: 10
Accepted
time: 92ms
memory: 3312kb

input:

50000 13490 4616703243118
286358449 133228996
162995754 17235506
661390160 561824344
282751480 15433...

output:

30961

result:

ok single line: '30961'

Test #5:

score: 10
Accepted
time: 54ms
memory: 3032kb

input:

50000 26352 8630976119100
70133466 32927792
90392510 89764542
307782646 75889114
123168574 66039130
...

output:

42944

result:

ok single line: '42944'

Test #6:

score: 10
Accepted
time: 27ms
memory: 2160kb

input:

50000 11800 213255455323
405512104 311547645
122797690 35257030
782246460 533338866
416860264 504733...

output:

9869

result:

ok single line: '9869'

Test #7:

score: 10
Accepted
time: 77ms
memory: 3116kb

input:

50000 19734 4267681411347
732638120 327229436
361949068 274173372
539440696 285784669
94445920 84513...

output:

32498

result:

ok single line: '32498'

Test #8:

score: 10
Accepted
time: 83ms
memory: 3592kb

input:

50000 254 18445304121
375481124 36148026
388507104 183081259
261838134 179691990
485282800 209534680...

output:

1564

result:

ok single line: '1564'

Test #9:

score: 10
Accepted
time: 89ms
memory: 3564kb

input:

50000 3260 4076050769242
210627773 8756794
68253913 5333287
812306900 176444281
561388618 94960450
6...

output:

23173

result:

ok single line: '23173'

Test #10:

score: 10
Accepted
time: 29ms
memory: 2632kb

input:

50000 44485 12129791734731
590854222 262410600
992399148 641692708
219274382 56485932
730651726 4088...

output:

49537

result:

ok single line: '49537'