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;
}
相关推荐
Kisorge35 分钟前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
铭哥的编程日记2 小时前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区2 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
cici158742 小时前
基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现
算法·matlab·最小二乘法
StarPrayers.3 小时前
自蒸馏学习方法
人工智能·算法·学习方法
大锦终3 小时前
【动规】背包问题
c++·算法·动态规划
智者知已应修善业4 小时前
【c语言蓝桥杯计算卡片题】2023-2-12
c语言·c++·经验分享·笔记·算法·蓝桥杯
hansang_IR4 小时前
【题解】洛谷 P2330 [SCOI2005] 繁忙的都市 [生成树]
c++·算法·最小生成树
Croa-vo4 小时前
PayPal OA 全流程复盘|题型体验 + 成绩反馈 + 通关经验
数据结构·经验分享·算法·面试·职场和发展
AndrewHZ5 小时前
【图像处理基石】 怎么让图片变成波普风?
图像处理·算法·计算机视觉·风格迁移·cv