%
% 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
%

\newlength{\thelinewidth}

\if@landscape
    \if@twocolumn
    \else
        \error Landscape is only supported for two column mode
    \fi

    \ifcase\@ptsize % 10 pt
        \hoffset=-15mm
        \voffset=-35mm
        \textheight=165mm
        \textwidth=272mm
    \or % 11 pt
        \hoffset=-13mm
        \voffset=-35mm
        \textheight=165mm
        \textwidth=272mm
    \or % 12 pt
        \hoffset=-12mm
        \voffset=-35mm
        \textheight=162mm
        \textwidth=268mm
    \fi
\else
    \ifcase\@ptsize % 10 pt
        \hoffset=-26.5mm
        \voffset=-35mm
        \textheight=250mm
        \textwidth=175mm
    \or % 11 pt
        \hoffset=-25mm
        \voffset=-35mm
        \textheight=250mm
        \textwidth=175mm
    \or % 12 pt
        \hoffset=-20mm
        \voffset=-35mm
        \textheight=245mm
        \textwidth=175mm
    \fi

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

\if@twocolumn
    \thelinewidth=0.47\textwidth
\else
    \thelinewidth=\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}{\normalsize}
\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@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@Feedback{Отображение результатов:}
\def\kw@stdin{стандартный поток ввода}
\def\kw@stdout{стандартный поток вывода}
\def\kw@Input{Формат входных данных}
\def\kw@Output{Формат выходных данных}
\def\kw@Example{Пример}
\def\kw@Examples{Примеры}
\def\kw@Explanation{Пояснение к примеру}
\def\kw@Explanations{Пояснения к примерам}
\def\kw@Illustration{Иллюстрация}
\def\kw@Scoring{Система оценки}
\def\kw@Note{Замечание}
\def\kw@Hint{Подсказка по решению}
\def\kw@page{Страница}
\def\kw@of{из}
\def\kw@Details{Подробности}
\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@Feedback{Feedback:}
\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@Explanation{Explanation}
\def\kw@Explanations{Explanations}
\def\kw@Illustration{Illustration}
\def\kw@Scoring{Scoring}
\def\kw@Note{Note}
\def\kw@Hint{Hint}
\def\kw@page{Page}
\def\kw@of{of}
\def\kw@Details{Details}
\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}
\createsection{\Explanation}{\kw@Explanation}
\createsection{\Explanations}{\kw@Explanations}
\createsection{\Illustration}{\kw@Illustration}
\createsection{\Scoring}{\kw@Scoring}
\createsection{\Note}{\kw@Note}
\createsection{\Hint}{\kw@Hint}
\createsection{\Details}{\kw@Details}

% -- End of problem sections

% -- Default limits --

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

% -- End of default limits --

% -- Declare "shortitems" environment: it's a "compact itemize" --
\if@twocolumn
    \def\shortitems{\vspace{-1mmplus6mm}\itemize\itemsep-0.618mmplus0.5mm\relax}%
    \def\endshortitems{\vspace{-1mmplus6mm}\enditemize}%
    \def\shortnums{\vspace{-1mmplus6mm}\enumerate\itemsep-0.618mmplus0.5mm\relax}%
    \def\endshortnums{\vspace{-1mmplus6mm}\endenumerate}%
\else
    \def\shortitems{\vspace{-3mmplus2mm}\itemize\itemsep-1.618mmplus0.5mm\relax}%
    \def\endshortitems{\vspace{-3mmplus2mm}\enditemize}%
    \def\shortnums{\vspace{-3mmplus2mm}\enumerate\itemsep-1.618mmplus0.5mm\relax}%
    \def\endshortnums{\vspace{-3mmplus2mm}\endenumerate}%
\fi
% -- end of shortitems declaration --

% -- Problem environment --

\newcounter{problem}

\newcommand{\ProblemSection}[1]{
  %\hbox{\hspace{1em}}
  %\vspace*{-2em}
  \section*{#1}
  \addcontentsline{toc}{section}{#1}
  \vspace*{-1.5em}
  \hspace*{\parindent}\unskip
}

\newenvironment{problem}[6]{
    {
        \noindent
        \refstepcounter{problem}
        
        \vspace*{-3.0em}
        \ProblemSection{\mbox{\textbf{\problemheadfont\textsf{\kw@Problem\ \ProblemLabel. \ #1 [\ProblemTL, \ProblemML]}}}}
        \vspace*{-1.5em}
%        \textbf{\problemheadfont\textsf{\kw@Problem\ #6 \ #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 & \ProblemTL \\
            %\kw@MemoryLimit & \ProblemML \\
            %\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}
    \@ifstar{
        \error Star must not be used in example environment any more
    }
}

% :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
    }

    \newcommand{\exmpfile}[2]{
       \exmp{
          \verbatiminput{##1}
       }{
          \verbatiminput{##2}
       }%
    }

    \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}%
        %{{\hfil}\kw@page\ \thepage\ \hfil}%
    }
}

\makeatother
 
\headheight=2cm
\headsep=6mm
\hfuzz=0.5pt

\sloppy
