【codeforces994D】Compute Power

9 篇文章 0 订阅

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 (1n50) ( 1   ≤   n   ≤   50 ) — the number of tasks.

The second line contains n integers a1,a2,,an(1ai108) 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 (1bi100) ( 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,判断 aibians ∑ a i ∑ b i ≤ a n s
化一化得 aibians0 ∑ a i − b i ∗ a n s ≤ 0
也就是要让左式尽量小
功率大的尽量抹去功率小的且 aibians a i − b i ∗ a n s 尽量大的
那么不妨按照功率从大到小排序
功率相同怎么办?不妨将相同功率的一块算
设f[x]表示目前可供x个任务被抹去,那么假如将同一功率中抹去k个,必然抹去 aibians a i − b i ∗ a n s 最大的k个,于是顺便给同块的任务按照 aibians a i − b i ∗ a n s 排序, f[xk]=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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值