Codeforces Round #444 (Div. 2) D 尺取区间

There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by a and after each fashion show decreases by b(designers do too many experiments nowadays). Moreover, sometimes top-models participates in talk shows. After participating in talk show model becomes more popular and increasing of her rating after photo shoots become c and decreasing of her rating after fashion show becomes d.

Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will never become negative. Help her to find a suitable moment for participating in the talk show.

Let's assume that model's career begins in moment 0. At that moment Izabella's rating was equal to start. If talk show happens in moment t if will affect all events in model's life in interval of time [t..t + len) (including t and not including t + len), where len is duration of influence.

Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will not become become negative before talk show or during period of influence of talk show. Help her to find a suitable moment for participating in the talk show.

Input

In first line there are 7 positive integers nabcdstartlen (1 ≤ n ≤ 3·1050 ≤ start ≤ 1091 ≤ a, b, c, d, len ≤ 109), where n is a number of fashion shows and photo shoots, abc and d are rating changes described above, start is an initial rating of model and len is a duration of influence of talk show.

In next n lines descriptions of events are given. Each of those lines contains two integers ti and qi (1 ≤ ti ≤ 1090 ≤ q ≤ 1) — moment, in which event happens and type of this event. Type 0 corresponds to the fashion show and type 1 — to photo shoot.

Events are given in order of increasing ti, all ti are different.

Output

Print one non-negative integer t — the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print  - 1.

input
5 1 1 1 4 0 5
1 1
2 1
3 1
4 0
5 0
output
6
input
1 1 2 1 2 1 2
1 0
output
-1

解析:

题意是一个模特有两种活动,一种是拍照片(+a)一种是参加时尚秀(-b),对这个模特的评分都有影响且模特初始的评分已经给出(start),现在这个模特要举办茶会,举办完茶会后,模特再拍照就+C再参加时尚秀就-D,现在要求最早的时间让模特在举办茶会前和举办茶会后len的时间内,评分都不低于0;

尺取法,设一个变量S首先让S达到距离i最大的时间后,之后每次移动i的时候就将S减掉在i时加上的数,然后设一个变量X表示在举办茶会前模特的总评分每次移动i就(+a or -b)。S是指这个区间总的权值,但模特的比较要用S区间中最小的那个数加上X,看是否大于0.

#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5+9;
int vec[MAX_N];
int res[MAX_N];
int main()
{
    int N,a,b,c,d,start,len;
    scanf("%d%d%d%d%d%d%d",&N,&a,&b,&c,&d,&start,&len);
    for(int i=0; i<N; i++) scanf("%d%d",&vec[i],&res[i]);
    int pos = 0;
    long long int s = 0,x = start;
    long long int minn = 0;
    for(int i=0; i<N; i++)
    {

        while(pos < N && vec[pos]-vec[i] <= len)
        {
            s += res[pos]?c:-d;
            minn = min(s,minn);
            pos ++;
        }
        if(x + minn >= 0)
        {
            if(i==0)printf("0\n");
            else printf("%d\n",vec[i-1] + 1);
            return 0;
        }
        s -= res[i]?c:-d;
        minn -= res[i]?c:-d;   //将minn之前的数据一个减去,减到minn的位置一定是0,所以不用担心minn继续向下减
        x += res[i]?a:-b;
        if(x < 0)
        {
            printf("-1\n");
            return 0;
        }
    }
    printf("%d\n",N+1);

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值