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

来源:互联网 发布:淘宝客服是什么工作 编辑:程序博客网 时间:2024/06/06 09:03
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 isli.

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
21 53 2
Output
2
Input
32 4 41 1 1
Output
0
Input
62 2 1 1 3 34 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
原创粉丝点击