Educational Codeforces Round 103 C

C. Longest Simple Cycle

显然针对ab相等的话 那我们就不能再往前走了
所以我们考虑分为几个层
我们考虑如何求出一个层的最长环

我们观察这个红色的环
显然我们正着做 反着做都是可以的 我们就正着做把
但是每次到一层 我们可以考虑两件事
第一就是继续往后走 我们就在现在环+c[i]-|a[i+1]-b[i+1]|+1就是我们现在的环的权重
当然我们也可以在该层不要之前的权重 重新从中间再往后走就是res=max(res,|a[i+1]-b[i+1]|+1)
第二种就是我们直接不往后走了 直接就在该层闭环 我们直接更新答案就可以了 ans=max(ans,res+c[i])
这样就做完了

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
void solve(){
int n;cin>>n;
vector<int>c(n+10),a(n+10),b(n+10);
for(int i=1;i<=n;i++)cin>>c[i];
for(int i=1;i<=n;i++)cin>>a[i];
vector<int>v;
v.push_back(1);
for(int i=1;i<=n;i++){
cin>>b[i];
if(i!=1&&a[i]==b[i])v.push_back(i-1);
}
v.push_back(n);
int ans=0,res;
for(int i=0;i<v.size()-1;i++){
int l=v[i],r=v[i+1];
i == 0 ? res = abs(a[2] - b[2])+1: res = 1;
while(l<r) {
l++;
ans = max(ans, res + c[l]);
if(l!=r)res += c[l] - abs(a[l + 1] - b[l + 1])+1;
res=max(res,abs(a[l + 1] - b[l + 1])+1);
ans = max(ans, res);
}
}
cout<<ans<<endl;
}
signed main(){
fast
int t;t=1;cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}
posted @   ycllz  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示