有一个n×m的网格,小A在(x, y), 有另外k个人,第i个人在(xi, yi), 每一秒每个人必须移动到相邻的格子,判断是否存在一个时刻小A和至少一个人在同一个格子。

这段C++代码是用于解决一个抓人游戏问题,判断第i个人能否抓住小A,条件是其与小A在x轴和y轴上的绝对差之和为偶数。如果满足这个条件的人数超过1,表示无法抓住小A;否则可以抓住。

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

题目

思路:独立地看第i个人,当且仅当(abs(x[i] - x) + abs(y[i] - y)) % 2 == 0, 第i个人才能抓到小A

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 1e6 + 5, inf = 1e18, maxm = 4e4 + 5, mod = 1e9 + 7, N = 1e6;
int a[maxn], b[maxn];
// bool vis[maxn];
int n, m;
int vis[105][105] = {0};
string s;

void solve(){
    int res = 0;
    int q, k;
    int x0, y0;
    int x[105], y[105];
    cin >> n >> m >> k;
    cin >> x[0] >> y[0];
    int cnt = 0;
    for(int i = 1; i <= k; i++){
        cin >> x[i] >> y[i];
        int dx = abs(x[0] - x[i]);
        int dy = abs(y[0] - y[i]);
        if((dx + dy) % 2 == 0){
            cnt++;
        }
    }
    if(cnt >= 1){
        cout << "No\n";
    }
    else cout << "Yes\n";
    
}
    
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

``` #include<bits/stdc++.h> using namespace std; #define ll long long const int N=2000+5; struct lianb{ int n; int a[N]; int ne[N]; int pr[N]; lianb(){ for(int i=1;i<N;++i){ a[i]=ne[i]=pr[i]=0; } } int pre(int x,int y){ y-=a[x]; while(y>0){ x=pr[x]; y-=a[x]; } return x; } int nex(int x,int y){ y-=a[x]; while(y>0){ x=ne[x]; y-=a[x]; } return x; } int pr_len(int x){ return x-pr[x]; } int ne_len(int x){ return ne[x]-x; } int run(int wn,int k){ n=wn; int la=0; pr[0]=0; a[0]=a[n+1]=1e9; for(int i=1;i<=n;++i){ if(a[i]!=0){ ne[la]=i; pr[i]=la; la=i; } } ne[la]=ne[n+1]=n+1; int ans=0; for(int l=ne[0];l<=n;l=ne[l]){ ans+=pr_len(l)*(n-nex(l,k)+1); } return ans; } }; int n,m,w,k; vector<int>v[N]; lianb ls; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>w>>k; for(int i=1;i<=w;++i){ int x,y; cin>>x>>y; v[x].push_back(y); } ll ans=0; for(int i=1;i<=n;++i){ ls=lianb(); for(int j=i;j<=n;++j){ for(int x:v[j]){ ++ls.a[x]; } } int wp=ls.run(m,k); for(int j=n;j>=i;--j){ ans+=wp; for(int x:v[j]){ for(int l=ls.pre(x,k);l<=x;l=ls.ne[l]){ wp-=ls.pr_len(l)*ls.ne_len(ls.nex(l,k)); } } for(int x:v[j]){ --ls.a[x]; } } } cout<<ans<<endl; return 0; }```debug 保罗在交响乐团。弦乐部分排列成一个 r × c 矩形网格,除了 n 位中提琴手外,其他都是小提琴手。保罗非常喜欢中提琴,所以他想拍一张照片,至少要包括 k 位中提琴手。保罗可以拍摄交响乐团中的任何轴平行矩形。请计算保罗可以拍摄的可能照片数量。 如果对应矩形的坐标不同,则认为两张照片是不同的。 输入 输入的第一行包含四个用空格分隔的整数 r, c, n, k (1 ≤ r, c, n ≤ 3000, 1 ≤ k ≤ min(n, 10)) — 弦乐部分的行数列数,中提琴的总数,保罗想要照片中至少包括的中提琴的数量。 接下来的 n 行,每行包含两个整数 xi yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c):第 i 位中提琴的位置。保证输入中没有重复的位置。 输出 输出一个整数 — 保罗可以拍摄的照片数量,其中至少包括 k 位中提琴手。 Verdict: WRONG_ANSWER Input 10 10 10 4 7 9 8 1 5 7 6 8 6 4 5 8 4 3 1 3 5 1 6 9 Participant's output 598 Jury's answer 619 Checker comment wrong answer 1st numbers differ - expected: '619', found: '598'
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

__night_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值