当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

3677:Chemical Weighing

题目描述

Facer is the chemical keeper in a laboratory. Every day he receives M requests of a certain kind of chemical which is vital to a large variety of experiments. The ith request asks for Ri grams of the chemical and Facer has to weigh the chemicals in the exact order as the requests listed or he will mess up all the experiments.

The balance used in the laboratory has N different kinds of poises. The ith kind weighs Wi grams. The amount of poises of each kind is so large that can be considered as infinite. The poises are all in a flat shape. When Facer wants to weigh the chemical, he has to stack the poises on the left side of the balance one by one in such a way that the lighter ones are always above the heavier ones. For example, a 2-gram poise is never allowed to be on the top of a 1-gram poise.

To accelerate his work, Facer tries to use the minimum steps to finish the weighing. For each step he can put one poise on the top of the stack (as long as no poises in the stack is lighter than it) or remove the poise which is currently on the top.

输入解释

The first line of the input contains the number of the test cases.
Each test case follows the format below:

  • One line contains an integer N (1 ≤ N ≤ 15), the number of different kinds of poises
  • One line contains N integers, W1, W2 ... WN, ( 1 ≤ Wi ≤ 30)
  • One line contains an integer M (1 ≤ M ≤ 500), the number of requests
  • One line contains M integers, R1, R2, ... RM ( 1 ≤ Ri ≤ 30)
输出解释

For each test case, output a line containing the minimum number of steps Facer needed to finish all the requests. If any request can not be weighed out, output -1 instead.

输入样例
2
3
1 2 3
3
4 5 6
2
3 4
3
1 6 8
输出样例
4
-1

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-3677

最后修改于 2020-10-29T07:07:58+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536