C. Cut the Deck
链接:link.
思路:找出max(R的数量-B的数量),从此处开始便利,n/2-d1[ i ] (B的数量)>=max,i即为所求。
int t;
char s[1000010];
int d1[1000010],d2[1000010];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>s;
int n=strlen(s);
int k1=0,k2=0;
int flag=0;
int maxn=-INF,maxf=-1;
for(int i=0;i<n;i++)
{
if(s[i]=='B')k1++;
if(s[i]=='R')k2++;
d1[i]=k1;
d2[i]=k2;
if(k1<k2)flag=1;
if(k2-k1>maxn)maxf=i,maxn=k2-k1;
}
if(flag==0){cout<<"0\n";continue;}
for(int i=maxf;i<n-1;i++)
{
if(n/2-d1[i]>=maxn)
{
cout<<i+1<<"\n";
flag=0;
break;
}
}
if(flag==1)cout<<"-1\n";
}
return 0;
}
D. Dislike the Raisins
链接:link.
注意:只要(c+r)%s!=0&&c%s>=(c+r)%s,结果一就为(c/s)++;
int t;
int c,r,s;
int main()
{
ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>c>>r>>s;
int x=(c+r)/s;
int y=(c+r)%s;
int s1=c/s,s2;
if(y!=0&&c%s>=y)s1++;
if(y!=0)x++;
if(x<=r)s2=0;
else s2=x-r;
cout<<s1<<" "<<s2<<"\n";
}
return 0;
}
G. Grid of Letters
链接:link.
思路:找到入度为0的点开始dfs,注意特判终止dfs。
int n,m;
char s[1010][1010];
typedef pair<int,int>PII;
vector<PII>v;
int dx[]={1,1,1,-1,-1,-1,0,0};
int dy[]={1,0,-1,1,0,-1,1,-1};
void dfs(int x,int y,int ss,int &maxn)
{
for(int i=0;i<8;i++)
{
int cx=x+dx[i];
int cy=y+dy[i];
if(cx>=0&&cx<n&&cy>=0&&cy<m&&s[cx][cy]==s[x][y]+1)
{
maxn=max(ss+1,maxn);
if(maxn==26)return;
if('Z'-s[cx][cy]+1+ss+1<=maxn)return ;
dfs(cx,cy,ss+1,maxn);
}
}
}
bool cmp(PII a,PII b)
{
return s[a.first][a.second]<s[b.first][b.second];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++)cin>>s[i];
//for(int i=0;i<n;i++)cout<<s[i]<<"\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int ss=0;
//cout<<s[i][j]<<" ";
for(int k=0;k<8;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x>=0&&x<n&&y>=0&&y<m)
{
//cout<<s[x][y];
if(s[x][y]+1==s[i][j])ss++;
}
}
//cout<<"\n"<<ss<<"\n";
if(ss==0)v.push_back({i,j});
// cout<<i<<" "<<j<<"\n";
}
}
//sort(v.begin(),v.end(),cmp);
int maxn=1;
for(int i=0;i<v.size();i++)
{
int x=v[i].first,y=v[i].second;
int ss=1;
if('Z'-s[x][y]+1>maxn)dfs(x,y,ss,maxn);
}
cout<<maxn<<"\n";
return 0;
}
I. Integer Multiplicative Persistence
链接:link.
int t,n;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
int k=0;
while(n>9)
{
k++;
int x=1;
while(n>0)
{
x=x*(n%10);
n=n/10;
}
n=x;
}
cout<<k<<"\n";
}
return 0;
}