• 【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 ? ' ' : '
    ',ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    输入年月日,输出这一天是这一年的多少天
    判断体重是否标准 男标准=身高-100±3 女标准=身高-110±3
    if 条件运算符
    24小时换算成12小时&&判断正负数
    运算符(编程)
    定义变量
    基础知识
    java线程阻塞中断与LockSupport使用介绍(转)
    01背包问题--动态规划解法(2)(转载)
    01背包问题--动态规划解法
  • 原文地址:https://www.cnblogs.com/lrj124/p/14345162.html
Copyright © 2020-2023  润新知