Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7651 | Accepted: 2886 |
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
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
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
#include <iostream>
#include <cstring>
#include <cstdio>
#define INF -1000000000
using namespace std;
int r[],t[];
int cnt,fir[],nxt[],to[],val[];
void addedge(int a,int b,int v)
{
nxt[++cnt]=fir[a];
fir[a]=cnt;to[cnt]=b;
val[cnt]=v;
}
int n,ans;
int dis[],in[],vis[],q[];
bool Spfa()
{
int S=,f=,b=;
for(int i=;i<=;i++)
dis[i]=INF;
memset(in,,sizeof(in));
memset(vis,,sizeof(vis)); q[b++]=S;
dis[S]=;
in[S]++;
vis[S]=;
while(f<b){
int node=q[f++];vis[node]=false;
for(int i=fir[node];i;i=nxt[i])
if(dis[to[i]]<dis[node]+val[i]){
dis[to[i]]=dis[node]+val[i];
if(!vis[to[i]]){
if(++in[to[i]]>n)
return false;
q[b++]=to[i];
vis[to[i]]=true;
}
}
}
return dis[]==ans;
} void Build()
{
memset(fir,,sizeof(fir));cnt=;
for(int i=;i<=;i++)
addedge(i-,i,r[i]); for(int i=;i<=;i++)
addedge(i+,i,r[i]-ans);
for(int i=;i<;i++){
addedge(i+,i,-t[i]);
addedge(i,i+,);
}
addedge(,,ans);
} void Solve()
{
int r=,h=n+;
while(h-r>)
{
ans=(r+h)>>;
Build();
if(Spfa())
h=ans;
else
r=ans;
}
if(h==n+)
printf("No Solution\n");
else
printf("%d\n",h);
} int main(){
int T;int x;
scanf("%d",&T);
while(T--)
{
for(int i=;i<=;i++)
scanf("%d",&r[i]);
scanf("%d",&n);
memset(t,,sizeof(t));
for(int i=;i<=n;i++){ scanf("%d",&x);
++t[x];
}
Solve();
}
return ;
}