QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#691105#7944. Max Minus MinKowerKoint#TL 214ms3632kbC++231.3kb2024-10-31 09:54:082024-10-31 09:54:09

Judging History

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

  • [2024-10-31 09:54:09]
  • 评测
  • 测评结果:TL
  • 用时:214ms
  • 内存:3632kb
  • [2024-10-31 09:54:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve() {
  int n; cin >> n;
  vector<ll> a(n);
  for(int i = 0; i < n; i++) cin >> a[i];
  ll ok = 2000000100LL, ng = -1;
  while(ok - ng > 1) {
    ll mid = midpoint(ok, ng);
    bool valid = false;
    int l = n;
    multiset<ll> in, out;
    for(int i = 0; i < n; i++) out.insert(a[i]);
    for(int r = n; r >= 0; r--) {
      auto expandable = [&](int nl) {
        if(nl < 0) return false;
        if(in.empty()) return true;
        ll mx = max(*in.rbegin(), a[nl]);
        ll mn = min(*in.begin(), a[nl]);
        if(mx-mn <= mid) return true;
        return false;
      };
      while(l>0 && expandable(l-1)) {
        l--;
        out.erase(out.find(a[l]));
        in.insert(a[l]);
      }
      //cerr << mid << ' ' << l << ' ' << r << endl;
      //cerr << '#' << ssize(out) << endl;
      //if(!out.empty()) cerr << ' ' << *out.rbegin() << ' ' << *out.begin() << endl;
      if(out.empty() || (*out.rbegin()-*out.begin() <= mid)) {
        valid = true;
        break;
      }
      if(r==0) break;
      assert(!in.empty());
      in.erase(in.find(a[r-1]));
      out.insert(a[r-1]);
    }
    if(valid) ok = mid;
    else ng = mid;
  }
  cout << ok << '\n';
}

int main() {
  int t; cin >> t;
  while(t--) solve();
}

详细

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: 214ms
memory: 3568kb

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: