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

【题目链接】:http://hihocoder.com/problemset/problem/1498

【题意】

一开始你有1个机器人;
你有n个工作;
每个工作都需要一个机器人花1小时完成;
然后每个机器人能够花Q时间建造另外一个机器人;
->有两个机器人了!
可以无限建造;
然后问你工作完成需要多长时间。

【题解】

贪心题;
这题直接枚举建了次的机器人;
建的时候;
之前所有建的机器人都在建;
也就是说在建新的机器人的时候;
没有其他机器人是在工作的;
贪心的道理就是;
新建机器人,比直接让机器人去干活来得划算;
毕竟建一次机器人;
机器人的数目就翻倍了;

t=n/(2^k) + k*Q;
k是枚举建了多少次机器人;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

LL n,q,ans,num;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n >> q;
    ans = n;
    LL temp = 1;
    while (1)
    {
        num++,temp = temp*2;
        LL tans = (n-1)/temp + 1 + num*q;
        ans = min(ans,tans);
        if (n<temp) break;
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626378.html