poj 1752 Advertisement (区间差分约束+最长路 输出可行解)

2023-05-16


Advertisement
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 919 Accepted: 331 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


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


思路:

常规区间差分约束, 这题让我意识到,最长路可以直接在spfa里 把 > 改成 < 号, 如果要变成最短路跑的话, 直接把权值取相反就好了, 构图跟原来一样的,仅仅改变权值

因为我们维护的是前缀和, 跑得是前缀和的最长路, 所以最后输出哪个广告牌存在时,得到dis[i]-dis[i-1]==1,即i-1到i有一块广告牌输出i,总的广告牌数存在在d[end],另外这题vector过不了- -

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 10005;
const int INF = 1e9;
//struct node
//{
//    int to, w;
//    node(){}
//    node(int tt, int ww) : to(tt), w(ww){}
//};
//vector<node> v[maxn*2];
int book[maxn*3], cnt[maxn*3], dis[maxn*3],  head[maxn*4], n, k, K, x, y;
struct node
{
    int v, next, w;
}edge[maxn*4];

void addEdge(int u, int v, int w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].next = head[u];
    head[u] = k;
}
void spfa(int s, int e)
{
    memset(book, 0, sizeof(book));
    for(int i = 0; i < maxn*3; i++) dis[i] = INF;
    queue<int> q;
    q.push(s);
    book[s] = 1;
    dis[s] = 0;
    cnt[s] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        book[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int to = edge[i].v;
            if(dis[u]+edge[i].w < dis[to])  //这里可以直接改成 > , 权值不用取负,那样初始值就是-inf
            {
                dis[to] = dis[u] + edge[i].w;
                if(!book[to])
                {
                    book[to] = 1;
                    q.push(to);
                }
            }
        }
    }
    printf("%d\n", -dis[e]);
     for(int i = s; i <= e; i++)  //输出可行解, 另外注意是 dis【i-1】-dis【i】, 因为权值取反了
        {
            if(dis[i-1]-dis[i] == 1)
                printf("%d\n", i-maxn);
        }
    return ;
}
int main()
{
    while(~scanf("%d%d", &K, &n))
    {
        memset(cnt, 0, sizeof(cnt));
        memset(dis, 0, sizeof(dis));
        memset(head, -1, sizeof(head));
        k = 0;
//        for(int i = 0; i < maxn*3; i++)
//            v[i].clear();
        int s = INF, e = -1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &x, &y);
            x += maxn;
            y += maxn;
            if(x > y) swap(x, y);
            s = min(s, x-1);
            e = max(e, y);
//            v[x-1].push_back(node(y, -min(y-x+1, k)));
            addEdge(x-1, y, -min(y-x+1, K));
            k++;
        }
        for(int i = s; i <= e; i++)
        {
//            v[i+1].push_back(node(i, 1));
            addEdge(i+1, i, 1);
            k++;
            addEdge(i, i+1, 0);
            k++;
//            v[i].push_back(node(i+1, 0));
        }
        spfa(s, e);
    }
    return 0;
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

