POJ 1752 Advertisement (离散化+差分约束)

Advertisement

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1086   Accepted: 403   Special Judge

Description

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 contains two integers K and N (1 <= K, N <= 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 fist line of the output file, 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.

Sample Input

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

Sample Output

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

题意:

给你一个数K(K<=1000)和N(N<=1000)个区间(abs(li,ri)<=1e4),要求每个区间的区间长度若>=K,则至少选这个区间中的K个数,否则就选择这个区间的所有的数。求最少选择多少数,并从小到大输出你选择的数字。

思路:

典型的区间差分约束。

将每个区间先固定为x

y-x>=z的形式(具体见代码)。最后再根据求出的距离d数组从小到大依次输出足够的数量即可(别忘了再偏移回来)。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=500005;
struct edge{
    int u;
    int v;
    int w;
    int next;
}e[maxn*4];
struct node
{
    int x,y;
}a[maxn];
int cnt,head[maxn],ma,mi;
int mp[maxn],id,rmp[maxn],c[maxn];
int pre[maxn];
char s[10];
void add(int u,int v,int w){
    //cout< "< que;
    for(int i=0;id[v]){
                //if(v==13)cout< "<=n)
                    {
                        //cout< "<y)
            swap(x,y);
            x--;
            x+=20000;
            y+=20000;
            a[i].x=x;a[i].y=y;
            if(!mp[x]) {c[++id]=x;mp[x]=1;}
            if(!mp[y]) {c[++id]=y;mp[y]=1;}
        }
        sort(c+1,c+1+id);
        for(int i=1;i<=id;i++)
        {
            mp[c[i]]=i;
           // cout<= i - i-1 >= 0
            add(i-1,i,0);
            add(i,i-1,-c[i]+c[i-1]);
        }
        for(int i=1;i<=m;i++)
        {
            int x=mp[a[i].x],y=mp[a[i].y];
            if(a[i].y-a[i].x>k)
            {
                //a[i].y-a[i].x >= y - x >= k
                add(x,y,k);
                add(y,x,a[i].x-a[i].y);
            }
            else
            {
                add(x,y,a[i].y-a[i].x);
                add(y,x,-a[i].y+a[i].x);
            }
        }
        n=id;
        pre[1]=-1;
        bool ans=spfa(1);
        printf("%d\n",d[n]);
        //cout<

 

你可能感兴趣的:(图论)