题目:http://acm.hdu.edu.cn/showproblem.php?pid=2616
#include <queue> #include <algorithm> #include <iostream> using namespace std; const int INF = 1 << 30; struct ka { int A, M; }a[11]; int main() { int i, j; int ans; int n, init_hp; int p[11]; while (~scanf("%d%d", &n, &init_hp)) { for (i = 0; i < n; i++) { scanf("%d%d", &a[i].A, &a[i].M); } for (i = 0; i < n; i++) { p[i] = i; } ans = INF; do { int hp = init_hp; for (i = 0; i < n; i++) { if (hp <= a[p[i]].M) { hp -= 2 * a[p[i]].A; } else { hp -= a[p[i]].A; } if (hp <= 0)//一杀死马上退出循环,以便后面判断是否杀死。 { break; } } if (ans > i)//i:0--->n-1 { ans = i; } }while (next_permutation(p, p + n)); if (ans == n) { puts("-1"); } else { printf("%d\n", ans+1); } } return 0; }