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
2、通过sum数组求和、美观又文雅
3、通过等效位运算,神级算法!!
对第二种思路,用sum数组存储每个地址的当前和即可
对第三种思路,读者可慢慢想,很有意思
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;
}
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 3output
Nao SimNoteIn 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 703D Mishka and Interesting sum (树状数组求区间内不同的数的异或和)
- CodeForces 703D Mishka and Interesting sum (树状数组处理异或和)
- Codeforces 703D Mishka and Interesting sum 树状数组
- codeforces CF703D Mishka and Interesting sum 树状数组
- CodeForces 703D Mishka and Interesting sum(树状数组 区间异或)
- 【codeforces 703 D】【离线询问 树状数组 前驱思想 前缀异或和】D. Mishka and Interesting sum【 区间内出现次数偶数的数的异或和】
- CodeForces 703D Mishka and Interesting sum (树状数组 区间偶数异或)
- codeforces 703D Mishka and Interesting sum (树状数组区间异或)
- Codeforces-Round 174(Cows and Sequence)(树状数组、高校算法)
- codeforces 703D D. Mishka and Interesting sum(树状数组)
- codeforces 703D Mishka and Interesting sum(数状数组维护前缀)
- 算法题:求一个数组中和为sum的所有组合
- 【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus
- LeetCode—*Combination Sum II(DFS算法C数组中有重复值)
- 算法课第十周作业 | Minimum Path Sum
- codeforces - Tricky Sum(模拟)
- JS实现的数组去除重复数据算法小结
- 算法题:求指定数组中和为N的出现的所有组合(二)
- 【每天一道算法题】给定一个存放整数的数组,要求数组左边为奇数,右边为偶数
- 现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数。