Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4907
N tasks on the worksheet are given. The I-th task takes so long as Ti (the duration is Ti ~ Ti + 1. There are m queries, and each query is a numerical Q, indicating that there is a task request other than N tasks in the Q time. The machine is executed according to the task time of the worksheet. If there is a blank time, it will execute the task request outside the worksheet.
Do it directly and Time Out decisively! 1e5*2e5 !!! (The worst case of M queries + q traversal)
Binary solution ~~~~ At first, I didn't just store the free time. I also stored the Time of the N tasks to be processed on the worksheet. As a result, the number of tasks written in the worksheet is no less than three ~~~~ =
The second thought is actually easy to understand. It's still the first time to actually use it ~~~ To commemorate ^_^
(1) This is for reference to others, but it takes a little time
EXE time:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 using namespace STD; 6 7 const int maxn = 2e5; 8 int A [maxn], B [maxn]; 9 10 int main () 11 {12 INT t, n, m, Ti, query; 13 while (scanf ("% d", & T )! = EOF) 14 {15 while (t --) 16 {17 memset (A, 0, sizeof (a); 18 memset (B, 0, sizeof (B )); 19 scanf ("% d", & N, & M); 20 for (INT I = 0; I <n; I ++) 21 {22 scanf ("% d", & Ti); 23 A [Ti] = 1; 24} 25 int Len = 0; 26 for (INT I = 1; I <= maxn; I ++) 27 {28 If (! A [I]) 29 B [Len ++] = I; // store the free time 30} 31 for (INT I = 0; I <m; I ++) 32 {33 scanf ("% d", & query); 34 if (! A [query]) 35 printf ("% d \ n", query); 36 else37 {38 int flag = 0; 39 int L = 0, r = len-1; 40 while (L <= R) 41 {42 int mid = (L + r)/2; 43 If (B [Mid] = query) 44 {45 printf ("% d \ n", B [Mid]); 46 flag = 1; 47 break; 48} 49 else if (B [Mid] <query) 50 L = Mid + 1; 51 else if (B [Mid]> query) 52 r = mid-1; 53} 54 if (! Flag) 55 printf ("% d \ n", B [l]); 56} 57} 58} 59} 60 return 0; 61}
(2) My improved version (in fact, you do not need to save maxn, that is, all the idle time of 2e5, as long as you save the maximum time of the original n tasks, save it at the time of + 1 !!!)
So that's what Maxi + 1 means.
EXE time:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 using namespace STD; 6 7 const int maxn = 2e5; 8 int vis [maxn], B [maxn]; 9 10 int main () 11 {12 INT t, n, m, t, q; 13 while (scanf ("% d", & T )! = EOF) 14 {15 while (t --) 16 {17 memset (VIS, 0, sizeof (VIS); 18 scanf ("% d", & N, & M); 19 int Maxi = 1; 20 for (INT I = 1; I <= N; I ++) 21 {22 scanf ("% d ", & T); 23 Maxi = max (Maxi, T); 24 vis [T] = 1; 25} 26 int Len = 0; 27 for (INT I = 1; I <= Maxi + 1; I ++) // Maxi + 1 indicates that the maximum time spent in N tasks is Maxi. Assume that a maxi/maxi + 1 task is encountered, the task execution time is Maxi + 1 28 {29 If (! Vis [I]) 30 B [Len ++] = I; 31} 32 While (M --) 33 {34 scanf ("% d", & Q ); 35 if (! Vis [Q]) 36 printf ("% d \ n", q); 37 else38 {39 int L = 0, r = len-1; 40 int flag = 0; 41 while (L <= r) 42 {43 int mid = (L + r)> 1; 44 If (B [Mid] = q) 45 {46 flag = 1; 47 printf ("% d \ n", B [Mid]); 48 break; 49} 50 else if (B [Mid]> q) 51 r = mid-1; 52 else if (B [Mid] <q) 53 L = Mid + 1; 54} 55 if (! Flag) 56 printf ("% d \ n", B [l]); 57} 58} 59} 60} 61 Return 0; 62}
Bestcoder3 1001 task schedule (HDU 4907) solution report