Codeforces Round 1049 (Div. 2)C. Ultimate Value

经过分析可知,游戏最多持续俩回合就必须结束是最优的

计算增量:

一共有5种case:

  1. alice直接end,没有可以改变的空间
  2. '-' '-'互换 只需要计算r-l即可,可以单独处理,区分n是奇数和偶数的情况
  3. '+' '+'互换
  4. '-' '+'互换 需要你计算derta: 这种case是:+2al -2 ar +r-l:---->变形后 (2al-l)-(2ar-r);因为左边是先迭代,我们可以计算前i个的是偶数的时候最大的L式,当遍历到奇数位时,在使用mx求最大值
  5. '+' '-'互换 case5只需要颠倒即可, -2al-l +2 ar+r:---->变形后 -(2al+l)+(2ar+ar),遍历到奇数位时只需要求al的最小值,遍历到偶数位时,求mx维护最大值即可

点击查看代码

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;           // constexpr 0LL
using ull = unsigned long long; // constexpr 0ULL
using ui = unsigned int;        // constexpr U
using ld = long double;         // constexpr 0.0L
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define endl '\n'
#define inf 0x3f3f3f3f
#define xx first
#define yy second
#define lowbit(x) x & -x
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
const ll linf = 1e18;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
void solved()
{
    int n;
    cin >> n;
    vector<ll> a(n + 1);
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    ll mx=-linf,mx2=-linf,mn1=linf;
    ll ans = 0;
    for(int i=1;i<=n;i++){
        if(i%2){
            ans += a[i];
            mn1 = min(mn1, 2 * a[i] + i);
            mx = max(mx, mx2 - 2 * a[i] + i);
        }else{
            ans -= a[i];
            mx2=max(mx2,2*a[i]-i);
            mx = max(mx, 2 * a[i] + i - mn1);
        }
    }
    if(n%2)mx=max(mx,(ll)n-1LL);
    else mx=max(mx,(ll)n-2);
    ans+=mx;
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--)
    {
        solved();
    }
    return 0;
}
相关推荐
一去不复返的通信er6 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
程序 代码狂人9 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
HDU-Dade9 小时前
【校招笔试真题】简要介绍 Self-Attention 的前沿算法
人工智能·算法
小郎君65811 小时前
Vue keep-alive 切标签后滚动位置回顶?因为 onDeactivated 里读到的 scrollTop 是 0
算法
邵奈一12 小时前
GPT-6.1 Terra速度是Sol的3倍,价格减半
算法
RaaS10012 小时前
Node.js Brotli RCE 漏洞自查指南:fetch、undici、axios 都在影响面,三分钟确认你的版本
人工智能·深度学习·算法
shehuiyuelaiyuehao12 小时前
算法56,重排链表
数据结构·算法·链表
qq_4523962313 小时前
第四篇:《索引原理与实战:B-tree、Hash、GIN、GiST、BRIN》
算法·哈希算法·gin
西索斯coding13 小时前
Qwen3-Max 与 Qwen-Plus:60 个真实修复任务评测方案
大数据·网络·算法·ai