QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#363690#7944. Max Minus Minjxu#WA 11ms3788kbC++173.0kb2024-03-24 01:57:042024-03-24 01:57:05

Judging History

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

  • [2024-03-24 01:57:05]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:3788kb
  • [2024-03-24 01:57:04]
  • 提交

answer

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

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<bool> vb;
typedef tuple<int,int,int> ti;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vi> vii;
typedef vector<vii> viii;

template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;

#define rep(i, a, b) for (int i=a; i<(b); i++)
#define FOR(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(x,A) for (auto& x : A)

#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ins insert

const int MOD = 1000000007;
const char nl = '\n';

pi minmax(vi &arr, int low, int high) {
    int mn = arr[low];
    int mx = arr[high];
    rep(i, low, high + 1) {
        mn = min(arr[i], mn);
        mx = max(arr[i], mx);
    }

    return mp(mn, mx);
}

int count_agree(vpi &arr, int low, int high) {
    int cnt = 0;
    for (auto &[a, b]: arr) {
        if (a >= low && b <= high) cnt++;
    }
    return cnt;
}

bool check(vi &arr, int gap) {

    int low = arr[0];
    int high = arr[0];

    vpi intervals;
    for (int x: arr) {
        if (x - low > gap || high - x > gap) {
            intervals.pb({low, high});
            low = x;
            high = x; 
        }

        low = min(low, x);
        high = max(high, x);
    }
    intervals.pb({low, high});


    // if most of these intervals are the same except one its possible
    pi cand = {intervals[0].first, intervals[0].first};
    for (auto &[a, _]: intervals) {
        if (a < cand.second) cand.second = a;
        if (cand.second < cand.first) swap(cand.second, cand.first);
    } 

    int x = count_agree(intervals, cand.first, cand.first + gap);
    if (x == sz(intervals) - 1) return true;

    x = count_agree(intervals, cand.second, cand.second + gap);
    if (x == sz(intervals) - 1) return true;
    return false;

}

int solve(int tt) {
    int n; cin >> n;
    vi arr(n);
    trav(x, arr) cin >> x;

    pi cur = minmax(arr, 0, n - 1);
    

    int ans = cur.second - cur.first;
    int low = 0;
    int high = ans;

    while(low <= high) {
        int mid = (high + low)/2;
        if (check(arr, mid)) {
            ans = mid;
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }
    cout << ans << nl;

    tt++;
    return 0;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int T = 1;
    cin >> T;
    for (int i = 1; i <= T; i++) {
        if (solve(i)) break;
    }
    T++;
    return 0;
}

詳細信息

Test #1:

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

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: -100
Wrong Answer
time: 11ms
memory: 3788kb

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
3
2
2
2
1
2
1
2
1
3
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:

wrong answer 38th numbers differ - expected: '2', found: '3'