【管道——二分+区间合并】

题目

代码

#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e5 + 10;
int n, len;
PII a[N];
bool cmp(PII a, PII b)
{
    return a.x + a.y < b.x + b.y;
}
bool check(ll t)
{
    ll l = -2e9, r = -2e9;
    for (int i = 1; i <= n; i++)
    {
        ll lt = max(a[i].x + a[i].y - t, 1ll), rt = a[i].x - a[i].y + t;
        if (lt > rt)
            continue;
        if (l == -2e9)
            l = lt, r = rt;
        if (i != 1 && r + 1 >= lt)
            r = max(r, rt);
        else false;
            
    }

    return l == 1 && r >= len;
}
int main()
{
    cin >> n >> len;
    for (int i = 1; i <= n; i++)
    {
        ll pos, s;
        cin >> pos >> s;
        a[i] = {pos, s};
    }
    sort(a + 1, a + n + 1, cmp);

    ll l = 0, r = 2e9 + 10;
    while (l < r)
    {
        ll mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid + 1;
    }
    cout << l;
}

以下是一个简单的示例代码,用于实现基于命名管道的服务器端和客户端通信。这里使用了C语言的标准库函数来进行命名管道的操作。 服务器端程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #define PIPE_NAME "/tmp/my_pipe" int main() { int pipe_fd; char buffer[256]; int bytes_read; // 创建命名管道 if (mkfifo(PIPE_NAME, 0666) < 0) { fprintf(stderr, "无法创建命名管道\n"); exit(EXIT_FAILURE); } // 连接命名管道 pipe_fd = open(PIPE_NAME, O_RDONLY); printf("连接命名管道成功\n"); // 读取数据 while (1) { memset(buffer, '\0', sizeof(buffer)); bytes_read = read(pipe_fd, buffer, sizeof(buffer)); if (bytes_read > 0) { printf("接收到数据:%s\n", buffer); } } // 拆除与命名管道的连接 close(pipe_fd); unlink(PIPE_NAME); return 0; } ``` 客户端程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #define PIPE_NAME "/tmp/my_pipe" int main() { int pipe_fd; char buffer[256]; int bytes_written; // 等待命名管道 while (access(PIPE_NAME, F_OK) == -1) { sleep(1); } // 建立与命名管道的连接 pipe_fd = open(PIPE_NAME, O_WRONLY); printf("连接命名管道成功\n"); // 写入数据 while (1) { printf("请输入要发送的数据:"); fgets(buffer, sizeof(buffer), stdin); bytes_written = write(pipe_fd, buffer, strlen(buffer)); if (bytes_written > 0) { printf("已发送数据:%s\n", buffer); } } return 0; } ``` 在运行这两个程序之前,需要先创建一个命名管道,可以使用以下命令: ``` mkfifo /tmp/my_pipe ``` 然后分别运行服务器端程序和客户端程序即可进行通信。在客户端程序中输入数据后,服务器端程序会自动接收并输出。注意,这里的代码仅作为示例,实际应用中需要对输入输出进行错误处理和数据格式校验等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值