#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
#题意:
对于一张桌子,评价他是否稳定是看所有桌脚中最长长度的桌脚比例是不是严格大于1/2
枚举每个桌脚的最长长度x,那么显然所有大于x的桌脚要全部去掉,而且长度x的桌脚一定全部保留
其中当d比较大的时候可以用权值线段树维护
另外n==2是的特判要特别注意,不然可能会WA on test 43
复杂度O(n*k)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn=1e5+5;
int n,sum[maxn],b[maxn],d[maxn];
bool vis[maxn];
struct node
{
int x,y;
}a[maxn];
inline bool cmp(const node &A,const node &B)
{
return (A.x<B.x)||(A.x==B.x&&A.y<B.y);
}
int main()
{
while(~scanf("%d",&n))
{
int N=-1;
memset(b,0,sizeof(b));
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i].x);
b[a[i].x]++;
if(N<a[i].x)N=a[i].x;
}
for(int i=1;i<=n;++i)scanf("%d",&a[i].y);
if(n==1)
{
printf("0\n");
continue;
}
if(n==2)
{
if(a[1].x!=a[2].x)printf("%d\n",min(a[1].y,a[2].y));
else printf("0\n");
continue;
}
sort(a+1,a+1+n,cmp);
memset(sum,0,sizeof(sum));
for(int i=n;i>0;--i)sum[a[i].x]+=a[i].y;
sum[N+1]=0;
for(int i=N;i>0;--i)sum[i]+=sum[i+1];
int ans=201*100001;
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
for(int i=1;i<=n;++i)
if(!vis[a[i].x])
{
vis[a[i].x]=1;
int k=i-1-b[a[i].x]+1;
if(k<=0)
{
ans=min(ans,sum[a[i].x+1]);
for(int j=i;a[j].x==a[i].x;++j)d[a[j].y]++;
continue;
}
int cnt=0,res=0;
for(int j=1;j<=200;++j)
{
if(cnt+d[j]<k)
{
cnt+=d[j];
res+=d[j]*j;
}
else
{
res+=j*(k-cnt);
cnt=k;
}
}
ans=min(ans,res+sum[a[i].x+1]);
for(int j=i;a[j].x==a[i].x;++j)d[a[j].y]++;
}
printf("%d\n",ans);
}
return 0;
}