POJ 1275 Cashier Employment

Source: Internet
Author: User
Tags integer numbers min

Description

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

The manager has provided you with the least number of cashiers needed for every one-hour slots of the day. This data is given as R (0), R (1), ..., R (All): R (0) represents the least number of cashiers needed from midnight to a.m. , R (1) shows this number for duration of the a.m. to. Note that these numbers is the same every day. There is N qualified applicants for the this job. Each applicant I works non-stop once each hours in a shift of exactly 8 hours starting from a specified hour, say Ti (0 <= ti <=), exactly from the start of the hour mentioned. That's, if the ith applicant is hired, he/she'll work starting from ti o ' clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there is enough cash registers and counters for th OSE who is hired.

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

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

For each test case, the output should are written in one line, and which is the least number of cashiers needed.
If There is no solution for the test case, you should write no solution for the 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
Source

Tehran 2000

Analysis:
Analysis:
We have dealt with the problem in the way that we have just mentioned. It is easy to think of the following inequality model:
Set num[i] for the number of people who can start working at a time, x[I] is the actual number of people employed, then x[i]<=num[i].
Set r[i] is at least the number of people who need to work at the moment, thus having the following relationship:
x[I-7]+x[I-6]+x[I-5]+x[I-4]+x[I-3]+x[I-2]+x[I-1]+x[i]>=r[i]
Set s[i]=x[1]+x[2]...+x[i], get
0<=s[i]-s[I-1]<=num[i], 0<=i<=23
s[i]-s[I-8]>=r[i], 8<=i<=23
s[]+s[i]-s[i+16]>=r[i], 0<=i<=7
For the above-mentioned set of inequalities, we use a very clumsy approach to this series of inequalities (which in fact makes messy formulas more orderly and easier to handle). First we need to understand the application object of the differential constraint system (which is usually for multiple two-item subtraction inequalities) so we convert all of the above to two unknowns on the left, the other constant on the right, and the middle with the >= connection, namely:
s[I]-s[I-1]>=0 (0<=i<=23)
s[I-1]-s[i]>=-num[i] (0<=i<=23)
s[i]-s[I-8]>=r[i] (8<=i<=23)
s[i]-s[i+16]>=r[i]-s[] (0<=i<= 7)
There are small difficulties here, and we find that the above formula is not a standard differential constraint system because there are three unknown units in the last equation. But notice that there are only two of them following I change, so s[23] becomes special, it seems that we need to deal with it alone, so we put s[23] as a known amount on the right.
After this arrangement, the entire diagram is easy to create, will be all shaped like a-b>=c of the formula we from the node B to a side of the weight of the right to the C (here note that because of the determination, the formula is a unified >= inequality, so a and B is relatively determined, the edge is necessarily point to a), The picture is built.
Finally enumerate all the possible values of all s[23], for each s[23], we do a conventional differential constraint system problem solving, to determine whether this situation is feasible, if feasible to find the optimal value, recorded in ans, the last ans value is the request.

Code:

Source code problem:1275 memory:892k time:63ms language:pascal result:accepted Source Code cons
T maxn=1000;
  Type Node=record u,v,w:longint;
End
  var n,m,casen,casei:longint;
  E:ARRAY[1..MAXN] of node;

T,R,D:ARRAY[-1..MAXN] of Longint;
  Procedure Add (X,y,z:longint);
    Begin Inc (M); With E[m] do begin u:=x; V:=y;
    W:=z;
  End

End
  Procedure Init;
  var i,x:longint;
    Begin for I:=0 to + do read (R[i]);
    READLN;
    READLN (n);
    Fillchar (t,sizeof (t), 0); For I:=1 to n do begin READLN (x); Inc (T[x]);
  End

End
  Procedure main;
    var i,j,min:longint;
  Check:boolean;
      Begin for Min:=1 to n do begin m:=0;
      for I:=-1 to d[i]:=100000;
      Add (23,-1,-min); For i:=0 to-do begin add (i,i-1,0);
          Add (I-1,i,t[i]);
        If I>7 then add (I,i-8,-r[i]) else Add (i,i+16,-r[i]+min);
      End For j:=0 to + do begin CHECK:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin D[v]:=d[u
                ]+w;
              Check:=false;
          End
        If check then break;
      End
      Check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin Check:=false; Break
      End If check then begin Writeln (min); Exit
    End
    End
  Writeln (' No solution ');

End
  Begin READLN (CASEN);
      For casei:=1 to Casen do BEGIN init;
    Main
