zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    AD设置PCB等比例打印
    leetcode------Word Search
    leetcode------Subsets II
    leetcode------Subsets
    leetcode------Palindrome Partitioning
    leetcode------Combinations
    leetcode------Binary Tree Zigzag Level Order Traversal
    leetcode------Populating Next Right Pointers in Each Node II
    leetcode------Populating Next Right Pointers in Each Node
    leetcode------Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/lrj124/p/14345162.html
Copyright © 2011-2022 走看看