C. Arthur and Table
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/557/problem/C
Description
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.
Sample Input
2
1 5
3 2
Sample Output
2
HINT
题意
桌子有k个桌腿,如果有大于k/2的最长腿的长度相同的话,那么就舒适
砍掉一个桌腿的代价是d[i],问你最少花费多大的代价,才能得到舒适的桌子
题解:
倒着做,依次去掉最长的桌腿,假设有num个
那么我们只留下num-1个花费最大的腿就好了
就是这样 喵
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 300005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** multiset<int, greater<int> >costs;
multiset<int>::iterator it;
vector<int> cost[maxn];
int l[maxn];
int main()
{
int n=read(),tot=;
for(int i=;i<=n;i++)
l[i]=read();
for(int i=;i<=n;i++)
{
int d=read();
costs.insert(d);
tot+=d;
cost[l[i]].push_back(d);
}
int ans=tot;
for(int i=;i>=;i--)
{
if(!cost[i].empty())
{
for(int j=;j<cost[i].size();j++)
{
costs.erase(costs.find(cost[i][j]));
tot-=cost[i][j];
}
int num=;
int sum=;
for(it=costs.begin();it!=costs.end();it++)
{
if(num==cost[i].size()-)
break;
num++;
sum+=*it;
}
ans=min(ans,tot-sum);
for(int j=;j<cost[i].size();j++)
tot+=cost[i][j];
}
}
cout<<ans<<endl;
}