Cod sursa(job #794771)

Utilizator harababurelPuscas Sergiu harababurel Data 6 octombrie 2012 23:31:02
Problema Dame Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#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;
    if(n==2 || n==3) n--;   //pe dimensiune de 2x2 si 3x3 pot pune n-1 dame
    g<<n<<"\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) {
        g<<"1 1\n";
        return 0;
    }
    if(n%2!=2 && n%2!=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);
    }

    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;
}