Cod sursa(job #794780)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
ifstream f("dame.in");
ofstream g("dame.out");
int n, i, j, aux;
vector <int> pare, impare;
//If the remainder from dividing N by 6 is not 2 or 3 then the list is simply all even numbers followed by all odd numbers <= N
//Otherwise, write separate lists of even and odd numbers (i.e. 2,4,6,8 - 1,3,5,7)
//If the remainder is 2, swap 1 and 3 in odd list and move 5 to the end (i.e. 3,1,7,5)
//If the remainder is 3, move 2 to the end of even list and 1,3 to the end of odd list (i.e. 4,6,8,2 - 5,7,9,1,3)
//Append odd list to the even list and place queens in the rows given by these numbers, from left to right
f>>n;
for(i=2; i<=n; i+=2) pare.push_back(i);
for(i=1; i<=n; i+=2) impare.push_back(i);
if(n==1 || n==2) {
g<<"1\n1 1\n";
return 0;
}
if(n==3) {
g<<"2\n1 1\n2 3\n";
return 0;
}
g<<n<<"\n";
if(n%6!=2 && n%6!=3) {
j = 0;
for(i=0; i<pare.size(); i++) j++, g<<j<<" "<<pare[i]<<"\n";
for(i=0; i<impare.size(); i++) j++, g<<j<<" "<<impare[i]<<"\n";
return 0;
}
if(n%6==2) {
aux = impare[0]; //aici se afla 1
impare[0] = impare[1]; //aici se afla 3
impare[1] = aux;
impare[2] = -1; //aici se afla 5
impare.push_back(5); //il pun pe 5 la capat
}
if(n%6==3) {
pare[0] = -1; //aici se afla 2
pare.push_back(2); //il pun pe 2 la capat
impare[0] = -1;
impare[1] = -1;
impare.push_back(1);
impare.push_back(3);
}
vector <int> sol;
for(i=0; i<pare.size(); i++) if(pare[i]!=-1) sol.push_back(pare[i]);
for(i=0; i<impare.size(); i++) if(impare[i]!=-1) sol.push_back(impare[i]);
for(i=0; i<sol.size(); i++) g<<i+1<<" "<<sol[i]<<"\n";
return 0;
}