Codeforces Round #381 (Div. 2) B. Alyona and flowers
来源:互联网 发布:秋瓷炫长相知乎 编辑:程序博客网 时间:2024/06/07 19:21
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 isai. 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.
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.
Print single integer — the maximum possible value added to the Alyona's happiness.
5 41 -2 1 3 -41 24 53 41 4
7
4 31 2 3 41 32 41 1
16
2 2-1 -21 11 2
0
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.
思路:一个简单的前缀和水题。直接给代码好了:
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<queue>#include<functional>typedef long long LL;using namespace std;#define maxn 105#define ll l,mid,now<<1#define rr mid+1,r,now<<1|1#define lson l1,mid,l2,r2,now<<1#define rson mid+1,r1,l2,r2,now<<1|1#define pi 3.14159#define inf 0x3f3f3f3fconst int mod = 1e9 + 7;int main(){ int n,m,a[maxn]; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a[i]+=a[i-1]; } int ans=0; while(m--){ int l,r; scanf("%d%d",&l,&r); ans+=max(0,a[r]-a[l-1]); } printf("%d\n",ans);}
- Codeforces Round #381 (Div. 2) B. Alyona and flowers
- Codeforces Round #209 (Div. 2) B. Alyona and flowers
- Codeforces Round #381 (Div. 2) A. Alyona and flowers
- Codeforces Round #381 (Div. 2) B. Alyona and flowers(思维题目)
- Codeforces Round #381 (Div. 2) B. Alyona and flowers(贪心 模拟)
- Codeforces#381(Div. 2) B.Alyona and flowers【思维+暴力】
- Codeforces Round #358 (Div. 2) B. Alyona and Mex
- Codeforces Round #358 (Div. 2) B. Alyona and Mex
- Codeforces Round #358 (Div. 2) B. Alyona and Mex
- Codeforces Round #358 (Div. 2) B. Alyona and Mex【水题】
- Codeforces Round #358 (Div. 2) B. Alyona and Mex 水题
- Codeforces Round #261 (Div. 2)B. Pashmak and Flowers
- Codeforces Round #261 (Div. 2) B.Pashmak and Flowers
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers【水】
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers
- Codeforces Round #261 (Div. 2)B. Pashmak and Flowers
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
- Codeforces Round #381 (Div. 2)C - Alyona and mex
- WNDCLASS结构体
- 预处理
- linux下的几种进程间通信方式的特点
- PS工具第二组。绘画与修饰工具(局部修饰!)
- LeetCode 19. Remove Nth Node From End of List
- Codeforces Round #381 (Div. 2) B. Alyona and flowers
- 自然数幂级数之和
- MFC Opengl 使用glm数学库进行空间坐标系矩阵变换
- 【miscellaneous】使用Google语音识别引擎(Google Speech API)[3月5日修改]
- 操作系统实践之第二章(特权级变换*理论篇)
- ssm框架整合
- 发送邮件工具类
- 文章标题
- Python lambda简介