CodeForces 306B

本文探讨了在程序代码中优化内存填充指令的方法,通过按起点和路径长度排序,采用贪心策略,逐个选择覆盖范围最大的指令进行删除,确保所有受影响的内存单元都能保持13值,同时最小化删除指令的数量。最后,展示了如何通过实例实现这一过程,并输出剩余指令的编号。
//#include<bits/stdc++.h>
#include<algorithm>
#include<stdio.h>
#include<iostream>
using namespace std;
const int maxx=1000000*2+10;
struct Node
{
    int a,l;
    int num;
} op[maxx];
int use[maxx];
bool cmp(Node a,Node b)
{
    if(a.a!=b.a) return a.a<b.a;
    else if(a.a==b.a) return a.l>b.l;
    return a.num<b.num;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&op[i].a,&op[i].l);
        op[i].num=i+1;
    }
    sort(op,op+m,cmp);
    int  right=op[0].a+op[0].l;
    use[op[0].num]=1;
    int incl=right;
    int preid=0;
    int ans=1;
    for(int i=1; i<m; i++)
    {
        if(op[i].a>incl)
        {
            if(preid)
            {
                use[preid]=1;
                incl=right;
                preid=0;
                i--;
                ans++;

            }
            else
            {
                preid=0;
                use[op[i].num]=1;
                right=op[i].a+op[i].l;
                incl=right;
                ans++;
            }
        }
        else if(op[i].a+op[i].l>right)
        {

            right=op[i].a+op[i].l;
            preid=op[i].num;
        }
    }
    if(preid)
    {
        ans++;
        use[preid]=1;
    }
    printf("%d\n",m-ans);
    if(m-ans)
        for(int i=1; i<=m; i++)
            if(!use[i]) printf("%d ",i);
    return 0;

}
/*100 6
1 100
2 20
3 10
5 10
6 1
2 5*/
View Code
 H
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 i fills a continuous memory segment of length li, starting from cell number ai, (that it cells with numbers ai, ai + 1, ..., ai + 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.

Input

The first line contains integers n and m (1 ≤ n ≤ 2·106, 1 ≤ m ≤ 2·105) — the number of bytes (memory cells) and the number of instructions in Polycarpus's code. Then m lines follow, each line contains a pair of integers aili (1 ≤ ai ≤ n, 1 ≤ li ≤ n - ai + 1).

Output

Print in the first line the sought maximum number of instructions that can be removed from the code. In the second line print the numbers of the instructions. The instructions are numbered from 1 to m in the order they appeared in the input. If there are multiple solutions, print any of them.

Sample Input

Input
10 4
3 3
3 1
4 1
9 2
Output
2
2 3
Input
1 1
1 1
Output
0


按照起点,路径长度排序。然后贪心,每次选择覆盖最大的一个边,知道遍历完所有的边。判断最后一个边应不应该加入。

转载于:https://www.cnblogs.com/superxuezhazha/p/5411138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值