Codeforces Round #311 (Div. 2) C. Arthur and Table

12 篇文章 0 订阅
10 篇文章 0 订阅
C. Arthur and Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 thei-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined numberdi — the amount of energy that he spends to remove thei-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 with5 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 integersli (1 ≤ li ≤ 105), whereli is equal to the length of thei-th leg of the table.

The third line of the input contains a sequence of n integersdi (1 ≤ di ≤ 200), wheredi is the number of energy units that Arthur spends on removing thei-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.

Sample test(s)
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

题意:给你一张桌子,有n条腿,告诉每条腿的长度(l)以及砍掉这条腿的花费(d),当最长腿的数量超过总数量的1/2,那么合法。问如何使得花费最小达到合法情况。
分析:暴力枚举。假设以腿长为a作为最长腿且腿a共有y条,计算花费cost需分两步:
(1)、腿长大于a的,都要砍掉,则cost+=bigger[a];(bigger[a]可预处理得到)
(2)、腿长小于a的,假设有x条,则需要从小到大依次减去x-y+1条。由于花费d的范围只有1~200,所以可以利用花费来计算。
题目链接: http://codeforces.com/contest/557/problem/C
代码清单:
#include<queue>
#include<stack>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 5;
const ll maxv = 1e10 + 5;

int n;
struct Edge{int l,d;}leg[maxn];
int cost[maxn]; 
ll Back[maxn];
int num[maxn]; 

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

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

    sort(leg,leg+n,cmp);
    ll sum=0;
    for(int i=n-1;i>0;i--){ //计算腿长比leg[i-1].l大的总花费
        sum+=(ll)leg[i].d;
        if(leg[i].l!=leg[i-1].l){
            Back[leg[i-1].l]=sum;
        }
    }

    ll ans=Back[leg[0].l];
    cost[leg[0].d]++;

    for(int i=1;i<n;i++){
        if(leg[i].l!=leg[i-1].l){
            int an=i-num[leg[i].l]+1;
            sum=Back[leg[i].l];
            if(an>0){
                for(int j=1;j<=200;j++){ //从小到大依次减去an数量的腿
                    if(cost[j]){
                        if(cost[j]>=an){
                            sum+=(an*j);
                            an=0;
                            break;
                        }
                        else{
                            sum+=(cost[j]*j);
                            an-=cost[j];
                        }
                    }
                }
            }
            ans=min(ans,sum);
        }
        cost[leg[i].d]++;
    }
    printf("%I64d\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值