A. Eugeny and Array
2000ms
2000ms
262144KB
64-bit integer IO format:%I64d Java class name:(Any)
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 integers n 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
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,要你对数列进行重排,看能否满足给定的标号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; }