Uvalive 4254 Processor Processor (two-part simulation + greedy)

Source: Internet
Author: User



There are n tasks, each task has ri,di,wi, and the [Ri,di] representing the task represents the time interval in which the task can be done, while WI represents the workload of the task; now there is a processor, if it is executed at a speed of s, then the time required to complete the I task is wi/s; Required to calculate the minimum value of the maximum speed during processor execution

The idea is very simple two points, but how to simulate is difficult, can simulate the processor every second of the work, for every second, with priority queue to store the current time under the task can be processed, preferential treatment of D small, if finished, then deal with the next task, if not finished, time plus one.

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt  #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = 10000 + 100;const int INF = 0x3f3f3f3f;int n;struct Task {int R, D, W;bool operator < (const TASK&AMP ; A) const {return d > A.D;}}; BOOL CMP (Task A, task B) {return A.R < B.R;} Task tasks[maxn];void init () {cin >> n;for (int i = 0; i < n; i++) {cin >> TASKS[I].R >> tasks[i].d &G T;> TASKS[I].W;} Sort (tasks, tasks+n, CMP);} BOOL Check (int speed) {priority_queue<task> Q;int today = 1, pcnt = 0;while (pcnt < n | |!q.empty ()) {while (pcnt &lt ;   n && tasks[pcnt].r = Now) Q.push (tasks[pcnt++]); int. = speed; Workload per unit time while (!q.empty () && work) {Task tmp = Q.top (); Q.pop (); if (Work > TMP.W) Work-= Tmp.w;else if (work = = TMP.W) {if (!q.empty () && q.top (). D = = now+1) return FAL se;work = 0;} else {if (TMP.D = = now+1) return FALSE;TMP.W-= work, work = 0;q.push (TMP);}} now++;} return true;}  void Solve () {int left = 1, right = Inf;while (left < right) {int mid = left + (right-left)/2;if (check (mid)) right = Mid;else left = mid + 1;} cout << right << Endl;} int main () {//freopen ("Input.txt", "R", stdin); int T; CIN >> T;while (t--) {init (); Solve ();} return 0;}

Uvalive 4254 Processor Processor (two-part simulation + greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.