动态规划

If we organize it correctly, …
Unknown
To 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).

Input
The 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
Output
Output “Sim” if it is possible to organize all the pairs in a way nobody sits alone or “Nao” otherwise.

Example
Input
5 2
1 4 10 3 2
3 5
2 3
Output
Nao
Sim
Note
In 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.
思路:
既然是要两个数之间的和,数据给的那么大,如果单纯的相加的话肯定是不对滴;所以就要用到动态规划了,每个数组里面存放的是前i项的和,假如说要P和q之间的数的和,那么就直接用a[q]-a[p-1],这里有a[p-1],所以就得考虑1 2的情况,这时候要减a[0]的,所以得提前把a[0]设置为0;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
long long int a[100001];
int main()
{
    long long int n,m,sum=0;
    a[0]=0;
    scanf("%lld%lld",&n,&m);
    for(long long int i=1; i<=n; i++)
    {
        long long int t;
        scanf("%lld",&t);
        sum+=t;
        a[i]=sum;
    }
    for(int i=1; i<=m; i++)
    {
        long long int p,q,t;
        scanf("%lld%lld",&p,&q);
        t=a[q]-a[p-1];
        if(t%2==0)printf("Sim\n");
        else printf("Nao\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值