A.Eugeny and Array

來源:互聯網
上載者:User
              A. Eugeny and ArrayTime Limit:2000msCase Time Limit:2000msMemory Limit:262144KB64-bit integer IO format: %I64d      Java class name: (Any)SubmitStatusPID:
29452 Font Size:Eugeny has array a =  a 1, 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 31 -11 11 22 2
Output
010
Input
5 5-1 1 1 1 -11 12 33 52 51 5
Output
01010


解題思路:本題為簡單題,只要搞懂題目,就能做出來。

給你一個數列,數組元素為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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.