End
End.
Const maxn=1000;
  Type Node=record u,v,w:longint;
End
  var n,m,casen,casei:longint;
  E:ARRAY[1..MAXN] of node;

T,R,D:ARRAY[-1..MAXN] of Longint;
  Procedure Add (X,y,z:longint);
    Begin Inc (M); With E[m] do begin u:=x; V:=y;
    W:=z;
  End

End
  Procedure Init;
  var i,x:longint;
    Begin for I:=0 to + do read (R[i]);
    READLN;
    READLN (n);
    Fillchar (t,sizeof (t), 0);
     For I:=1 to N do Begin READLN (x); Inc (T[x]);
  End

End
  Procedure main;
    var i,j,min:longint;
  Check:boolean;
      Begin for Min:=1 to n do begin m:=0;
      for I:=-1 to d[i]:=100000;
      Add (23,-1,-min); For i:=0 to-do begin add (i,i-1,0);
          Add (I-1,i,t[i]);
        If I>7 then add (I,i-8,-r[i]) else Add (i,i+16,-r[i]+min);
      End
          For j:=0 to + do begin check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin D[v]:=d[u
                ]+w;
              Check:=false;
          End
        If check then break;
      End
      Check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin Check:=false; Break
      End If check then begin Writeln (min); Exit
    End
    End
  Writeln (' No solution ');

End
  Begin READLN (CASEN);
      For casei:=1 to Casen do BEGIN init;
    Main
End
End.Const maxn=1000;
  Type Node=record u,v,w:longint;
End
  var n,m,casen,casei:longint;
  E:ARRAY[1..MAXN] of node;

T,R,D:ARRAY[-1..MAXN] of Longint;
  Procedure Add (X,y,z:longint);
    Begin Inc (M); With E[m] do begin u:=x; V:=y;
    W:=z;
  End

End
  Procedure Init;
  var i,x:longint;
    Begin for I:=0 to + do read (R[i]);
    READLN;
    READLN (n);
    Fillchar (t,sizeof (t), 0); For I:=1 to n do begin READLN (x); Inc (T[x]);
  End

End
  Procedure main;
    var i,j,min:longint;
  Check:boolean;
      Begin for Min:=1 to n do begin m:=0;
      for I:=-1 to d[i]:=100000;
      Add (23,-1,-min); For i:=0 to-do begin add (i,i-1,0);
          Add (I-1,i,t[i]);
        If I>7 then add (I,i-8,-r[i]) else Add (i,i+16,-r[i]+min);
      End
          For j:=0 to + do begin check:=true;
  For I:=1 to M does with E[i] does if d[v]>d[u]+w then BEGIN              D[v]:=d[u]+w;
              Check:=false;
          End
        If check then break;
      End
      Check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin Check:=false; Break
      End If check then begin Writeln (min); Exit
    End
    End
  Writeln (' No solution ');

End
  Begin READLN (CASEN);
      For casei:=1 to Casen do BEGIN init;
    Main
End
End.
Const maxn=1000;
  Type Node=record u,v,w:longint;
End
  var n,m,casen,casei:longint;
  E:ARRAY[1..MAXN] of node;

T,R,D:ARRAY[-1..MAXN] of Longint;
  Procedure Add (X,y,z:longint);
    Begin Inc (M); With E[m] do begin u:=x; V:=y;
    W:=z;
  End

End
  Procedure Init;
  var i,x:longint;
    Begin for I:=0 to + do read (R[i]);
    READLN;
    READLN (n);
    Fillchar (t,sizeof (t), 0); For I:=1 to n do begin READLN (x); Inc (T[x]);
  End

End
  Procedure main;
    var i,j,min:longint;
  Check:boolean; Begin FOR min:=1 to n do begin m:=0;
      for I:=-1 to d[i]:=100000;
      Add (23,-1,-min); For i:=0 to-do begin add (i,i-1,0);
          Add (I-1,i,t[i]);
        If I>7 then add (I,i-8,-r[i]) else Add (i,i+16,-r[i]+min);
      End
          For j:=0 to + do begin check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin D[v]:=d[u
                ]+w;
              Check:=false;
          End
        If check then break;
      End
      Check:=true; For I:=1 to M does with E[i] does if d[v]>d[u]+w then begin Check:=false; Break
      End If check then begin Writeln (min); Exit
    End
    End
  Writeln (' No solution ');

End
  Begin READLN (CASEN);
      For casei:=1 to Casen do BEGIN init;
    Main
End End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.