51nod 1461 稳定桌

18 篇文章 0 订阅

http://www.51nod.com/onlineJudge/problemSolution.html#!problemId=1461

题意:有n个桌腿,要砍掉某些桌腿使得剩下的桌腿能支撑桌子。规定剩下的桌腿中长度最大的桌腿的数量如果超过一半即可支撑桌子。砍掉每个桌腿需要付出代价。求最小的代价和。

枚举。假如最后剩下的桌腿的最大长度为lenth,这样长度的桌腿有num个。那么长度大于lenth的桌腿肯定都被砍去了,然后在剩下的桌腿中按照代价从大到小选择num - 1个桌腿留下来(不砍),将剩余的再砍去,这样一定是最小的代价和。当然也不是无脑枚举,能枚举是因为代价种类很少,才200个。首先我们按桌腿的长度排序,然后枚举每种长度作为最后剩下的长度。从小到大枚举。因为可以维护之前的值。用total记录需要砍掉的长度大于当前枚举到的长度lenth的桌腿的代价和,这样从最小的长度开始枚举,枚举到某个长度lenth时可以直接减去所有长度为lenth的桌腿代价,并保存下来用于枚举下一个长度。然后用cnt数组保存cnt[i] = 长度小于当前枚举的长度lenth的代价为 i 的桌腿总数。这就是要从小到大枚举的原因。因为长度大于lenth的在当前枚举时不用记录,枚举完当前长度后再更新cnt数组即可。

主要精华就是从小到大更新的过程,可以保证记录的d值都是长度小于i的,还有那个num-1比较66!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int MAX = 100005;
const int INF = 100000*200;

struct Leg
{
    int l;
    int d;
};

int n;
Leg leg[MAX];
int cnt[205]; //每种代价的桌腿个数
int total; //总代价

bool cmp(Leg l1, Leg l2)
{
    return l1.l < l2.l;
}

void input()
{
    total = 0;
    for(int i = 0; i < n; i++)
        scanf("%d", &leg[i].l);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &leg[i].d);
        total += leg[i].d;
    }
}

void solve()
{
    memset(cnt, 0, sizeof(cnt));
    sort(leg, leg + n, cmp);
    int i = 0, num, temp, cost, ans = INF;
    while(i < n)
    {
        num = 0;
        cost = total;
        for(temp = i; leg[temp].l == leg[i].l; temp++)
        {
            num++;
            cost -= leg[temp].d;
        }
        num--;
        for(int j = 200; j >= 1 && num > 0; j--)
        {
            cost -= min(num, cnt[j])*j;
            num -= cnt[j];
        }
        ans = min(ans, cost);
        for(temp = i; leg[temp].l == leg[i].l; temp++)
            cnt[leg[temp].d]++;
        i = temp;
    }
    printf("%d\n", ans);
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        input();
        solve();
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值