Codeforces Round 444 div2 D Ratings and Reality Shows (双指针)
2017-11-08 11:57
453 查看
题解 : 这个题目其实就让让我们不断动态维护一个区间的区间最小值,看这个最小值是否小于 0;对于这种题目我们可以使用双指针的方法来进行求解 第一个指针指向开始的位置,第二个指针指向结束的位置。在指针不断的移动过程中我们需要维护一个是当前的 rating 一个是这个区间改变的 rating 一个是改变 rating 的最小值,这三个量都可以在更新的过程中维护,时间复杂度就是 o (n)。 具体看代码 很短很好懂
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxn = 3e5 + 10; int n,a,b,c,d,st,len; int t[maxn],q[maxn]; int main () { ios_base :: sync_with_stdio(false); cin >> n >> a >> b >> c >> d >> st >> len; for (int i = 1;i <= n; ++ i) { cin >> t[i] >> q[i]; } t[0] = 0; int MIN = 0,rating = st,change = 0; int now = 1; for (int i = 1;i <= n; ++ i) { while (now <= n && t[now] - t[i] < len) { if (q[now] == 1) change += c; else change -= d; MIN = min (MIN,change); now ++; } if (MIN + rating >= 0) { cout << t[i - 1] + 1 << endl; return 0; } rating += (q[i] ? a : -b); change -= (q[i] ? c : -d); MIN -= (q[i] ? c : -d); if (rating < 0) { cout << -1 << endl; return 0; } } cout << t + 1 << endl; return 0; }
相关文章推荐
- 【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度
- Educational Codeforces Round 37 (Rated for Div. 2)-F-SUM and REPLACE(线段树)
- Codeforces Round #371 (Div. 2) C. Sonya and Queries (map)
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round 370 (Div 2) D.Memory and Scores 【前缀和 DP】双人随机数值增减K A比B多的方案数
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
- Codeforces Round #291 (Div. 2)C.Watto and Mechanism——字典树+dfs
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
- 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round 281 div2 C Vasya and Basketball
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set