跳到正文

题解

题解:CF2173B Niko's Tactical Cards

qinglin_zcr约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年12月7日
收录
2025年12月12日
更新
2026年8月21日
篇幅
约 2 分钟
题目
CF2173B

版本

  1. v2当前
  2. v1

若要使用红卡使结果最大,则 kk 应尽可能大。

若要使用蓝卡使结果最大,则 kk 应尽可能小。

容易想到只需考虑 kk 最大和最小的情况即可。

证明:

称 kk 最大值为 mxmx,最小值为 mnmn,

若有 mn<k<mxmn<k<mx

则对于红卡:

mx−ai>k−aimx-a_i>k-a_i

对于蓝卡:

bi−mn>bi−kb_i-mn>b_i-k

最小值同理。

Code:

C++
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
inline int read(){
    int x=0;bool f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=gc();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=gc();
    return f?x:-x;
}
const int N=1e6+5;
int a[N],b[N];
signed main(){
    int T=read();
    while(T--){
        int n=read();
        for(int i=1;i<=n;i++)a[i]=read();
        for(int i=1;i<=n;i++)b[i]=read();
        int mx=0,mn=0;
        for(int i=1;i<=n;i++){
            int nmx=max(mx-a[i],b[i]-mn);
            int nmn=min(mn-a[i],b[i]-mx);
            mx=nmx,mn=nmn;
        }
        printf("%lld\n",mx);
    }
    return 0;
}
原文