D - Ratings and Reality Shows /* Codeforces Round #444 (div.2) */

D. Ratings and Reality Shows
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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·105, 0 ≤ start ≤ 109, 1 ≤ 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 lenis 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 ≤ 109, 0 ≤ 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.

Examples
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


题意:

两种活动:fashion show or photo shoots
属性rate:= start
fashion show -b
photo shoots +a
参加了talk show :
change —— —— fashion show -d
photo shoots +c

len: 某一年参加talk show 后收到talk show 影响的时间数

求最早参加的talk show 时间,使得talk show 之前和受影响期间 rate都>=0

解题思路:

/*对每一年进行影响模拟判断。

代码:

不知道哪里错了的代码 WA on test 4 :
 1 #include <bits/stdc++.h>
 2 #define R(x,y,z) for(int x=y;x<z;x++)
 3 using namespace std;
 4 
 5 const int INF=0x3f3f3f3f;
 6 const int SIZE=1e5+10;
 7 typedef long long LL;
 8 int t[3*SIZE],thing[3*SIZE];
 9 
10 int main()
11 {
12     LL a,b,c,d,start,len,n;
13 
14     scanf("%lld%lld%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d,&start,&len);
15     for(int i=1;i<n+1;i++)
16         scanf("%d%d",&t[i],&thing[i]);
17 
18     int now=1; ///参加的期间(影响期间)
19     LL ori=start,ans=0,MIN=0; ///ori指不参加脱口秀原来的rate ans指参加了之后期间的rate min用于接受脱口秀期间最低成绩
20     for(int i=1;i<n+1;i++)
21     {
22        // printf("ori:%lld ans:%lld min:%lld \n",ori,ans,MIN);
23         ///在脱口秀期间表现 (这里的now是随着每一次尝试脱口秀的最后一天增加的,
24         ///                    就是你假设了一次脱口秀之后,以后就只要循环一次就够了)
25         while(now<=n&&t[now]-t[i]<=len)
26         {
27             ans+=thing[now]?c:-d;
28             MIN=min(ans,MIN);
29             now++;
30         }
31 
32         ///talk show perfect
33         if(MIN+ori>=0) ///有一回总rate没有小于0
34         {
35             printf("%d\n",t[i-1]+1);return 0;
36         }
37 
38         ///ans和MIN减去参加最后一天的脱口秀的影响,ori加上当天普通节目的影响
39         ans-=(thing[i]?c:-d);
40         MIN-=(thing[i]?c:-d);
41         ori+=(thing[i]?a:-b);
42 
43         ///自己就不能满足自己
44         if(ori<0)
45         {
46             printf("-1\n");return 0;
47         }
48     }
49     printf("%d\n",t[n]+1);
50     return 0;
51 }
WA ?????

改过之后AC的代码:

 1 #include <bits/stdc++.h>
 2 #define R(x,y,z) for(int x=y;x<z;x++)
 3 using namespace std;
 4 
 5 const int INF=0x3f3f3f3f;
 6 const int SIZE=1e5+10;
 7 typedef long long LL;
 8 int t[SIZE*3],thing[SIZE*3];
 9 int main()
10 {
11     int a,b,c,d,start,len,n;
12 
13     scanf("%d%d%d%d%d%d%d",&n,&a,&b,&c,&d,&start,&len);
14     for(int i=0;i<n;i++)
15         scanf("%d%d",&t[i],&thing[i]);
16     vector<int>T;
17     T.push_back(-1);
18     for(int i=0;i<n;i++) T.push_back(t[i]);
19     int now=0; ///参加的期间(影响期间)
20     LL ori=start,ans=0,MIN=0; ///ori指不参加脱口秀原来的rate ans指参加了之后期间的rate min用于接受脱口秀期间最低成绩
21     for(int i=0;i<n;i++)
22     {
23        // printf("ori:%lld ans:%lld min:%lld \n",ori,ans,MIN);
24         ///在脱口秀期间表现 (这里的now是随着每一次尝试脱口秀的最后一天增加的,
25         ///                    就是你假设了一次脱口秀之后,以后就只要循环一次就够了)
26         while(now<n&&t[now]-t[i]<len)
27         {
28             ans+=thing[now]?c:-d;
29             MIN=min(ans,MIN);
30             now++;
31         }
32 
33         ///talk show perfect
34         if(MIN+ori>=0) ///有一回总rate没有小于0
35         {
36             printf("%d\n",T[i]+1);return 0;
37         }
38 
39         ///ans和MIN减去参加最后一天的脱口秀的影响,ori加上当天普通节目的影响
40         ans-=(thing[i]?c:-d);
41         MIN-=(thing[i]?c:-d);
42         ori+=(thing[i]?a:-b);
43 
44         ///自己就不能满足自己
45         if(ori<0)
46         {
47             printf("-1\n");return 0;
48         }
49     }
50     printf("%d\n",T[n]+1);
51     return 0;
52 }
AC me!

贴出思路一样的AC代码:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int n,a,b,c,d,start,len;
 7     scanf("%d%d%d%d%d%d%d",&n,&a,&b,&c,&d,&start,&len);
 8     vector <int> t (n), q(n);
 9     for (int i = 0; i < n; i++) scanf("%d%d",&t[i],&q[i]);
10     int j = 0;
11     long long df = 0, mn = 0, ac = start;
12     vector <int> T;
13     T.push_back(-1);
14     for (int i = 0; i < n; i++) T.push_back(t[i]);
15     for (int i = 0; i < n; i++) {
16         while (j < n && t[j]-t[i] < len) {
17             df += (q[j] ? c : -d);
18             mn = min(mn,df);
19             j++;
20         }
21         if (ac+mn >= 0) {
22             cout << T[i]+1 << endl;
23             return 0;
24         }
25         df -= (q[i] ? c : -d);
26         mn -= (q[i] ? c : -d);
27         ac += (q[i] ? a : -b);
28 
29         if (ac < 0) {
30             puts("-1");
31             return 0;
32         }
33     }
34     cout << T[n]+1 << endl;
35 }
哼!

 

posted @ 2017-11-08 14:39  小可爱的小可爱  阅读(387)  评论(0编辑  收藏  举报