0
点赞
收藏
分享

微信扫一扫

POJ 1752 Advertisement(差分约束+最短路/最长路+输出路径)

少_游 2023-02-08 阅读 24


 

Advertisement

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 1098

 

Accepted: 409

 

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


Source

​​Northeastern Europe 1999​​

题目大意:题目大意:有n个人在一条路上跑步,广告商准备在这条路上设置广告牌,假设这条路上每一个点有一个广告牌 
现在已知这n个人从Ai开始跑,到Bi结束,那么他可以看到max(Ai,Bi)-min(Ai,Bi)+1的广告牌数,现在广告商 
需要每个人都要看到至少k个广告牌(如果区间长度不够K,那么需要看到区间长度),求需要设置的最少广告牌数以及一组合法解

分析:

设dis[i]表示1−i中广告牌的数量,那么区间[a,b]内广告牌的数量就是:dis[b]−dis[a−1]
题目条件:
dis[b]−dis[a−1]>=K

隐含条件:

1.区间长度不足K,则:dis[b]−dis[a−1]=len=b-a+1即 
dis[b]−dis[a−1]>=len  ,dis[b]−dis[a−1]<=len
2.每个位置只能设置一个广告牌 
0<=dis[b]−dis[a−1]<=1
其他细节:

因为有负数,所以整体向右移动10001
输出路径:只要dis[i-1]==dis[i]-1,说明这里有广告牌,输出即可

建立图,寻找最长路即可

最长路

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int N = 10000 + 5;
int head[N*2+10],tot,cnt[N*2+10],dis[N*2+10];
bool vis[N*2+10];

struct node{
int next,to,v;
}e[2000000];
void adde( int a, int b, int c ){
e[++tot].next = head[a];
head[a] = tot;
e[tot].to = b;
e[tot].v = c;
}
int spfa(int be,int en){

memset(vis,false,sizeof(vis));
memset(dis,-128,sizeof(dis));///注意要清空全部
memset(cnt,0,sizeof(cnt));

queue<int>q;
q.push(be);
cnt[be]++;
vis[be] = true;
dis[be] = 0;
while( !q.empty() ){
int u = q.front();
q.pop();
vis[u] = false;
for( int i = head[u]; i; i = e[i].next){
int v = e[i].to;
if( dis[v] <dis[u] + e[i].v ) {
dis[v] = dis[u] + e[i].v;
if( !vis[v] ) {
vis[v] = true;
//if( ++cnt[v]>n ) return false;
q.push(v);
}
}
}
}

return dis[en];
}
int main(){
int a ,b ,c;
int n,m;
while(scanf("%d %d",&n ,&m)!=-1){

memset(head ,0 ,sizeof(head));
tot = 1;

int minn=1e9,maxx=-1e9;

for(int i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
if(a>b) swap(a,b);///易错点
a+=10001;
b+=10001;
minn=min(a-1,minn);
maxx=max(b,maxx);
if(b-a+1>=n)
adde(a-1,b,n);
else
{
adde(b,a-1,-(b-a+1));
adde(a-1,b,b-a+1);
}
}
for(int i=minn;i<maxx;i++)
{
adde(i+1,i,-1);
adde(i,i+1,0);
}
printf("%d\n",spfa(minn,maxx));
for (int i=minn;i<=maxx;i++)
if (dis[i-1]==dis[i]-1)
printf("%d\n",i-10001);

}
return 0;
}

最短路:

要倒着建立,最大值为起点,最小值为终点,且求出的最短路是负值

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int N = 10000 + 5;
int head[N*2+10],tot,cnt[N*2+10],dis[N*2+10];
bool vis[N*2+10];

struct node{
int next,to,v;
}e[2000000];

void adde( int a, int b, int c ){
e[++tot].next = head[a];
head[a] = tot;
e[tot].to = b;
e[tot].v = c;
}
int spfa(int be,int en){

memset(vis,false,sizeof(vis));
memset(dis,127,sizeof(dis));
memset(cnt,0,sizeof(cnt));

queue<int>q;
q.push(be);
cnt[be]++;
vis[be] = true;
dis[be] = 0;
while( !q.empty() ){
int u = q.front();
q.pop();
vis[u] = false;
for( int i = head[u]; i; i = e[i].next){
int v = e[i].to;
if( dis[v] > dis[u] + e[i].v ) {
dis[v] = dis[u] + e[i].v;
if( !vis[v] ) {
vis[v] = true;
//if( ++cnt[v]>n ) return false;
q.push(v);
}
}
}
}
return dis[en];
}
int main(){
int a ,b ,c;
int n,m;
while(scanf("%d %d",&n ,&m)!=-1){

memset(head ,0 ,sizeof(head));
tot = 1;
int minn=1e9,maxx=-1e9;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
if(a>b) swap(a,b);
a+=10001;
b+=10001;
minn=min(a-1,minn);
maxx=max(b,maxx);
if(b-a+1>=n)
adde(b,a-1,-n);
else
{
adde(a-1,b,b-a+1);
adde(b,a-1,-(b-a+1));
}
}
for(int i=minn;i<=maxx;i++)
{
adde(i,i+1,1);
adde(i+1,i,0);
}

printf("%d\n",-spfa(maxx, minn));
for (int i=minn;i<=maxx;i++)
if (dis[i-1]==dis[i]-1)
printf("%d\n",(i-10001)) ;
}
return 0;
}

 

举报

相关推荐

0 条评论