i 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 Input2 31 -11 11 22 2
Output010
Input5 5-1 1 1 1 -11 12 33 52 51 5
Output01010
解題思路:本題為簡單題,只要搞懂題目,就能做出來。
給你一個數列,數組元素為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;}