Codeforces Round #703 (Div. 2)补题

本文讲述了在一维奇偶选择策略的基础上,作者扩展到了二维情况,并介绍了如何利用曼哈顿距离解决一场比赛中的交互问题,通过二分查找策略找到最大值的位置。最后强调了比赛中的清醒思维对解决问题的重要性。

前言

一题自闭,掉大分。

题目

A

注意只能后移不能前移即可。

B

之前学长讲过一维推广到二维的情况,赛时也回想到了,但是没想到曼哈顿距离也能推广。算是个结论吧。

一维:奇数取中位数,偶数取两个中位数之间的任何位置都可以。

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-11-26 09:12:46
 * @LastEditTime: 2021-02-19 15:31:36
 * @Description: 栽种绝处的花
 */
#include <bits/stdc++.h>
using namespace std;

int x[1010];
int y[1010];

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> x[i] >> y[i];
        sort(x + 1, x + 1 + n);
        sort(y + 1, y + 1 + n);
        if (n & 1)
            cout << 1 << endl;
        else
            cout << 1ll * (abs(x[n / 2] - x[n / 2 + 1]) + 1) * (abs(y[n / 2] - y[n / 2 + 1]) + 1) << endl;
    }

    return 0;
}

C2

交互题限制次数。1e5 20次,显然二分。

赛时炸了脑子不够清醒,在那码了删,删了码也不知道码了些啥,比赛一结束理了一下思路就知道正解了。

先查询一次[1,n],得到position

  • position==1,那么最大值肯定在[2,n],对这个区间进行二分,每次查询[1,mid],如果结果不为1,那么说明最大值在[mid+1,n],否则最大值在[2,mid]
  • position==n,那么最大值肯定在[1,n-1],对这个区间进行二分,每次查询[mid,n],如果结果不为n,那么说明最大值在[1,mid-1],否则最大值在[mid,n-1]
  • position在[2,n-1],我们再查询一次[1,position],如果结果还是position,那么最大值在区间[1,position],我们把position看做n,按照情况二处理即可。否则把position看做1,按照情况一处理。

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-11-26 09:12:46
 * @LastEditTime: 2021-02-19 15:39:29
 * @Description: 栽种绝处的花
 */
#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n;
    cin >> n;
    int pre = n, ans, flag = 1, res;
    cout << "? " << 1 << ' ' << n << endl;
    cout.flush();
    cin >> pre;
    if (n == 2)
    {
        int l = 1, r = 2;
        cout << "? " << l << ' ' << r << endl;
        cout.flush();
        cin >> pre;
        if (pre == l)
            return cout << "! " << r << endl, 0;
        else
            return cout << "! " << l << endl, 0;
    }
    if (pre == 1) //情况1
    {
        int l = 2, r = n;
        ans = n;
        while (l <= r)
        {
            int mid = l + r >> 1;
            cout << "? " << pre << ' ' << mid << endl;
            cout.flush();
            int temp;
            cin >> temp;
            if (temp != pre)
                l = mid + 1;
            else
            {
                r = mid - 1;
                ans = mid;
            }
        }
    }
    else if (pre == n) //情况2
    {
        int l = 1, r = n - 1;
        ans = 1;
        while (l <= r)
        {
            int mid = l + r >> 1;
            cout << "? " << mid << ' ' << pre << endl;
            cout.flush();
            int temp;
            cin >> temp;
            if (temp != pre)
                r = mid - 1;
            else
            {
                l = mid + 1;
                ans = mid;
            }
        }
    }
    else //情况3
    {
        int l = 1, r = n;
        int re1, re2;
        cout << "? " << 1 << ' ' << pre << endl;
        cout.flush();
        cin >> re1;

        if (re1 == pre) //类比情况2
        {
            int l = 1, r = pre - 1;
            ans = 1;
            while (l <= r)
            {
                int mid = l + r >> 1;
                cout << "? " << mid << ' ' << pre << endl;
                cout.flush();
                int temp;
                cin >> temp;
                if (temp != pre)
                    r = mid - 1;
                else
                {
                    l = mid + 1;
                    ans = mid;
                }
            }
        }
        else //类比情况1
        {
            int l = pre + 1, r = n;
            ans = r;
            while (l <= r)
            {
                int mid = l + r >> 1;
                cout << "? " << pre << ' ' << mid << endl;
                cout.flush();
                int temp;
                cin >> temp;
                if (temp != pre)
                    l = mid + 1;
                else
                {
                    r = mid - 1;
                    ans = mid;
                }
            }
        }
    }
    cout << "! " << ans << endl;
    return 0;
}

打比赛时头脑还是不够清醒,得改掉这个坏习惯。

### Codeforces Round 703 Div. 2 比赛题解与总结 #### A. Adjacent Replacements Problem 对于此问题,给定一个长度为 \(n\) 的整数数组 \(a_1, a_2, \ldots , a_n\) 和一个正整数 \(k\). 如果存在一对相邻位置 \(i,i+1\) (\(1≤i<n\)),使得 \(|a_i−a_{i+1}|>k\) 则可以执行一次操作:选择任意一对这样的相邻元素并使它们相等。目标是最少的操作次数让所有的差值不超过 \(k\)[^1]。 解决方法是遍历整个数组,计算每对相邻元素之间的差异,并判断这些差异是否超过了 \(k\) 。如果超过,则记录下需要调整的位置数量作为最终的结果返回。 ```cpp #include <iostream> using namespace std; void solveA(){ int t; cin >> t; while(t--){ int n,k; cin>>n>>k; vector<int>a(n); for(auto& x:a){ cin>>x; } bool flag=true; int cnt=0; for(int i=0;i<n-1;++i){ if(abs(a[i]-a[i+1])>k){ ++cnt; flag=false; } } cout<<(flag?"Yes":"No")<<endl; } } ``` #### B. Minimum Grid Path 该题目要求在一个无限大小的网格上找到一条从起点到终点的最短路径,在移动过程中某些格子可能被标记为障碍物不可通过。为了最小化步数,应当优先考虑横向或纵向直线前进直到遇到阻碍为止;之后转向其他方向继续前行直至抵达目的地[^2]。 实现思路在于模拟行走过程中的决策逻辑——当面临多个可行的选择时总是挑选能够最快接近目标的那个选项。具体做法可以通过广度优先搜索算法来完成。 ```cpp // 假设此处省略了完整的B题解答代码... ``` #### C. Maximum width of Binary Tree 这个问题涉及到二叉树的最大宽度定义以及如何有效地求得这一数值。最大宽度是指某一层拥有最多的节点数目。一种有效的策略是从根部开始逐层遍历整棵树结构,利用队列辅助存储每一级待访问结点的信息,从而轻松统计各层的实际规模并找出其中最大的那个层次所含有的节点总数即为我们所需的答案[^3]。 上述三种不同类型的挑战展示了编程竞赛中常见的几种思考模式和技术手段的应用实例。无论是简单的贪心法还是较为复杂的图论模型构建都体现了选手们灵活运用基础知识解决问题的能力。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值