Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream fin("pachete.in");
- ofstream fout("pachete.out");
- int n,a[10002],s,k,i,sol[10002],y,pozg,ok;
- int valid (int n)
- {
- if (a[n+1]!=0) return 0;
- for(int i=1; i<=n; i++)
- if (a[i]!=i) return 0;
- return 1;
- }
- int main ()
- {
- fin>>n;
- k=1;
- for (i=1; i<=n; i++)
- fin>>a[i];
- pozg=n+1;
- a[pozg]=0;
- while (valid(n)==0)
- {
- if (pozg==n+1)
- {
- ok=0;
- for (i=1; i<=n; i++)
- if (a[i]!=i and ok==0)
- {
- swap(a[pozg],a[i]);
- sol[k++]=i;
- sol[k++]=pozg;
- pozg=i;
- ok=1;
- }
- }
- else
- for (i=1; i<=n+1; i++)
- if (a[i]==pozg)
- {
- swap(a[i],a[pozg]);
- sol[k]=i;
- k++;
- sol[k]=pozg;
- k++;
- pozg=i;
- }
- }
- fout<<k/2<<endl;
- for (i=1; i<=k-1; i++)
- {
- if (i%2!=0 and i>=2) fout<<endl;
- fout<<sol[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement