D. The Union of k-Segments(贪心)

本文介绍了一个算法问题,即找出n个线段中至少重叠k次的线段集合,并确定这些线段的最少数量及各自范围。通过贪心算法思想,有效地查找并确定重叠部分的起始和结束位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:D. The Union of k-Segments

D. The Union of k-Segments
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output
First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Examples
inputCopy
3 2
0 5
-3 2
3 8
outputCopy
2
0 2
3 5
inputCopy
3 2
0 5
-3 3
3 8
outputCopy
1
0 5

题目大意:给你n个线段,问:最少重叠k次的部分有多少个(最少个数),每一部分的范围是多少。

思路:
贪心的思想,查找重叠部分超过m次的开始和结尾。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<string>
#include<algorithm>
using namespace std;
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;

const int maxn=1e6+9;

struct node
{
    int s,p;
} edge[maxn*2];
int cun[maxn*2];
bool cmp(node a,node b)
{
    if(a.s==b.s) return a.p>b.p;//一定是开始端点在前
    return a.s<b.s;
}



int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int len=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&edge[len].s,&edge[len+1].s);
            edge[len++].p=1;//开始端
            edge[len++].p=0;//结束端
        }
        sort(edge,edge+len,cmp);

        int ans=0;
        int sum=0;
        for(int i=0;i<len;i++)
        {
            if(edge[i].p)//遇到开始端
            {
                sum++;//要先加加
                if(sum==m)//刚好遇到重叠m次的开始端
                    cun[ans++]=edge[i].s;
            }
            else//结束端
            {
                if(sum==m)//刚好遇到重叠m次的结束端
                    cun[ans++]=edge[i].s;
                sum--;//判断完,再减减
            }
        }
        printf("%d\n",ans/2);
        for(int i=0;i<ans;i+=2)
            printf("%d %d\n",cun[i],cun[i+1]);
    }
    return 0;
}
<think>嗯,用户遇到了编译错误,提示在链接阶段出现了“multiple definition of `wifi_my_config'”的问题。首先,我需要分析错误信息。错误指出在wifiscan.h的第55行,wifi_scan库的wifiscan.c.obj和main库的main.c.obj中都重复定义了`wifi_my_config`变量。这说明这个变量被多个源文件包含,导致链接器发现多个定义。 可能的原因有几个。首先,用户可能在头文件中直接定义了这个变量,而没有使用extern关键字。通常,头文件中应该只声明变量,而不是定义。正确的做法是在头文件中用extern声明变量,然后在某一个源文件中进行定义。例如,在头文件中写`extern int wifi_my_config;`,然后在.c文件中写`int wifi_my_config = 0;`。 其次,检查是否在头文件中没有使用头文件保护符(#ifndef、#define、#endif),可能导致头文件被多次包含,虽然这通常会引起重定义错误,但在这里可能不是主要原因,因为错误是链接时的多重定义,而不是编译时的。 另外,需要确认这个变量是否确实需要在多个文件中共享。如果不需要,可以考虑将其改为静态变量,限制其作用域到单个文件。或者,检查是否有不必要的包含头文件,导致变量在多个地方被定义。 用户提供的错误信息显示,问题出在wifiscan.h的第55行,所以需要查看该行的具体内容。如果该行是变量的定义,那么修改为extern声明,并在对应的.c文件中定义即可解决。同时,检查所有包含该头文件的源文件,确保没有重复定义。 可能的解决步骤包括:修改头文件中的变量声明为extern,在其中一个.c文件中定义变量,确保头文件保护符正确,以及清理并重新编译项目。如果用户按照这些步骤操作,应该能解决链接错误。</think>这个编译错误是由于符号重复定义导致的链接阶段错误。让我们逐步分析问题原因和解决方案: $$...$$ 编译错误核心信息: `multiple definition of 'wifi_my_config'` $$...$$ **根本原因分析:** 1. 在项目组件中`wifiscan.h`头文件第55行处 2. 变量`wifi_my_config`被重复定义: - 第一次定义出现在`main.c`编译生成的目标文件 - 第二次定义出现在`wifiscan.c`编译生成的目标文件 3. 链接器发现两个不同目标文件包含同名全局变量 **分步解决方案:** $\textcolor{blue}{步骤 1}$:检查头文件定义方式 - 打开`components/wifi_scan/wifiscan.h` - 确认第55行的变量声明方式: ```c // 错误写法(在头文件中直接定义): int wifi_my_config = 0; // 正确写法(头文件只声明): extern int wifi_my_config; ``` $\textcolor{blue}{步骤 2}$:添加定义到源文件 - 在`wifiscan.c`中添加实际定义: ```c int wifi_my_config = 0; // 实际初始化值 ``` $\textcolor{blue}{步骤 3}$:验证头文件保护 - 确保头文件有防止重复包含的宏: ```c #ifndef __WIFISCAN_H__ #define __WIFISCAN_H__ // 头文件内容... #endif ``` $\textcolor{blue}{步骤 4}$:检查包含关系 - 确认`main.c`是否正确定义: - 应包含`#include "wifiscan.h"` - 不应有重复的变量定义 $\textcolor{blue}{步骤 5}$:清理并重新编译 ```bash idf.py fullclean idf.py build ``` **预防措施:** 1. 遵循C语言变量声明规范: - 头文件中使用`extern`声明 - 源文件中进行实际定义 2. 使用`static`限定符修饰仅限本文件使用的变量 3. 定期运行`idf.py fullclean`避免残留编译文件干扰 **扩展建议:** 如果这是跨组件共享的配置变量,建议采用更安全的访问方式: ```c // wifi_config.h #ifdef WIFI_CONFIG_IMPL #define EXTERN #else #define EXTERN extern #endif EXTERN int wifi_my_config; ``` 在某个源文件中: ```c #define WIFI_CONFIG_IMPL #include "wifi_config.h" ``` 这种模式可以确保变量仅被定义一次,同时保持类型安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值