Test instructions
There are drivers, the afternoon route, the evening route each n. Give each driver exactly one afternoon course and night route. Give the time to drive each route, and if the driver is driving more than D, pay the DXR for overtime. Ask how to assign routes to make the least amount of overtime.
Although the code looks very water. But have not understood why, find the explanation of the great God with reference to you. Http://www.cnblogs.com/AOQNRMGYXLMV/p/4122236.html
Suppose: a1≥a2,b1≤b2, the horizontal line represents D
Situation One:
, the driver has to pay overtime, the driver two do not, if we will B1, B2 Exchange:
Because of the B1≤B2, so pay the driver one of the overtime will not be less, and driver two driving time will not increase, so do not pay overtime.
Therefore, the total overtime pay will not be reduced after the exchange.
Situation Two:
Two drivers have to pay overtime, the time exceeded (A1 + b1-d) + (A2 + b2-d)
If Exchange B1, B2:
- If two drivers still exceed normal working hours, the total overtime cost will not change.
- If the driver does not work overtime after the exchange, the driver will exceed the time (A1 + b2-d). Use this minus the original time: (A1 + b2-d)-(A1 + b1-d)-(A2 + b2-d) = D-(B1 + A2), because at this time the driver two do not work overtime, so the original ≥0, so the total out of time will not reduce
Situation Three:
The driver pays the driver without paying overtime. Overtime at this time is (A2 + b2-d)
If Exchange B1, B2:
by B1≤B2,A1≥A2, so B2 added to the A1, the driver will work overtime, driver two will not work overtime, at this time overtime for (A1 + b2-d), minus the original time for (A1 + b2-d)-(A2 + b2-d) = (A1-A2) ≥0
So the total overtime time will not be reduced.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6 7 using namespacestd;8 Const intMAXN =1010;9 intMOR[MAXN], AFT[MAXN];Ten intMain () One { A intN, D, R; - while(~SCANF ("%d%d%d", &n, &d, &R)) - { the if(!n &&!d &&!r) Break; - intsum =0; - for(inti =0; I < n; i++) -scanf"%d", &mor[i]); + for(inti =0; I < n; i++) -scanf"%d", &aft[i]); +Sort (mor, mor+n); Sort (aft, aft+n); A intAns =0; at for(inti =0; I < n; i++) - { -Ans + = ((mor[i]+aft[n-i-1]-d) *r) >=0? (mor[i]+aft[n-i-1]-d) *r:0; - } -printf"%d\n", ans); - } in}View Code
"Strategy" UVa 11389-the Bus Driver problem