稳定桌

有一张桌子,有n个腿。第i根腿的长度是li。

现在要拿掉一些腿,使得桌子稳定,拿掉第i根腿需要di的能量。

稳定的条件是,假如拿掉若干条腿之后,桌子还有k个腿,那么长度最长的腿的数目要超过一半。比如桌子有5根腿,那么至少要有三根腿是最长的。另外,只有一根腿的桌子是稳定的,两个腿的桌子想要稳定,必需长度是一样的。

你的任务是拿掉若干腿,使得桌子稳定,并且所消耗的能量要最少。


Input
单组测试数据。
第一行有一个整数n (1 ≤ n ≤ 10^5),表示刚开始桌子腿的数目。
第二行包含n个整数li (1 ≤ li ≤ 10^5),表示第i个腿的长度。
第三行包含n个整数 di (1 ≤ di ≤ 10^5),表示拿掉第i个腿所消耗的能量。
Output
输出使得桌子稳定所消耗的最少能量。
Input示例
6
2 2 1 1 3 3
4 3 5 5 2 1
Output示例
8
#include <iostream>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;
const int MAXN = 1e5 + 5;
struct leg
{
    int length;
	int cost;
}legs[MAXN];

bool cmp_leg(const leg &a, const leg &b)
{
	return a.length < b.length;
}

int n;
ll result;
ll sums[MAXN];

int main()
{
	cin >> n;
    for (int i = 0; i < n; i++)
	{
		cin >> legs[i].length;
	}
    for (int i = 0; i < n; i++)
	{
		cin >> legs[i].cost;
	}
    sort(legs, legs + n, cmp_leg);

	sums[0] = legs[0].cost;
	for (int i = 1; i < n; i++)
	{
		sums[i] = sums[i-1] + legs[i].cost;
	}

	multiset<int> buf;

	ll result = sums[n-1];
	ll total = sums[n-1];

	for (int i = 0; i < n;)
	{
		ll temp = 0;
		int j = i;
		while (j < n && (legs[j].length == legs[i].length))
		{
			j++;
		}

		temp += (sums[n-1] - sums[j-1]);
		int size1 = i - (j - i - 1);
		int size2 = i - size1;

		if (size1 < size2)
		{
			for (multiset<int>::iterator it = buf.begin(); size1 > 0; it++)
			{
				temp += *it;
				size1--;
			}
		}
		else
		{
			temp += sums[i-1];
			for (multiset<int>::reverse_iterator it = buf.rbegin(); size2 > 0; it++)
			{
				temp -= *it;
				size2--;
			}
		}

		result = min(result, temp);
		for (int k = i; k < j; k++)
		{
			buf.insert(legs[k].cost);
		}

		i = j;
	}

	cout << result << endl;
    
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值