LDU training game: racing thinking

Title description

analysis

We first sort the array to form a non-decreasing sequence, and then we need to form the maximum value, then we require the minimum value + n, the maximum value +1, and then we maintain the maximum value on the left side of each number, the maximum value on the right side, and then To enumerate each digit, if the number is required to be the largest, then +n is required. First, it must be greater than each digit on the left, and secondly, it must be greater than the maximum value of -1 on the right, because its n is replaced by With the current value, he can only +n-1

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 = 300010;
int a[N];
int b[N];
int lm[N];
int rm[N];
int n;

bool cmp(int x,int y){
    
    
    return x > y;
}

int main(){
    
    
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
    sort(a + 1,a + 1 + n,cmp);
    for(int i = 1;i <= n;i++){
    
    
        b[i] = a[i] + i;
    }
    for(int i = 1;i <= n;i++) lm[i] = max(lm[i - 1],b[i]);
    for(int i = n;i;i--) rm[i] = max(rm[i + 1],b[i]);
    int ans = 0;
    for(int i = 1;i <= n;i++)
        if(a[i] + n >= lm[i] && a[i] + n >= rm[i] - 1) ans++;
    printf("%d\n",ans);
    return 0;
}

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

Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112976243