A.Eugeny and Array
2013-08-11 19:48
459 查看
A. Eugeny and Array
Time Limit:2000msCase Time Limit:2000ms
Memory Limit:262144KB
64-bit integer IO format:%I64d Java class name:(Any)
SubmitStatusPID:
29452
Font Size:
Eugeny has arraya = a1, a2, ..., an,
consisting ofn integers. Each integer
ai equals to -1, or to 1. Also, he hasm queries:
Query number i is given as a pair of integersli,ri(1 ≤ li ≤ ri ≤ n).
The response to the query will be integer
1, if the elements of arraya can berearranged(重新排列) so as the sumali + 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 integersn and m
(1 ≤ n, m ≤ 2·105). The second line containsn integersa1, a2, ..., an(ai = -1, 1).
Nextm lines contain Eugene's queries. Thei-th line contains integersli, ri(1 ≤ li ≤ ri ≤ n).
Output
Print m integers — the responses to Eugene's queries in the order they occur in the input.Sample Input
Input2 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 01
0
解题思路:本题为简单题,只要搞懂题目,就能做出来。
给你一个数列,数组元素为1或-1,要你对数列进行重排,看能否满足给定的标号l和r使得数列中l~r之间的数的和为0,若可以,输出1,否则输出0。
只要记录数列中1,-1的个数然后根据给定的l和r判断即可。若l~r区间中的数正好有偶数个数,而给定的数列中1,和-1的个数都不小于l~r区间中的数的个数的1/2,那么,我们就可以重排序列使其满足要求。如若这样,输出1,否则,输出0。
#include<stdio.h>
int main()
{
int n,m;
int x,y,a,b,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
a=0;
b=0;
for(i=0; i<n; i++)
{
scanf("%d",&x);
if(x==1)
a++;//记录给定数列中1的个数
else
b++; //记录给定数列中-1的个数
}
for(i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
if((y-x+1)%2==0&&(y-x+1)/2<=a&&(y-x+1)/2<=b)//l~r区间中的数正好有偶数个数
//给定的数列中1,和-1的个数都不小于l~r区间中的数的个数的1/2
printf("1\n");
else
printf("0\n");
}
}
return 0;
}
相关文章推荐
- codeforces-302A-Eugeny and Array
- Eugeny and Array
- A. Eugeny and Array
- BNU Eugeny and Array
- CF:Eugeny and Array
- A. Eugeny and Array
- A.Eugeny and Array
- Eugeny and Array
- [Codeforces Round #373 DIV1C (CF718C)] Sasha and Array
- Pythonic Data Structures and Algorithms(Array 2)
- LeetCode Find All Duplicates in Array and Find Disappeared Numbers in Array
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- B. Amr and The Large Array
- Codeforces Round #331 (Div. 2) B. Wilbur and Array
- Codeforces Round #187 (Div. 2) B. Sereja and Array
- Codeforces Round #136 (Div. 2) Little Elephant and Array
- Codeforces 361C - Levko and Array Recovery(贪心)
- Reference to array and hash
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array