time limit:per test 1 second
memory limit:per test 256 megabytes
standard input
standard output
Description
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.
You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned.
If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible.
Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up.
Input
The first line contains a single integer n
(1 ≤ n ≤ 50)
(
1
≤
n
≤
50
)
— the number of tasks.
The second line contains n integers a1, a2, ⋯, an(1 ≤ai ≤ 108) a 1 , a 2 , ⋯ , a n ( 1 ≤ a i ≤ 10 8 ) , where ai represents the amount of power required for the i-th task.
The third line contains n integers b1, b2, …, bn (1≤ bi ≤ 100) ( 1 ≤ b i ≤ 100 ) , where bi is the number of processors that i-th task will utilize.
Output
Print a single integer value — the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up.
Examples
input1
6
8 10 9 9 8 10
1 1 1 1 1 1
output1
9000
input2
6
8 10 9 9 8 10
1 10 5 5 1 10
output2
1160
Hint
In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9.
In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round.
题目大意
一堆任务,有各自的功率和需要的处理器的个数
现在要选择一种分配方式,使得通过完成某个任务只能抹去一个功率更小的任务,每个任务最多只能被一个任务抹去,且抹去其它任务的任务不能被抹去,并且让没被抹去的任务的总功率与总处理器的商最小,输出这个最小值。
Analysis
二分一个答案ans,判断
∑ai∑bi≤ans
∑
a
i
∑
b
i
≤
a
n
s
化一化得
∑ai−bi∗ans≤0
∑
a
i
−
b
i
∗
a
n
s
≤
0
也就是要让左式尽量小
功率大的尽量抹去功率小的且
ai−bi∗ans
a
i
−
b
i
∗
a
n
s
尽量大的
那么不妨按照功率从大到小排序
功率相同怎么办?不妨将相同功率的一块算
设f[x]表示目前可供x个任务被抹去,那么假如将同一功率中抹去k个,必然抹去
ai−bi∗ans
a
i
−
b
i
∗
a
n
s
最大的k个,于是顺便给同块的任务按照
ai−bi∗ans
a
i
−
b
i
∗
a
n
s
排序,
f[x−k]=minf′[x]+这块中前k大之外的和
f
[
x
−
k
]
=
min
f
′
[
x
]
+
这
块
中
前
k
大
之
外
的
和
这样枚举通过计算是O(n^2)的甚至更小,所以总复杂度是
O(n2log)
O
(
n
2
l
o
g
)
codes
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 60
#define ll long long
using namespace std;
int n,a[N],c[N],top;
ll f[N][N],ans;
struct node{int a;ll v;}d[N];
bool cmp(node a,node b){return a.a>b.a || (a.a==b.a && a.v>b.v);}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
long long h=1,t=1e13,m,res=t+1;
while(h<=t){
m=(h+t)/2;ans=1e18;
for(int i=1;i<=n;i++)d[i]=(node){a[i],a[i]*(ll)10000-m*c[i]};
sort(d+1,d+n+1,cmp);
for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)f[i][j]=1e18;
f[0][0]=0.0;int k=0;ll sum=d[1].v,dec=0;
for(int i=1,r=0;i<=n;sum+=d[++i].v)if(d[i].a!=d[i+1].a){
++k;
for(int x=0;x<=r;x++,dec=0)for(int y=0;y<=x && y<=i-r;y++){
if(y)dec+=d[r+y].v;f[k][x-y+i-r-y]=min(f[k][x-y+i-r-y],f[k-1][x]+sum-dec);
}
r=i;sum=0;
}
for(int i=0;i<=n;i++)ans=min(ans,f[k][i]);
if(ans<=0)res=m,t=m-1;else h=m+1;
}printf("%I64d",(res+9)/10);
return 0;
}