Codeforces Round #381 (Div. 2) B. Alyona and flowers(贪心 模拟)

B. Alyona and flowers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.

Let’s define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl’s happiness its mood multiplied by the number of chosen subarrays the flower is in.

For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:

the first flower adds 1·1 = 1 to the girl’s happiness, because he is in one of chosen subarrays,
the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,
the third flower adds 1·2 = 2, because he is in two of chosen subarrays,
the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,
the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.
Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl’s happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!

Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother.

The second line contains the flowers moods — n integers a1, a2, …, an ( - 100 ≤ ai ≤ 100).

The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], …, a[ri].

Each subarray can encounter more than once.

Output
Print single integer — the maximum possible value added to the Alyona’s happiness.

Examples
input
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
output
7
input
4 3
1 2 3 4
1 3
2 4
1 1
output
16
input
2 2
-1 -2
1 1
1 2
output
0
Note
The first example is the situation described in the statements.

In the second example Alyona should choose all subarrays.

The third example has answer 0 because Alyona can choose none of the subarrays.

题意:给出N个数字ai,M组(left,right),对于每组(left,right)可以选择要或者不要,如果要,则记录下标(left,right)之间的数出现次数加一,求出最终(ai*ai出现的次数)的最大和。

思路:贪心,对于每组(left,right)假设对应数字均只出现一次,则只要他们的和大于等于0,那就选择要。

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
struct node
{
    int num;
    int num_num;
}num[105];
int main()
{
    int N,M;
    scanf("%d%d",&N,&M);
    num[0].num=0;
    num[0].num_num=0;
    for(int i=1;i<=N;i++)
    {
        int flag;
        scanf("%d",&flag);
        num[i].num=num[i-1].num+flag;
        num[i].num_num=0;
    }
    while(M--)
    {
        int left,right;
        scanf("%d%d",&left,&right);
        if(left==right&&num[left].num-num[left-1].num>0)
            num[left].num_num++;
        else if(left<right&&num[right].num-num[left-1].num>=0)
        {
            for(int i=left;i<=right;i++)
                num[i].num_num++;
        }
    }
    long long int sum=0;
    for(int i=1;i<=N;i++)
    {
        sum+=(num[i].num-num[i-1].num)*num[i].num_num;
    }
    printf("%I64d\n",sum);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值