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