QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#195999#6420. Certain Scientific RailgunpanhuachaoWA 1ms5668kbC++204.9kb2023-10-01 10:41:022023-10-01 10:41:02

Judging History

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

  • [2023-10-01 10:41:02]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5668kb
  • [2023-10-01 10:41:02]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
using namespace std;
const ll inf = 1e12;

ll n; pll a[100010];
set<pll> s1, s2; ll s3;
ll max0, min0, max1[100010], min1[100010];

ll cost(ll l0, ll l1, ll r0, ll r1){
    return max(l1, r1) - min(l0, r0) + abs(max(l1, r1));
}
ll solve(int revx, int revy){
    // printf("revx=%d, revy=%d:\n", revx, revy);
    for(int i = 1; i <= n; i++)
        a[i].first *= revx, a[i].second *= revy;
    sort(a+1, a+n+1);
    // for(int i = 1; i <= n; i++)
        // printf("(%-3lld %-3lld)", a[i].first, a[i].second);
    // printf("\n");
    max1[n] = -inf; min1[n] = inf;
    for(int i = n-1; i >= 1; i--){
        max1[i] = max(max1[i+1], a[i+1].second);
        min1[i] = min(min1[i+1], a[i+1].second);
    }
    for(int i = n-1; i >= 1; i--)
        if(a[i].first == a[i+1].first)
            max1[i] = max1[i+1], min1[i] = min1[i+1];
    // for(int i = 1; i <= n; i++)
        // printf("(%-3lld %-3lld)", min1[i], max1[i]);
    // printf("\n");
    
    ll ans = inf;
    max0 = -inf, min0 = inf;
    for(int i = n; i >= 1 && a[i].first > 0; i--)
        max0 = max(max0, a[i].second), min0 = min(min0, a[i].second);
    for(int i = 1; i <= n && a[i].first <= 0; i++){
        if(max0 == -inf && i == 1)
            ans = min(ans, -a[i].first);
        else
            ans = min(ans, -a[i].first + max0 - min0 + min(abs(max0), abs(min0)));
        min0 = min(min0, a[i].second);
        max0 = max(max0, a[i].second);
    }
    // printf("ans=%lld\n", ans);

    int tp = -1;
    s1.clear(); s2.clear(); s3 = inf;
    max0 = -inf, min0 = inf;
    ll t1 = n, t2 = n;
    for(int i = n; i >= 1 && a[i].first > 0; i--)
        s1.insert(make_pair(max1[i]-min1[i]+abs(max1[i])+a[i].first, i));
    a[0].first = -inf;
    for(int i = 1; i <= n && a[i].first < 0; i++){
        if(a[i].first == a[i-1].first){
            max0 = max(a[i].second, max0);
            min0 = min(a[i].second, min0);
            continue;
        }

        while(t2 > t1 && (max1[t2] <= max0 && min1[t2] >= min0)){
            ll t = (tp? max1[t2]+abs(max1[t2])+a[t2].first: -min1[t2]+a[t2].first);
            set<pll>::iterator it = s2.find(make_pair(t, t2));
            assert(it != s2.end());
            s3 = min(s3, a[t2].first);
            s2.erase(it);
            t2--;
        }
        if(t1 == t2) tp = -1;

        while(a[t1].first > 0 && (max1[t1] <= max0 || min1[t1] >= min0)){
            set<pll>::iterator it = s1.find(make_pair(max1[t1]-min1[t1]+abs(max1[t1])+a[t1].first, t1));
            assert(it != s1.end());
            if(max1[t1] <= max0 && min1[t1] < min0){
                // printf("# %lld %lld %d\n", t1, t2, tp);
                assert(tp != 1);
                s2.insert(make_pair(-min1[t1]+a[t1].first, t1)), tp = 0;
            }
            if(max1[t1] > max0 && min1[t1] >= min0){
                // printf("# %lld %lld %d\n", t1, t2, tp);
                assert(tp != 0);
                s2.insert(make_pair(max1[t1]+abs(max1[t1])+a[t1].first, t1)), tp = 1;
            }
            if(max1[t1] <= max0 && min1[t1] >= min0){
                assert(t1 == t2);
                s3 = min(s3, a[t2].first);
                t2--;
            }
            s1.erase(it);
            t1--;
        }
        // printf("%d %lld %lld %lld %lld\n", i, t1, t2, min0, max0);
        
        if(a[t1].first > 0){
            ans = min(ans, 2*(-a[i].first) + s1.begin()->first);
            // printf("1* %d %lld %lld\n", i, ans, s1.begin()->second);
        }

        if(t2 > t1){
            assert(tp != -1);
            if(tp) ans = min(ans, 2*(-a[i].first) - min0 + (s2.begin()->first));
              else ans = min(ans, 2*(-a[i].first) + max0 + abs(max0) + (s2.begin()->first));
            // printf("2* %d %lld %lld %d\n", i, ans, s2.begin()->second, tp);
        }

        if(max0 != -inf)
            ans = min(ans, 2*(-a[i].first) + max0 - min0 + abs(max0) + s3);
        else
            ans = min(ans, 2*(-a[i].first) + s3);
        // printf("3* %d %lld %lld\n", i, ans, s3);
        
        max0 = max(a[i].second, max0);
        min0 = min(a[i].second, min0);
    }

    // printf("ans=%lld\n", ans);
    for(int i = 1; i <= n; i++)
        a[i].first *= revx, a[i].second *= revy;
    return ans;
}

