Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool check(int i, vector<int>&a){
- if(i == 0){
- if(a[0]<=a[1])
- return true;}
- else
- if(a.size()-1 == i){
- if(a[a.size()-1]>=a[a.size()-2])
- return true;}
- else
- if(a[i-1]<=a[i] && a[i+1]>=a[i])
- return true;
- return false;
- }
- void update_balans(int i, vector<int>&a,vector<bool>&b,int &balans){
- if(check(i,a)){
- if(!b[i]){
- balans++;
- b[i]=true;
- }
- }else{
- if(b[i]){
- balans--;
- b[i]=false;
- }
- }
- }
- int main()
- {
- // ifstream file("data.txt");
- int n,q;
- cin >> n >> q;
- vector<int>a(n);
- vector<bool>b(n,false);
- for(int i =0;i<n;++i){
- cin >> a[i];
- }
- int balans = 0;
- for(int i = 0;i<n;++i){
- if(check(i,a)){
- balans++;
- b[i] = true;
- }
- }
- /*for(int i =0;i<n;++i){
- cout << a[i]<< " " ;
- }
- cout<<endl;
- for(int i =0;i<n;++i){
- cout << b[i]<<" ";
- }
- cout<<endl<<balans;
- */
- int c1,c2;
- for(int i =0 ; i < q ;++i){
- cin>>c1>>c2;
- c1--;
- c2--;
- swap(a[c1],a[c2]);
- if(c1 == 0 ){
- update_balans(c1,a,b,balans);
- update_balans(c1+1,a,b,balans);
- }else
- if(c1 == a.size()-1){
- update_balans(c1,a,b,balans);
- update_balans(c1-1,a,b,balans);
- }else{
- update_balans(c1,a,b,balans);
- update_balans(c1-1,a,b,balans);
- update_balans(c1+1,a,b,balans);
- }
- if(c2 == 0 ){
- update_balans(c2,a,b,balans);
- update_balans(c2+1,a,b,balans);
- }else
- if(c2 == a.size()-1){
- update_balans(c2,a,b,balans);
- update_balans(c2-1,a,b,balans);
- }else{
- update_balans(c2,a,b,balans);
- update_balans(c2-1,a,b,balans);
- update_balans(c2+1,a,b,balans);
- }
- if(balans == a.size()){
- cout<<"Sorted!"<<endl;
- }else
- cout<<"Unsorted!"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement