Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7695 | Accepted: 2909 |
Description
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
Output
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
题意:已经提供你一天的每一小时需要出纳员的最少数量——R(0), R(1), ..., R(23)。
R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上午1:00
到2:00之间需要的,等等。以及知道应聘者开始工作的时间ti(连续 8 小时),
问最少需要请多少个出纳员。
分析:
设 have[i] 为来应聘的在第 i 个小时开始工作的人数
r[i] 为第 i 个小时至少需要的人数
x[i] 为招到的在第 i 个小时开始工作的人数
根据题意有:
0 <= x[i] <= have[i] //某个特定时间点招的人数最少为0,最多为应聘者人数
x[i-7] + x[i-6] + …+ x[i] >= r[i] (题目中的连续工作8小时)
再设 s[i] = x[1] + … + x[i]
则有: 由 0 <= x[i] <= have[i]
=> s[i] - s[i-1] >= 0
s[i-1] - s[i] >= -have[i]
由 x[i-7] + x[i-6] + …+ x[i] >= r[i]
=> s[i] - s[i-8] >= r[i] (8 <= i <= 24)
s[i] - s[i-8+24] >= r[i] - s[24] (1<= i <= 7)(代入理解)
还需要添加一个隐藏不等式: s[24] - s[0] >= ans(枚举的答案最小)
通过枚举 s[24],来检测是否满足条件,题目是求最小值,即求最长路,以0为源点。
注意也可以将不等式全部转化为 "<=" 形式,求的就是最短路而非最长路。
代码如下:
法一(直接从小到大找符合条件的答案)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define N 1200
#define inf 0x3f3f3f3f
int k, n, vis[N], dis[N], head[N], r[N], have[N], judge[N];
//s[i] <=> 第 1--i 小时工作的人数和
struct r
{
int u, v, w, next;
}rr[N];
void add(int u, int v, int w)
{
rr[k].v = v;
rr[k].w = w;
rr[k].next = head[u];
head[u] = k++;
}
void build(int ans)
{
k = 0;
memset(head, -1, sizeof(head));
add(0, 24, ans);
for (int i = 1; i <= 24; i++){
add(i - 1, i, 0);//s[i]-s[i-1]>=0
add(i, i - 1, -have[i]);//s[i]-s[i-1]<=have[i]
}
for (int i = 1; i <= 7; i++)
add(i + 16, i, r[i] - ans);//s[i]-s[i-8+24]>=r[i]-s[24]
for (int i = 8; i <= 24; i++)
add(i - 8, i, r[i]); //s[i] - s[i - 8] >= r[i](连续8个小时)
}
bool spfa(int ans)
{
int i, j, k;
memset(vis, 0, sizeof(vis));
memset(judge, 0, sizeof(judge));
memset(dis, -inf, sizeof(dis));
queue<int >q;
q.push(0);
vis[0] = judge[0] = 1;
dis[0] = 0;
while (!q.empty()){
k = q.front(); q.pop();
vis[k] = 0;
for (i = head[k]; i + 1; i = rr[i].next){
if (dis[rr[i].v] < dis[k] + rr[i].w){//求最长路
dis[rr[i].v] = dis[k] + rr[i].w;
if (!vis[rr[i].v]){
q.push(rr[i].v);
vis[rr[i].v] = 1;
if (++judge[rr[i].v]>25) return false;//判环
}
}
}
}
if (dis[24] == ans) return true;
return false;
}
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
int i, j, m, t, tt;
scanf("%d", &t);
while (t--){
for (i = 1; i <= 24; i++)
scanf("%d", &r[i]);
memset(have, 0, sizeof(have));
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%d", &tt);
have[tt + 1]++;//从1开始
}
bool ok = false;
for (i = 0; i <= n; i++){
build(i);
if (spfa(i)){
printf("%d\n", i);
ok = true; break;
}
}
if (!ok)
puts("No Solution");
}
return 0;
}
法二(利用二分查找)
<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define N 1200
#define inf 0x3f3f3f3f
int k, n, vis[N], dis[N], head[N], r[N], have[N], judge[N];
//s[i] <=> 第 1--i 小时工作的人数和
struct r
{
int u, v, w, next;
}rr[N];
void add(int u, int v, int w)
{
rr[k].v = v;
rr[k].w = w;
rr[k].next = head[u];
head[u] = k++;
}
void build(int ans)
{
k = 0;
memset(head, -1, sizeof(head));
add(0, 24, ans);
for (int i = 1; i <= 24; i++){
add(i - 1, i, 0);//s[i]-s[i-1]>=0
add(i, i - 1, -have[i]);//s[i]-s[i-1]<=have[i]
}
for (int i = 1; i <= 7; i++)
add(i + 16, i, r[i] - ans);//s[i]-s[i-8+24]>=r[i]-s[24]
for (int i = 8; i <= 24; i++)
add(i - 8, i, r[i]); //s[i] - s[i - 8] >= r[i](连续8个小时)
}
bool spfa(int ans)
{
int i, j, k;
memset(vis, 0, sizeof(vis));
memset(judge, 0, sizeof(judge));
memset(dis, -inf, sizeof(dis));
queue<int >q;
q.push(0);
vis[0] = judge[0] = 1;
dis[0] = 0;
while (!q.empty()){
k = q.front(); q.pop();
vis[k] = 0;
for (i = head[k]; i + 1; i = rr[i].next){
if (dis[rr[i].v] < dis[k] + rr[i].w){//求最长路
dis[rr[i].v] = dis[k] + rr[i].w;
if (!vis[rr[i].v]){
q.push(rr[i].v);
vis[rr[i].v] = 1;
if (++judge[rr[i].v]>25) return false;//判环
}
}
}
}
if (dis[24] == ans) return true;
return false;
}
int binary_search(int l, int r){
int ok = -1;
while (l <= r){
int mid = (l + r) >> 1;
build(mid);
if (spfa(mid)){// mid 表示一共能够雇佣的员工数量
ok = mid;
r = mid - 1;
//因为此题要求的是最小的雇佣人数,所以得判断 r=mid-1 是否满足题意,使之越来越小
}
else
l = mid + 1;
}
return ok;
}
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
int i, j, m, t, tt;
scanf("%d", &t);
while (t--){
for (i = 1; i <= 24; i++)
scanf("%d", &r[i]);
memset(have, 0, sizeof(have));
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%d", &tt);
have[tt + 1]++;
}
if (binary_search(0, n) != -1)
printf("%d\n", binary_search(0, n));
else
puts("No Solution");
}
return 0;
}