B. The Queue
链接:
http://codeforces.com/contest/767/problem/B
题解:
肯定要在某个人前一秒到达,到的更早没有意义,到的更晚就被他抢先了。
这样可以O(n)枚举一遍解决。
但是要考虑各种情况,比如在所有人之后到、别人到的时间都不在规定时间内。
代码:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <cstring> 10 #include <iostream> 11 #include <algorithm> 12 #include <functional> 13 using namespace std; 14 #define rep(i,a,n) for (int i=a;i<=n;i++) 15 #define per(i,a,n) for (int i=n;i>=a;i--) 16 #define pb push_back 17 #define mp make_pair 18 #define all(x) (x).begin(),(x).end() 19 #define fi first 20 #define se second 21 #define SZ(x) ((int)(x).size()) 22 typedef vector<int> VI; 23 typedef long long ll; 24 typedef pair<int, int> PII; 25 const ll mod = 1000000007; 26 const double eps = 1e-7; 27 // head 28 29 const int maxn = 1e5 + 7; 30 ll a[maxn]; 31 32 int main() 33 { 34 ll ts, tf, t, n; 35 cin >> ts >> tf >> t >> n; 36 rep(i, 1, n) scanf("%I64d", a + i); 37 ll now = ts, ans, ansv = 1e15; 38 if (a[1] > now) return 0 * printf("%I64d", now); 39 40 for (int i = 1; i <= n; i++){ 41 if (max(0LL, now - a[i] + 1)<ansv && a[i] - 1 + t <= tf){ 42 ansv = max(0LL, now - a[i] + 1); 43 ans = a[i] - 1; 44 } 45 now = max(now, a[i]) + t; 46 } 47 if (now + t <= tf) printf("%I64d ", now); 48 else printf("%I64d ", ans); 49 50 return 0; 51 }