Language: C++


#include <bits/stdc++.h>
using namespace std;

int main()
{
	int  a;
	vector<int>p(a);
 	cin >> a >> b ;
	for (int i = 0; i < a ; i++)
	{
		cin >> p[i];
	}
	
	if(is_sorted(p.begin,p.end)){
		cout << "YES" << endl; 
	}else{
		int first = -1;
		int second = -1;
		for(int i = 1; i <= a; i++){
			if(first == -1 && p[i-1]!= i)
				first = i-1;
			if(second == -1 && p[i-1]!= i){
				second = i-1;
				break;
			}
		}
		swap(p[first],p[second]);
		if(is_sorted(p.begin,p.end)){
			cout << "YES" << endl;
		}else{
			cout << "NO" << endl;
		}
	}

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:9:21: error: 'b' was not declared in this scope
    9 |         cin >> a >> b ;
      |                     ^
a.cc:15:21: error: no matching function for call to 'is_sorted(<unresolved overloaded function type>, <unresolved overloaded function type>)'
   15 |         if(is_sorted(p.begin,p.end)){
      |            ~~~~~~~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algo.h:3191:5: note: candidate: 'template<class _FIter> bool std::is_sorted(_FIter, _FIter)'
 3191 |     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
      |     ^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3191:5: note:   template argument deduction/substitution failed:
a.cc:15:21: note:   couldn't deduce template parameter '_FIter'
   15 |         if(is_sorted(p.begin,p.end)){
      |            ~~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3206:5: note: candidate: 'template<class _FIter, class _Compare> bool std::is_sorted(_FIter, _FIter, _Compare)'
 3206 |     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3206:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:390:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> std::is_sorted(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Compare)'
  390 | is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
      | ^~~~~~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:390:1: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:394:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> std::is_sorted(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator)'
  394 | is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
      | ^~~~~~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:394:1: note:   candidate expects 3 arguments, 2 provided
a.cc:29:29: error: no matching function for call to 'is_sorted(<unresolved overloaded function type>, <unresolved overloaded function type>)'
   29 |                 if(is_sorted(p.begin,p.end)){
      |                    ~~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3191:5: note: candidate: 'template<class _FIter> bool std::is_sorted(_FIter, _FIter)'
 3191 |     is_sorted(_ForwardIterator __first, _ForwardIterator __last)
      |     ^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3191:5: note:   template argument deduction/substitution failed:
a.cc:29:29: note:   couldn't deduce template parameter '_FIter'
   29 |                 if(is_sorted(p.begin,p.end)){
      |                    ~~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3206:5: note: candidate: 'template<class _FIter, class _Compare> bool std::is_sorted(_FIter, _FIter, _Compare)'
 3206 |     is_sorted(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3206:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:390:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> std::is_sorted(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Compare)'
  390 | is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp);
      | ^~~~~~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:390:1: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:394:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> std::is_sorted(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator)'
  394 | is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
      | ^~~~~~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:394:1: note:   candidate expects 3 arguments, 2 provided