链接:戳这里
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条桌腿的长度li,以及拆每根所要花费的精力di
(1<=n<=1e5) (1<=li<=1e5) ( 1<=li<=200)
思路:
枚举当前的长度i作为最大值的最少花费,O(1e5)
处理一下大于i的花费的后缀和sum
满足超过一半的桌腿为i,则当前的桌腿中只保留<=i的数目为:num[i]-1 (num[i]表示长度为i的桌腿条数)
暴力找这num[i]-1条的话肯定T
枚举花费j,h[j]表示花费为j且桌腿长度小于i的桌腿数为h[j]
贪心每次都减去num[i]-1个小的花费,更新ans值
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1<<30)-1
using namespace std;
struct node{
int l,d;
bool operator < (const node &a)const{
if(l==a.l) return d<a.d;
return l<a.l;
}
}s[100100];
int len[100100],sum[100100];
int num[100100];
int tot[100100],h[210];
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&s[i].l);
tot[s[i].l]++;
}
for(int i=1;i<=n;i++) {
scanf("%d",&s[i].d);
len[s[i].l]+=s[i].d;
}
for(int i=100000;i>=1;i--){
if(tot[i]==0) {
sum[i]=sum[i+1];
num[i]=num[i+1];
}
else {
sum[i]=sum[i+1]+len[i];
num[i]=num[i+1]+tot[i];
}
}
sort(s+1,s+n+1);
int ans=INF;
int cnt=1;
for(int i=1;i<=100000;i++){
if(tot[i]==0) continue;
int t=sum[i+1];
int x=n-num[i+1];
if(tot[i]*2>x) ans=min(ans,t);
else {
int y=x-2*tot[i]+1;
while(cnt<=n && s[cnt].l<i) h[s[cnt++].d]++;
int tmp=0;
for(int j=1;j<=200;j++){
if(h[j]==0) continue;
if(y-h[j]>=0){
y-=h[j];
tmp+=j*h[j];
} else {
tmp+=y*j;
y=0;
break;
}
}
ans=min(ans,tmp+t);
}
}
cout<<ans<<endl;
return 0;
}
/*
9
1 1 1 3 3 3 5 5 5
9 8 7 1 1 1 1 1 2
10
5 6 7 14 15 16 17 19 19 19
195 70 54 67 140 183 56 25 132 186
*/