题目链接:Codeforces 557C Arthur 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
题意:一个桌子有n条腿,桌子被认为是稳定的当且仅当最大高度的腿数超过一半。现在给出砍掉第i条腿的代价,问你最少需要多少代价使得桌子稳定。
思路:我们考虑最大高度为i时付出的代价,显然>i的腿都要砍掉。
记Count[i]为高度为i的腿数,剩余腿数为total。
当Count[i] > total / 2,直接代价就是 > i的所有腿的代价。
反之,我们需要从< i腿中去掉need = total - 2 * Count[i] + 1条腿。
我们可以以代价为限定 离散化所有的腿。
然后在高度< i的限制下插入符合条件的腿,用一个树状数组维护个数,一个维护代价,二分找出右端点R使得[1, R]里面插入的腿数 == need,然后最小代价就是[1, R]里面的代价和。
枚举高度,然后维护最小代价。时间复杂度O(n*logn+Max*logn*logn)。
AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
struct Node {
int h, c, id;
};
Node num[MAXN];
bool cmp1(Node a, Node b) {
return a.c != b.c ? a.c < b.c : a.h < b.h;
}
bool cmp(Node a, Node b) {
if(a.h != b.h) return a.h < b.h;
else if(a.c != b.c) return a.c < b.c;
return a.id < b.id;
}
int lowbit(int x) {
return x & (-x);
}
int n;
int C1[MAXN], C2[MAXN];
void Update(int x, int d) {
while(x <= n) {
C1[x] += 1;
C2[x] += d;
x += lowbit(x);
}
}
int Sum(int x, int op) {
int s1 = 0, s2 = 0;
while(x > 0) {
s1 += C1[x];
s2 += C2[x];
x -= lowbit(x);
}
if(op == 1) return s1;
return s2;
}
int cost[MAXN], Count[MAXN];
int scost[MAXN], scount[MAXN];
int main()
{
while(scanf("%d", &n) != EOF) {
int Max = 0; CLR(cost, 0); CLR(Count, 0);
for(int i = 1; i <= n; i++) {
scanf("%d", &num[i].h);
Max = max(Max, num[i].h);
Count[num[i].h]++;
}
for(int i = 1; i <= n; i++) {
scanf("%d", &num[i].c);
cost[num[i].h] += num[i].c;
}
sort(num+1, num+n+1, cmp1);
for(int i = 1; i <= n; i++) {
num[i].id = i;
}
scost[0] = scount[0] = 0;
for(int i = 1; i <= Max; i++) {
scost[i] = scost[i-1] + cost[i];
scount[i] = scount[i-1] + Count[i];
}
sort(num+1, num+n+1, cmp);
int j = 1; CLR(C1, 0); CLR(C2, 0); int ans = INF;
for(int i = 1; i <= Max; i++) {
if(cost[i] == 0) continue;
while(j <= n && num[j].h < i) {
Update(num[j].id, num[j].c); j++;
}
int Tcost = scost[Max] - scost[i];
int total = n - (scount[Max] - scount[i]);
//cout << total << endl;
if(Count[i] < total / 2 + 1) {
int need = total - 2 * Count[i] + 1;
//cout << need << endl;
int R;
int l = 1, r = n;
while(r >= l) {
int mid = (l + r) >> 1;
if(Sum(mid, 1) >= need) {
R = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
Tcost += Sum(R, 2);
}
//cout << Tcost << endl;
ans = min(ans, Tcost);
}
printf("%d\n", ans);
}
return 0;
}