HDU 5700 - 区间交 (优先队列)

区间交

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1383    Accepted Submission(s): 522


Problem Description
小A有一个含有 n 个非负整数的数列与 m 个区间。每个区间可以表示为 li,ri

它想选择其中 k 个区间, 使得这些区间的交的那些位置所对应的数的和最大。

例如样例中,选择 [2,5] [4,5] 两个区间就可以啦。
 

Input
多组测试数据

第一行三个数 n,k,m(1n100000,1km100000)

接下来一行 n 个数 ai ,表示lyk的数列 (0ai109)

接下来 m 行,每行两个数 li,ri ,表示每个区间 (1lirin)

 

Output
一行表示答案
 

Sample Input
  
  
5 2 3 1 2 3 4 6 4 5 2 5 1 4
 

Sample Output
  
  
10
 

Source
 

给左端点排序,按顺序把每个右端点放入优先队列

枚举左端点当答案。

超过k个了,就pop掉一个右端点。

如果删掉的是刚加入的右端点,那么这次的答案更新是不对的,但是这个答案没有贡献。(因为他被包含了)



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>\
#include<cmath>
using namespace std;
typedef long long LL;
#define lt x<<1
#define rt x<<1|1
const int maxn = 100000+4;
const int inf = 0x3f3f3f3f;

struct node
{
	int l,r;
	friend bool operator < (node a, node b)
	{
		return a.l<b.l;
	}
}a[maxn];

priority_queue<int,vector<int>,greater<int> >q;

LL sum[maxn];
int main()
{
	int n,k,m;

	while(~scanf("%d%d%d",&n,&k,&m)){
		while(!q.empty()) q.pop();
		for(int i=1;i<=n;i++){
			LL c;
			scanf("%lld",&c);
			sum[i]=sum[i-1]+c;
		}
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a[i].l,&a[i].r);
		}
		sort(a+1,a+1+m);
		LL ans = 0;
		for(int i=1;i<=m;i++){
			q.push(a[i].r);
			if(q.size()<k) continue;
			if(q.size()>k) q.pop();//如果删掉的是刚加入的右端点,那么这次的答案更新是不对的,但是这个答案没有贡献。(因为他被包含了)
			ans = max(ans,sum[q.top()]-sum[a[i].l-1]);
		}
		printf("%lld\n",ans);
	}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值