QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#646218#5076. Prof. Pang and AntsKevin5307WA 905ms3812kbC++233.0kb2024-10-16 21:44:242024-10-16 21:44:24

Judging History

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

  • [2024-10-16 21:44:24]
  • 评测
  • 测评结果:WA
  • 用时:905ms
  • 内存:3812kb
  • [2024-10-16 21:44:24]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
ll a[100100];
struct block
{
	ll A,B,M;
	block(){}
	block(ll _A,ll _B,ll _M):A(_A),B(_B),M(_M){};
	inline ll length(){return (B-A+1)*M;}
	inline ll lget(ll x){return (x-1)/M+A;};
	inline ll rget(ll x){return lget(length()-x+1);}
	inline ll lgetd(ll x){return (x-1)/M*M+M-(x-1);}
	inline ll rgetd(ll x){return lgetd(x);}
	inline ll lgetd2(ll x){return (x-1)-(x-1)/M*M+1;}
	inline ll rgetd2(ll x){return lgetd2(x);}
};
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		ll m;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			a[i]++;
		}
		sort(a+1,a+n+1);
		ll l=0,r=1e18;
		while(l<r)
		{
			ll tmp=m;
			ll mid=(l+r)/2;
			vector<block> vec;
			vector<pair<ll,ll>> vect;
			for(int i=1;i<=n;i++)
			{
				vect.pb(a[i],1);
				vect.pb(a[i]+mid/2,-1);
			}
			int cnt=0;
			srt(vect);
			for(int i=0;i<sz(vect)-1;i++)
			{
				cnt+=vect[i].second;
				if(!cnt) continue;
				if(vect[i].first==vect[i+1].first) continue;
				if((vect[i+1].first-vect[i].first)<=m/cnt)
				{
					vec.pb(vect[i].first,vect[i+1].first-1,cnt);
					m-=(vect[i+1].first-vect[i].first)*cnt;
				}
				else
				{
					if(m/cnt)
						vec.pb(vect[i].first,vect[i].first+m/cnt-1,cnt);
					if(m%cnt)
						vec.pb(vect[i].first+m/cnt,vect[i].first+m/cnt,m%cnt);
					m=0;
				}
			}
			if(m)
			{
				m=tmp;
				l=mid+1;
				continue;
			}
			ll L=0,R=0;
			int lp=0,rp=sz(vec)-1;
			ll ans=0;
			while(lp<sz(vec))
			{
				if(L==vec[lp].length())
				{
					L=0;
					lp++;
					continue;
				}
				if(R==vec[rp].length())
				{
					R=0;
					rp--;
					continue;
				}
				ll len=min(vec[lp].length()-L,vec[rp].length()-R);
				ans=max(ans,vec[lp].lget(L+1)+vec[rp].rget(R+1));
				ans=max(ans,vec[lp].lget(L+len)+vec[rp].rget(R+len));
				if(vec[lp].M>=vec[rp].M)
					if(vec[lp].lgetd(L+1)<vec[rp].rgetd(R+1)&&vec[lp].lgetd(L+1)<=len-1)
						ans=max(ans,vec[lp].lget(L+1)+vec[rp].rget(R+1)+1);
				if(vec[lp].M<=vec[rp].M)
					if(vec[lp].lgetd2(L+len)>vec[rp].rgetd2(R+len)&&vec[rp].rgetd2(R+len)<=len-1)
						ans=max(ans,vec[lp].lget(L+len)+vec[rp].rget(R+len)+1);
				L+=len;
				R+=len;
			}
			if(ans<=mid)
				r=mid;
			else
				l=mid+1;
			m=tmp;
		}
		cout<<l<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 4
1 2
3 10
1 2 3
5 1
1 2 3 4 5

output:

6
9
4

result:

ok 3 number(s): "6 9 4"

Test #2:

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

input:

1
1 100000000000000
1000000000

output:

200000000000000

result:

ok 1 number(s): "200000000000000"

Test #3:

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

input:

1
10 1
1 1 1 1 1 1 1 1 1 1

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 379ms
memory: 3652kb

input:

100000
1 76
95
1 60
68
1 81
86
1 88
67
1 69
28
1 75
65
1 56
22
1 88
60
1 51
41
1 64
11
1 54
71
1 63
19
1 88
5
1 89
66
1 80
66
1 81
48
1 67
99
1 94
36
1 54
46
1 51
37
1 82
17
1 74
41
1 69
61
1 79
65
1 78
10
1 71
17
1 87
88
1 83
2
1 58
29
1 59
43
1 78
53
1 75
73
1 77
71
1 52
82
1 63
69
1 83
19
1 61
27...

output:

267
197
254
223
138
206
112
209
134
128
197
126
176
222
213
178
266
188
147
126
164
157
192
210
156
142
264
166
117
146
185
222
220
217
202
166
122
162
250
214
160
158
220
132
108
213
198
276
226
197
204
140
146
215
122
142
201
269
269
188
144
246
251
200
217
184
192
139
234
128
190
242
254
166
152
...

result:

ok 100000 numbers

Test #5:

score: -100
Wrong Answer
time: 905ms
memory: 3812kb

input:

100000
2 87
72 38
2 73
58 25
2 84
71 78
2 91
83 34
2 56
26 24
2 63
97 73
2 67
11 91
2 56
44 55
2 67
34 53
2 93
19 72
2 55
29 91
2 78
2 70
2 56
28 83
2 77
33 5
2 98
60 76
2 63
82 59
2 82
50 72
2 73
91 40
2 85
97 41
2 60
35 14
2 86
51 74
2 61
52 37
2 96
35 45
2 85
23 15
2 96
45 92
2 96
76 4
2 58
55 98...

output:

155
121
192
160
79
203
114
128
122
132
114
101
113
78
186
174
164
154
168
80
169
121
129
86
186
118
169
207
114
166
165
150
98
129
183
171
225
158
117
94
88
190
119
128
187
112
187
184
138
177
166
149
76
179
125
102
64
99
188
185
126
128
88
80
88
150
127
191
102
166
214
96
116
122
187
206
120
122
98...

result:

wrong answer 24th numbers differ - expected: '85', found: '86'