Untitled
unknown
plain_text
a year ago
2.1 kB
12
Indexable
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
// Hàm để tính tổng khoảng cách di chuyển của hành khách từ một cửa
int calculateDistance(int N, int startPos, int numPassengers, bool seats[]) {
int totalDistance = 0;
for (int i = 0; i < numPassengers; ++i) {
int distance = 1;
while (true) {
// Kiểm tra vị trí ngồi bên trái và phải của cửa
if ((startPos - distance >= 0 && !seats[startPos - distance]) || (startPos + distance < N && !seats[startPos + distance])) {
if (startPos - distance >= 0 && !seats[startPos - distance]) {
totalDistance += distance;
seats[startPos - distance] = true;
} else if (startPos + distance < N && !seats[startPos + distance]) {
totalDistance += distance;
seats[startPos + distance] = true;
}
break;
}
distance++;
}
}
return totalDistance;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
int positions[3], passengers[3];
for (int i = 0; i < 3; ++i) {
cin >> positions[i] >> passengers[i];
positions[i]--; // Để vị trí bắt đầu từ 0
}
int minDistance = INT_MAX;
int order[3] = {0, 1, 2};
// Duyệt tất cả các thứ tự mở cửa
do {
bool seats[60] = {false}; // Mảng trạng thái ghế trống
int currentDistance = 0;
for (int i = 0; i < 3; ++i) {
currentDistance += calculateDistance(N, positions[order[i]], passengers[order[i]], seats);
}
minDistance = min(minDistance, currentDistance);
} while (next_permutation(order, order + 3));
cout << "Case #" << T + 1 << "\n" << minDistance << "\n";
}
return 0;
}
Editor is loading...
Leave a Comment