QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625803#9427. Collect the Coinsnhat2004WA 0ms3576kbC++202.5kb2024-10-09 21:05:372024-10-09 21:05:37

Judging History

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

  • [2024-11-06 15:56:27]
  • hack成功,自动添加数据
  • (/hack/1139)
  • [2024-10-09 21:05:37]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3576kb
  • [2024-10-09 21:05:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct coin1 {
    int timer, pos;
};
struct vt {
    int first, second, third;
};
int n;
coin1 coin[1000006];

// Check if two robots can collect all coins with a maximum allowed movement range k
int check(int k) {
    vt robot[2];
    robot[0].first = robot[0].second = robot[0].third = 0;
    robot[1].first = robot[1].second = robot[1].third = 0;
    
    for (int i = 1; i <= n; i++) {
        if (robot[0].first == 0) {
            robot[0].second = coin[i].timer;
            robot[0].third = coin[i].pos + k;
            robot[0].first = coin[i].pos - k;
        } else if ((robot[0].first - (robot[0].second - coin[i].timer) * k) <= coin[i].pos && 
                   (robot[0].third + (robot[0].second - coin[i].timer) * k) >= coin[i].pos) {
            robot[0].first = max(coin[i].pos, robot[0].first - ((robot[0].second - coin[i].timer) * k));
            robot[0].third = min(coin[i].pos, robot[0].third + ((robot[0].second - coin[i].timer) * k));
            robot[0].second = coin[i].timer;
        } else if (robot[1].first == 0) {
            robot[1].third = coin[i].pos + k;
            robot[1].first = coin[i].pos - k;
            robot[1].second = coin[i].timer;
        } else if ((robot[1].first - (robot[1].second - coin[i].timer) * k) <= coin[i].pos && 
                   (robot[1].third + (robot[1].second - coin[i].timer) * k) >= coin[i].pos) {
            robot[1].first = max(coin[i].pos, robot[1].first - ((robot[1].second - coin[i].timer) * k));
            robot[1].third = min(coin[i].pos, robot[1].third + ((robot[1].second - coin[i].timer) * k));
            robot[1].second = coin[i].timer;
        } else {
            return 0;
        }
    }
    return 1;
}

// Comparator for sorting coins based on time and position
bool cmp(coin1 a, coin1 b) {
    if (a.timer != b.timer)
        return a.timer < b.timer;
    else 
        return a.pos < b.pos;
}

signed main() {
    int tc;
    cin >> tc;
    while (tc--) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> coin[i].timer >> coin[i].pos;
        }
        sort(coin + 1, coin + 1 + n, cmp);

        int l = 0, r = 1e9, res = -1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (check(mid) == 1) {
                res = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        cout << res << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3576kb

input:

3
5
1 1
3 7
3 4
4 3
5 10
1
10 100
3
10 100
10 1000
10 10000

output:

-1
0
900

result:

wrong answer 1st lines differ - expected: '2', found: '-1'