贪心问题,其实我觉得贪心就是合理的考虑最优情况,证明贪心可行即可。这题目没话多久一次ac。这道题需要注意房间号的奇偶性。1 3、2 4的测试数据。答案应该为20。
#include <stdio.h>
#include <stdlib.h> #define MAXNUM 505 int moves[MAXNUM][];
int visit[MAXNUM]; int comp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
} int main() {
int case_n, n;
int i, j, k, tmp; scanf("%d", &case_n); while (case_n--) {
scanf("%d", &n);
for (i=; i<n; ++i) {
scanf("%d %d", &moves[i][], &moves[i][]);
if (moves[i][] > moves[i][]) {
tmp = moves[i][];
moves[i][] = moves[i][];
moves[i][] = tmp;
}
} memset(visit, , sizeof(visit));
qsort(moves, n, sizeof(int)*, comp); k = ;
for (i=; i<n; ++i) {
if (visit[i])
continue;
tmp = moves[i][];
if (tmp % )
tmp++;
visit[i] = ;
k=k+;
for (j=i+; j<n; ++j) {
if (visit[j] == && moves[j][] > tmp) {
tmp = moves[j][];
if (tmp % )
tmp++;
visit[j] = ;
}
}
} printf("%d\n", k);
} return ;
}