http://acm.hdu.edu.cn/showproblem.php?pid=2616
枚举所有情况,STL直接生成排列
View Code
1 #include <cstdio>
2 #include <algorithm>
3 using namespace std;
4 int main()
5 {
6 int n,m,a[10],b[10],p[10],ans,i;
7 while (scanf("%d%d",&n,&m)!=EOF)
8 {
9 ans=n+1;
10 for (i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
11 for (i=1;i<=n;i++) p[i]=i;
12 do{
13 int t=m;
14 for (i=1;i<=n;i++)
15 {
16 if (t<=b[p[i]]) t-=a[p[i]]*2;
17 else t-=a[p[i]];
18 if (t<=0) break;
19 }
20 if (i<ans) ans=i;
21 }while (next_permutation(p+1,p+n+1));
22 if (ans>n) ans=-1;
23 printf("%d\n",ans);
24 }
25 }