Codeforces Round #345 (Div. 2) D. Image Preview __ two pointers and pretreat
2016-04-14 11:31
330 查看
D. Image Preview
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
input
output
input
output
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Source
Codeforces Round #345 (Div. 2) D. Image Preview
My Solution
This
is really a good problem for me ☺☺
![](http://img.my.csdn.net/uploads/201604/14/1460604092_4362.jpg)
pretreat whhw...... to array val[maxn] when read the test data
then use two pointers to find the
maximum number of photos Vasya is able to watch during those T seconds.
if a is
really small, we can go right and come back , go left, then we can go right again. But for the final result it is only come back once,
so we just
make it that it is only come back once in the progress.
1.go right first
//if this progress end,and sum == T, it's a hard problem to solve,★★ please read my code for more details.
2.then r--, l--;the left pointer begin to move towards the left
3.if r == 0 when break the while above , we are supposed to run r = 0, l = 0,sum = 0; l-- to go left.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 5*1e5 +8;
int val[maxn];
int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
#endif // LOCAL
memset(val, 0, sizeof val); //initial the array will make the program running faster
int n, a, b, T;
char ph;
scanf("%d%d%d%d", &n, &a, &b, &T);
getchar();
for(int i = 0; i < n; i++){
scanf("%c", &ph);
if(i == 0) {if(ph == 'w') val[0] = b+1; else val[0] = 1;}
else{
if(ph == 'w') val[i] = b+a+1;
else val[i] = a+1;
}
//cout<<val[i]<<" ";
}
//cout<<endl;
//two pointers,
//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).
//but it will only get back once, unless it will wast time which is not necessary.
int l = 0, r = 0, sum = 0;
int ans = -1;
//go rights first
while(sum < T && r < n){
sum += val[r++]; //!from WA13 to Accepted
if(sum > T ) r--; //!if sum == T we can't r-- (but at line44 sum -= val[r];we should add if(sum != T) before),
//! unless line41 ans = max(ans,(r-0)); and line45 r--; will be wrong.
} //!the last time has r++, and this val[r] is not added to sum
ans = max(ans,(r-0)); // r-0+1 -1
//if(sum == T) ans++; this is not enough and r should r++ //!!!!!! this two place
if(ans > n) ans = n;
if(sum != T)sum -= val[r]; //!so if(sum == T) we shouldn't run the sentence sum -= val[r];
r--; //!but here we should do as the normal time
/* above can also be wrote as here
sum += val[0];
while(sum < T && r < n){
r++;sum += val[r];
}
if(sum == T) r++;
//Oh almost the same
ans = max(ans,(r-0));
if(ans > n) ans = n;
if(sum != T)sum -= val[r];
r--;
*/
//cout<<ans<<endl;
//then left , the one short is is the direction the pointer go back
bool first = true; // first time from l to r -> r to l
while(ans != n && ans != 0){
while(sum <= T && -l + r <= n-1 && l > -n){ //!!!!!!l > r-n-1 and at the same time l > -n otherwise maybe TLE
l--; sum += val[(l + n)%n]; //!!!!!!i-- ,first from WA5 to WA7
if(-l < r && first) sum += a;
else {if(first){sum -= a*(-l - 1);sum += a*r; first = false;} } // this will happen only once
//!!!!!! a*(-l - 1) not a*(-l) because the last time sum didn't add a 'a' from WA5 to WA7
}
if(sum < T) break;
ans = max(ans,(r-l)); //! r + -l - 1 + 1
if(first)sum -= val[r];
else sum -= (val[r] + a);
if(r>0) r--;
if(r == 0) break; //!!!!!! r not be r <= 0 from WA7 to WA8
}
//!!!!!! for r == 0 from WA8 to WA13
if(r == 0){
l = 0, sum = 0;
sum += val[0];
while(sum < T && l > -n){
l--; sum += val[(l + n)%n];
}
if(sum == T) l--;
ans = max(ans,(0-l));
if(ans > n) ans = n;
}
printf("%d", ans);
return 0;
}
Thank you all!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
4 2 3 10 wwhw
output
2
input
5 2 4 13 hhwhh
output
4
input
5 2 4 1000 hhwhh
output
5
input
3 1 100 10 whw
output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Source
Codeforces Round #345 (Div. 2) D. Image Preview
My Solution
This
is really a good problem for me ☺☺
![](http://img.my.csdn.net/uploads/201604/14/1460604092_4362.jpg)
pretreat whhw...... to array val[maxn] when read the test data
then use two pointers to find the
maximum number of photos Vasya is able to watch during those T seconds.
if a is
really small, we can go right and come back , go left, then we can go right again. But for the final result it is only come back once,
so we just
make it that it is only come back once in the progress.
1.go right first
//if this progress end,and sum == T, it's a hard problem to solve,★★ please read my code for more details.
2.then r--, l--;the left pointer begin to move towards the left
3.if r == 0 when break the while above , we are supposed to run r = 0, l = 0,sum = 0; l-- to go left.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 5*1e5 +8;
int val[maxn];
int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
#endif // LOCAL
memset(val, 0, sizeof val); //initial the array will make the program running faster
int n, a, b, T;
char ph;
scanf("%d%d%d%d", &n, &a, &b, &T);
getchar();
for(int i = 0; i < n; i++){
scanf("%c", &ph);
if(i == 0) {if(ph == 'w') val[0] = b+1; else val[0] = 1;}
else{
if(ph == 'w') val[i] = b+a+1;
else val[i] = a+1;
}
//cout<<val[i]<<" ";
}
//cout<<endl;
//two pointers,
//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).
//but it will only get back once, unless it will wast time which is not necessary.
int l = 0, r = 0, sum = 0;
int ans = -1;
//go rights first
while(sum < T && r < n){
sum += val[r++]; //!from WA13 to Accepted
if(sum > T ) r--; //!if sum == T we can't r-- (but at line44 sum -= val[r];we should add if(sum != T) before),
//! unless line41 ans = max(ans,(r-0)); and line45 r--; will be wrong.
} //!the last time has r++, and this val[r] is not added to sum
ans = max(ans,(r-0)); // r-0+1 -1
//if(sum == T) ans++; this is not enough and r should r++ //!!!!!! this two place
if(ans > n) ans = n;
if(sum != T)sum -= val[r]; //!so if(sum == T) we shouldn't run the sentence sum -= val[r];
r--; //!but here we should do as the normal time
/* above can also be wrote as here
sum += val[0];
while(sum < T && r < n){
r++;sum += val[r];
}
if(sum == T) r++;
//Oh almost the same
ans = max(ans,(r-0));
if(ans > n) ans = n;
if(sum != T)sum -= val[r];
r--;
*/
//cout<<ans<<endl;
//then left , the one short is is the direction the pointer go back
bool first = true; // first time from l to r -> r to l
while(ans != n && ans != 0){
while(sum <= T && -l + r <= n-1 && l > -n){ //!!!!!!l > r-n-1 and at the same time l > -n otherwise maybe TLE
l--; sum += val[(l + n)%n]; //!!!!!!i-- ,first from WA5 to WA7
if(-l < r && first) sum += a;
else {if(first){sum -= a*(-l - 1);sum += a*r; first = false;} } // this will happen only once
//!!!!!! a*(-l - 1) not a*(-l) because the last time sum didn't add a 'a' from WA5 to WA7
}
if(sum < T) break;
ans = max(ans,(r-l)); //! r + -l - 1 + 1
if(first)sum -= val[r];
else sum -= (val[r] + a);
if(r>0) r--;
if(r == 0) break; //!!!!!! r not be r <= 0 from WA7 to WA8
}
//!!!!!! for r == 0 from WA8 to WA13
if(r == 0){
l = 0, sum = 0;
sum += val[0];
while(sum < T && l > -n){
l--; sum += val[(l + n)%n];
}
if(sum == T) l--;
ans = max(ans,(0-l));
if(ans > n) ans = n;
}
printf("%d", ans);
return 0;
}
Thank you all!
相关文章推荐
- 基于GB28181RTPoverTCP的发送程序拾遗
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- Java基础学习(二)—数组
- 设置Android app的字体不随系统全局字体大小的变动而变动
- Eclipse创建Tomcat时Next按钮是灰色,解决方案!
- ReactiveCocoa入门教程——第一部分<转>
- 过滤器,监听器,拦截器的区别
- 记ASP.NET 使用 X509Certificate2 出现的一系列问题
- 【Android Demo】通过WebService获取今日天气情况
- Web前端开发-资料分享-JS篇
- hibernate加载对象(get和load的区别)
- 原始套接字SOCK_RAW
- 阿里云之企业及分布式应用EDAS使用操作
- MongoDB使用索引
- java 开发环境配置
- 【java入门点滴】String... args VS String[] args
- SQL Server 调优:set statistics profile on
- python 解释器内建函数001
- learning-blog
- 浅析Android手机卫士保存手机安全号码