Language: C++

#include <bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
const int LIM = 3e5+3;
int cost[LIM+3], d[LIM+3];
ll INF = 1e18;
ll Ans = 0;

void solve(int l,int r)
{
  if(l >= r)return;
  int mid = (l+r)/2;
  vector<pair<int,ll> > V,W;
  int mx = 0; ll cur = 0;
  for(int i = mid+1;i<=r;i++)
  {
    cur += cost[i];
    if(abs(d[i]-d[i-1]) > mx)
      mx = abs(d[i]-d[i-1]), V.push_back(mp(mx,cur));
    else 
      V[V.size()-1].S = min(V.back().S, cur);
  }
  for(int i = 1; i<V.size();i++)
    V[i].S = min(V[i].S, V[i-1].S);
  mx = 0, cur = cost[mid];
  W.push_back(mp(0,cur));
  for(int i = mid-1;i>=l;i--)
  {
    cur += cost[i];
    if(abs(d[i+1]-d[i]) > mx)
      mx = abs(d[i+1]-d[i]), W.push_back(mp(mx,cur));
    else 
      W[W.size()-1].S = min(W.back().S, cur);
    int ind = upper_bound(V.begin(), V.end(), mp(mx,0ll))-V.begin();
    ind--;
    if(ind < 0)continue;
    Ans = min(Ans, cur+1ll*mx*mx+V[ind].S); 
  }
  for(int i = 1; i<W.size();i++)
    W[i].S = min(W[i].S, W[i-1].S);
  mx = 0, cur = 0;
  for(int i = mid+1;i<=r;i++)
  {
    cur += cost[i];
    mx = max(mx,abs(d[i]-d[i-1]));
    int ind = upper_bound(W.begin(), W.end(), mp(mx,0ll))-W.begin();
    ind--;
    if(ind < 0)continue;
    Ans = min(Ans, cur+1ll*mx*mx+W[ind].S); 
  }
  solve(l, mid);
  solve(mid+1,r);
}
int arr[LIM+3];

int main(){
    int n;cin >> n;
    for(int i  =0;i<n; i++)
      cin >> arr[i];
    for(int i  = 0;i<n;i++)
      for(int j = i+1;j<n;j++)
      {
        swap(arr[i],arr[j]);
        bool ok = true;
        for(int i = 1;i<n;i++)
          if(arr[i] <= arr[i-1])ok = false;
        if(ok){puts("YES");return 0;}
        swap(arr[i],arr[j]);
      }
    puts("NO");
}#include <bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
const int LIM = 3e5+3;
int cost[LIM+3], d[LIM+3];
ll INF = 1e18;
ll Ans = 0;

void solve(int l,int r)
{
  if(l >= r)return;
  int mid = (l+r)/2;
  vector<pair<int,ll> > V,W;
  int mx = 0; ll cur = 0;
  for(int i = mid+1;i<=r;i++)
  {
    cur += cost[i];
    if(abs(d[i]-d[i-1]) > mx)
      mx = abs(d[i]-d[i-1]), V.push_back(mp(mx,cur));
    else 
      V[V.size()-1].S = min(V.back().S, cur);
  }
  for(int i = 1; i<V.size();i++)
    V[i].S = min(V[i].S, V[i-1].S);
  mx = 0, cur = cost[mid];
  W.push_back(mp(0,cur));
  for(int i = mid-1;i>=l;i--)
  {
    cur += cost[i];
    if(abs(d[i+1]-d[i]) > mx)
      mx = abs(d[i+1]-d[i]), W.push_back(mp(mx,cur));
    else 
      W[W.size()-1].S = min(W.back().S, cur);
    int ind = upper_bound(V.begin(), V.end(), mp(mx,0ll))-V.begin();
    ind--;
    if(ind < 0)continue;
    Ans = min(Ans, cur+1ll*mx*mx+V[ind].S); 
  }
  for(int i = 1; i<W.size();i++)
    W[i].S = min(W[i].S, W[i-1].S);
  mx = 0, cur = 0;
  for(int i = mid+1;i<=r;i++)
  {
    cur += cost[i];
    mx = max(mx,abs(d[i]-d[i-1]));
    int ind = upper_bound(W.begin(), W.end(), mp(mx,0ll))-W.begin();
    ind--;
    if(ind < 0)continue;
    Ans = min(Ans, cur+1ll*mx*mx+W[ind].S); 
  }
  solve(l, mid);
  solve(mid+1,r);
}
int arr[LIM+3];

int main(){
    int n;cin >> n;
    for(int i  =0;i<n; i++)
      cin >> arr[i];
    for(int i  = 0;i<n;i++)
      for(int j = i+1;j<n;j++)
      {
        swap(arr[i],arr[j]);
        bool ok = true;
        for(int i = 1;i<n;i++)
          if(arr[i] <= arr[i-1])ok = false;
        if(ok){puts("YES");return 0;}
        swap(arr[i],arr[j]);
      }
    puts("NO");
}

Compiler output:
a.cc:74:2: error: stray '#' in program
   74 | }#include <bits/stdc++.h>
      |  ^
a.cc:74:3: error: 'include' does not name a type
   74 | }#include <bits/stdc++.h>
      |   ^~~~~~~
a.cc:80:11: error: redefinition of 'const int LIM'
   80 | const int LIM = 3e5+3;
      |           ^~~
a.cc:7:11: note: 'const int LIM' previously defined here
    7 | const int LIM = 3e5+3;
      |           ^~~
a.cc:81:5: error: redefinition of 'int cost [300006]'
   81 | int cost[LIM+3], d[LIM+3];
      |     ^~~~
a.cc:8:5: note: 'int cost [300006]' previously declared here
    8 | int cost[LIM+3], d[LIM+3];
      |     ^~~~
a.cc:81:18: error: redefinition of 'int d [300006]'
   81 | int cost[LIM+3], d[LIM+3];
      |                  ^
a.cc:8:18: note: 'int d [300006]' previously declared here
    8 | int cost[LIM+3], d[LIM+3];
      |                  ^
a.cc:82:4: error: redefinition of 'long long int INF'
   82 | ll INF = 1e18;
      |    ^~~
a.cc:9:4: note: 'long long int INF' previously defined here
    9 | ll INF = 1e18;
      |    ^~~
a.cc:83:4: error: redefinition of 'long long int Ans'
   83 | ll Ans = 0;
      |    ^~~
a.cc:10:4: note: 'long long int Ans' previously defined here
   10 | ll Ans = 0;
      |    ^~~
a.cc:85:6: error: redefinition of 'void solve(int, int)'
   85 | void solve(int l,int r)
      |      ^~~~~
a.cc:12:6: note: 'void solve(int, int)' previously defined here
   12 | void solve(int l,int r)
      |      ^~~~~
a.cc:130:5: error: redefinition of 'int arr [300006]'
  130 | int arr[LIM+3];
      |     ^~~
a.cc:57:5: note: 'int arr [300006]' previously declared here
   57 | int arr[LIM+3];
      |     ^~~
a.cc:132:5: error: redefinition of 'int main()'
  132 | int main(){
      |     ^~~~
a.cc:59:5: note: 'int main()' previously defined here
   59 | int main(){
      |     ^~~~