void solve0(){
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++)
        scanf("%lld%lld", &a[i].first, &a[i].second);
    ll ans = inf;
    ans = min(ans, solve(1, 1));
    ans = min(ans, solve(1, -1));
    ans = min(ans, solve(-1, 1));
    ans = min(ans, solve(-1, -1));
    printf("%lld\n", ans);
    return;
}

int main(){
    int t; scanf("%d", &t);
    while(t--) solve0();
    return 0;
}

/*
5
-1 -4
0 3
-1 1
-4 1
2 -4

5
-1 4
-2 -1
3 0
-1 0
4 4

5
4 -4
4 1
1 1
-1 4
-2 4

10
11 -1
0 -15
1 -3
13 -11
14 -5
13 -7
-14 -3
-11 -6
-1 -10
12 3

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2
0 1
1 0
4
1 1
-3 -3
4 -4
-2 2
4
1 100
3 100
-100 1
3 -100

output:

0
8
4

result:

ok 3 number(s): "0 8 4"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 5668kb

input:

120
4
11 11
-22 -22
33 -33
-44 44
4
-11 11
22 -22
-33 -33
44 44
4
-11 -11
22 22
-33 33
44 -44
4
11 -11
-22 22
33 33
-44 -44
4
-11 11
22 -22
33 33
-44 -44
4
11 11
-22 -22
-33 33
44 -44
4
11 -11
-22 22
-33 -33
44 44
4
-11 -11
22 22
33 -33
-44 44
4
1 1
-2 -2
3 -3
-4 4
4
-1 1
2 -2
-3 -3
4 4
4
-1 -1
2 2
...

output:

99
99
99
99
99
99
99
99
9
9
9
9
9
9
9
9
7
7
7
7
5
5
5
5
4
4
4
4
4
4
4
4
0
0
0
0
0
0
0
0
4
4
4
4
5
5
5
5
4
4
4
4
4
4
4
4
12
12
12
12
110
110
110
110
0
0
0
0
0
0
0
0
11
11
11
11
11
11
11
11
111
111
111
111
111
111
111
111
305
305
305
305
300
300
300
300
100
100
100
100
5
5
5
5
2
2
2
2
2
2
2
2
48
48
48...

result:

wrong answer 17th numbers differ - expected: '5', found: '7'