LDU training match: small srf game monotonous queue + DP

Title description

Insert picture description here

analysis

Good question,
first we define the array f[n][3];
f[i][1] represents the optimal strategy when srf starts from the i + 1 digit, f[i][2] represents qtc from the i The optimal strategy when + 1 number indicates the final score.
If srf is currently selected, it means that qtc has selected a number in i + 1~i + m, and the final result must be the smallest according to optimality. , So we need to find the smallest one, and then +a[i], otherwise, find the largest one.
Use priority queues or line segment trees to maintain the best value, and optimize the cycle.

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 200010;
ll a[N];
ll f[N][3];
int q1[N],hh1,tt1;
int q2[N],hh2,tt2;
ll s1[N],s2[N];
int n,m;

void push1(ll x,int i){
    
    
	while (hh1 <= tt1 && s1[tt1] >= x) --tt1;
	s1[++tt1] = x,q1[tt1] = i;
}
void push2(ll x,int i){
    
    
	while (hh2 <= tt2 && s2[tt2] <= x) --tt2;
	s2[++tt2] = x,q2[tt2] = i;
}
void pop1(int i) {
    
    while (q1[hh1] >= i) ++hh1;}
void pop2(int i) {
    
    while (q2[hh2] >= i) ++hh2;}


int main(){
    
    
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++) scanf("%lld",&a[i]);
    hh1 = 0,tt1 = -1;
    hh2 = 0,tt2 = -1;
    push1(-a[n],n);
    push2(a[n],n);
    for(int i = n - 1;~i;i--){
    
    
        f[i][1] = s2[hh2];
        f[i][2] = s1[hh1];
        pop1(i + m);
        pop2(i + m);
        push1(f[i][1] - a[i],i);
        push2(f[i][2] + a[i],i);
    }
    printf("%lld\n",f[0][1]);
    printf("%lld\n",f[0][2]);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112976251
Recommended