Back From Summer '17 P6: Crowded Cities

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Java 8 1.4s
Python 1.8s
Memory limit: 256M
Java 8 512M
Python 1G

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Drake giving good city building advice.

For many 9th grade students enthusiastically starting high school, geography is often one of the first new subjects they encounter. A very important part of geography class is teaching students about the importance of city design. The teacher explains to students how denser cities allow for more efficient transportation and use of resources. Clearly, the key to denser cities is taller buildings that house more people.

Toronto, Canada is facing a housing shortage, so for the last project of the semester, the teacher goes up to the aspiring city designers and asks them to design a building that houses the most people. While all your classmates are off designing weird buildings that will probably crash and burn, your buddy Alex convinces you to use giant lego houses like all the hip kids are supposedly doing.

Each housing block is a rectangular prism with integer length, width, and height. Block i has a length of L_i, a width of W_i, a height of H_i respectively and can house P_i people.

Unfortunately for medieval designers, overhangs are not popular in Toronto, so they are banned by the city for aesthetic reasons. In addition, city regulations require that windows on a building are aligned by a lattice grid (aka blocks may only be rotated by 90 degrees), and no block may be taller than the block below it. Formally, block i can be stacked on top of block j if L_i \leq L_j, W_i \leq W_j and H_i \leq H_j.

You are really interested in qualifying for the geomatics program at the University of Waterloo, so you want to prove your geography skills to the teacher.

Find a design that houses the greatest number of people!

Input Specification

The first line will contain N, the number of blocks that you have available.

In each of the next N lines, there will be 4 integers. On the i^{th} (1 \leq i \leq N) line there will be L_i, W_i, H_i, P_i that specify the length, width, height in the i^{th} block, and the number of people you can fit inside.

Output Specification

On the first line, print the greatest number of people that your building can support while adhering to regulations.

On the next line print K, the number of blocks in your design.

In the next line, print K space separated integers indicating the indices of blocks you plan to use. You should first print the blocks from the base upwards.

NOTE: Block indices start at 1.

Subtasks and Scoring

Subtask Constraints Score
1 N \leq 9 10
2 L_i = W_i = H_i 10
3 N \leq 500 20
4 H_i = 1 20
5 L_i, W_i \leq 1\,000 20
6 No additional constraints 20

For all test cases, N \leq 100\,000. In addition, L_i, W_i, H_i \leq 5\,000 and P_i \leq 1\,000\,000\,000 .

Sample Input 1

1 100 1 4
2 2 1 5
2 4 2 6

Sample Output 1

3 2

Sample Explanation 1

Block 2 is stacked on top of block 3. This is allowed since no dimension of block 2 exceeds that of block 3. When combined, the building can house 5+6=11 people.

Sample Input 2

8 8 8 3
8 8 4 4
5 5 5 5

Sample Output 2

1 3

Sample Explanation 2

The optimal solution would be to stack block 3 on top of block 1. Even though stacking block 3 on top of block 2 would allow 4+5=9 housing units, city regulation does not allow blocks to be stacked on other blocks shorter than them.


  • 1
    discoverMe  commented on Jan. 7, 2019, 12:11 p.m.

    can you put 2 blocks on top of a block if they both fit

    eg. 10x10x10 and 20x5x5 blocks on 100x100x100 block

    • 0
      Riolku  commented on Feb. 4, 2019, 1:35 p.m.