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.
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.
Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.
2 1 5 3 2
2
3 2 4 4 1 1 1
0
6 2 2 1 1 3 3 4 3 5 5 2 1
8
题意:
有一个桌子有n条腿,每条腿的长度值不一,现在需要去掉其中一些以使桌子平稳,去掉第li条腿需耗费di力气,而桌子平稳的条件是长度最大的腿的数量占所有腿的一半以上,求所需耗费的最少力气。
分析:
每种长度值都可能成为最大值,所以必须先把每种值遍历一遍,当作是最大值。假设这个最大值的个数有x个,那么接下来只要选x-1(当然如果不足x-1个的话肯定就全选了)个长度值比它小且耗费力气值尽可能大的腿就行了。我只想到了O(n^2)的算法==,想了很久于是最终还是偷看了别人的思路~~~发现只需要遍历一遍,由于di的值最大只有200,所以可以遍历值1~200选出长度值比最大值小且耗费力气值尽可能大的腿了(想想只是找出x-1个耗费力气值最大的腿是很简单的,而要满足条件"长度值比最大值小"该如何实现呢:那就是第一遍从大到小枚举长度最大值时将其耗费力气值从记录中去掉,这样后面就不会找出比此时的长度最大值更大的腿了)。
吐糟:
做这题想的东西比较多,在脑中很6的就过去了,但要用文字写出来真是难,很简单的思路也写半天都写不清==。。花的时间都赶上做题时间了。。所以就简略的写下题解咯,题目质量还是不错的呢。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 100000+10
struct P
{
int l, d;
}p[N];
bool cmp(P x, P y) {return x.l > y.l;}
int main()
{
int n, sum = 0, m[210] = {0};
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%d", &p[i].l);
for(int i=0; i<n; i++)
{
scanf("%d", &p[i].d);
m[p[i].d]++; sum += p[i].d;
}
sort(p,p+n,cmp); //按照长度值从大到小排序
int res = 0, temp = 0, cnt = 0;
for(int i=0; i<=n; i++)
{
if(i == 0 || p[i].l == p[i-1].l)
{
temp += p[i].d;
m[p[i].d]--; //避免后面找到长度值比它大
cnt++; //记录这种长度值的个数
}
else
{
for(int k=200; k>=1; k--)
{
if(m[k] > 0)
{
if(m[k] <= cnt-1) temp += m[k]*k, cnt -= m[k];
else temp += (cnt-1)*k, cnt = 1;
}
if(cnt <= 1) break;
}
res = max(res, temp);
temp = p[i].d; cnt = 1; m[p[i].d]--;
}
}
printf("%d\n", sum-res);
return 0;
}