您的位置:首页 > 其它

Codeforces——大数组求sum超时、神级算法__

2018-03-28 22:13 921 查看
原题链接:http://codeforces.com/gym/101744/problem/E

E. MaratonIME rides the university bustime limit per test3.0 smemory limit per test256 MBinputstandard inputoutputstandard outputIf we organize it correctly, ...UNKNOWNTo make the trip to the subway less boring and tiring, the SPSU, Sao Paulo State University, tried one of its most famous inventions: buses with Infinite Inner Length! In such a modern engineering wonder, there's always a couple of empty seats for the students to sit and chat during the trip.MaratonIME crew is very popular, so popular that they have friends at every SPSU institute. Like everyone else from this university, they need to take the bus after a long day learning how to fix the Wi-Fi network. Because they don't practice sports like rowing, every SPSU student sits right after entering the bus, making pairs whenever possible. Thinking about that, Gi, an ICPC expert, comes with a problem to think on the way to the subway: given a number n which indicates the number of institutes at SPSU and n integers ai representing the amount of people waiting for the bus at the institute i, Gi wants to know for m pairs lj, rj (lj ≤ rj) if all the people waiting for the bus at any point between lj e rj (inclusive) took an empty bus, they could sit together in pairs (nobody would sit alone).InputThe input consist in one line with two integers n and m, the number of institutes and the number of Gi's questions. In the second line there are n integers ai, the number of people waiting for the bus at the ith institute. Then follows m lines with two integers each, li and ri, the first and last institute of Gi's question.1 ≤ n, m ≤ 105
0 ≤ ai ≤ 105
1 ≤ li ≤ ri ≤ n
OutputOutput "Sim" if it is possible to organize all the pairs in a way nobody sits alone or "Nao" otherwise.ExampleinputCopy
5 2
1 4 10 3 2
3 5
2 3
output
Nao
Sim
NoteIn the first sample we have 5 institutes with 1, 4, 10, 3 and 2 students. Gi asks if it is possible to form only couples if the ones between the 3rd and the 5th institutes takes an empty bus and the ones between the 2nd and the 3rd. For the first we have 15 so we can't and for the second we have 14 so we can.

题目大意:

给出n个研究所和m个问题,问题是一个范围,在指定范围内求输入第二行的范围和,若为单数则输出Nao,否则输出Sim。

解题思路:

1、用普通方法求和,发现暴力不能过
2、通过sum数组求和、美观又文雅
3、通过等效位运算,神级算法!!

代码思路:

对第一种思路,没有代码思路
对第二种思路,用sum数组存储每个地址的当前和即可
对第三种思路,读者可慢慢想,很有意思

核心:本题关键在于这是一道水题!

代码(第一思路就不列举了,太low):#include <bits/stdc++.h>
using namespace std;
int a[100010];
int sum[100010]={0};
int main()
{
std::ios::sync_with_stdio(false);
int n,m,l,r;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}

while(m--)
{
cin>>l>>r;
if((sum[r]-sum[l-1]) %2 == 0) printf("Sim\n");
else printf("Nao\n");
}
return 0;
}高级算法:#include <bits/stdc++.h>
using namespace std;
int a[100010];
int n, m, l, r;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
scanf("%d %d", &n, &m);
a[0] = 0;
for(int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
a[i] &= 1;
a[i] ^= a[i-1];
}

while(m--)
{
scanf("%d %d", &l, &r);
if(a[r] - a[l-1]) puts("Nao");
else puts("Sim");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces
相关文章推荐