【CF306B】Optimizer - 贪心

题目描述

A process RAM is a sequence of bytes that are indexed from 1 to \(n\) . Polycarpus's program contains such instructions as "memset", that is, the operations of filling memory cells on a segment with some value. The details are: the code only contains \(m\) instructions that look like "set13 a_i l_i". Instruction ii fills a continuous memory segment of length \(l_{i}\), starting from cell number \(a_{i}\) , (that it cells with numbers \(a_{i},a_{i+1},...,a_{i+li-1}\) with values 13.

In Polycarpus's code, the optimizer's task is to remove the maximum number of instructions from his code in such a way that the remaining instructions set value 13 in all the memory bytes that got this value from the code before the optimization. Also, the value 13 should be set only in the memory bytes that got this value from the code before the optimization. Your task is to implement the optimizer for such program.

题目大意

\(n\) 个区间,要删除尽量多的区间,使原来区间覆盖到的数删除后也都覆盖到。

思路

先选定第一个区间,再对于起点在当前区间中的区间,选出一个右端点最远的作为下一个区间,这样不断选来覆盖

#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 2e6 + 10;
const int inf = 99999999;
int n,m,p,now,maxv,d[maxn],mx[maxn],num[maxn],ans[maxn];
bool cho[maxn];
int main() {
	scanf("%d%d",&n,&m);
	for (int i = 1,l,r,s;i <= m;i++) {
		scanf("%d%d",&l,&s);
		r = l+s-1;
		d[l]++; d[r+1]--;
		if (r > mx[l]) { mx[l] = r; num[l] = i; }
	}
	for (int i = 1;i <= n;i++) d[i] += d[i-1];
	for (int i = 1;i <= n;i++) {
		if (mx[i] > maxv) { maxv = mx[i]; p = num[i]; }
		if (d[i] && now < i) { now = maxv; cho[p] = true; }
	}
	for (int i = 1;i <= m;i++) if (!cho[i]) ans[++ans[0]] = i;
	printf("%d",ans[0]);
	for (int i = 1;i <= ans[0];i++) printf("%c%d",i ^ 1 ? ' ' : '\n',ans[i]);
	return 0;
}
posted @ 2021-01-29 15:22  lrj124  阅读(72)  评论(0编辑  收藏  举报