【CF】Day139——杂题 (绝对值变换 | 异或 + 二分 | 随机数据 + 图论)

B. Meeting on the Line

题目:

思路:

数形结合

首先考虑如果没有 t 的影响该怎么写

显然我们就是让最大时间最小化,那么显然选择最左端点和最右端点的中间值即可,即 (mi + mx) / 2,那么现在有了 t 该怎么办

我们不妨考虑拆开绝对值,由 |a - b| = max(a-b, b-a) 可得原式 t - |x - x0| 拆解为

max(t - (x - x0), t - (x0 - x)) = max(t - x + x0, t + x - x0) = max(x0 - (x - t), (t + x) - x0)

我们令 x-t 和 t+x 为两个新点,那么显然 x-t 在 x0 左边,t+x 在 x0 右边,所以我们找到最大的右端点和最小的左端点即可,这就是我们上面的普通情况

除此之外,我们可以还能使用二分or三分

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1), t(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> t[i];
    int mx = -1e18, mi = 1e18;
    for (int i = 1; i <= n; i++)
    {
        mx = max(mx, a[i] + t[i]);
        mi = min(mi, a[i] - t[i]);
    }
    double res = (mx + mi) * 1.0 / 2.0;
    printf("%.6lf\n", res);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

C1. Sheikh (Easy version)

题目:

思路:

异或性质

这题看似无从下手,但是我们关键点在于发现异或的性质,不难发现一个特点,异或是不带进位的加法,那么对于任意 x,y,我们都有 x+y >= x ^ y,这是显然的

那么我们就豁然开朗了,既然 x+y >= x ^ y,那么对于一个区间我们肯定是多加数的,因为 x+y - x^y >= 0 恒成立,所以多加显然是不劣的

那么最大值显然就是选取整个区间,但是题目让我们输出最小的长度,所以考虑如何最小

显然由上诉结论可知,我们区间长度越长,那么值就越大,这是单调的,所以我们考虑二分区间长度,我们枚举每一个左端点,然后二分最大长度即可

特别注意预处理前缀和即可,还有越界问题

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int n,q;
    cin >> n >> q;
    vector<int> a(n+1),sum(n+1,0),sumxor(n+1,0);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum[i] = sum[i-1] + a[i];
        sumxor[i] = sumxor[i-1] ^ a[i];
    }
    cin >> q >> q;
    int mx = sum[n] - sumxor[n];
    auto check = [&](int l,int len)->int{
        int r = min(l + len - 1,n);
        int nmx = (sum[r] - sum[l-1]) - (sumxor[r] ^ sumxor[l-1]);
        return nmx >= mx;
    };
    int le = 1e18;
    int mxl = 0;
    for (int i = 1; i <= n; i++)
    {
        int l = 1,r = 1e18;
        while (l+1 < r)
        {
            int mid = l+r >> 1;
            if(check(i,mid))
            {
                r = mid;
            }
            else
            {
                l = mid;
            }
        }
        int templen = 0;
        if(check(i,l)) templen = l;
        else templen = r;
        if(templen < le)
        {
            le = templen;
            mxl = i;
        }
    }
    cout << mxl << " " << mxl + le - 1 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

E. Guess the Cycle Size

题目:

思路:

随机数据的特征

注意到题目数据随机,那么可以考虑稍微暴力的做法

我们假设对于 a,b 点对进行询问,如果 a,b 和 b,a 的询问结果不同,那么显然二者相加就是答案

但是由于 a,b 和 b,a 的询问结果不同,因此我们要多次判断,可以发现 50 次内基本上不可能会错,如果担心错的话可以在代码结尾加上 while(1) 使得其超时,因为TLE 会重测 3 次,3 次全错肯定不可能

最后特别注意特判 -1 情况即可

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int ans1, ans2;
    for (int i = 2; i <= 1e18; i++)
    {
        cout << "? " << 1 << " " << i << endl;
        cin >> ans1;
        if (ans1 == -1)
        {
            cout << "! " << i - 1 << endl;
            return;
        }
        cout << "? " << i << " " << 1 << endl;
        cin >> ans2;       
        if(ans1 != ans2)
        {
            cout << "! " << ans1 + ans2 << endl;
            return;
        } 
    }
    while (1);
}

signed main()
{
    int t = 1;
    while (t--)
    {
        solve();
    }
    return 0;
}
相关推荐
TAN-90°-4 分钟前
Deep Learning for Computer Vision——Generative Models 2
人工智能·深度学习·神经网络·算法·目标检测·机器学习·计算机视觉
Rabitebla12 分钟前
【Linux系统编程】指令(三):创建、删除、复制、移动、看内容
linux·数据结构·c++·算法
qq_410194291 小时前
.NET 8.0 下基于 PaddleOCRSharp 的离线文字识别落地指南
算法·.net
Omics Pro1 小时前
1个月2轮融资!长寿虚拟细胞
数据库·人工智能·算法·机器学习·自然语言处理
residual_fan1 小时前
航空发动机故障诊断专用智能体(六):实际机队健康管理中的应用考量与展望
人工智能·算法·数据挖掘·数据分析
hansang_IR1 小时前
【讲解】CSP-S2026 第一轮初赛
c++·算法
hanlin031 小时前
刷题笔记:力扣第76题-最小覆盖子串
笔记·算法·leetcode
Zane19941 小时前
比较排序跑不出O(nlogn)是数学上证明过的,计数、桶、基数排序凭什么能绕开这堵墙
算法·排序算法
tiger8651 小时前
大语言模型面试和题解,梳理中国主流开源 LLM 系列的发展脉络、技术路线与工程取舍
人工智能·gpt·深度学习·算法·自然语言处理·面试·transformer
我不会起名字3222 小时前
一天一道力扣Hot100(36):深度优先算法---组合总和
数据结构·c++·后端·python·算法·go