codeforces-368

题目链接:点击打开链接

B. Sereja and Suffixes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja has an array a, consisting of n integers a1a2...an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions lili + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?

Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105). The second line contains n integers a1a2...an (1 ≤ ai ≤ 105) — the array elements.

Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 ≤ li ≤ n).

Output

Print m lines — on the i-th line print the answer to the number li.

Examples
input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
output
6
6
6
6
6
5
4
3
2
1
大意:给出 a 和 cnt 的数组,cnt 数组中的数字是统计这个位置之后有几个不同的数字

#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;
}


题目链接:点击打开链接

C. Sereja and Algorithm
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:

  1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
  2. Rearrange the letters of the found subsequence randomly and go to step 1.

Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.

Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of mtests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.

Input

The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.

The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers liri (1 ≤ li ≤ ri ≤ n).

Output

For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.

Examples
input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
output
YES
YES
NO
YES
NO
Note

In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.


大意:给定一个串,问你 区间 [ 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值