HDU 5700 优先队列(或者multiset) 或 线段树

题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大。数列的数字均>=0

优先队列思路:

按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即可。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int n, k, m;
LL a[maxn], sum[maxn];
vector<int> ve[maxn];
struct Point{
int rb;
bool operator < (const Point& a) const{
return a.rb < rb;
}
Point(int x = ): rb(x){};
}; int main(){
while (scanf("%d%d%d", &n, &k, &m) == ){
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++){
scanf("%lld", a + i);
sum[i] = sum[i - ] + a[i];
ve[i].clear();
}
for (int i = ; i <= m; i++){
int u, v; scanf("%d%d", &u, &v);
ve[u].push_back(v);
}
LL ans = ;
priority_queue<Point> que;
for (int i = ; i <= n; i++){
for (int j = ; j < ve[i].size(); j++){
que.push(Point(ve[i][j]));
}
while (que.size() > k) que.pop();
if (que.size() == k) ans = max(ans, sum[que.top().rb] - sum[i - ]);
}
printf("%lld\n", ans);
}
return ;
}

线段树思路:

仔细一想,发现线段树思路其实也大致相同,最后只需要找线段树中cnt=k的就可以了

上一篇:js深入研究之匿名函数


下一篇:如何在IDEA上 添加GIT和maven、mybatis插件