题目链接:点击打开链接
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int a[100010];
int cnt[100010];
bool vis[100010];
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=n,j=1;i>=1;i--,j++)
{
if(!vis[a[i]])
{
cnt[j]=cnt[j-1]+1;
vis[a[i]]=1;
}
else
cnt[j]=cnt[j-1];
}
while(m--)
{
int id;
scanf("%d",&id);
printf("%d\n",cnt[n-id+1]);
}
}
return 0;
}
题目链接:点击打开链接
大意:给定一个串,问你 区间 [ l, r ] 的串是否存在一个排列使得任意三个连续的字符均属于 ’zyx’、’xzy’、’yxz’ 三个串的一个。
思路:构成一个最长满足要求的串 zyxzyxz ,发现只有最多的字符 - 最小的字符 <= 1即可。或者是给定区间内的串中 x、y、z 数量三者中任意两个都要求相等或最多相差1,则输出 “YES” ,否则输出“NO”;注意:区间长度小于3的直接输出 “YES” 。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=1e5+10;
int n,a,b;
int x[MAXN],y[MAXN],z[MAXN];
char str[MAXN];
int main()
{
while(~scanf("%s",str+1))
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(z,0,sizeof(z));
for(int i=1;str[i];i++)
{
if(str[i]=='x')
x[i]=x[i-1]+1;
else
x[i]=x[i-1];
if(str[i]=='y')
y[i]=y[i-1]+1;
else
y[i]=y[i-1];
if(str[i]=='z')
z[i]=z[i-1]+1;
else
z[i]=z[i-1];
}
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
int numx=x[b]-x[a-1];
int numy=y[b]-y[a-1];
int numz=z[b]-z[a-1];
if(b-a+1<3)
puts("YES");
else if(abs(numx-numy)>1||abs(numx-numz)>1||abs(numy-numz)>1)
puts("NO");
else
puts("YES");
}
memset(str,0,sizeof(str));
}
return 0;
}