package D3_23;
import java.util.Scanner;
/**
* 思路: 寻找重叠走廊最大值,最后乘以10即为最大用时 。注意:对面房间公用同一问题
*
* @author Ming
*
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int[] result = new int[t];
while (t > 0) {
int n = sc.nextInt();
int[] a = new int[401];
for (int i = 0; i < n; i++) {
int b = sc.nextInt();
int c = sc.nextInt();
if (b > c) {
int tmp = b;
b = c;
c = tmp;
}
// ----------注意------------
if (b % 2 == 0) {
b--;
}
if (c % 2 == 1) {
c++;
}
// -------------------------
for (int j = b; j <= c; j++) {
a[j]++;
}
}
int ans = 0;
for (int i = 1; i <= 400; i++) {
if (a[i] > ans) {
ans = a[i];
result[t - 1] = ans;
}
}
t--;
}
for (int i = result.length - 1; i >= 0; i--) {
System.out.println(result[i] * 10);
}
}
}
一开始是思路是将没有走廊重叠的情况放在一组,然后每一组10分钟,求总时间,但一直不通过,暂时不知道哪里出错了,现贴出代码。
package D3_23;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DP_MovingTables {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
int is;
int[] result;
int[] start;
int[] end;
int[] visit;
List<Integer> list = new ArrayList<>();
int tmp;
n = sc.nextInt();
result = new int[n];
while (n > 0) {
is = sc.nextInt();
start = new int[is];
end = new int[is];
visit = new int[is];
for (int i = 0; i < is; i++) {
start[i] = sc.nextInt();
end[i] = sc.nextInt();
if (start[i] > end[i]) {
tmp = start[i];
start[i] = end[i];
end[i] = tmp;
}
if (start[i] % 2 == 0) {
start[i]--;
}
if (end[i] % 2 == 1) {
end[i]++;
}
}
for (int j = 0; j < is; j++) {
if (visit[j] == 1) {
continue;
}
list.add(j);
for (int a = j; a < is; a++) {
if (visit[a] == 1) {
continue;
}
int b = 0;
// 遍历添加时间错开的数据
for (; b < list.size(); b++) {
if (start[a] - end[list.get(b)] < 0 && end[a] - start[list.get(b)] > 0) {
b = -1;
break;
}
}
if (b != -1) {
visit[a] = 1;
list.add(a);
}
}
result[n - 1] += 10;
list.clear();
}
n--;
}
for (int i = result.length - 1; i >= 0; i--) {
System.out.println(result[i]);
}
}
}