D. Image Preview

题目连接:

http://www.codeforces.com/contest/651/problem/D

Description

Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.

For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.

Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.

Help Vasya find the maximum number of photos he is able to watch during T seconds.

Input

The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.

Second line of the input contains a string of length n containing symbols 'w' and 'h'.

If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.

If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.

Output

Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.

Sample Input

4 2 3 10

wwhw

Sample Output

2

Hint

题意

有n张照片,你看一张照片需要1秒,你滑动照片到左边或者右边,需要a秒,翻转照片需要b秒,问你在T秒内最多看多少张照片

照片必须看,不能跳过。

手机是h的,一直保持着h不变。

题解:

暴力枚举左边看多少张,然后二分右边最多看多少张

暴力枚举右边看多少张,然后二分左边。

具体实现,就当成模拟题去做吧……

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 5e5+7;
  4. int n,a,b,T;
  5. string s;
  6. int d[maxn],d2[maxn];
  7. int check(char k)
  8. {
  9. if(k=='w')return 0;
  10. else return 1;
  11. }
  12. int update(int x,int y)
  13. {
  14. if(x+y>1e9)return 1e9+1;
  15. return x+y;
  16. }
  17. int main()
  18. {
  19. scanf("%d%d%d%d",&n,&a,&b,&T);
  20. T+=a;
  21. cin>>s;
  22. int now = 1;
  23. for(int i=0;i<n;i++)
  24. {
  25. if(i!=0)d[i]=d[i-1];
  26. if(check(s[i])!=now)
  27. d[i]=update(d[i],a+b+1);
  28. else
  29. d[i]=update(d[i],a+1);
  30. }
  31. now = 1;
  32. reverse(s.begin(),s.end());
  33. for(int i=0;i<n;i++)
  34. {
  35. if(i!=0)d2[i]=d2[i-1];
  36. if(check(s[i])!=now)
  37. d2[i]=update(d2[i],a+b+1);
  38. else
  39. d2[i]=update(d2[i],a+1);
  40. }
  41. reverse(s.begin(),s.end());
  42. int ans = 0;
  43. for(int i=0;i<n;i++)
  44. {
  45. if(T<d[i])break;
  46. int las = T - d[i];
  47. ans=max(i+1,ans);
  48. if(i==n-1)continue;
  49. if(las<a*i+d2[0])continue;
  50. las-=a*i;
  51. int l=0,r=n-i-2,Ans=-1;
  52. while(l<=r)
  53. {
  54. int mid=(l+r)/2;
  55. if(d2[mid]<=las)l=mid+1,Ans=mid;
  56. else r=mid-1;
  57. }
  58. ans=max(i+1+Ans+1,ans);
  59. }
  60. if(s[n-1]!=s[0])T-=b;
  61. for(int i=0;i<n-1;i++)
  62. {
  63. if(T<d2[i]+d[0]+a)break;
  64. int las = T - d2[i] - d[0] - a;
  65. ans=max(i+2,ans);
  66. if(las<a*i)continue;
  67. las-=a*i;
  68. las+=d[0];
  69. int l=0,r=n-i-2,Ans=-1;
  70. while(l<=r)
  71. {
  72. int mid=(l+r)/2;
  73. if(d[mid]<=las)l=mid+1,Ans=mid;
  74. else r=mid-1;
  75. }
  76. ans=max(i+1+Ans+1,ans);
  77. }
  78. cout<<ans<<endl;
  79. }

Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分的更多相关文章

  1. Codeforces Round #345 (Div. 1) B. Image Preview

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  2. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

    B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...

  3. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  6. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

  7. Codeforces Round #345 (Div. 2)

    DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h> typedef long ...

  8. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  9. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

随机推荐

  1. Pythone3 sys模块

    1.sys.argv 可以实现从程序外部向程序传递参数2.sys.exit() 程序中间退出,exit(0)正常退出,其他为异常退出3.sys.getdefaultencoding() 获取系统编码方 ...

  2. 判断cookie创建的时间是否已经24小时

    def read_cookie(self): cookiesfilepath="cookies%s" % self.uid if os.path.exists(cookiesfil ...

  3. Android内存溢出解决方案总结

    我的视频会议中有三个内存泄露的崆点: 1) BNLiveControlView mView = this; 未释放 (自定义view中自己引用自己造成) 2) 在自定义View中区注册了系统的网络变化 ...

  4. javascript方法--apply()

    今天琢磨了一下apply,以前对这个方法觉得比较懵,今天一琢磨确实觉得挺好玩的. 一开始把MDN的apply文档看了一遍,感觉不是很理解,而且有一些东西也是知道但是比较模糊,所以还是一步一步来,不懂查 ...

  5. 创建数据库表的SQL语句

    创建表.视图.索引的sql语句如下: CREAT TABLE (列名,数据类型,约束) create view(创建视图) create index (创建索引) 1.primary key(主键) ...

  6. 获取分组后的TOP 1和TOP N记录

    MySQL获取分组后的TOP 1和TOP N记录 有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,在一些别的数据库可能有窗口函数可以方面的查出来,但是MyS ...

  7. mac系统安装mysql

    1.下载 打开官网:https://www.mysql.com 进入DOWNLOADS--->Community--->MySQL Community Server--->DOWNL ...

  8. 关于Free的override不能省略的问题,切记,虚方法是可以被覆盖的方法。

     

  9. 《java并发编程实战》读书笔记6--取消与关闭

    第7章 取消与关闭 这章的主要内容是关于如何使任务和线程安全,快速,可靠的停止下来. 7.1 任务取消 在Java中没有一种安全的抢占方式来停止线程,但是可以使用一些协作机制,比如: 让素数生成器运行 ...

  10. Oracle中的case when then else end 应用

    Case when 的用法,简单Case函数 简单CASE表达式,使用表达式确定返回值. 语法: CASE search_expression WHEN expression1 THEN result ...