现在的位置: 首页 > 综合 > 正文

A. Eugeny and Array

2013年02月23日 ⁄ 综合 ⁄ 共 1434字 ⁄ 字号 评论关闭
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Eugeny has array a = a1, a2, ..., an,
consisting of n integers. Each integer ai equals
to -1, or to 1. Also, he has m queries:

  • Query number i is given as a pair of integers liri (1 ≤ li ≤ ri ≤ n).
  • The response to the query will be integer 1, if the elements of array a can
    be rearranged so as the sum ali + ali + 1 + ... + ari = 0,
    otherwise the response to the query will be integer 0.

Help Eugeny, answer all his queries.

Input

The first line contains integers n and m (1 ≤ n, m ≤ 2·105).
The second line contains n integers a1, a2, ..., an (ai = -1, 1).
Next m lines contain Eugene's queries. The i-th
line contains integers li, ri (1 ≤ li ≤ ri ≤ n).

Output

Print m integers — the responses to Eugene's queries in the order they occur in the input.

Sample test(s)
input
2 3
1 -1
1 1
1 2
2 2
output
0
1
0
input
5 5
-1 1 1 1 -1
1 1
2 3
3 5
2 5
1 5
output
0
1
0
1
0

解题说明:题目中给定一个只由-1,1组成的数列,在给定起始位置和结束位置后,问在可以对数列中数字进行任意排列的情况下能否让从给定的起始位置到结束位置之间的数字之和为0. 做法是先统计出数列中1和-1的个数,然后判断起始位置和结束位置中间包含多少个数字,如果为奇数那和肯定不为0,如果为偶数就判断数列中1和-1的个数能否达到该数的一半,只有一半1和一半-1时才能确保加起来为0.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;


int main()
{
	int i,n,m;
	int l,r;
	int a,pos,neg;
	pos=neg=0;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a);
		if(a>0)
		{
			pos++;
		}
		else
		{
			neg++;
		}
	 }
	 if(pos>neg)
	 {	
		 pos=neg;
	 }
	 for(i=0;i<m;i++)
	 {
		scanf("%d %d",&l,&r);
		l=r-l+1; 
		if (l%2==1)
		{
			printf("0\n");
		}
		else if (pos>=l/2)
		{
			printf("1\n");
		}
		else
		{
			printf("0\n");
		}
	 }
	 return 0;
}

抱歉!评论已关闭.