LOJ #10008. 「一本通 1.1 练习 4」家庭作业 解题报告

本文介绍了一种利用并查集优化的家庭作业调度算法,解决了一个类似于经典的课程安排问题,但数据范围更大,需要在O(Nt)级别的时间复杂度下进行优化。通过将任务按学分降序排列,并使用并查集来动态维护每个时间点的可用状态,实现了高效的任务调度。

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

题目链接

LOJ #10008. 「一本通 1.1 练习 4」家庭作业

解题思路

好题!
这道题和这题很像,几乎一模一样。但是不同的是,这道题的数据范围有些改变在这里插入图片描述
显然 O ( N t ) O(Nt) O(Nt)的算法过不了
我们考虑其他做法。
显然我们还是使用同一种贪心方法,以学分为标准降序排序,但是我们需要优化我们的判断。
f [ t ] f[t] f[t]表示从 t t t时间往前推,最远可到达的空位时间。
我们可以使用并查集维护!

详细代码

#define USEFASTERREAD 1 

#define rg register
#define inl inline
#define DEBUG printf("[Passing [%s] in line %d.]\n", __func__, __LINE__)
#define putline putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
#include<cstdio>

#if USEFASTERREAD
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
#endif
struct IO {
	void RS() {freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);} 
	template<typename T> inline IO r(T& x)const	{
	    x = 0; T f = 1; char ch = getchar();
	    for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
	    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
	    x *= f; return *this;
	}
	template<typename T> inline IO w(T x)const {
	    if(x < 0) {putchar('-'); x = -x;}
	    if(x >= 10) w(x / 10);
	    putchar(x % 10 + '0'); return *this;
	}
	template<typename T> inline IO wl(const T& x)const {w(x), putline; return *this;}
	template<typename T> inline IO ws(const T& x)const {w(x), putsp; return *this;}
	inline IO l() {putline; return *this;}
	inline IO s() {putsp; return *this;}
}io;
template<typename T> inline T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> inline T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> inline void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
template<typename T> inline T Abs(const T& x) {return x > 0 ? x : -x;} 
#include<algorithm>
using namespace std;
const int MAXN = 1000005;
const int MAXT = 700005;
int n;
int fa[MAXT];
int getfa(int x) {
	return fa[x] == x ? x : (fa[x] = getfa(fa[x]));
}
struct Node {
	int t, w;
	bool operator < (const Node& x)const {
		return w > x.w;
	} 
}A[MAXN];
bool flag[MAXT];
int ans;
int main() {
    //io.RS();
    io.r(n);
    int maxt = 0;
	for(rg int i = 1; i <= n; i++) io.r(A[i].t).r(A[i].w), maxt = Max(maxt, A[i].t);
	sort(A + 1, A + 1 + n);
	for(rg int i = 1; i <= maxt; i++) fa[i] = i;
	for(rg int i = 1; i <= n; i++) {
		int ft = getfa(A[i].t);
		if(ft) ans += A[i].w, fa[ft] = getfa(ft - 1);
	}
	io.wl(ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

日居月诸Rijuyuezhu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值