VBLOCKS - Blocks

no tags 

Bom and Cuoi are playing a puzzle game together. The game consists of a horizontal board of L unit cells (size 1x1) and some horizontal segments of size 1xS (made from S unit cubes). Bom has to put these segments on the board so that two consecutive segments have to be at least D cells apart from each other (i.e. there are at lease D empty cells between them).

To make the game more difficult, Cuoi gives Bom some more conditions. Each condition has the form: "the ith cell must be covered" or "the ith cell must not be covered" (by a cube).

Help Bom to find a way to put the segments so that Cuoi's conditions are satisfied. If one way exists, determine the maximum number of segments that Bom can use.


  • The first line contains three integers L, S, D (1 ≤ L ≤ 100000).
  • The second line contains an integer K that is the number of Cuoi's conditions.
  • Each line in the next K lines contains two integers i and d (d=1 or d=2) representing a Cuoi's condition: d=1 means "the ith cell must be covered" and d=2 means "the ith cell must not be covered". The values of i are in ascending order.


If there is no way for Bom to put the segments satisfying Cuoi's conditions, print -1. Otherwise, print the maximum number of segments that Bom can use.


There are 50% of the test cases corresponding to 50% of the grades in which 1 ≤ L ≤ 1000.


10 4 2
2 1
5 2	


4 2 1
1 1
3 1	


hide comments
monkey_coder: 2015-03-29 09:59:39

finally AC after realizing that i from 1 :'(

Added by:Duc
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:VNOI Marathon '08 - Round 5/DivA
Problem Setter: Ngô Minh Đức