Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)
B. Alyona and flowers
Problem Description:
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.
Sample Input:
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
Sample Output:
7
【题目链接】B. Alyona and flowers
【题目类型】
&题意:
给m个区间,你要选几个使得区间内的元素乘区间选择的次数最大,问这个最大值是多少?
&题解:
模拟遍样例,再加仔细想想,发现只有这个区间所有数加起来>0的时候才要选,并且对答案的贡献也就是>0的这个数,所有把符合区间的这个数相加就好了。
【时间复杂度】O(\(n^2\))
&代码:
#include <bits/stdc++.h>
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
const int maxn = 100 + 9 ;
int n, m, a[maxn], u, v, res;
int main() {
while (~SII(n, m)) {
res = 0;
rez(i, 1, n) SI(a[i]);
rez(i, 1, m) {
SII(u, v);
int ans = 0;
rez(j, u, v) {
ans += a[j];
}
res += ans > 0 ? ans : 0;
}
printf("%d\n", res);
}
return 0;
}
Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)的更多相关文章
- Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题
A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题
A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos 水题
A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题
A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...
随机推荐
- DOM和IE中的 事件对象
DOM中的事件对象:(符合W3C标准) preventDefault() 取消事件默认行为 stopImmediatePropagation() 取消事件冒泡同时阻止当前节点 ...
- 想一想social VR might just work
昨天玩了Oculus上的Casino VR(其实之前就知道这个瑞士公司,也下过standalone的PC client). 几把下来,居然觉得fun,总结起来: 1.是在一个immersive的环境中 ...
- 吴奇隆刘诗诗婚礼场地:巴厘岛Ayana酒店,美到窒息!
导读:忍不住转载一下,原文地址:http://www.sjq315.com/news/270768.html 3月20日,吴奇隆和刘诗诗在巴厘岛五星级酒店Ayana Resort and Spa酒店举 ...
- pageHelp的使用
以前使用ibatis/mybatis,都是自己手写sql语句进行物理分页,虽然稍微有点麻烦,但是都习惯了.最近试用了下mybatis的分页插件 PageHelper,感觉还不错吧.记录下其使用方法. ...
- javascript学习笔记全记录
js的初步了解 1.就是用来修改样式的,修改的是行内样式.任何样式都能够修改. 2.css里面怎么写js就怎么写. 3.任何元素都能加事件:事件都要小写 js的三大 ...
- DOCTYPE声明的作用是什么?严格模式与混杂模式如何区分?
HTML语言已经存在太久了,目前必然会有一些不同版本的文档存在,为了能够让浏览器清楚你的文档的版本类型和风格,需要在文档的起始用DOCTYPE声明制定当前文档的版本和风格.如果在网页中提供了版本信息, ...
- Ext.Window 的常见属性
Ext.Window 的常见属性: plain:true,(默认不是) resizable:false,(是否可以改变大小,默认可以) maximizable:true,(是否增加最 ...
- C# .Net中七层架构浅析
Model实体层,DBUtility数据访问抽象类,IDAL数据访问接口层,SQLServerDAL数据访问层,DALFactory数据访问工厂类,BLL业务逻辑层,UI界面层 一.项目名称及描述:( ...
- C堆栈入门
原文:http://student.csdn.net/link.php?url=http://www.top-e.org%2Fjiaoshi%2Fhtml%2F427.html 格式和部分内容稍作修改 ...
- 11.14 T2 小x的旅行(小x的旅行)
1.小x的旅行 (travel.pas/c/cpp) [问题描述] 小x大学毕业后,进入了某个公司做了高层管理,他每年的任务就是检查这个公司在全国各地N个分公司的各种状况,每个公司都要检查一遍,且 ...