QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#46519#4202. Even ElectricityMoQzTL 2ms3768kbC++945b2022-08-30 10:00:002022-08-30 10:00:02

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-30 10:00:02]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3768kb
  • [2022-08-30 10:00:00]
  • 提交

answer

#include<iostream>
#include<cstdio>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
int read(){
	int u=0;
	char c=getchar();
	while(c>'9'||c<'0')c=getchar();
	while(c>='0'&&c<='9')u=u*10+c-48,c=getchar();
	return u;
}
int n;
long r2;
long s[100011],w[100011];
bool solve_l(long l){
	long R=0;
	fo(i,1,n){
		R=min(R+w[i],min(r2,R+w[i]-l+s[i]));
		if(R<0)return 0;
	}
	return 1;
}
bool solve_r(long r){
	long L=0;
	fo(i,1,n){
		L=L+w[i]-r+s[i];
		if(L<0)L=0;
		if(L>r2||(i==n&&L>0))return 0;
	}
	return 1;
}
int main(){
	n=read();
	r2=read();
	fo(i,1,n){
		s[i]=read();
		w[i]=read();
	}
	int ans=0;
	int l=0,r=1999999999,mid;
	while(l<r){
		mid=(l+r+1)/2;
		if(solve_l(mid))l=mid;
		else r=mid-1;
	}
	ans-=l;
	l=0,r=1999999999,mid;
	while(l<r){
		mid=(l+r)/2;
		if(solve_r(mid))r=mid;
		else l=mid+1;
	}
	printf("%d",ans+r);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3728kb

input:

3 1
4 2
2 0
5 1

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3740kb

input:

1 10
10 9

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3732kb

input:

2 10
6 10
9 6

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3680kb

input:

10 10
5 6
8 0
1 8
2 2
7 5
5 3
10 6
3 0
9 2
0 9

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3684kb

input:

10 10
6 1
9 1
8 10
2 5
8 10
6 10
10 9
4 8
0 1
0 6

output:

8

result:

ok single line: '8'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

10 10
4 9
4 3
2 2
10 7
6 9
3 5
5 8
7 4
0 4
8 2

output:

4

result:

ok single line: '4'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3740kb

input:

10 10
6 7
5 0
5 9
9 6
2 2
0 3
2 10
4 8
1 6
2 9

output:

3

result:

ok single line: '3'

Test #8:

score: -100
Time Limit Exceeded

input:

100000 1000000000
413794426 487008076
857157934 416640558
335458362 938175747
331906268 815014691
884031347 954760972
747185588 109876528
795877270 347023646
687257733 17478773
649166691 883831823
683020200 450159267
250807459 945914456
375003437 674695585
585101823 862146285
765383066 602155570
285...

output:


result: