CodeForces - 557CArthur and Table(贪心+桶排或权值线段树)

#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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值