zoukankan      html  css  js  c++  java
  • TyvjP2018 「Nescafé26」小猫爬山

    P2018 「Nescafé26」小猫爬山
    时间: 1000ms / 空间: 131072KiB / Java类名: Main

    背景

    Freda和rainbow饲养了N只小猫,这天,小猫们要去爬山。经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<)。

    描述

    Freda和rainbow只好花钱让它们坐索道下山。索道上的缆车最大承重量为W,而N只小猫的重量分别是C1、C2……CN。当然,每辆缆车上的小猫的重量之和不能超过W。每租用一辆缆车,Freda和rainbow就要付1美元,所以他们想知道,最少需要付多少美元才能把这N只小猫都运送下山?

    输入格式

    第一行包含两个用空格隔开的整数,N和W。
    接下来N行每行一个整数,其中第i+1行的整数表示第i只小猫的重量Ci。

    输出格式

    输出一个整数,最少需要多少美元,也就是最少需要多少辆缆车。

    测试样例1

    输入

    5 1996
    1
    2
    1994
    12
    29

    输出

    2

    备注

    对于100%的数据,1<=N<=18,1<=Ci<=W<=10^8。
    Nescafé26

     【题解】

    分显然

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 inline void read(int &x){
     7     x = 0;char ch = getchar(),c = ch;
     8     while(ch < '0' || ch > '9')  c = ch, ch = getchar();
     9     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    10     if(c == '-')x = -x;
    11 }
    12 const int MAXN = 18 + 5;
    13 const int INF = 0x3f3f3f3f;
    14 int n,w,weight[MAXN],value[MAXN],ans;
    15 void dfs(int now, int cnt){
    16     if(cnt > ans)return;
    17     if(now == n + 1)
    18     {
    19         ans = cnt;
    20         return;
    21     }
    22     for(register int i = 1;i <= cnt;++ i)
    23         if(weight[i] + value[now] <= w)
    24             weight[i] += value[now], dfs(now + 1, cnt), weight[i] -= value[now];
    25     weight[cnt + 1] = value[now],dfs(now + 1, cnt + 1),weight[cnt + 1] = 0;
    26 }
    27 int main(){
    28     read(n);read(w);
    29     for(register int i = 1;i <= n;++ i) read(value[i]);
    30     ans = INF;
    31     std::sort(value + 1, value + 1 + n, std::greater<int>());
    32     dfs(1, 1);
    33     printf("%d", ans);
    34     return 0;
    35 } 
    Tyvj2018
  • 相关阅读:
    CodeForces
    CodeForces
    CodeForces 718C && HDU 3572 && Constellation
    CodeForces
    USACO 5.4 tour的dp解法
    10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)
    codeforces 724D
    codeforces 724C
    hdu5909 Tree Cutting
    hdu5822 color
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7340976.html
Copyright © 2011-2022 走看看