Submission
Status:
(PPPPPPPPPPPPPPP)(-SSSSSSS)(-SSSSSSSSS)(PPPPPPPPPP)(PP-SSSSSSS)(-SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS)
Subtask/Task Score:
{3/3}{0/7}{0/12}{17/17}{0/21}{0/40}
Score: 20
User: pxsit
Problemset: รถไฟตู้เสบียง (Dining Car)
Language: cpp
Time: 0.003 second
Submitted On: 2025-06-29 13:24:31
#include "dining_car.h"
#include <bits/stdc++.h>
using namespace std;
int solve(int l, int r) {
while (l < r) {
int mid = l + (r - l) / 2;
if (compare_cars(mid, mid + 1) <= 0) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
pair<int, int> locate_dining_cars(int n) {
if (n <= 10) {
int fs = solve(1, n), nd = -1;
if (fs > 1) {
int cand = solve(1, fs - 1);
if (compare_cars(cand, fs) == 0) {
nd = cand;
}
}
if (nd == -1 && fs < n)
nd = solve(fs + 1, n);
return {min(fs, nd), max(fs, nd)};
}
int x = solve(1, n);
int y = -1;
if (x > 1) {
int l = solve(1, x - 1);
if (l != x) {
y = l;
}
}
if (y == -1 && x < n) {
int r = solve(x + 1, n);
if (r != x) {
y = r;
}
}
return {min(x, y), max(x, y)};
}