Multiple Testcases CodeForces - 1342D(贪心+树状数组)

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.
思路:怎么说呢,其实要分的组数,我们一开始就是可以计算出来的。我们可以求出大于等于i的数字一共是sum[i]个,那么这sum[i],每组最多是c[i]个,那么这sum[i]个分多少组我们是可以求出来的。对于1-k来说,我们求每一个数的出来取最大值,这就是要分的组数。求sum[i]的过程我用的树状数组。组数知道以后,就对原数组排序之后,依次放入每一组中就可以了。这样我们就保证了每一组都不会有不符合条件的。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
int m[maxx],c[maxx],sum[maxx];
int n,k;

inline int lowbit(int x){return x&-x;}
inline void update(int x)
{
	while(x<maxx)
	{
		sum[x]+=1;
		x+=lowbit(x); 
	}
}
inline int query(int x)
{
	int ans=0;
	while(x)
	{
		ans+=sum[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&m[i]);
	for(int i=1;i<=k;i++) scanf("%d",&c[i]);
	memset(sum,0,sizeof(sum));
	for(int i=1;i<=n;i++) update(m[i]);
	int _max=0;
	for(int i=1;i<=k;i++) 
	{
		int ss=query(maxx-1)-query(i-1);
		_max=max(_max,ss/c[i]+(ss%c[i]?1:0));
	}
	cout<<_max<<endl;
	vector<int> p[_max+1];
	sort(m+1,m+1+n);
	for(int i=1;i<=n;i++) p[i%_max].push_back(m[i]);
	for(int i=0;i<_max;i++)
	{
		cout<<p[i].size();
		for(int j=0;j<p[i].size();j++) cout<<" "<<p[i][j];
		cout<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值