Vera and Dogs
Time limit2sMemory limit512 MB
Construct an assignment of primary and secondary doghouses to M = X*N dogs so that closing any single doghouse keeps every open doghouse at or below X+1 sleepers.
- Level
Medium7 of 10
- Topics
- Combinatorics, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Vera has doghouses numbered to and dogs numbered to , where . Doghouse must be the primary home of exactly dogs and the secondary home of exactly dogs . Every dog has one primary home and one secondary home, and the two are different doghouses.
Every night at most one doghouse is closed for cleaning. A dog sleeps in its primary home when that home is open, and in its secondary home otherwise. On every possible night, including the night when no doghouse is closed, each open doghouse must hold at most sleeping dogs.
Find an assignment that meets all of these conditions, or report that none exists.
Input
The first line contains two integers and (, ).
Output
If no valid assignment exists, print -1 on a single line.
Otherwise print lines. Line contains integers: first the dogs whose primary home is doghouse , in increasing order of dog number, then the dogs whose secondary home is doghouse , in increasing order of dog number.
Several assignments can satisfy the conditions, so only this one counts as correct. For every from to and every from to , the dog numbered has doghouse as its primary home and doghouse as its secondary home.
Notes
Doghouse numbers and dog numbers both start at . The night on which no doghouse is closed also has to satisfy the capacity rule, and on that night each doghouse holds exactly the dogs that call it their primary home.