QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#347998#7944. Max Minus Mincactus#TL 49ms3632kbC++172.2kb2024-03-09 16:32:472024-03-09 16:32:48

Judging History

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

  • [2024-03-09 16:32:48]
  • 评测
  • 测评结果:TL
  • 用时:49ms
  • 内存:3632kb
  • [2024-03-09 16:32:47]
  • 提交

answer

#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#define int long long

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;

bool is_possible2(vector<int> a, int l, int r, int d) {
	if (r - l > d) return false;
	if (a.empty()) return true;
	int mx = *max_element(a.begin(), a.end());
	int mn = *min_element(a.begin(), a.end());
	
	
	vector<int> haveto(a.size());
	int sm = 0;
	for (int i = 0; i < a.size(); ++i) {
		haveto[i] = max(mx, r) - d > a[i];
		sm += haveto[i];
	}

	if (sm == 0) return true;

	int ls = a.size();
	int rs = 0;
	for (int i = 0; i < a.size(); ++i) if (haveto[i]) {
		ls = min(ls, i);
		rs = max(rs, i);
	}

	int diff = max(mx, r) - d - mn;

	for (int i = ls; i <= rs; ++i) a[i] += diff;

	mx = *max_element(a.begin(), a.end());
	mn = *min_element(a.begin(), a.end());

	return max(mx, r) - min(mn, l) <= d;
}

bool is_possible(vector<int> a, int d) {
	int n = a.size();
	int mx = *max_element(a.begin(), a.end());
	vector<vector<int>> segments;
	vector<int> cur;
	multiset<int> elements;
	for (int v : a) if (v == mx) {
		segments.push_back(cur);
		cur = vector<int>();
	} else {
		cur.push_back(v);
		elements.insert(v);
	}
	elements.insert(mx);
	segments.push_back(cur);
	for (vector<int> seg : segments) {
		for (int v : seg) elements.extract(v);
		int upper = *elements.rbegin();
		int lower = *elements.begin();

		if (is_possible2(seg, lower, upper, d)) return true;

		for (int v : seg) elements.insert(v);
	}
	return false;
}

int solve(vector<int> a) {
	int l = 0, r = *max_element(a.begin(), a.end()) - *min_element(a.begin(), a.end());
	while (l < r) {
		int m = (l + r) / 2;
		if (is_possible(a, m)) r = m;
		else l = m + 1;
	}
	return l;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	int tc; cin >> tc;
	while (tc--) {
		int n; cin >> n;
		vector<int> a(n);
		for (int& v : a) cin >> v;
		int res = solve(a);
		for (int& v : a) v *= -1;
		res = min(res, solve(a));
		cout << res << '\n';
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

input:

4
3
42 42 42
4
1 2 2 1
5
1 100 1 100 1
6
1 2 3 4 5 6

output:

0
0
99
2

result:

ok 4 number(s): "0 0 99 2"

Test #2:

score: 0
Accepted
time: 49ms
memory: 3480kb

input:

19530
6
2 3 3 3 4 3
6
2 4 4 2 5 5
6
2 5 2 5 1 3
6
4 4 1 2 4 1
5
5 4 5 3 3
5
2 2 1 5 1
6
3 1 2 4 2 3
5
5 5 4 4 5
6
5 3 3 1 4 2
6
3 3 2 4 2 4
6
1 2 4 5 2 5
5
3 4 5 5 3
6
4 3 3 3 4 3
6
1 5 1 2 3 1
5
5 5 2 1 4
6
1 2 5 3 5 2
6
4 5 2 4 5 2
5
2 4 2 4 1
4
2 3 3 3
6
1 2 2 1 4 5
6
3 2 1 3 3 2
6
2 1 1 2 5 3
6
...

output:

1
2
3
3
1
1
2
0
3
2
3
1
1
2
1
2
3
2
0
1
1
2
0
3
2
2
3
2
2
2
3
3
2
2
1
2
2
2
2
2
2
1
2
1
2
1
2
2
2
1
1
2
2
1
2
2
1
1
1
2
1
2
2
1
2
2
3
2
2
1
1
2
1
2
3
2
0
2
1
1
2
1
1
2
2
2
1
3
2
1
2
3
2
1
1
2
2
3
1
1
1
2
2
1
1
1
1
2
2
2
2
2
3
2
1
2
0
1
1
1
1
1
1
2
2
2
2
2
2
1
2
1
2
4
1
1
1
3
2
2
2
1
2
1
2
1
2
2
1
3
...

result:

ok 19530 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

1
199996
95228303 201285494 63848235 748936712 940169142 379639162 189291770 224201078 335564466 792345215 948056869 35198826 312793561 194588099 297198853 665606109 163797196 584404459 996890415 458867609 331820116 713293915 858136035 520976262 519894660 918315819 660535535 639896052 141007070 1151...

output:


result: