zoukankan      html  css  js  c++  java
  • Codeforces Round #398 B题The Queue(贪心)解题报告

    Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.

    He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves).

    Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.

     "Reception 1"

    For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.

    Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

    Input

    The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office.

    All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

    Output

    Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

    Example

    Input
    10 15 2
    2
    10 13
    Output
    12
    Input
    8 17 3
    4
    3 4 5 8
    Output
    2

    Note

    In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.

    In the second example, Vasya has to come before anyone else to be served.

    需要注意的是文中标红的句子。以及数据范围需要用long long。其他的就是模拟队列,在到每个人时求如果恰在这个人之前1min来需要的时间(如果有多个人同时来,“我”提前他们1分钟来,不同人算的结果不一样,但是是递增的,只要第一个就行了,为了保证统一,才将这些人的都算出来)。记录这些中的最小值即可。

     1 #include <iostream>
     2 #include<bits/stdc++.h>
     3 #include <queue>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <algorithm>
     7 using namespace std;
     8 typedef long long ll;
     9 typedef unsigned long long ull;
    10 const int MAX=1e6+5;
    11 queue <ll> que;
    12 int main()
    13 {
    14     ll an,ts,tf,t;
    15     ll n;
    16     ll tem;
    17     ll time=ts;
    18     ll mint;
    19     ll cnt;
    20     ll pot;
    21     cin>>ts>>tf>>t;
    22     tf-=(t-1);
    23     cin>>n;
    24     if(n==0)
    25     {
    26         cout<<ts<<"
    ";
    27         return 0;
    28     }
    29     cin>>tem;
    30     an=tem-1;
    31     n--;
    32     if(t==0)
    33     {
    34         cout<<ts<<"
    ";
    35         return 0;
    36     }
    37     if(tem>ts)
    38     {
    39         cout<<ts<<"
    ";
    40         return 0;
    41     }
    42     mint=ts-tem+1;
    43     while(n--)
    44     {
    45         cin>>tem;
    46         que.push(tem);
    47     }
    48     time=ts+t;
    49 //    que.push(tf-1);
    50     while((!que.empty())&&time<tf)
    51     {
    52         tem=que.front();
    53         que.pop();
    54         if(tem>time)
    55         {
    56             cout<<time;
    57             return 0;
    58         }
    59 //        cnt=1;
    60         pot=time-tem+1;
    61         if(pot<mint)
    62         {
    63             mint=pot;
    64             an=tem-1;
    65         }
    66         time+=t;
    67     }
    68     if(time<=tf)
    69         cout<<time<<"
    ";
    70     else
    71     cout<<an<<"
    ";
    72 }
  • 相关阅读:
    ThinkPHP5专题
    php截取中文字符串
    跨域/非跨域接口专题
    JS检查输入项是否为手机号码或者固话号码的正则表达式
    TinkPHP去重统计查询
    模型类设计模式
    经典排序方法 python
    leetcode122 买卖股票的最佳时机 python
    找到链表的倒数第k个节点 python
    链表的实现、输出和反向 python
  • 原文地址:https://www.cnblogs.com/quintessence/p/6415528.html
Copyright © 2011-2022 走看看