CodeForces - 557C. Arthur and Table

C. Arthur and Table
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

In total the table Arthur bought has n legs, the length of the i-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.

The second line of the input contains a sequence of n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.

The third line of the input contains a sequence of n integers di (1 ≤ di ≤ 200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

Output
Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

Examples
input
2
1 5
3 2
output
2
input
3
2 4 4
1 1 1
output
0
input
6
2 2 1 1 3 3
4 3 5 5 2 1
output
8
暴力题,思路要清晰!!!如果想不清楚稍微漏掉一两个细节那debug调到死也不一定出得来。。大体思路其他地方都有,我看重复很严重就不打了,看注释吧,还算详细。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e5 + 10;

int n, cnt[maxn], sum = 0;

struct node
{
    int l, d;
}table[maxn];

bool cmp(node a, node b)
{
    return a.l < b.l;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> table[i].l;
    }
    for(int i = 1; i <= n; i++)
    {
        cin >> table[i].d;
        sum += table[i].d;
    }
    sort(table+1, table+1+n, cmp);
    memset(cnt, 0, sizeof(cnt));
    int ans = 0x3f3f3f3f;
    int i = 1, j;
    while(i <= n)
    {
        int num = 0;
        int cost = sum;
        for(j = i; table[j].l==table[i].l; j++)
        {
            num++;
            cost -= table[j].d;
        }
        num--;
        for(j = 200; j >= 1 && num > 0; j--)
        {
            cost -= min(num, cnt[j])*j;//若没有被遍历过,则长度比当前桌腿长,必须砍掉;若被遍历过了,则从大到大,尽可能留下大的,最多剩下num-1个桌腿
            num -= cnt[j];
        }
        ans = min(ans, cost);
        for(j = i; table[j].l==table[i].l; j++)
        {
            cnt[table[j].d]++;//记录已经被遍历过的较长的桌腿
        }
        i = j;//j此时为第一个与i长度不同的桌腿
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值