您的位置:首页 > 其它

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