CodeForces 650B(二分)
2016-08-11 00:58
405 查看
B. 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.
这题题意就是你有一个手机,你现在要看照片,照片有横着和竖着的,你要把横着的竖过来。看一张照片需要一秒,翻页需要a秒,调整照片需要b秒。问你有t秒,最多能看多少张照片。
因为他是递增的,向左走二分右面的照片和向右左二分左面的照片,求一个最大值即可。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=500100;
int num[maxn];
ll zuo[maxn],you[maxn];
int main(){
char c;
int n,a,b,t;
memset(num,0,sizeof(num));
memset(zuo,0,sizeof(zuo));
memset(you,0,sizeof(you));
scanf("%d%d%d%d%*c",&n,&a,&b,&t);
for(int i=1;i<=n;i++){
scanf("%c",&c);
if(c=='w') num[i]=(1+a+b);
else num[i]=(1+a);
}
num[1]-=a;
for(int i=1;i<=n;i++)
you[i]=you[i-1]+num[i];
for(int i=n;i>=1;i--)
zuo[i]=zuo[i+1]+num[i];
int l=0,r=n,m;
int ans=-1;
if(you
<=t){
cout<<n<<endl;
return 0;
}
if(you[1]>t){
cout<<0<<endl;
return 0;
}
for(int i=1;i<=n;i++){
l=0,r=n;
while(l+1<r){
m=(l+r)/2;
if(zuo[m]+you[i]+(i-1)*a>t)
l=m;
else r=m;
}
if(zuo[r]+you[i]+(i-1)*a<=t)
ans=max(n-r+1+i,ans);
else if(you[i]<=t)
ans=max(i,ans);
}
for(int i=n;i>1;i--){
l=1,r=n;
while(l+1<r){
m=(l+r)/2;
if(you[m]+zuo[i]+(n-i+1)*a>t)
r=m;
else l=m;
}
if(you[l]+zuo[i]+(n-i+1)*a<=t)
ans=max(n-i+1+l,ans);
else if(you[1]+zuo[i]<=t)
ans=max(n-i+2,ans);
}
cout<<ans<<endl;
}
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.
这题题意就是你有一个手机,你现在要看照片,照片有横着和竖着的,你要把横着的竖过来。看一张照片需要一秒,翻页需要a秒,调整照片需要b秒。问你有t秒,最多能看多少张照片。
因为他是递增的,向左走二分右面的照片和向右左二分左面的照片,求一个最大值即可。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=500100;
int num[maxn];
ll zuo[maxn],you[maxn];
int main(){
char c;
int n,a,b,t;
memset(num,0,sizeof(num));
memset(zuo,0,sizeof(zuo));
memset(you,0,sizeof(you));
scanf("%d%d%d%d%*c",&n,&a,&b,&t);
for(int i=1;i<=n;i++){
scanf("%c",&c);
if(c=='w') num[i]=(1+a+b);
else num[i]=(1+a);
}
num[1]-=a;
for(int i=1;i<=n;i++)
you[i]=you[i-1]+num[i];
for(int i=n;i>=1;i--)
zuo[i]=zuo[i+1]+num[i];
int l=0,r=n,m;
int ans=-1;
if(you
<=t){
cout<<n<<endl;
return 0;
}
if(you[1]>t){
cout<<0<<endl;
return 0;
}
for(int i=1;i<=n;i++){
l=0,r=n;
while(l+1<r){
m=(l+r)/2;
if(zuo[m]+you[i]+(i-1)*a>t)
l=m;
else r=m;
}
if(zuo[r]+you[i]+(i-1)*a<=t)
ans=max(n-r+1+i,ans);
else if(you[i]<=t)
ans=max(i,ans);
}
for(int i=n;i>1;i--){
l=1,r=n;
while(l+1<r){
m=(l+r)/2;
if(you[m]+zuo[i]+(n-i+1)*a>t)
r=m;
else l=m;
}
if(you[l]+zuo[i]+(n-i+1)*a<=t)
ans=max(n-i+1+l,ans);
else if(you[1]+zuo[i]<=t)
ans=max(n-i+2,ans);
}
cout<<ans<<endl;
}
相关文章推荐
- codeforces 650D D. Image Preview (暴力+二分+dp)
- Codeforces 650B Image Preview 【二分】
- CodeForces 650 B.Image Preview(二分)
- Codeforces 650B 二分
- 【codeforces 782B】The Meeting Place Cannot Be Changed(二分)
- Codeforces 651D:Image Preview 二分
- Codeforces 651D Image Preview【思维+二分+前缀后缀和】
- codeforces 812C Sagheer and Nubian Market (二分)
- codeforces 650 D. Zip-line
- CodeForces 597A Divisibility(水题)(作死的二分)
- codeforces 626C 二分判定
- CodeForces 672D Robin Hood(二分 + 思维)
- 【CodeForces】702C - Cellular Network 二分
- codeforces 812C——Sagheer and Nubian Market(二分)
- Codeforces 578C Weakness and Poorness(二分 + 最大(小)子段和)
- Codeforces 702C Cellular Network(二分)
- CodeForces 377B Preparing for the Contest 贪心(二分加优先队列)
- codeforces 817C Really Big Numbers 二分
- CodeForces 626C - Block Towers(二分)
- CodeForces540E【树状数组+二分】