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 ...
随机推荐
- KI的斐波那契_DFS
Description KI十分喜欢美丽而优雅的斐波那契数列,最近他新认识了一种斐波那契字符串,定义如下 f (0) = b, f (1) = a, f (2) = f (1) + f (0) = a ...
- 调用接口传递的XML 及排查原因
2fq3ej15nv8fgns30firbqtlo3 <Interface type="edit" model="object" value=" ...
- JavaScript和Java之间的关系
今天来简单而又详细地说说JavaScript和Java的关系. 开门见山总结性一句话,它们之间的关系 = 雷锋和雷峰塔之间的关系,换句话说:它们之间没什么关系. 但往往有不少初学者甚至中级者认为它们之 ...
- win7系统下的FTP配置
2016-07-12 工作中需要在win7操作系统下配置FTP,遇到许多问题,所以记录下来方便以后解决问题. FTP是文件传输协议的简称.用于Internet上的控制文件的双向传输.同时,它也是一个应 ...
- C++模板元编程(C++ template metaprogramming)
实验平台:Win7,VS2013 Community,GCC 4.8.3(在线版) 所谓元编程就是编写直接生成或操纵程序的程序,C++ 模板给 C++ 语言提供了元编程的能力,模板使 C++ 编程变得 ...
- python 后台爆破工具(多线程)
非阻塞 q.put(item) 写入队列,timeout等待时间 q.put_nowait(item) 相当q.put(item, False) threads多线程 首先导入threadin ...
- FMS 4中multicast脚本的小修正
FMS 4中multicast脚本的小修正 http://help.adobe.com/en_US/flashmediaserver/devguide/WS7812b00092aae0dc-2829d ...
- js 获取小数点位数方法及 字符串与数字之间相互转换方法
1.获取小数点位数方法 a. 使用 js 中 subsrting,indexOf,parseFloat三个函数,代码如下: var s = "22.127456" ;//s 为 字 ...
- cache manifest 格式
下面说明书写 cache manifest 文件需要遵循的格式.1 首行必须是 CACHE MANIFEST.2 其后,每一行列出一个需要缓存的资源文件名.3 可根据需要列出在线访问的白名单.白名单中 ...
- JS的字符串处理
1.字符串包含判断 var a = "qwer"; var b = "q"; if (a.contains(b)) { alert("1") ...