codeforces 767B The Queue【贪心】

本文解析了CodeForces竞赛中的一道题目B,通过计算每个人办理护照的时间段,找到主角Vasya等待时间最短的时机。文章提供了一种算法解决方案,并附带完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:

http://codeforces.com/contest/767/problem/B


题意:

Vasya要去护照,然而接待员在【ts,tf)的时间段内上班,处理一件业务需要花费时间t,并且有n个人在T【i】的时间去办护照,当Vasya与另一个人同时去办护照时,Vasya排在他的后面,问Vasya什么时候去等待的时间最少。


题解:

将n个人的办护照的时间段都计算出来,然后因为对于Vasya来说他只要在某个人来之前的前一秒先到所花费的时间最少,还要注意判断最后一个人办完护照后Vasya是否还能办护照。


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include <string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
using namespace std;
#define MAX_N 1000005
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
const LL INF = 9e18;
const int mod = 100000000;
typedef pair<LL, LL>P;

int n;
LL ts, tf, t;
LL T[MAX_N];
P V[MAX_N];
int main()
{
    cin >> ts >> tf >> t;
    cin >> n;
    for(int i=1; i<=n; i++) {
        scanf("%lld",&T[i]);
    }
    V[0].second = -INF;
    for(int i=1; i<=n; i++) {
        if(T[i] > V[i-1].second) {
            if(T[i] >= ts)
                V[i].first = T[i];
            else
                V[i].first = ts;
            V[i].second = V[i].first + t - 1;
        }
        else {
            V[i].first = V[i-1].second + 1;
            V[i].second = V[i].first + t - 1;
        }
    }/*
    for(int i=1; i<=n; i++) {
        printf("%lld %lld\n",V[i].first, V[i].second);
    }*/
    LL minT = INF;
    LL ans;
    for(int i=1; i<=n; i++) {
        P tmp;
        if(T[i]-1 > V[i-1].second) {
            if(T[i]-1 >= ts)
                tmp.first = T[i] - 1;
            else
                tmp.first = ts;
            tmp.second = tmp.first + t - 1;
        }
        else {
            tmp.first = V[i-1].second + 1;
            tmp.second = tmp.first + t - 1;
        }
        if(minT>tmp.first-T[i]+1 && tmp.second<tf) {
            minT = tmp.first - T[i] + 1;
            ans = T[i] - 1;
        }
    }
    if(V[n].second+t<tf)
        ans = V[n].second + 1;
    if(minT != INF)
        printf("%lld\n",ans);
    else
        printf("%lld\n",ts);
}


### 关于Codeforces编号为997的Div.2比赛的信息 #### 比赛概述 Codeforces编号为997的比赛属于Div.2级别,即面向较低评级选手的比赛。这类比赛通常包含多个编程挑战题目,旨在测试参赛者的算法设计能力和编码技巧。 #### 题目列表及其解决方案概览 ##### A. Multiple Testcases 此题描述了一个场景,在准备好的一系列单个测试案例基础上被要求转换成多组输入输出形式的任务[^1]。对于此类问题,处理方式涉及读取多次输入直到遇到特定终止条件为止,并针对每次输入执行相同逻辑操作后给出相应结果。 ```cpp #include <iostream> using namespace std; int main(){ int t; cin >> t; while(t--){ // 处理每一组数据... cout << "Result\n"; } } ``` ##### B. Valera and Contest 该题目设定围绕着Valera参加的一场竞赛展开,其中涉及到如何计算朋友间共同完成项目的数量[^3]。解决方法依赖于解析给定的数据集来统计满足一定条件下可以合作成功的项目数目。 ```cpp // 假设已经定义好了必要的变量和函数用于接收并处理输入数据... for (int i = 0; i < n; ++i){ if ((a[i] && b[i]) || (!a[i] && !b[i])){ count++; } } cout << count << endl; ``` ##### C. Problem with Priority Queue Implementation 考虑到提到贪心策略加上优先队列的应用背景,这里假设存在一道关于实现或应用优先队列解法的问题。这可能意味着要构建一个能够高效管理元素进出顺序的数据结构实例。 ```cpp priority_queue<int> pq; pq.push(5); pq.push(1); pq.push(10); while(!pq.empty()){ cout << pq.top() << ' '; pq.pop(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值