跳到正文

题解

题解:AT_abc457_g [ABC457G] Catch All Apples

NoneOfTheAbove约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月14日
篇幅
约 3 分钟

版本

  1. v1当前

luogu link

atcoder link

Solution

神秘题目。

一个机器人想从第 jj 个苹果移动到第 ii 个苹果(Tj<TiT_j < T_i),就需要满足 ∣Xi−Xj∣≤Ti−Tj|X_i - X_j| \le T_i - T_j,解方程得到:

{Xi−Xj≤Ti−TjXj−Xi≤Ti−Ti\begin{cases} X_i - X_j \le T_i - T_j&\\ X_j - X_i \le T_i - T_i& \end{cases}

,移项可得到:

{Tj−Xj≤Ti−XiTj+Xj≤Ti+Xi\begin{cases} T_j- X_j \le T_i - X_i&\\ T_j + X_j \le T_i + X_i& \end{cases}

把机器人和苹果看成二维平面上的点,坐标为 (Ti−Xi,Ti+Xi)(T_i - X_i, T_i + X_i),则一个机器人可以从苹果 jj 移动到苹果 ii 当且仅当苹果 jj 的坐标在苹果 ii 的左下角,后面的就是求最少用多少个反链能覆盖这个图,不多说了,复杂度 O(nlog⁡n)O(n \log n)。

Code

code
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=3e5+1;
const i128 Inf=1e35;
ll n,t,x;
struct point
{
    ll x,y;
    bool operator<(point p) const
    {
        return ((x==p.x)?(y<p.y):(x<p.x));
    }
}a[maxn];
multiset<ll> ms;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>t>>x;
        a[i].x=t-x,a[i].y=t+x;
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        auto it=ms.upper_bound(a[i].y);
        if(it==ms.begin()) ms.insert(a[i].y);
        else
        {
            it--;
            ms.erase(it);
            ms.insert(a[i].y);
        }
    }
    cout<<ms.size();
}
原文