码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 557C Arthur and Table 砍桌腿

时间:2015-07-01 10:10:58      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

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








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





#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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 557C Arthur and Table 砍桌腿

标签:

原文地址:http://blog.csdn.net/firstlucker/article/details/46705391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!