南阳71----独木舟上的旅行

 1 #include <cstdio>
 2 #include <cstring>
 3 int a[205];//a[i]表示体重为i的人的个数
 4 int main(){
 5     int s,w,n,x;
 6     scanf("%d",&s);
 7     while(s--){
 8         scanf("%d%d",&w,&n);
 9         for(int i = 0; i < n; ++i){
10             scanf("%d",&x);
11             ++a[x];
12         }
13         int t = w, ans = 0, count = 0;//t表示船剩余可承载量,ans记录船只数,count记录船上人数
14         while(n){
15             int i;
16             for(i = t; i; --i)
17                 if(a[i]){
18                     if(t < i) continue;//可承载量不足,找更轻的人
19                     if(t == w) ++ans;
20                     --n; --a[i];
21                     ++count;
22                     t -= i;
23                     break;
24                 }
25             if(!i || !t || count == 2) { t = w; count = 0; }
26             //在满足条件内找不到人,或者刚好装满,或者装够两个人,使用新船只,更新相关变量
27         }
28         printf("%d
",ans);
29         memset(a,0,sizeof(a));
30     }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/qq188380780/p/8868102.html