Clock Adjusting

TimeLimit: 2500/1500 MS (Java/Others)  MemoryLimit: 131072/131072 K (Java/Others)
64-bit integer IO format:%I64d
未提交 | 登录后收藏
Problem Description
The International Committee for Programmable Clocks (ICPC) recently ran into an absolutely catastrophic disaster. A member from their rivalry
― Advanced Clock Machinery (ACM) broke into ICPC’s headquarters! The security guards were quick to act but still unable to take him down before he shot at ICPC’s most valuable assets ― clocks, with his powerful railgun. As a consequence, the hour hands on these clocks are now pointing to incorrect positions. You are now hired to help them salvage the clocks by restoring them to their initial states, which is 12 o’clock.

To formalize this problem, initially ICPC has $n (n \leq 8)$ clocks laid sequentially on your table, with hour hands pointing to arbitrary positions from 1 to 12.

You are given a set of $m$ tools $(m \leq 4)$. With tool $i$ (denoted by $(l_i, x_i)$), you can first choose a segment of $l_i$ clocks, then rotate their hour hands. If $x_i$ is positive, then tool $i$ rotates the hour hands by $x_i$ clockwise. If $x_i$ is negative, then tool $i$ rotates the hour hands by -$x_i$ counter-clockwise. The cost of using any tool is 1, and you may use the same tool more than once. However, you may rearrange the order of the clocks without any cost.

So now you are wondering: What is the minimum cost require to restore all the clocks?
Input
Input contains multiple test cases. There will be no more than $60$ test cases. The first line in the input file contains a positive integer $T$, indicating the number of test cases to follow. Each test case starts with two integers $n$ and $m$ on the first line, where $1 \leq n \leq 8$ and $1 \leq m \leq 4$. $n$ is the number of clocks ICPC has and $m$ is the number of tools you have. You may assume that there are at most $45$ test cases containing $n = 8$, and the minimum costs are at most $8$ in at least two thirds of these test cases. The next line contains $n$ integers $h_1, . . . , h_n ∈ \{1, . . . , 12\}$, denoting where the hour hands are pointing to initially. The last $m$ lines provide information regarding the tools you have. The $i-th$ one contains two integers $l_i$ and $x_i$ where $1 \leq l_i \leq n$ and $-11 \leq x_i \leq 11$. The explanation of which are given in the problem description.
Output
For each test case, output the minimum cost in a single line. If there is no solution or the answer is greater than $8$, output $-1$.
SampleInput
2
4 2
7 10 7 9
3 2
3 3
2 1
4 5
1 -1
SampleOutput
2
-1
Submit
题目统计信息详细
总AC数1
通过人数1
尝试人数4
总提交量5
AC率20.00%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: