[代码元]#503. 工作安排

该博客介绍了如何使用贪心算法和优先队列解决一个时间管理和任务优化的问题。通过按截止时间递增排序任务,并在有限时间内选择价值最高的任务完成,确保工作效率最大化。代码示例展示了具体的实现过程。

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

题目:

 分析:

时间是一秒一秒过的,而且任务也是一秒就可以完成,那么工作时长就是我们可以完成的任务了。

我们如果按照工作截止时间递增排序,对于我们的工作时间来说,如果当前任务还没有截止,那么我们就可以直接完成它,如果当前任务时间已经截至了,我们可以看一下前面完成的任务有没有价值比他低的,并替换掉。毕竟同样是在有限的时间内完成的任务,我们肯定要更贪心的选择价值更高的任务完成。这一操作可以使用小根堆的优先队列来实现。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;

int n;
struct Node{
	int a, b;
	bool operator< (const Node &W) const{
		return a < W.a;
	}
}a[N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i ++ )
	{
		scanf("%d%d", &a[i].a, &a[i].b);	
	}	
	
	sort(a + 1, a + 1 + n);
	
	priority_queue<int , vector<int>, greater<int> > q;
	// 队列表示当前工作的时长 
	for (int i = 1; i <= n; i ++ )
	{
		int x = a[i].a, y = a[i].b;
		
		if (x <= 0) continue;
		if (x > q.size()) // 这个工作截止时间还没到 
		{
			q.push(y);
		}
		else if (x <= q.size()) // 这个工作已经截止了 
		{
			if (y > q.top()) // 这个工作的价值截止了,但是已经完成的其他工作来说,价值更高 
			{
				q.pop();
				q.push(y);
			}
		}
	 } 
	 
	 int res = 0;
	 while (q.size())
	 {
	 	res += q.top();
	 	q.pop();
	 	
	 }
	 cout << res << endl;
	 return 0;
}