poj 1752 Advertisement (区间差分约束+最长路 输出可行解) 的相关文章

  • iOS本地数据搜索 - UISearchController的简单实用

    在页面数据很多的时候我们通常会被要求加一个本地的搜索功能 xff0c 苹果给我们提供了一个封装的很好的控件UISearchController xff0c 下边介绍一下他的简单使用 定义需要的全局变量并初始化 span class hljs
  • 矩阵遍历问题

    这里记录些常见的矩阵遍历问题 xff0c 矩阵遍历没有什么简单的方法 xff0c 必须要遍历矩阵的每个元素 xff0c 因此在时间复杂度上没有什么简单的方法 xff0c 不过遍历时的方式可以不同 首先看下面例题 leetcode54 给定一
  • ubuntu系统添加环境变量3种方法

    说明 工作中 xff0c 我们自己编译安装的软件 xff0c 在系统中是无法在全局目录下自动识别的 xff0c 只能进入到相关目录下才能运行 xff0c 如在命令行下运行编译安装的php程序 xff0c 就得 usr local LAMP
  • Codeforces 897C(递归)

    点击打开链接 扎心题 xff0c 当时想法完全正确 xff0c 姿势不对 xff08 思维不够细腻 xff09 没过 题意 xff1a 给出四个字符串x y f0 z xff0c 并且给出递推公式 xff1a fi 61 x 43 fi 1
  • 天气预报API汇总

    目录 文章目录 一 天气预报平台 1 中国气象平台 2 心知天气 3 实况天气 4 YY天气 5 聚合天气 6 和风天气 7 彩云天气 8 咕咕天气 9 彩云天气 总结 一 天气预报平台 1 中国气象平台 优点 xff1a 中国气象局对外提
  • ResizeObserve 在 Echarts 的使用

    前言 前端图表经常要进行 resize 操作 xff0c 一般我们会想到监听 window resize event xff0c 但是这个事件只能监听 window 窗口大小的改变 xff0c 没有办法监听到某个div大小的改变 目前解决方
  • 运行java命令出现 Error: Invalid or corrupt jarfile XXX.jar

    运行java命令出现 Error Invalid or corrupt jarfile XXX jar 基本可以断定 xff0c 是jar不完整导致的 不完整 xff01 xff01 xff01 记住关键字 检查1 xff1a 检查是不是传
  • 页面间传值的方式

    从一个页面转向另一个页面的请求方式有两种 xff0c Post和Get 如果从原理上来探究他们的区别 xff0c 涉及到Http传输协议的细节 xff0c 这样深究下去 xff0c 就成华为人干的事了 xff0c 有空可以请教一下华为高人
  • 你现在无法访问 xxx.xxxx.com,因为网站使用的是 HSTS。网络错误和攻击通常是暂时的,因此该页面以后可能会恢复正常

    你现在无法访问 xxx xxxx com xff0c 因为网站使用的是 HSTS 网络错误和攻击通常是暂时的 xff0c 因此该页面以后可能会恢复正常 自己本地通过openSSL和nginx 搭建https证书 xff0c 过段时间通过域名
  • VMware通过vmdk安装Kali linux

    1 根据官网指引下载VMware专用kali linux版本 2 打开虚拟机 xff0c 文件 gt 扫描虚拟机 3 文件路径选择kali压缩包解压出来的文件夹的路径 4 点击下一步 xff0c 点击完成即可 5 这个就是我们刚刚创建的ka
  • 为什么中断子程序中不能使用延时和过长的程序?

    A回答 xff1a 通常在中断子程序中是不调用延时子程序的 xff0c 这样会增加中断处理时间 xff0c 如果有其它低级中断了 xff0c 就会延误响应中断了 所以 xff0c 中断子程序中不要写调用延时子程序 xff0c 中断子程序也不
  • iOS多线程详解:实践篇

    iOS多线程实践中 xff0c 常用的就是子线程执行耗时操作 xff0c 然后回到主线程刷新UI 在iOS中每个进程启动后都会建立一个主线程 xff08 UI线程 xff09 xff0c 这个线程是其他线程的父线程 由于在iOS中除了主线程
  • 【Qt】显示文件/文件夹下所有文件的路径

    一 条件与目的 给一个正确的文件夹绝对路径 xff0c QString字符串形式 要求打印出其中所有子目录以及其下的全部文件路径 二 废多看崩 名称 xff1a 遍历显示函数 参量 xff1a path 绝对路径 方法类 xff1a QDi
  • CMD执行命令行时卡住的问题

    公司编译工程项目时用了一些bat文件以命令行的方式来自动完成编译过程 xff0c 但是发现一个问题 xff0c 执行bat的时候Windows下弹出命令行窗口 xff0c 总是会时不时出现 假死 的情况 xff0c 然后命令执行就停在那里了
  • GET、POST、PUT、DELETE请求方式的区别以及用途

    1 GET GET请求是用来获取数据的 xff0c 不对服务器的数据做任何的修改 xff0c 新增 xff0c 删除等操作 GET请求就像数据库的SELECT操作一样 xff0c 只是用来查询一下数据 xff0c 不会修改 增加数据 xff
  • 通俗易懂-对于快慢指针找到链表环入口的理解

    图1是一个链表环 xff0c 此链表有8个结点 xff0c 分别为A H 假设起点为G xff0c 快指针fast和慢指针slow都从G出发 xff0c 慢指针一次遍历一个结点 xff0c 快指针一次遍历两个结点 xff0c 无论他们走多少
  • 通俗易懂-对于归并排序的细节理解python

    首先归并排序的原理就是将一个待排序的列表分成二等份 xff0c 四等份 xff0c 八等份 直到每一份只有一个元素的时候 xff0c 然后合并 xff0c 合并的时候进行排序 听起来有点绕 不多说 上代码 span class token
  • vnc连接失败可能的方案

    vnc连接失败可能的解决方案 上图表示还没有在windows power shell里边打开端口 xff0c 在windows power shell里边输入命令打开端口 xff0c 如下图所示 xff0c 密码和端口号换成服务器提供的即可
  • No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

    关于No module named matplotlib pyplot matplotlib is not a package报错 在学习matlibplot的时候运行一个 py文件出现这样的报错 xff1a No module named
  • Error tokenizing data. C error: out of memory

    用pandas读入数据的时候发现数据读入时出错了 xff0c 数据量感觉也不是很大 十万多条数据 电脑内存是8个G 报错信息为 xff1a Error tokenizing data C error out of memory 想想不对啊

