您的位置:首页 > 编程语言

微软2017年预科生计划在线编程笔试第二场B题Diligent Robots

2017-04-08 21:39 357 查看
原题见链接

点击打开链接

这题一开始我也不知道怎么做,然后大致列举了几种最基本的输入,发现,如果设花x小时造机器人,那么经过小时后,会有2^(x/Q)个机器人,那么去完成N个工作所需的时间就是B = N/(2^(x/Q))向上取整个小时,那么一共话费的时间就是x+B小时,当x = 0时表示只有最一开始的机器人一直在做工作,所花时间是N小时。那么题目就可以转变成求函数y = x + ceil(N/(2^(x/Q)))   (x = 0, Q, 2Q, 3Q...)时的最小值。

最后附上我写的c/c++代码

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int main() {
long long n, q, res;
scanf("%lld %lld", &n, &q);
if(n == 1) res = 1;
else if(n == 2) res = 2;
else if(n == 3) res = 3;
else {
res = n;
long long x = q, c = 2;
long long f = x + (long long)(n * 1.0 / c + 0.5);
while (f < res) {
res = f;
x += q;
c*= 2;
f = x + (long long)ceil(n * 1.0 / c);
}
}
printf("%lld\n", res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微软 编程 c++