A、Linear Keyboard
题意:给定一串字母,求出每个字母在26个英文字母中的位置,求后一个减前一个的绝对值,求他们的和。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int main()
{
ios::sync_with_stdio(false);
string s,ss;
int t;
cin>>t;
while(t--)
{
cin>>s>>ss;
int x=s.find(ss[0]);
int y;
int sum=0;
for(int a=1;a<ss.size();a++)
{
y=s.find(ss[a]);
sum+=(abs(y-x));
x=y;
}
cout<<sum<<endl;
}
return 0;
}
B、 Odd Grasshopper
题意:给定一个初始位置i和次数m(从一开始),当i是偶数的时候向左走mi步(mi表示第几次),反之向右走mi步,求出最后所在的位置。
不难发现四次一循环,分为当初始位置为奇数、偶数两种情况,再分别讨论%4的4种情况。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int main()
{
ios::sync_with_stdio(false);
ll x,y;
int t;
cin>>t;
while(t--)
{
ll xx;
cin>>x>>y;
if(x%2==0)
{
if(y%4==0)xx=x;
else if(y%4==1)xx=x-y;
else if(y%4==2)xx=x+1;
else xx=x+y+1;
}
else
{
if(y%4==0)xx=x;
else if(y%4==1)xx=x+y;
else if(y%4==2)xx=x-1;
else xx=x-y-1;
}
cout<<xx<<endl;
}
return 0;
}
C、Minimum Extraction
题意:一组数列,删除其中的最小值,让剩下的数分别减去这个最小值,得出新的一组数列,问在执行这个过程时,得到的数列中最小值的最大值是多少。
排完序之后最左边的就是最小值,反正要得到n个不同的序列,我们则可以构造一个当前序列最小值的序列,求其中的最大值就是答案。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll x[500010];
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int a=0;a<n;a++)
cin>>x[a];
sort(x,x+n);
ll maxx=x[0];
x[1]-=x[0];
ll sum=x[0]+x[1];
for(int a=2;a<n;a++)
{
x[a]-=sum;
sum+=x[a];
}
for(int a=1;a<n;a++)
maxx=max(maxx,x[a]);
cout<<maxx<<endl;
}
return 0;
}
D、Blue-Red Permutation
题意:一组数列,其中的数不是蓝色就是红色,红色的数可以加上任意值,蓝色的数可以减去任意值,问最后能不能将这n个数变成从1到n的任意顺序的一组数。
首先,需要分别将红色和蓝色的数挑出来,蓝色只能减,所以将蓝色从小到大排序,如果当前蓝色的数小于它的序号,那么就得不到这样的一组数。同样,对于红色的数,从大到小排序,如果当前红色的数大于n减去当前序号,则也得不到。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll p[500010];
ll x[500010];
ll y[500010];
int f[500010];
int ff[500010];
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n;
string s;
cin>>n;
for(int a=0;a<n;a++)
cin>>p[a];
cin>>s;
int ans=0,ant=0;
int flag=0;
for(int a=0;a<n;a++)
{
if(s[a]=='B')
{
x[ans++]=p[a];
}
if(s[a]=='R')
{
y[ant++]=p[a];
}
}
sort(x,x+ans);
sort(y,y+ant);
for(int a=0;a<ans;a++)
{
if(x[a]<a+1)flag=1;
}
for(int a=0;a<ant;a++)
{
if(y[ant-a-1]>n-a)flag=1;
}
if(flag==1)cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
return 0;
}