tyvj/joyoi 2018 小猫爬山
2018,这个题号吼哇!
搜索第一题,巨水。
WA了一次,因为忘了还原...
1 #include <cstdio> 2 const int N = 20; 3 4 int n, W, ans, weigh[N], cost[N]; 5 6 inline void min(int &a, int b) { 7 if(a > b) a = b; 8 return; 9 } 10 11 void DFS(int k, int cnt) { 12 if(k == n + 1) { 13 min(ans, cnt); 14 return; 15 } 16 if(cnt >= ans) return; 17 for(int i = 1; i <= cnt; i++) { 18 if(weigh[i] + cost[k] <= W) { 19 weigh[i] += cost[k]; 20 DFS(k + 1, cnt); 21 weigh[i] -= cost[k]; 22 } 23 } 24 weigh[++cnt] = cost[k]; 25 DFS(k + 1, cnt); 26 cnt--; 27 return; 28 } 29 30 int main() { 31 scanf("%d%d", &n, &W); 32 ans = n; 33 for(int i = 1; i <= n; i++) { 34 scanf("%d", &cost[i]); 35 } 36 37 DFS(1, 0); 38 printf("%d", ans); 39 return 0; 40 }