Codeforces Round 973 (Div. 2) F1. Game in Tree (Easy Version)(思维题 博弈)

题目

思路来源

乱搞ac

题解

两个人的策略是一样的,把1到u的路径标记,

如果能走旁边的链(也就是当前点,刨去标记链以外的子树中最长的链),

使得对面走剩余的连通块无法比你大,就走旁边的链,并宣告获胜

否则沿着标记链,朝对面的方向走一步

维护实际是一个区间最值,可以用set或者st表

代码

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr<<(#x)<<":"<<x<<" ";
#define dbg2(x) cerr<<(#x)<<":"<<x<<endl;
#define SZ(a) (int)(a.size())
#define sci(a) scanf("%d",&(a))
#define pt(a) printf("%d",a);
#define pte(a) printf("%d\n",a)
#define ptlle(a) printf("%lld\n",a)
#define debug(...) fprintf(stderr, __VA_ARGS__)
const int N=2e5+10;
int t,n,u,v,f[N],g[N],to;
vector<int>e[N],tmp;
set<P>my,your;
bool is[N];
void dfs(int u,int fa){
    f[u]=0;
    for(auto &v:e[u]){
        if(v==fa)continue;
        dfs(v,u);
        if(is[v])is[u]=1;
        else f[u]=max(f[u],f[v]+1);
    }
    if(u==1 || u==to)is[u]=1;
    if(is[u])tmp.pb(u);
}
int main(){
    sci(t);
    while(t--){
        sci(n);
        rep(i,1,n)e[i].clear(),is[i]=0;
        rep(i,2,n){
            sci(u),sci(v);
            e[u].pb(v);
            e[v].pb(u);
        }
        sci(to);sci(to);
        tmp.clear();
        dfs(1,0);
        rep(i,1,n)g[i]=f[i];
        tmp.clear();
        dfs(to,0);
        my.clear();your.clear();
        int cm=0,cy=0;
        for(auto &v:tmp){
            g[v]+=cm;
            cm++;
            if(v==to)break;
            my.insert(P(g[v],v));
        }
        reverse(tmp.begin(),tmp.end());
        for(auto &v:tmp){
            f[v]+=cy;
            cy++;
            if(v==1)break;
            your.insert(P(f[v],v));
        }
        reverse(tmp.begin(),tmp.end());
        int op=0,l=0,r=SZ(tmp)-1,nm=0,ny=0;
        while(SZ(my) || SZ(your)){
            if(op==0 && !SZ(my))break;
            if(op==1 && !SZ(your))break;
            if(op==0){
                int p=tmp[l],yy=SZ(your)==0?0:(*your.rbegin()).fi;
                if(g[p]>max(ny,yy)){
                    nm=g[p];
                    break;
                }
                if(l+1<r){
                    my.erase(P(g[p],p));
                    p=tmp[++l];
                    your.erase(P(f[p],p));
                    nm=l;
                }
                else{
                    nm=g[p];
                    ny=max(ny,yy);
                    break;
                }
            }
            else{
                int p=tmp[r],mm=SZ(my)==0?0:(*my.rbegin()).fi;
                if(f[p]>=max(nm,mm)){
                    ny=f[p];
                    break;
                }
                if(l+1<r){
                    your.erase(P(f[p],p));
                    p=tmp[--r];
                    my.erase(P(g[p],p));
                    ny=SZ(tmp)-1-r;
                }
                else{
                    ny=f[p];
                    nm=max(nm,mm);
                    break;
                }
            }
            op^=1;
        }
        puts(nm>ny?"Alice":"Bob");
    }
    return 0;
}

Codeforces Round 1000 Div. 2 F1 目 "Counting Is Not Fun (Easy Version)" 中,问的核心是计算满足特定条件的子数组数量,而这些子数组的元素值与它们在子数组中的位置相关。 ### 目简述 目要求找到数组中满足特定条件的子数组数量。具体而言,对于一个长度为 $ n $ 的数组 $ a $,要求统计所有子数组 $ [l, r] $(其中 $ 1 \leq l \leq r \leq n $)的数量,使得对于该子数组内的每个位置 $ i $(相对于子数组的起始点 $ l $ 的偏移),满足 $ a[i] = i - l $。 ### 解思路 1. **滑动窗口方法**:可以使用滑动窗口的思想来解决这个问。我们遍历数组,尝试找到尽可能长的连续段,其中每个元素的值等于其相对于当前段起始位置的偏移量。对于每一个起始位置 $ l $,找到最大的 $ r $,使得所有 $ a[i] = i - l $ 对于 $ i \in [l, r] $ 成立。 2. **统计符合条件的子数组数量**:对于每一个满足条件的连续段,长度为 $ len $,则该段内可以形成的子数组数目为 $ len \times (len + 1) / 2 $。这是因为长度为 $ len $ 的连续段可以形成 $ len + (len - 1) + ... + 1 = len \times (len + 1) / 2 $ 个子数组。 3. **遍历数组**:通过遍历数组并维护当前段的起始位置,可以高效地找到所有符合条件的连续段并统计其对应的子数组数量。 ### 时间复杂度 该算法的时间复杂度为 $ O(n) $,因为每个元素最多被访问一次,且没有嵌套循环。 ### 示例代码 以下是一个实现该思路的 Python 示例代码: ```python n = int(input()) a = list(map(int, input().split())) count = 0 i = 0 while i < n: if a[i] != 0: i += 1 continue # 找到最长的连续段 j = i while j < n and a[j] == j - i: j += 1 length = j - i count += length * (length + 1) // 2 i = j print(count) ``` ### 代码说明 1. 首先读取输入的数组。 2. 遍历数组,寻找所有起始位置 $ i $,其中 $ a[i] = 0 $,因为这是可能的子数组起点。 3. 对于每一个起点 $ i $,找到最长的连续段 $ [i, j) $,使得所有元素满足 $ a[k] = k - i $。 4. 根据连续段的长度计算符合条件的子数组数量,并累加到最终结果中。 ### 相关优化 在实现中,需要注意避免重复计算或遗漏某些情况,例如当数组中存在多个连续段时,确保每个段都被正确分割并计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小衣同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值