Uva10148

Problem F
"Advertisement"


 

The Department of Recreation has decided that it must be more profitable, and it wants to sell advertising space along a popular jogging path at a local park. They have built a number of billboards (special signs for advertisements) along the path and have decided to sell advertising space on these billboards. Billboards are situated evenly along the jogging path, and they are given consecutive integer numbers corresponding to their order along the path. At most one advertisement can be placed on each billboard.

A particular client wishes to purchase advertising space on these billboards but needs guarantees that every jogger will see it's advertisement at least K times while running along the path. However, different joggers run along different parts of the path.

Interviews with joggers revealed that each of them has chosen a section of the path which he/she likes to run along every day. Since advertisers care only about billboards seen by joggers, each jogger's personal path can be identified by the sequence of billboards viewed during a run. Taking into account that billboards are numbered consecutively, it is sufficient to record the first and the last billboard numbers seen by each jogger.

Unfortunately, interviews with joggers also showed that some joggers don't run far enough to see K billboards. Some of them are in such bad shape that they get to see only one billboard (here, the first and last billboard numbers for their path will be identical). Since out-of-shape joggers won't get to see K billboards, the client requires that they see an advertisement on every billboard along their section of the path. Although this is not as good as them seeing K advertisements, this is the best that can be done and it's enough to satisfy the client.

In order to reduce advertising costs, the client hires you to figure out how to minimize the number of billboards they need to pay for and, at the same time, satisfy stated requirements.

Input

The first line of the input consist of an integer indicating the number of test cases in theinput. Then there's a blank line and the test cases separated by a blank line.

The first line of each test case contains two integers K and N (1 ≤ KN ≤ 1000) separated by a space. K is the minimal number of advertisements that every jogger must see, and N is the total number of joggers.

The following N lines describe the path of each jogger. Each line contains two integers Ai and Bi (both numbers are not greater than 10000 by absolute value). Ai represents the first billboard number seen by jogger number i and Bi gives the last billboard number seen by that jogger. During a run, jogger i will see billboards Ai, Bi and all billboards between them.

Output

On the first line of the output fof each test case, write a single integer M. This number gives the minimal number of advertisements that should be placed on billboards in order to fulfill the client's requirements. Then write M lines with one number on each line. These numbers give (in ascending order) the billboard numbers on which the client's advertisements should be placed. Print a blank line between test cases.

Sample input

 

1

5 10
1 10
20 27
0 -3
15 15
8 2
7 30
-1 -10
27 20
2 9
14 21

Sample output for the sample input

 

19
-5
-4
-3
-2
-1
0
4
5
6
7
8
15
18
19
20
21
25
26
27

 

双贪+优先级队列。偶然习得优先级队列,挺不错的。 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#define Nm 10000
using namespace std;
int M[21000];
struct T
{
    int a,b;
} m[1200];
typedef struct
{
    int mk,k;
} Node;
struct cmp
{
    bool operator()(const Node &t1,const Node &t2)
    {
        if(t1.mk!=t2.mk)return t1.mk>t2.mk;
        else return t1.k>t2.k;
    }
};
int ccmp(const void *_a,const void *_b)
{
    T*a=(T*)_a;
    T*b=(T*)_b;
    if(a->b - b->b)return a->b - b->b;
    else return b->a - b->a;
}
int S[20100];
int main()
{
   // freopen("1.txt","r",stdin);
    int num,N,K;
    cin>>num;
    for(int i=0; i<num; i++)
    {
        cin>>K>>N;
        int a,b;
        memset(M,0,sizeof(M));
        for(int j=0; j<N; j++)
        {
            cin>>a>>b;
            if(a>b)m[j].a=b+Nm,m[j].b=a+Nm;
            else m[j].a=a+Nm,m[j].b=b+Nm;
            if(m[j].b-m[j].a<K)
            {
                for(int k=m[j].a; k<=m[j].b; k++)
                    M[k]=-1;
                j--;
                N--;
            }
            else
            {
                for(int k=m[j].a; k<=m[j].b; k++)
                    if(M[k]!=-1)M[k]++;
            }
        }
        priority_queue<Node,vector<Node>,cmp>D;
        qsort(m,N,sizeof(T),ccmp);
        for(int j=0; j<N; j++)
        {
            int OKK=0;
            for(int t=j-1; t>=0; t--)
            {
                if(m[j].a<=m[t].a)
                {
                    OKK=1;
                    for(int k=m[j].a; k<=m[j].b; k++)
                        if(M[k]!=-1)M[k]--;
                    break;
                }
            }
            if(OKK)continue;
            int size=K;
            for(int k=m[j].a; k<=m[j].b; k++)
            {
                if(!size)break;
                else
                {
                    Node u;
                    u.mk=M[k];
                    u.k=k;
                    int ok=0;
                    if(M[k]==-1)
                    {
                        size--;
                        ok=1;
                    }
                    if(!ok)D.push(u);
                    while(D.size()>size)D.pop();
                }
            }
            while(!D.empty())
            {
                Node u=D.top();
                M[u.k]=-1;
                D.pop();
            }
            for(int k=m[j].a; k<=m[j].b; k++)
                if(M[k]!=-1)M[k]--;
        }
        if(i)cout<<endl;
        int k=0;
        for(int j=0; j<20001; j++)
            if(M[j]==-1)S[k++]=j-Nm;
        cout<<k<<endl;
        for(int j=0;j<k;j++)
        cout<<S[j]<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值