随机推荐

  • kubeadm方式部署的k8s修改证书年限

    说明 kubeadm方式部署的k8s默认证书的年限为1一年 xff0c 当集群更新时 xff0c 证书也会更新 xff0c 如果集群每年都会更新 xff0c 那么证书年限就不用修改 但是大部分情况下 xff0c 为了保证线上环境稳定 xff
  • C++智能指针以及循环引用的解决办法

    1 为什么要使用智能指针 我们知道C 43 43 的内存管理是让很多人头疼的事情 xff0c 当我们写一个new语句时 xff0c 一般就会立即把delete语句直接也写了 xff0c 但是我们不能避免程序还未执行到delete时就跳转了或
  • Debian安装JDK的RPM包

    环境 xff1a Linux内核版本4 4 59 43 jdk1 8安装 Debian9系统 注意 xff1a 1 本文介绍的是在Debian中使用jdk的rpm包进行安装 JDK完全卸载 xff08 需要在root模式下进行操作 xff0
  • XShell 收费?5款免费且超赞的SSH工具,一个比一个香

    SSH客户端是后端程序员日常工作必备的工具之一 xff0c 一款趁手的工具也能让工作效率事半功倍 xff1b 上周的时候 xff0c 有小伙伴在群里面求免费的SSH软件 xff1b 说来也坑 xff0c 公司不允许使用PJ版的 xff0c
  • LaTeX公式按照章节编号

    LaTeX公式按照章节编号 问题 xff1a 如何让LaTeX公式按照章节编号 xff1f 答案 xff1a 在导言区加入如下语句 usepackage amsmath numberwithin equation section
  • xrdp 老版本返回上一次登录以及error - problem connecting

    连接服务器失败总结以下几种处理方式 xff1a 首先确定不是网络问题 xff0c ping ip地址 说明服务器端的网络没有问题 xff0c 通常linux开机默认监听22 3350 3389几个端口 22端口是用来连接ssh xff0c
  • C51编程23-应用篇(HC 06蓝牙模块)

    现在的手机 平板 xff0c 笔记本电脑都会自带蓝牙 本文将会介绍51单片机使用HC 06 蓝牙模块实现手机与笔记本电脑的通讯 HC 06 模块 购买HC 06模块后需要检测蓝牙模块是否是好的 xff0c 使用串口线与HC 06 模块连接起
  • BiliBili自动签到

    前情提要 emmm怎么突然会出这个教程呢 xff0c 因为俺要白嫖TX xff0c 如下图 xff1a 这是腾讯云函数公众号的一个搭建环境送礼品的活动 xff0c 截止日期 xff1a 10 9 这活动多亏群友分享 xff0c 不然俺也不知
  • RHEL8-配置IP地址

    前导 本文主要讲解如何重启RHEL 8或者CentOS 8网络以及如何解决RHEL8和CentOS8系统的网络管理服务报错 xff0c 当我们安装好RHEL 8或者 CentOS 8 xff0c 重启启动网络时 xff0c 会出现以下报错
  • redhat-8-0重置root密码

    前导 如果你忘记了RHEL 8系统中的root密码 xff0c 那就得重置root密码 xff0c 以下为在Grub启动菜单中在RHEL 8上进行手动密码恢复 引导 重启RHEL 8系统 将系统重启 xff0c 在看到grub菜单后 xff
  • Debian10使用本地ISO搭建APT源

    前言 这是个坑 xff01 是个大坑 xff01 如果在配置debian10本地源的时候 xff0c 直接使用apt cdrom add命令创建本地源后 xff0c 在安装软件的时候会有很大几率找不到软件包的位置然后报错 报错 E The
  • Debian10.x创建Raid5

    技术简介 RAID5技术是把硬盘设备的数据奇偶校验信息保存到其他硬盘设备中 RAID 5磁盘阵列组中数据的奇偶校验信息并不是单独保存到某一块硬盘设备中 xff0c 而是存储到除自身以外的其他每一块硬盘设备上 xff0c 这样的好处是其中任何
  • Debian10.x创建NFS

    技术简介 NFS xff08 网络文件系统 xff09 服务可以将远程Linux系统上的文件共享资源挂载到本地主机的目录上 xff0c 从而使得本地主机 xff08 Linux客户端 xff09 基于TCP IP协议 xff0c 像使用本地
  • Windows下 gcc编译环境的构建(Sublime + Mingw)

    1 起源 Windows 7下VC 6 0装起来很困难 xff0c 不得不装了个Visual Studio 2010 Express 但感觉比庞大 xff0c 并且在程序运行的最后不会暂停 2 计划采用Sublime加Mingw构建一个gc
  • Linux下安装xrdp

    Linux下安装xrdp 使用rdp协议访问远程Linux桌面 一般情况下 xff0c 如果需要登陆远程Linux系统 xff0c 我们会使用ssh telnet来完成 xff0c 如果需要登陆到远程Linux系统的桌面环境 xff0c 我
  • bootstrap模态对话框宽度设置

    39 addFormbox 39 modal css width 39 auto 39 39 margin left 39 function return this width 2
  • Debian10.x简易配置DHCP服务器

    环境 Client 网卡1 系统 xff1a debian10 Server xff1a 三网卡IP 10 10 100 254 IP 172 16 100 254 IP xff1a 192 16 100 254 实施步骤 安装 apt i
  • haproxy各个指标的打印报告解析

    1 使用这个命令就可以获取haproxy所有的数据 span class hljs keyword echo span span class hljs string 34 show stat 34 span socat span class
  • CentOS7使用firewalld打开关闭防火墙与端口火墙与端口

    CentOS7使用firewalld打开关闭防火墙与端口火墙与端口 启动 xff1a systemctl start firewalld 关闭 xff1a systemctl stop firewalld 查看状态 xff1a system
  • poj 1752 Advertisement (区间差分约束+最长路 输出可行解)

    Advertisement Time Limit 1000MS Memory Limit 10000KTotal Submissions 919 Accepted 331 Special Judge Description The Depa