首页 > 其他分享 >Cows in a Skyscraper G

Cows in a Skyscraper G

时间:2024-07-12 09:10:50浏览次数:5  
标签:cnt return int dfs cat Skyscraper ans Cows

dfs版本

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 2e1;

int cat[N], cab[N];
int n, w;
int ans;

bool cmp(int a, int b) {
    return a > b;
}

void dfs(int now, int cnt) {
    if (cnt >= ans) {
        return;
    }
    if (now == n + 1) {
        ans = min(ans, cnt);
        return;
    }
    //尝试分配到已经租用的缆车上
    for (int i = 1; i <= cnt; i++) {  //分配到已租用缆车
        if (cab[i] + cat[now] <= w) {
            cab[i] += cat[now];
            dfs(now + 1, cnt);
            cab[i] -= cat[now];  //还原
        }
    }

    // 新开一辆缆车
    cab[cnt + 1] = cat[now];
    dfs(now + 1, cnt + 1);
    cab[cnt + 1] = 0;
}

int main() {
    cin >> n >> w;
    for (int i = 1; i <= n; i++) {
        cin >> cat[i];
    }

    sort(cat + 1, cat + 1 + n, cmp);

    ans = n;

    dfs(1, 0);

    cout << ans << endl;

    return 0;
}

标签:cnt,return,int,dfs,cat,Skyscraper,ans,Cows
From: https://www.cnblogs.com/caterpillor/p/18297518

相关文章

  • P7411 [USACO21FEB] Comfortable Cows S (搜索)
    P7411[USACO21FEB]ComfortableCowsS搜索容易知道任意时刻的不合法的位置,并且决策只有将空着的位置补起来。每次加入一个点,判断其自身、上下左右是否变得不合法,往下递归即可。复杂度分析,每个点只会不合法一次(修改后就变得合法),所以只会遍历一次,复杂度是\(O(n^2)\)。#inclu......
  • 有趣的Python库——CowSay
    有趣的Python库——CowSay安装:pipinstallcowsay命令式使用:cowsay-cpig-t你好,我是一只猪哦!输出:__________|你好,我是一只猪哦!|==========\\\\,.(_|,......
  • P3056 [USACO12NOV] Clumsy Cows S
    [USACO12NOV]ClumsyCowsS题目描述Bessiethecowistryingtotypeabalancedstringofparenthesesintohernewlaptop,butsheissufficientlyclumsy(duetoherlargehooves)thatshekeepsmis-typingcharacters.Pleasehelpherbycomputingthemi......
  • 洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows
    题意:给定n个区间,左端点和右端点表示工作开始时间和结束时间。求最长一直有人在工作的时间和无人工作的时间。思路:想到了并查集,还有差分树状数组,最后选择差分数组。左端点加,右端点减,然后一次遍历即可。总结:习惯性的在右端点+1的位置减少了1,但是不适用于这个题目的逻辑。因为在右......
  • P3052 [USACO12MAR] Cows in a Skyscraper G
    原题链接题解模拟,遍历n个物品,一开始一个箱子不给,遍历到某个物品时,先把所有已经给了的箱子放进去试试,再创一个新箱子放进去试试code#include<bits/stdc++.h>usingnamespacestd;intn,w;intcnt,ans;intchongdie=0;intbox[20],c[20];voidmoni(intnow,intcnt)//now......
  • P2742 [USACO5.1] 圈奶牛Fencing the Cows /【模板】二维凸包
    原题链接题解这么优质的文章我写什么题解好难解释必然性感觉像模拟??code#include<bits/stdc++.h>usingnamespacestd;intq[100005]={0};structnode{doublex,y;}a[100005];doubledis(intb,intc){nodei=a[b],j=a[c];returnsqrt((i.x-j.x)*(i.x-......
  • P3047 [USACO12FEB] Nearby Cows G
    原题链接题解核心技巧:两次搜索第一次搜索:搜索出\(f[now][i]\)以\(now\)为根节点的子树且距离根节点恰好为\(i\)的节点的个数搜索完了之后,把范围\(k\)以内的累加第二次搜索:由于整棵树的根节点的\(f\)等于整棵树里距离不大于\(k\)的节点个数,即已经符合题目要求......
  • P7154 Sleeping Cows 题解
    传送门题意:给定两个数组\(a_i,b_i\),若\(a_i\leb_j\),则他俩可配对。求极大匹配的方案数。(极大不是最大,最大一定是极大)先考虑最大匹配方案数怎么求。把\(a\)和\(b\)从小到大排序。则每个\(a_i\)能匹配的\(b\)都是一段后缀,且随着\(i\)增大,这个后缀越来越小。于是从......
  • P1204 [USACO1.2] 挤牛奶Milking Cows
    原题链接题解细节颇多看代码code#include<bits/stdc++.h>usingnamespacestd;structunit{ints,e;}milk[5005];boolcmp(unita,unitb){returna.s<b.s;}intmain(){intn;cin>>n;for(inti=1;i<=n;i++)cin>>milk[i].s......
  • [USACO07DEC] Sightseeing Cows G
    [USACO07DEC]SightseeingCowsG题目描述FarmerJohnhasdecidedtorewardhiscowsfortheirhardworkbytakingthemonatourofthebigcity!Thecowsmustdecidehowbesttospendtheirfreetime.Fortunately,theyhaveadetailedcitymapshowingthe$L......