1342D. Multiple Testcases

So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests… Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem!
Initially, each test in that problem is just an array. The maximum size of an array is k. For simplicity, the contents of arrays don’t matter. You have n tests — the i-th test is an array of size mi (1≤mi≤k).
Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than c1 arrays of size greater than or equal to 1 (≥1), no more than c2 arrays of size greater than or equal to 2, …, no more than ck arrays of size greater than or equal to k. Also, c1≥c2≥⋯≥ck.
So now your goal is to create the new testcases in such a way that:
each of the initial arrays appears in exactly one testcase;
for each testcase the given conditions hold;
the number of testcases is minimum possible.
Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase.

Input

The first line contains two integers n and k (1≤n,k≤2⋅105) — the number of initial tests and the limit for the size of each array.
The second line contains n integers m1,m2,…,mn (1≤mi≤k) — the sizes of the arrays in the original tests.
The third line contains k integers c1,c2,…,ck (n≥c1≥c2≥⋯≥ck≥1); ci is the maximum number of arrays of size greater than or equal to i you can have in a single testcase.

Output

In the first line print a single integer ans (1≤ans≤n) — the minimum number of testcases you can achieve.
Each of the next ans lines should contain the description of a testcase in the following format:
t a1 a2 … at (1≤t≤n) — the testcase includes t arrays, ai is the size of the i-th array in that testcase.
Each of the initial arrays should appear in exactly one testcase. In particular, it implies that the sum of t over all ans testcases should be equal to n.
Note that the answer always exists due to ck≥1 (and therefore c1≥1).
If there are multiple answers, you can output any one of them.

Examples

input
4 3
1 2 2 3
4 1 1
output
3
1 2
2 1 3
1 2
input
6 10
5 8 1 10 8 7
6 6 4 4 3 2 2 2 1 1
output
2
3 8 5 7
3 10 8 1
input
5 1
1 1 1 1 1
5
output
1
5 1 1 1 1 1
input
5 1
1 1 1 1 1
1
output
5
1 1
1 1
1 1
1 1
1 1

Note

In the first example there is no way to distribute the tests into less than 3 testcases. The given answer satisfies the conditions: each of the testcases includes no more than 4 arrays of size greater than or equal to 1 and no more than 1 array of sizes greater than or equal to 2 and 3.
Note that there are multiple valid answers for this test. For example, testcases with sizes [[2],[1,2],[3]] would also be correct.
However, testcases with sizes [[1,2],[2,3]] would be incorrect because there are 2 arrays of size greater than or equal to 2 in the second testcase.
Note the difference between the third and the fourth examples. You can include up to 5 arrays of size greater than or equal to 1 in the third example, so you can put all arrays into a single testcase. And you can have only up to 1 array in the fourth example. Thus, every array should be included in a separate testcase.

题目大意:
给出n个数(a[]),n个数的范围是1-k。再给出k个数(c[])。要求将a[i]进行分组,且每组中大于等于i的数不能超过c[i]个,求最小的组数以及每组的分配情况(我讨厌阅读理解题)。

题目分析:
这道题的难点其实就是如何进行分组。
我们可以首先用cnt[i]记录a[]中大于等于i的数的个数。然后最小组数即为max(cnt[i]/c[i])(1<=i<=k) //注意要上取整。将组数求出来以后,这道其实就可以说是做出来了。再将a[]进行排序,然后按顺序放到每一组中即可。
注:整数除法上取整的方法为(a+b-1)/b

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int a[N],cnt[N],c[N];
vector<int> f[N];     //用vector来存每组中的数字
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)
	{
	    cin>>a[i];
	    cnt[a[i]]++;
	}
	for(int i=1;i<=k;i++)
	cin>>c[i];
	int ans=0;
	for(int i=k;i>=1;i--)
	{   //遍历求出最小组数
		cnt[i]+=cnt[i+1];
		ans=max(ans,(cnt[i]+c[i]-1)/c[i]);
	}
	sort(a,a+n);
	for(int i=0;i<n;i++)
	{   //a[]排序后依次放入每组中即可,f[i]为第i组中的数
		f[i%ans].push_back(a[i]);
	}
	cout<<ans<<endl;          //输出答案
	for(int i=0;i<ans;i++)
	{
		cout<<f[i].size()<<' ';
		for(int j=0;j<f[i].size();j++)
		cout<<f[i][j]<<' ';
		cout<<endl; 
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值