# Untitled

unknown
plain_text
21 days ago
1.8 kB
6
Indexable
Never
```import java.util.Scanner;

public class Main {
static int[] pos;
static int[] pas;
static boolean[] seat;
static int[] order = { 0, 1, 2 };
static int min;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

for (int tc = 1; tc <= T; ++tc) {
int n = sc.nextInt();

pos = new int[3];
pas = new int[3];
for (int i = 0; i < 3; ++i) {
pos[i] = sc.nextInt() - 1; // To make position start from 0
pas[i] = sc.nextInt();
}

min = 999999; // Initial large value

do {
seat = new boolean[60]; // orderay to keep track of seat occupancy

int cd = 0;
for (int i = 0; i < 3; ++i) {
cd += distance(n, pos[order[i]], pas[order[i]]);
}

if (cd < min) {
min = cd;
}
} while (np());

System.out.println("Case #" + tc + "\n" + min);
}
}

static int distance(int n, int sp, int np) {
int total = 0;
for (int i = 0; i < np; ++i) {
int d = 0;
while (true) {
// Check the seat position at sp
if (sp + d < n && !seat[sp + d]) {
total += d + 1;
seat[sp + d] = true;
break;
}
if (sp - d >= 0 && !seat[sp - d]) {
total += d + 1;
seat[sp - d] = true;
break;
}
d++;
}
}
return total;
}

static boolean np() {
int s = order.length;
int i = order.length - 2;
while (i >= 0 && order[i] >= order[i + 1]) {
i--;
}
if (i < 0)
return false;
int j = s - 1;
while (order[j] <= order[i]) {
j--;
}
int t = order[i];
order[i] = order[j];
order[j] = t;
for (int k = i + 1, l = s - 1; k < l; k++, l--) {
t = order[k];
order[k] = order[l];
order[l] = t;
}
return true;
}
}
```
Leave a Comment