The main idea: there are n tasks sent to the processor processing, each task information includes R,D,W,R representative start time, W represents the time that must end, W refers to how much time to deal with.
Where the processor speed can be variable, ask the processor minimum how much speed to complete the work?
Input:
3,
5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 4 8 Ten 7 5 8 one
5
10 13 1 0 of
5
8 (
8
) (1 6) 3 5 (22). 25 10
Output:
2
5
7
The topic shows moderate (medium difficulty), but in fact it is a difficult subject, without which the thought of this process cannot be done.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Processing thought: The rate per second is equivalent to a pillar, each column can accommodate the number of W, priority to deal with urgent tasks!
With the use of the two-point method, the priority queue greedy method can finally be solved.
The original topic is as follows:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4168
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include &l
t;cmath> #include <queue> #include <map> #include <vector> using namespace std;
struct Work {int R, D, W;
BOOL operator< (const Work &W2) const//The original Priority_queue < is not easy to write to, note the const {return d > w2.d;
}
};
inline bool Compare_r (const Work &a, const Work &b) {return A.R < B.R;
Class Process {public:const static int max_speed = 5000;//maximum certainty: 20000 * 1000;
Enum SPEED {over_speed, need_speed};
void processing () {int T = 0;
cin>>t;
int n = 0;
Work tmp;
while (t--) {cin>>n;
Vector<work> ws;
for (int i = 0; i < n; i++) { cin>>tmp.r>>tmp.d>>tmp.w;
Ws.push_back (TMP);
Sort (Ws.begin (), Ws.end (), compare_r);
Cout<<bisearchspeed (WS, 1, max_speed) <<endl; } int Bisearchspeed (vector<work> &ws, int low, int up) {while (Low <= u
p) {int mid = low + ((Up-low) >>1);
if (Over_speed = = Adjustspeed (mid, ws)) up = mid-1;
else low = mid+1;
return to Low;
//A little leetcode the problem of water storage.
Processing thought: The rate per second is equivalent to a pillar, each column can accommodate the number of W, priority to deal with urgent tasks!
SPEED adjustspeed (const unsigned SPEED, vector<work> &ws) {priority_queue<work> PQ;
int cursec = 1;
int i = 0; while (I < ws.size () | |!pq.empty ()) {for (; I < ws.size () && WS[I].R <= Curse C
i++) Pq.push (Ws[i]); IntSecpower = speed;
while (Secpower &&!pq.empty ()) {//Note headache Error: This is to manipulate top, not copy!!!
Work wk = Pq.top ();
if (wk.d <= cursec) return need_speed;//error: note must be <=, otherwise the answer is wrong!!!
Pq.pop ();
if (Wk.w > Secpower) {wk.w-= secpower;//use up to the ' sec
Pq.push (wk);
Secpower = 0;//has been used up} else secpower-= WK.W;
} cursec++;
return over_speed; }
};
Debug functions:
int main ()
{
Process pro;
Pro.processing ();
return 0;
}