%
% Macros for the contest problems
% for MikTeX: use latex.exe
% (C) Dmitry S. Lomov (SPb SU Training Centre), 2001-2002
% (C) Kitten Computing [Andrew Lopatin & Nick Durov], 2001-2002,2003
% (C) Andrew Stankevich (SPb IFMO Training Center), 2003,2004
%
%
% This is work in progress, please do not try to modify it.
% 
% Visit http://neerc.ifmo.ru/develop/olymp-sty/index.html for
% project details.
%

\ProvidesPackage{olymp}


\newif\if@landscape\@landscapefalse
\newif\if@russian\@russianfalse
\newif\if@arabic\@arabicfalse

\DeclareOption{landscape}{
    \@landscapetrue
}
\DeclareOption{russian}{
    \@russiantrue
}
\DeclareOption{arabic}{
    \@arabictrue
}
\ProcessOptions\relax


% -- Setup margins --
%
% Tex defines to large margins for our purposes. 
% So we redefine this to use paper more efficiently
%

\ifcase\@ptsize % 10 pt
\hoffset=-26.5mm
\voffset=-35mm
\textheight=245mm
\textwidth=175mm
\or % 11 pt
\hoffset=-25mm
\voffset=-35mm
\setlength{\textheight}{250mm}
\setlength{\textwidth}{175mm}
\or % 12 pt
\hoffset=-20mm
\voffset=-35mm
\textheight=245mm
\textwidth=175mm
\fi

\newlength{\thelinewidth}
\thelinewidth=\textwidth

\if@twocolumn
\hoffset=-14.3mm
\voffset=-38mm
\textheight=255mm
\textwidth=188mm

\thelinewidth=0.47\textwidth
\fi

% -- End of setup margins --

%---------- From package "lastpage" ------------------
\def\lastpage@putlabel{\addtocounter{page}{-1}%
   \immediate\write\@auxout{\string\newlabel{LastPage}{{}{\thepage}}}%
   \addtocounter{page}{1}}
\AtEndDocument{\clearpage\lastpage@putlabel}%
%---------- end of "lastpage" ------------------

% -- Setup sizes --
\newlength{\exmpwidinf}
\newlength{\exmpwidouf}
\newlength{\exmpwidewid}

\exmpwidinf=0.43\thelinewidth
\exmpwidouf=0.43\thelinewidth
\exmpwidewid=0.9\thelinewidth

\newlength{\afterproblemhead}
\newlength{\afterconstraints}
\afterproblemhead=3mm
\afterconstraints=2mm

\newcommand{\problemheadfont}{\LARGE}
\newcommand{\problemsectionfont}{\Large}
\newcommand{\problemend}{\clearpage}
\newcommand{\problemtextfont}{\normalsize}
\newcommand{\beforeproblemsectioncaption}{\smallbreak\smallskip}
\newcommand{\afterproblemsectioncaption}{\smallskip}

\if@twocolumn
    \afterproblemhead=1mm
    \afterconstraints=1mm
    \renewcommand{\problemheadfont}{\large}
    \renewcommand{\problemsectionfont}{\normalsize}
    \renewcommand{\problemend}{\par\medskip}
    \renewcommand{\problemtextfont}{\footnotesize}
    \renewcommand{\beforeproblemsectioncaption}{\smallbreak\smallskip}
    \renewcommand{\afterproblemsectioncaption}{}
\fi

\if@landscape
    \exmpwidinf=15cm
\fi

\if@russian\else
\parindent=0mm
\parskip=1ex
\fi

% -- End of setup sizes --

% -- Setup keywords --

\if@russian
\def\kw@Problem{Задача}
\def\kw@InputFileName{Имя входного файла:}
\def\kw@OutputFileName{Имя выходного файла:}
\def\kw@TimeLimit{Ограничение по времени:}
\def\kw@MemoryLimit{Ограничение по памяти:}
\def\kw@stdin{стандартный поток ввода}
\def\kw@stdout{стандартный поток вывода}
\def\kw@Input{Формат входного файла}
\def\kw@Output{Формат выходного файла}
\def\kw@Example{Пример}
\def\kw@Examples{Примеры}
\def\kw@page{Страница}
\def\kw@of{из}
\else
\def\kw@Problem{Problem}
\def\kw@InputFileName{Input file:}
\def\kw@OutputFileName{Output file:}
\def\kw@TimeLimit{Time limit:}
\def\kw@MemoryLimit{Memory limit:}
\def\kw@stdin{standard input}
\def\kw@stdout{standard output}
\def\kw@Input{Input}
\def\kw@Output{Output}
\def\kw@Example{Example}
\def\kw@Examples{Examples}
\def\kw@page{Page}
\def\kw@of{of}
\fi

% -- End of setup keywords --


% -- Problem sections --

\newcommand{\createsection}{\@newsection}

\def\@newsection#1#2{\DeclareRobustCommand{#1}{
{\beforeproblemsectioncaption\noindent\bf\problemsectionfont
\textsf{#2}}
\nopagebreak\par\afterproblemsectioncaption}
}

