解题说明:题目中给定一个只由-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; }