read this code. look at the initialisation of variables l, r and lw, rw; some of the…
- User
read this code. look at the initialisation of variables l, r and lw, rw; some of the comments are wrong. please tell me the correct
#pragma GCC optimize("Ofast")
// faster generation method: use space (array of MAX_N elements) to save time /// AC
#include<bits/stdc++.h> using namespace std;
const int MAX_N = 100001;
int main() { freopen("compin.txt", "r", stdin); freopen("compout.txt", "w", stdout);
codeint n; cin >> n; int a[n]; for (int& x : a) cin >> x; int l[n], r[n]; // length of longest pyramid starting at a[i] (l -> increasing, r -> decreasing) int lw[MAX_N] = {0} , rw[MAX_N] = {0}; // length of longest pyramid starting at a[i] l[0] = 1; lw[a[0]] = 1; for (int i = 1; i < n; i++) { l[i] = lw[a[i] - 1] + 1; lw[a[i]] = l[i]; } r[n-1] = 1; rw[a[n-1]] = 1; for (int i = n-2; i >= 0; i--) { r[i] = rw[a[i] - 1] + 1; rw[a[i]] = r[i]; } int ans = 0; int len; for (int i = 0; i < n; i++) { len = min(l[i], r[i]); ans = max(ans, len); } cout << n - (2 * ans - 1) << endl;}
- Assistant
Let's analyze the code to understand the initialization of the variables
l,r,lw, andrw, as well as the comments associated with them.-
Variable
l[i]:- The comment implies that represents the length of the longest increasing sequence starting at .
-