CF1342D Multiple Testcases(构造题)

该博客介绍了如何解决CF1342D问题,这是一个涉及构造背包策略的数学题目。博主首先阐述了题目的具体要求,即在限制每个背包内特定数值个数的情况下,确定最小的背包数量。博主分析了比赛时采用的复杂方法,并给出了题解提供的简洁解法。解法主要包括计算所需背包总数cnt,然后对排序后的数值进行均匀分配。实现过程中,利用了抽屉原理和背包容量约束。最后,博主提供了问题的O(nlogn)时间复杂度的解决方案。
摘要由CSDN通过智能技术生成

题意

n n n 个数 { a i } \{a_i\} {ai},每个 a i ≤ k a_i\leq k aik
每个背包大于等于 i i i 的数的个数不能超过 c i ( i = 1 , 2 , . . , k ) c_i(i=1,2,..,k) ci(i=1,2,..,k)
问最少要多少个背包才能放所有数,并且输出每个背包中的数。
n , k ≤ 200000 n,k \leq 200000 n,k200000

分析

完了小号也上紫了(要被迫打div1了
比赛时写了沙雕线段树,比较复杂。
题解给了比较简单的做法。
先求出总共要多少个背包 c n t cnt cnt,再将每个数均匀分配到背包中。
那么 c n t cnt cnt 怎么求呢?
假设大于等于 t t t 的数有 b t b_t bt 个,那么根据抽屉原理,背包个数要大于等于 ⌈ b t c t ⌉ \lceil\frac{b_t}{c_t}\rceil ctbt
所以背包个数就是 c n t = max ⁡ { ⌈ b t c t ⌉ } cnt=\max\{\lceil\frac{b_t}{c_t}\rceil\} cnt=max{ctbt}
接下来怎么构造方案呢?
a i a_i ai 从小到大排序,假设 a i a_i ai t i t_i ti 个。接下来只用把第 i i i 个放进第 i   m o d   c n t i~mod~cnt i mod cnt 个背包即可。因为这样一个背包中 a i a_i ai 的个数不会超过 ⌈ t i c n t ⌉ \lceil\frac{t_i}{cnt}\rceil cntti 个。
复杂度为 O ( n l o g n ) O(nlogn) O(nlogn),其实就是排序的复杂度=。=

代码如下

#include <bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define N 200005
using namespace __gnu_pbds;
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
struct custom_hash {
	   static uint64_t splitmix64(uint64_t x) {
	   	x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
LL z = 1;
int read(){
	int x, f = 1;
	char ch;
	while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1;
	x = ch - '0';
	while(ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48;
	return x * f;
}
int ksm(int a, int b, int p){
	int s = 1;
	while(b){
		if(b & 1) s = z * s * a % p;
		a = z * a * a % p;
		b >>= 1;
	}
	return s;
}
int a[N], c[N], tot, cnt;
vector<int> ans[N];
int main(){
	int i, j, n, k, m;
	n = read(); k = read();
	for(i = 1; i <= n; i++) a[i] = read();
	sort(a + 1, a + i);
	for(i = 1; i <= k; i++) c[i] = read();
	for(i = n; i >= 1; i--) cnt = max(cnt, (n - i) / c[a[i]] + 1);
	for(i = 1; i <= n; i++) ans[i % cnt].push_back(a[i]);
	printf("%d\n", cnt);
	for(i = 0; i < cnt; i++){
		printf("%d ", ans[i].size());
		for(auto x: ans[i]) printf("%d ", x);
		printf("\n");
	}
	return 0;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值