Codeforces Round #398 (Div. 2) B. The Queue(贪心模拟)

版权声明:希望能在自己成长的道路上帮到更多的人,欢迎各位评论交流 https://blog.csdn.net/yiqzq/article/details/82148452

原题地址:http://codeforces.com/contest/767/problem/B

题意:一群人排队办理业务,每次每个人办理业务的所花费的时间是k,且工作人员只在l-r时间段内工作,即r-1是最后工作的时间。给出你n个人来排队的时间,若你和某个人来的时候时间相同,则另一个先办理业务。问你你什么时候来办理业务所需要等待的时间最少?

思路:每次枚举判断如果是任意一个人的前面1秒来是不是最优的就行了.注意特判开头和结尾.

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll ts, tf, t, n;
ll a[maxn];
int main() {
    scanf("%lld%lld%lld%lld", &ts, &tf, &t, &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    ll time = ts;
    ll MIN = 1e18;
    ll ans = 0;
    if (a[1] > ts) {//如果开始上班还没人,那么就先去办是最优的
        printf("%lld\n", ts);
        return 0;
    }
    for (int i = 1; i <= n; i++) {//其余的最优方案他一定来自于选择一个人的前面一个人 -1
        if (a[i] + t - 1 > tf) {
            break;
        }
        ll res = max(0LL, time - a[i] + 1);
        if (res < MIN) {
            MIN = res;
            ans = a[i] - 1;

        }
        time = max(time, a[i]) + t;
    }
    if (time + t <= tf) {//注意特判当所有人都办了之后最后还能不能办
        MIN = 0;
        ans = time;
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/82148452