CodeForces - 1701C

该问题是一个关于工人效率和任务分配的优化问题。有n个工人和m个任务,每个任务由特定的工人在1小时内完成,否则需要2小时。目标是找到一种任务分配方式,使得所有任务能在最短时间内完成。通过使用二分查找策略来确定最小完成时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeForces - 1701C

There are �n workers and �m tasks. The workers are numbered from 11 to �n. Each task �i has a value ��ai​ — the index of worker who is proficient in this task.

Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in 11 hour. Otherwise, it takes them 22 hours.

The workers work in parallel, independently of each other. Each worker can only work on one task at once.

Assign the workers to all tasks in such a way that the tasks are completed as early as possible. The work starts at time 00. What's the minimum time all tasks can be completed by?

Input

The first line contains a single integer �t (1≤�≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains two integers �n and �m (1≤�≤�≤2⋅1051≤n≤m≤2⋅105) — the number of workers and the number of tasks.

The second line contains �m integers �1,�2,…,��a1​,a2​,…,am​ (1≤��≤�1≤ai​≤n) — the index of the worker proficient in the �i-th task.

The sum of �m over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print a single integer — the minimum time all tasks can be completed by.

#include<bits/stdc++.h>
using namespace std;
#define Acode ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
typedef long long ll;
#define endl '\n'
const int inf = 0x3f3f3f3f;

map<int, int>mp;
int n, m;

bool check(int mid)
{
	ll cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (mp[i] <= mid) cnt -= (mid - mp[i]) / 2;
		else cnt += (mp[i] - mid);
	}
	if (cnt > 0) return false;
	else return true;
}
int main()
{
	Acode;
	int t;
	cin >> t;
	while (t--)
	{
		mp.clear();
		cin >> n >> m;
		while (m--)
		{
			int x;
			cin >> x;
			mp[x]++;
		}
		int l = 0, r = inf;
		while (l <= r)
		{
			int mid = l + r >> 1;
			if (check(mid)) r = mid - 1;
			else l = mid + 1;
		}
		cout << l << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值