root@ESM8400:~# gdb -p 178418 -ex "thread apply all bt" -ex "detach" -ex "quit" GNU gdb (GDB) 14.2 Copyright (C) 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "aarch64-poky-linux". Type "show configuration" for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word". Attaching to process 178418 [New LWP 178425] [New LWP 178426] [New LWP 178427] [New LWP 178432] [New LWP 178433] [New LWP 178434] [New LWP 178435] [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/libthread_db.so.1". ioctl () at ../sysdeps/unix/sysv/linux/aarch64/ioctl.S:25 warning: 25 ../sysdeps/unix/sysv/linux/aarch64/ioctl.S: No such file or directory Thread 8 (Thread 0xffff95821f20 (LWP 178435) "QQmlThread"): #0 0x0000ffffb3cb902c in __GI___poll (fds=0xaaab17ed8f30, nfds=1, timeout=<optimized out>) at /usr/src/debug/glibc/2.39+git/sysdeps/unix/sysv/linux/poll.c:41 #1 0x0000ffffb298d2c0 in ?? () from /usr/lib/libglib-2.0.so.0 #2 0x0000ffffb298dae8 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0 #3 0x0000ffffb452ed58 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #4 0x0000ffffb425c63c in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #5 0x0000ffffb436fcd0 in QThread::exec() () from /usr/lib/libQt6Core.so.6 #6 0x0000ffffb4402538 in ?? () from /usr/lib/libQt6Core.so.6 #7 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #8 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 7 (Thread 0xffff96031f20 (LWP 178434) "QFileInfoGather"): #0 0x0000ffffb3c5d5b4 in __futex_abstimed_wait_common64 (private=0, cancel=true, abstime=0x0, op=393, expected=0, futex_word=0xaaab16195f78) at futex-internal.c:57 #1 __futex_abstimed_wait_common (cancel=true, private=0, abstime=0x0, clockid=0, expected=0, futex_word=0xaaab16195f78) at futex-internal.c:87 #2 __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0xaaab16195f78, expected=expected@entry=0, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=0) at futex-internal.c:139 #3 0x0000ffffb3c60128 in __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0xaaab16195f20, cond=0xaaab16195f50) at pthread_cond_wait.c:503 #4 ___pthread_cond_wait (cond=0xaaab16195f50, mutex=0xaaab16195f20) at pthread_cond_wait.c:618 #5 0x0000ffffb440fb84 in QWaitCondition::wait(QMutex*, QDeadlineTimer) () from /usr/lib/libQt6Core.so.6 #6 0x0000ffffb571f894 in QFileInfoGatherer::run() () from /usr/lib/libQt6Gui.so.6 #7 0x0000ffffb4402538 in ?? () from /usr/lib/libQt6Core.so.6 #8 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #9 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 6 (Thread 0xffff97ffef20 (LWP 178433) "app"): #0 ioctl () at ../sysdeps/unix/sysv/linux/aarch64/ioctl.S:25 #1 0x0000ffffb1931b70 in gcoOS_DeviceControl () from /usr/lib/libGAL.so #2 0x0000ffffb1932d30 in gcoOS_WaitSignal () from /usr/lib/libGAL.so #3 0x0000ffffb2b2c980 in ?? () from /usr/lib/libEGL.so.1 #4 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #5 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 5 (Thread 0xffff9cdfef20 (LWP 178432) "app"): #0 ioctl () at ../sysdeps/unix/sysv/linux/aarch64/ioctl.S:25 #1 0x0000ffffb1931b70 in gcoOS_DeviceControl () from /usr/lib/libGAL.so --Type <RET> for more, q to quit, c to continue without paging-- #2 0x0000ffffb1932d30 in gcoOS_WaitSignal () from /usr/lib/libGAL.so #3 0x0000ffffb17b2f10 in ?? () from /usr/lib/libGAL.so #4 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #5 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 4 (Thread 0xfffface1ef20 (LWP 178427) "WaylandEventThr"): #0 0x0000ffffb3cb902c in __GI___poll (fds=0xfffface1e5c8, nfds=2, timeout=<optimized out>) at /usr/src/debug/glibc/2.39+git/sysdeps/unix/sysv/linux/poll.c:41 #1 0x0000ffffadf31fd4 in ?? () from /usr/lib/libQt6WaylandClient.so.6 #2 0x0000ffffb4402538 in ?? () from /usr/lib/libQt6Core.so.6 #3 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #4 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 3 (Thread 0xffffad62ef20 (LWP 178426) "WaylandEventThr"): #0 0x0000ffffb3c5d5b4 in __futex_abstimed_wait_common64 (private=0, cancel=true, abstime=0x0, op=393, expected=0, futex_word=0xaaab15746d68) at futex-internal.c:57 #1 __futex_abstimed_wait_common (cancel=true, private=0, abstime=0x0, clockid=0, expected=0, futex_word=0xaaab15746d68) at futex-internal.c:87 #2 __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0xaaab15746d68, expected=expected@entry=0, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=0) at futex-internal.c:139 #3 0x0000ffffb3c60128 in __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0xaaab15746d10, cond=0xaaab15746d40) at pthread_cond_wait.c:503 #4 ___pthread_cond_wait (cond=0xaaab15746d40, mutex=0xaaab15746d10) at pthread_cond_wait.c:618 #5 0x0000ffffb440fb84 in QWaitCondition::wait(QMutex*, QDeadlineTimer) () from /usr/lib/libQt6Core.so.6 #6 0x0000ffffadf31f74 in ?? () from /usr/lib/libQt6WaylandClient.so.6 #7 0x0000ffffb4402538 in ?? () from /usr/lib/libQt6Core.so.6 #8 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #9 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 2 (Thread 0xffffade7ef20 (LWP 178425) "QDBusConnection"): #0 0x0000ffffb3cb902c in __GI___poll (fds=0xaaab15742240, nfds=2, timeout=<optimized out>) at /usr/src/debug/glibc/2.39+git/sysdeps/unix/sysv/linux/poll.c:41 #1 0x0000ffffb298d2c0 in ?? () from /usr/lib/libglib-2.0.so.0 #2 0x0000ffffb298dae8 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0 #3 0x0000ffffb452ed58 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #4 0x0000ffffb425c63c in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #5 0x0000ffffb436fcd0 in QThread::exec() () from /usr/lib/libQt6Core.so.6 #6 0x0000ffffb287e7b0 in ?? () from /usr/lib/libQt6DBus.so.6 #7 0x0000ffffb4402538 in ?? () from /usr/lib/libQt6Core.so.6 #8 0x0000ffffb3c60dc4 in start_thread (arg=0x0) at pthread_create.c:447 #9 0x0000ffffb3cc2f4c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone3.S:76 Thread 1 (Thread 0xffffb6509020 (LWP 178418) "app"): #0 ioctl () at ../sysdeps/unix/sysv/linux/aarch64/ioctl.S:25 #1 0x0000ffffb1931b70 in gcoOS_DeviceControl () from /usr/lib/libGAL.so #2 0x0000ffffb1932d30 in gcoOS_WaitSignal () from /usr/lib/libGAL.so #3 0x0000ffffb1817590 in ?? () from /usr/lib/libGAL.so #4 0x0000ffffb2c4ffd8 in ?? () from /usr/lib/libGLESv2.so.2 #5 0x0000ffffb2c5004c in ?? () from /usr/lib/libGLESv2.so.2 #6 0x0000ffffb2bf2a10 in ?? () from /usr/lib/libGLESv2.so.2 #7 0x0000ffffb2b9973c in glTexSubImage2D () from /usr/lib/libGLESv2.so.2 #8 0x0000ffffb565d040 in ?? () from /usr/lib/libQt6Gui.so.6 #9 0x0000ffffb565e9ac in ?? () from /usr/lib/libQt6Gui.so.6 #10 0x0000ffffb54ffae0 in QRhi::endFrame(QRhiSwapChain*, QFlags<QRhi::EndFrameFlag>) () from /usr/lib/libQt6Gui.so.6 #11 0x0000ffffb539d198 in ?? () from /usr/lib/libQt6Gui.so.6 #12 0x0000ffffb54c023c in QPlatformBackingStore::rhiFlush(QWindow*, double, QRegion const&, QPoint const&, QPlatformTextureList*, bool) () from /usr/lib/libQt6Gui.so.6 #13 0x0000ffffb5cd70c4 in QWidgetRepaintManager::flush(QWidget*, QRegion const&, QPlatformTextureList*) () from /usr/lib/libQt6Widgets.so.6 #14 0x0000ffffb5cd8658 in QWidgetRepaintManager::flush() () from /usr/lib/libQt6Widgets.so.6 #15 0x0000ffffb5cda874 in QWidgetRepaintManager::paintAndFlush() () from /usr/lib/libQt6Widgets.so.6 #16 0x0000ffffb5ccf3bc in QWidget::event(QEvent*) () from /usr/lib/libQt6Widgets.so.6 #17 0x0000ffffb5c72af8 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #18 0x0000ffffb424f43c in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt6Core.so.6 #19 0x0000ffffb425368c in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/libQt6Core.so.6 #20 0x0000ffffb452f540 in ?? () from /usr/lib/libQt6Core.so.6 #21 0x0000ffffb29899e4 in ?? () from /usr/lib/libglib-2.0.so.0 --Type <RET> for more, q to quit, c to continue without paging-- #22 0x0000ffffb298d318 in ?? () from /usr/lib/libglib-2.0.so.0 #23 0x0000ffffb298dae8 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0 #24 0x0000ffffb452ed58 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #25 0x0000ffffb425c63c in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #26 0x0000ffffb42585e0 in QCoreApplication::exec() () from /usr/lib/libQt6Core.so.6 #27 0x0000aaaae0c2d5ec in main (argc=1, argv=0xffffcb8ec8b8) at ../appHDQt6/main.cpp:198 Detaching from program: /mnt/mmc/app, process 178418 [Inferior 1 (process 178418) detached] 分析一下卡死在哪里了
07-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值