\createsection{\InputFile}{\kw@Input}
\createsection{\OutputFile}{\kw@Output}
\createsection{\Example}{\kw@Example}
\createsection{\Examples}{\kw@Examples}

% -- End of problem sections

% -- Default limits --

\if@russian
\def\defaultmemorylimit{256 мегабайт}
\else
\def\defaultmemorylimit{64 megabytes}
\fi

% -- End of default limits --

% -- Problem environment --

\newcounter{problem}

\newenvironment{problem}[5]{

% -- Default memory limit --
% :FIXME:
\def\@t{#5}

\ifx\@t\empty
    \def\@memorylimit{\defaultmemorylimit}
\else
\ifcat\par\@t
    \def\@memorylimit{\defaultmemorylimit}
\else
    \def\@memorylimit{#5}
\fi
\fi
% -- End of default memory limit --

{\noindent\refstepcounter{problem}\textbf{\problemheadfont\textsf{\kw@Problem\
\if@arabic\arabic{problem}.\else\Alph{problem}.\fi\ #1}}
\nopagebreak\par\vspace{\afterproblemhead}
\problemtextfont\parindent=6.5mm
\vbox{
    \begin{tabular}{l@{\extracolsep{1cm}}l}
    \kw@InputFileName & \texttt{#2} \\
    \kw@OutputFileName & \texttt{#3} \\
    \kw@TimeLimit & #4 \\
    \kw@MemoryLimit & \@memorylimit \\
    \end{tabular}
}
\nopagebreak\par\vspace{\afterconstraints}\problemtextfont}

\newcommand{\InputFileName}{#2}
\newcommand{\OutputFileName}{#3}
}
{\problemend}

\def\s@tm@cr@s{%
\def\widthin##1{\exmpwidinf=##1\relax}\def\widthout##1{\exmpwidouf=##1\relax}%
\def\stretchin##1{\advance\exmpwidinf by ##1\relax}%
\def\stretchout##1{\advance\exmpwidouf by ##1\relax}%
}%

% :FIXME:
\newenvironment{example}[1][]{
\s@tm@cr@s#1\ttfamily\obeylines\obeyspaces\frenchspacing%
\newcommand{\exmp}[2]{
\begin{minipage}[t]{\exmpwidinf}\rightskip=0pt plus 1fill\relax##1\medskip\end{minipage}&
\begin{minipage}[t]{\exmpwidouf}\rightskip=0pt plus 1fill\relax##2\medskip\end{minipage}\\
\hline}

\begin{tabular}{|l|l|}
\hline
\multicolumn{1}{|c|}{\bf\texttt{\InputFileName}}&%
\multicolumn{1}{|c|}{\bf\texttt{\OutputFileName}}\\%
\hline
}
{\end{tabular}}

\newenvironment{examplewide}[1][]{%
\s@tm@cr@s#1\ttfamily\obeylines\obeyspaces\frenchspacing%
\newcommand{\exmp}[2]{%
\begin{tabular}{|c|}
\hline
\multicolumn{1}{|c|}{\bf\texttt{\InputFileName}}\\%
\hline
\begin{minipage}[t]{\exmpwidewid}\rightskip=0pt plus 1fill\relax
##1
\medskip\end{minipage}\\%
\hline
\multicolumn{1}{|c|}{\bf\texttt{\OutputFileName}}\\%
\hline
\begin{minipage}[t]{\exmpwidewid}\rightskip=0pt plus 1fill\relax
##2    
\medskip\end{minipage}\\%
\hline
\end{tabular}
}
}{}

% -- End of problem environment --


% -- Declare "shortitems" environment: it's a "compact itemize" --
\def\shortitems{\vspace{-3mmplus2mm}\itemize\itemsep-1.618mmplus0.5mm\relax}%
\def\endshortitems{\vspace{-3mmplus2mm}\enditemize}%
% -- end of shortitems declaration --

\newcommand{\thecontestname}{Olympiad in Informatics}
\newcommand{\thecontestlocation}{Somewhere}
\newcommand{\thecontestdate}{Once upon a time}

\DeclareRobustCommand{\contestname}{\thecontestname\par\thecontestlocation\unskip, \thecontestdate}

\DeclareRobustCommand{\contest}[3]{
\renewcommand{\thecontestname}{#1}
\renewcommand{\thecontestlocation}{#2}
\renewcommand{\thecontestdate}{#3}
}

\makeatletter

\renewcommand{\@oddhead}{%
\parbox{\textwidth}{%
\sffamily\begin{center}
\protect\contestname%
\\[2pt]
\hrule%
\end{center}}%
}

\renewcommand{\@oddfoot}{%
\parbox{\textwidth}{%
\hrule%
\vspace{6pt}%
\sffamily{{\hfil}\kw@page\ \thepage\ \kw@of\ \pageref{LastPage}\hfil}}%
}%

\makeatother
 
\setlength{\headheight}{2cm}
\setlength{\headsep}{6mm}

\hfuzz=0.5pt

\sloppy

