#P9339. [JOISC 2023 Day3] Cookies

[JOISC 2023 Day3] Cookies

题目描述

Rie likes to make cookies. She made NN types of cookies. She made AiA _ i cookies of type i(1iN)i(1 \leq i \leq N). In order to sell the cookies made by her, she will pack them into boxes. However, the following conditions should be satisfied.

  • For every box, the types of the cookies in it should be different.
  • For every box, the number of cookies in it should be equal to one of the following MM numbers: B1,B2,,BMB _ 1, B _ 2, \cdots, B _ M.

Write a program which, given information of cookies made by Rie and the conditions to pack the cookies into boxes, determines whether it is possible to pack all the cookies into boxes. Moreover, if it is possible to pack all the cookies into boxes, your program should output a way to pack the cookies into the minimum number of boxes.

输入格式

Read the following data from the standard input.

NN
A1 A2  ANA _ 1 \ A _ 2 \ \cdots \ A _ N
MM
B1 B2  BMB _ 1 \ B _ 2 \ \cdots \ B _ M

输出格式

If it is possible to pack all the cookies into boxes so that the conditions are satisfied, let xx be the number of used boxes, ckc _ k be the number of cookies in the kk-th box (1kx1\leq k \leq x), and vk,1,vk,2,,vk,ckv _ {k, 1}, v _ {k, 2}, \cdots, v _ {k, c _ k} be the types of the cookies in the kk-th box. Write these numbers to the standard output as in the following format.

xx
$c _ 1 \ v _ {1, 1} \ v _ {1, 2} \ \cdots \ v _ {1, c _ 1}$
$c _ 2 \ v _ {2, 1} \ v _ {2, 2} \ \cdots \ v _ {2, c _ 2}$
\vdots
$c _ x \ v _ {x, 1} \ v _ {x, 2} \ \cdots \ v _ {x, c _ x}$

Here, the number of used boxes xx should be the minimum possible number. If there are several ways to pack the cookies into boxes satisfying the conditions, output any one of them.

If it is impossible to pack all the cookies into boxes so that the conditions are satisfied, write 1-1 to the standard output.

题目大意

题目描述

莉婕喜欢做饼干。她制作了 NN 种饼干。第 ii 种饼干有 AiA_i 个。为了出售她制作的饼干,她将它们装入盒子中。但是,应该满足以下条件。

  • 对于每个盒子,其中的饼干种类应不同。

  • 对于每个盒子,其中的饼干数量应等于以下 MM 个数字之一:B1,B2,,BMB_1,B_2,⋯ ,B_M

编写一个程序,给出莉婕制作的饼干信息和将饼干装箱的条件,确定是否可能将所有饼干包装到盒子中。此外,如果可以将所有饼干包装在盒子中,则您的程序应输出最少的盒子数量。

输入格式

从标准输入读取以下数据。

NN
A1 A2  ANA _ 1 \ A _ 2 \ \cdots \ A _ N
MM
B1 B2  BMB _ 1 \ B _ 2 \ \cdots \ B _ M

输出格式

如果可以将所有的饼干装入盒子并且满足上述条件,则设 xx 是所需的盒子数,ckc_k 是第 kk 个盒子中的饼干数 (1kx)(1≤k≤x)vk,1,vk,2,,vk,ckv_{k,1},v_{k,2},⋯ ,v_{k,c_k} 是第 kk 个盒子中的饼干种类。请将这些数字按以下格式编写到标准输出。

xx
$c _ 1 \ v _ {1, 1} \ v _ {1, 2} \ \cdots \ v _ {1, c _ 1}$
$c _ 2 \ v _ {2, 1} \ v _ {2, 2} \ \cdots \ v _ {2, c _ 2}$
\vdots
$c _ x \ v _ {x, 1} \ v _ {x, 2} \ \cdots \ v _ {x, c _ x}$

在此,使用的盒子数量 xx 应该是可能的最小数量。如果有多种方式可以满足条件地将饼干装入盒子,请输出其中任何一种方法。

如果无法将所有饼干包装在盒子中以满足条件,输出 1-1

【样例解释 #1】

对于该样例输入,可以按照以下方式将 77 个饼干装入 33 个盒子中满足条件:

  • 将第 11 类和第 77 类的饼干装入第一个盒子中。每种类型放 11 个。
  • 将第 22 类和第 66 类的饼干装入第二个盒子中。每种类型放 11 个。
  • 将第 33 类、第 44 类和第 55 类的饼干装入第三个盒子中。每种类型放 11 个。

因为不能用少于或等于 22 个盒子来包装 77 个饼干,所以以上方法是正确的。判断为正确答案。还有其他正确方法。

Translate by

https://www.luogu.com.cn/user/661274

7
1 1 1 1 1 1 1
3
1 2 3
3
2 1 7
2 2 6
3 3 4 5
5
5 3 1 2 4
1
4
-1
7
5 4 4 2 1 1 1
2
2 6
7
6 1 2 3 4 5 6
2 2 1
2 3 1
2 4 1
2 7 1
2 3 2
2 3 2

提示

【样例解释 #1】

In this sample input, it is possible to pack the 77 cookies into 33 boxes so that the conditions are satisfied as follows.

  • Pack cookies of types 1,71, 7 into the first box. Pack one cookie for each type.
  • Pack cookies of types 2,62, 6 into the second box. Pack one cookie for each type.
  • Pack cookies of types 3,4,53, 4, 5 into the third box. Pack one cookie for each type.

Your program is judged as correct if it outputs the above way to pack the cookies because it is impossible to pack the 77 cookies into less than or equal to 22 boxes so that the conditions are satisfied. There are other ways to pack the cookies which are judged as correct.

This sample input satisfies the constraints of Subtasks 1, 3, 4, 5, 6.

【样例解释 #2】

In this sample input, it is impossible to pack the 1515 cookies into boxes so that the conditions are satisfied.

Therefore, output 1-1.

This sample input satisfies the constraints of Subtasks 2, 3, 4, 5, 6.

【样例解释 #3】

This sample input satisfies the constraints of Subtasks 4, 5, 6.

【数据范围】

对于所有测试数据,满足 1N150001 \leq N \leq 15 000Ai1A _ i \geq 11in1 \leq i \leq n),A1+A2++AN15000A _ 1 + A _ 2 + \cdots + A _ N \leq 15 0001MN1 \leq M \leq N1BjN1 \leq B _ j \leq N1jM1 \leq j \leq M),Bj<Bj+1B _ j < B _ {j + 1}1jM11 \leq j \leq M - 1),保证所有输入均为整数。

子任务编号 分值 限制
11 66 N500N \leq 500Ai=1A _ i = 11iN1 \leq i \leq N
22 77 N500N \leq 500M=1M = 1
33 1212 A1+A2++AN15A _ 1 + A _ 2 + \cdots + A _ N \leq 15
44 4545 A1+A2++AN500A _ 1 + A _ 2 + \cdots + A _ N \leq 500
55 1515 A1+A2++AN3000A _ 1 + A _ 2 + \cdots + A _ N \leq 3000
66 没有额外的限制