Problem A. Diwali lightings Google APAC 2017 University Test Round E
2017-02-04 10:43
501 查看
可以先根据前缀和和后缀和算出在pattern S内每个position前后有多少个blue bulbs(包括 当前position)。
然后分类讨论I,J的情况。
如果I,J属于同一个pattern,直接前缀和相减即可。
如果I在J之前的一个pattern,I的后缀和+J的前缀和即可。
如果I和J之间相隔多个pattern,可以计算相隔的pattern中有多少个blue bulbs,再考虑I,J所在的pattern中有多少个blue bulb。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
//2017 RoundE Problem A. Diwali lightings
const int maxn=110;
int T;
int S;
long long I;
long long J;
char str[maxn];
int preblue[maxn];
int nextblue[maxn];
int bluenum;
long long ans=0;
int main()
{
freopen("A-large-practice.in","r",stdin);//input.txt
freopen("output.txt","w",stdout);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
memset(preblue,0,sizeof(preblue));
memset(nextblue,0,sizeof(nextblue));
bluenum=0;
ans=0;
scanf("%s",&str);
//cout<<str<<endl;
scanf("%lld %lld",&I,&J);
I--;
J--;
S=strlen(str);
for(int i=0;i<S;i++)
{
if(str[i]=='B')
{
bluenum++;
if(i==0)
{
preblue[i]=1;
//nextblue[i]=1;
}
else
{
preblue[i]=preblue[i-1]+1;
}
}
else
{
if(i==0)
{
preblue[i]=0;
//nextblue[i]=1;
}
else
{
preblue[i]=preblue[i-1];
}
}
}
for(int i=S-1;i>=0;i--)
{
if(str[i]=='B')
{
if(i==S-1)
{
nextblue[i]=1;
}
else
{
nextblue[i]=nextblue[i+1]+1;
}
}
else
{
if(i==S-1)
{
nextblue[i]=0;
}
else
{
nextblue[i]=nextblue[i+1];
}
}
}
long long groupi=I/S;
long long groupj=J/S;
int idxi=I%S;
int idxj=J%S;
if(groupi==groupj)
{
if(str[idxi]=='B')
{
ans=preblue[idxj]-preblue[idxi]+1;
}
else
{
ans=preblue[idxj]-preblue[idxi];
}
}
else if(groupi+1==groupj)
{
ans=nextblue[idxi]+preblue[idxj];
}
else
{
ans=(long long)(groupj-groupi-1)*bluenum;
ans+=nextblue[idxi]+preblue[idxj];
}
printf("Case #%d: %lld\n",ca,ans);
}
return 0;
}
然后分类讨论I,J的情况。
如果I,J属于同一个pattern,直接前缀和相减即可。
如果I在J之前的一个pattern,I的后缀和+J的前缀和即可。
如果I和J之间相隔多个pattern,可以计算相隔的pattern中有多少个blue bulbs,再考虑I,J所在的pattern中有多少个blue bulb。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
//2017 RoundE Problem A. Diwali lightings
const int maxn=110;
int T;
int S;
long long I;
long long J;
char str[maxn];
int preblue[maxn];
int nextblue[maxn];
int bluenum;
long long ans=0;
int main()
{
freopen("A-large-practice.in","r",stdin);//input.txt
freopen("output.txt","w",stdout);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
memset(preblue,0,sizeof(preblue));
memset(nextblue,0,sizeof(nextblue));
bluenum=0;
ans=0;
scanf("%s",&str);
//cout<<str<<endl;
scanf("%lld %lld",&I,&J);
I--;
J--;
S=strlen(str);
for(int i=0;i<S;i++)
{
if(str[i]=='B')
{
bluenum++;
if(i==0)
{
preblue[i]=1;
//nextblue[i]=1;
}
else
{
preblue[i]=preblue[i-1]+1;
}
}
else
{
if(i==0)
{
preblue[i]=0;
//nextblue[i]=1;
}
else
{
preblue[i]=preblue[i-1];
}
}
}
for(int i=S-1;i>=0;i--)
{
if(str[i]=='B')
{
if(i==S-1)
{
nextblue[i]=1;
}
else
{
nextblue[i]=nextblue[i+1]+1;
}
}
else
{
if(i==S-1)
{
nextblue[i]=0;
}
else
{
nextblue[i]=nextblue[i+1];
}
}
}
long long groupi=I/S;
long long groupj=J/S;
int idxi=I%S;
int idxj=J%S;
if(groupi==groupj)
{
if(str[idxi]=='B')
{
ans=preblue[idxj]-preblue[idxi]+1;
}
else
{
ans=preblue[idxj]-preblue[idxi];
}
}
else if(groupi+1==groupj)
{
ans=nextblue[idxi]+preblue[idxj];
}
else
{
ans=(long long)(groupj-groupi-1)*bluenum;
ans+=nextblue[idxi]+preblue[idxj];
}
printf("Case #%d: %lld\n",ca,ans);
}
return 0;
}
相关文章推荐
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem C. Not So Random Google APAC 2017 University Test Practice Round
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Problem B. Robot Rock Band Google APAC 2017 University Test Practice Round
- Round E APAC Test 2017 Problem A. Diwali lightings (C++)
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Problem A. Sherlock and Parentheses Google APAC 2017 University Test Round B
- Problem A. Country Leader Google APAC 2017 University Test Round A
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Problem B. Safe Squares Google APAC 2017 University Test Round C
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem C. Codejamon Cipher Google APAC 2017 University Test Round D
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem B. gWheels Google APAC 2016 University Test Round B