C. Arthur and Table
time limit per test
memory limit per test
input
output
Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.
n legs, the length of the i-th leg is li.
di — the amount of energy that he spends to remove the i-th leg.
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 5legs 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
n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.
n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.
n integers di (1 ≤ di), 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 test(s)
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个男孩,n个女孩,男孩的水是一样的,女孩的也是一样的,但是男孩的是女孩的两倍,有2*n个杯子,求最多能分给他们的总水量
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int n;
struct node {
int x;
int y;
} q[1001000];
int v[201];
int minn;
int small;
bool cmp(node a,node b) {
return a.x > b.x;
}
int main() {
while(scanf("%d",&n)!=EOF) {
memset(v,0,sizeof(v));
for(int i=0; i<n; i++) {
scanf("%d",&q[i].x);
}
for(int i=0; i<n; i++) {
scanf("%d",&q[i].y);
v[q[i].y]++;
}
if(n == 2 && q[0].x!=q[1].x)
{
printf("%d\n",min(q[0].y,q[1].y));
continue;
}
sort(q,q+n,cmp);
minn = 99999999;
int cnt = 1;
int sum = 0;
int ans = 0;
int pn = n;
small = 0;
v[q[0].y]--;
ans = q[0].y;
for(int i=1; i<n; i++) {
if(q[i].x == q[i-1].x) {
v[q[i].y]--;
ans += q[i].y;
cnt++;
} else {
pn -= cnt;
int num = pn - cnt + 1;
if(num>0) {
for(int j=1; j<=201; j++) {
if(v[j]>=num) {
small += num * j;
num = 0;
break;
} else {
small += v[j]*j;
num -= v[j];
}
}
}
small += sum;
minn = min(small,minn);
cnt = 1;
sum += ans;
ans = q[i].y;
v[q[i].y]--;
small = 0;
}
}
minn = min(minn,sum);
printf("%d\n",minn);
}
return 0;
}