1. 程式人生 > >poj 1275 Cashier Employment - 差分約束 - 二分答案

poj 1275 Cashier Employment - 差分約束 - 二分答案

log ger 開始 tput namespace tin sam sin code

A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired you to help him, solve his problem. The problem is that the supermarket needs different number of cashiers at different times of each day (for example, a few cashiers after midnight, and many in the afternoon) to provide good service to its customers, and he wants to hire the least number of cashiers for this job.


The manager has provided you with the least number of cashiers needed for every one-hour slot of the day. This data is given as R(0), R(1), ..., R(23): R(0) represents the least number of cashiers needed from midnight to 1:00 A.M., R(1) shows this number for duration of 1:00 A.M. to 2:00 A.M., and so on. Note that these numbers are the same every day. There are N qualified applicants for this job. Each applicant i works non-stop once each 24 hours in a shift of exactly 8 hours starting from a specified hour, say ti (0 <= ti <= 23), exactly from the start of the hour mentioned. That is, if the ith applicant is hired, he/she will work starting from ti o‘clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there are enough cash registers and counters for those who are hired.

You are to write a program to read the R(i) ‘s for i=0..23 and ti ‘s for i=1..N that are all, non-negative integer numbers and compute the least number of cashiers needed to be employed to meet the mentioned constraints. Note that there can be more cashiers than the least number needed for a specific slot.

Input

The first line of input is the number of test cases for this problem (at most 20). Each test case starts with 24 integer numbers representing the R(0), R(1), ..., R(23) in one line (R(i) can be at most 1000). Then there is N, number of applicants in another line (0 <= N <= 1000), after which come N lines each containing one ti (0 <= ti <= 23). There are no blank lines between test cases.

Output

For each test case, the output should be written in one line, which is the least number of cashiers needed.
If there is no solution for the test case, you should write No Solution for that case.

Sample Input

1
1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
5
0
23
22
1
10

Sample Output

1

題目大意

  一個超市在第$i$小時中工作的員工數目不能少於$req[i]$個。有$n$個應聘的人,第$i$個人願意從$t_{i}$開始工作8小時,問最少需要聘請多少人才能使得達到要求。

  設$x_{i}$表示第$i$個小時中開始工作的員工數目。為了表示八個小時內的員工數目,定義$s_{i} = x_{0} + \cdots + x_{i - 1}$。用$own[i]$表示願意從時刻$i$開始工作的人數

  於是便有如下一些不等式:

  • $0 \leqslant s_{i} - s_{i - 1} \leqslant own[i - 1]$
  • $\left\{\begin{matrix}s_{i} - s_{i - 8}\geqslant req[i - 1]\ \ \ \ \ \ \ \ \ \ \left ( i \geqslant 8 \right ) \\ s_{16 + i} - s_{i}\leqslant s_{24} - req[i - 1]\ \left ( i \leqslant 8 \right )\end{matrix}\right.$

  但是發現第二個不等式組中的第二個不等式含有3個未知量,即$s_{24}$,但是總共就只有這麽一個,可以考慮枚舉它。

  顯然答案滿足二分性質,所以二分它,增加限制$s_{24} = mid$。

Code

 1 /**
 2  * poj
 3  * Problem#1275
 4  * Accepted
 5  * Time: 16ms
 6  * Memory: 672k
 7  */
 8 #include <iostream>
 9 #include <cstring>
10 #include <cstdio>
11 #include <queue>
12 using namespace std;
13 typedef bool boolean;
14 
15 const int N = 26;
16 
17 int T;
18 int n;
19 int req[N], own[N];
20 int g[N][N];
21 int f[N];
22 int lab[N];
23 boolean vis[N];
24 
25 inline void init() {
26     for (int i = 0; i < 24; i++)
27         scanf("%d", req + i);
28     scanf("%d", &n);
29     memset(own, 0, sizeof(own));
30     for (int i = 1, x; i <= n; i++) {
31         scanf("%d", &x);
32         own[x]++;
33     }
34 }
35 
36 queue<int> que;
37 inline boolean check(int mid) {
38     for (int i = 1; i < 8; i++)
39         g[16 + i][i] = req[i - 1] - mid;
40     g[0][24] = mid;
41     g[24][0] = -mid;
42     fill(f, f + 25, -2333);
43     memset(lab, 0,    sizeof(lab));
44     que.push(0);
45     f[0] = 0;
46     while (!que.empty()) {
47         int e = que.front();
48         que.pop();
49         vis[e] = false;
50         if (++lab[e] >= 25)    return false;
51         for (int i = 0; i < 25; i++)
52             if (g[e][i] >= -1000 && f[e] + g[e][i] > f[i]) {
53                 f[i] = f[e] + g[e][i];
54                 if (!vis[i]) {
55                     que.push(i);
56                     vis[i] = true;
57                 }
58             }
59     }
60 //    for (int i = 0; i <= 24; i++)
61 //        cerr << f[i] << " ";
62 //    cerr << endl;
63     return true;
64 }
65 
66 inline void solve() {
67     memset(g, 0x80, sizeof(g));
68     for (int i = 0; i < 24; i++)
69         g[i][i + 1] = 0, g[i + 1][i] = -own[i];
70     for (int i = 8; i <= 24; i++)
71         g[i - 8][i] = req[i - 1];
72     int l = 0, r = n;
73     while (l <= r) {
74         int mid = (l + r) >> 1;
75         if (check(mid))
76             r = mid - 1;
77         else
78             l = mid + 1;
79     }
80     if (r == n)
81         puts("No Solution");
82     else
83         printf("%d\n", r + 1);
84 }
85 
86 int main() {
87     scanf("%d", &T);
88     while(T--) {
89         init();
90         solve();
91     }
92     return 0;
93 }

poj 1275 Cashier Employment - 差分約束 - 二分答案