CF:Eugeny and Array
2013-05-29 21:27
281 查看
A. Eugeny and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Eugeny has array a = a1, a2, ..., an,
consisting of n integers. Each integer ai equals
to -1, or to 1. Also, he has m queries:
Query number i is given as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
The response to the query will be integer 1, if the elements of array a can
be rearranged so as the sum ali + 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 contains n integers a1, a2, ..., an (ai = -1, 1).
Next mlines contain Eugene's queries. The i-th line
contains integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
Print m integers — the responses to Eugene's queries in the order they occur in the input.
Sample test(s)
input
output
input
output
解题报告:
大概的意思是先给出一个串a(1or-1),然后给出m行l,r两个数,然后重新对a进行排列,如果al+..........+ar=0;输出1否则输出0;
思路,如果r-l+1是奇数,那么数列和肯定不能为0,如果是偶数的话判断一下-1或者1的数量是否够用。
参考代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(){
// freopen("in.txt","r",stdin);
int n,m,i,j,l,r,t,n1,m1,count,m_min;
while(cin>>n>>m)
{
l=0;
r=0;
n1=0;
m1=0;
for(i=0;i<n;i++)
{
cin>>t;
if(t==-1)
n1++;
else
m1++;
}
for(i=0;i<m;i++)
{
cin>>l>>r;
count=r-l+1;
if(count%2!=0)
{
cout<<"0"<<endl;
continue;
}
m_min=min(n1,m1);
if(count/2<=m_min)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Eugeny has array a = a1, a2, ..., an,
consisting of n integers. Each integer ai equals
to -1, or to 1. Also, he has m queries:
Query number i is given as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
The response to the query will be integer 1, if the elements of array a can
be rearranged so as the sum ali + 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 contains n integers a1, a2, ..., an (ai = -1, 1).
Next mlines contain Eugene's queries. The i-th line
contains integers li, ri (1 ≤ li ≤ ri ≤ n).
Output
Print m integers — the responses to Eugene's queries in the order they occur in the input.
Sample test(s)
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 01
0
解题报告:
大概的意思是先给出一个串a(1or-1),然后给出m行l,r两个数,然后重新对a进行排列,如果al+..........+ar=0;输出1否则输出0;
思路,如果r-l+1是奇数,那么数列和肯定不能为0,如果是偶数的话判断一下-1或者1的数量是否够用。
参考代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(){
// freopen("in.txt","r",stdin);
int n,m,i,j,l,r,t,n1,m1,count,m_min;
while(cin>>n>>m)
{
l=0;
r=0;
n1=0;
m1=0;
for(i=0;i<n;i++)
{
cin>>t;
if(t==-1)
n1++;
else
m1++;
}
for(i=0;i<m;i++)
{
cin>>l>>r;
count=r-l+1;
if(count%2!=0)
{
cout<<"0"<<endl;
continue;
}
m_min=min(n1,m1);
if(count/2<=m_min)
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
}
return 0;
}
相关文章推荐
- BNU Eugeny and Array
- [CF Gym 100372C] Sergey and array
- CF-Codeforces Round #210 (Div. 1)-A-Levko and Array Recovery
- cf 721 D Maxim and Array
- CF 558B(Amr and The Large Array-计数)
- Eugeny and Array
- codeforces-302A-Eugeny and Array
- cf Sereja and Array
- A.Eugeny and Array
- cf 442C. Artem and Array
- CF--B. Wilbur and Array
- A.Eugeny and Array
- CF 295A - Greg and Array 差分數列
- CF 439C(251C题)Devu and Partitioning of the Array
- A. Eugeny and Array
- CF_315B_Sereja and Array
- 【CF】220B Little Elephant and Array
- CF 360B - Levko and Array (二分+dp)
- CF 558B(Amr and The Large Array-计数)
- CF 315B - Sereja and Array(树状数组)