前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ1275 Cashier Employment(差分约束)

POJ1275 Cashier Employment(差分约束)

作者头像
attack
发布2018-04-10 16:58:49
5960
发布2018-04-10 16:58:49
举报

Description

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

代码语言:javascript
复制
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

代码语言:javascript
复制
1

Source

题意:

在一家超市里,每个时刻都需要有营业员看管,R(i) (0 <= i < 24)表示从i时刻开始到i+1时刻结束需要的营业员的数目,现在有N(N <= 1000)个申请人申请这项工作,并且每个申请者都有一个起始工作时间 ti,如果第i个申请者被录用,那么他会从ti时刻开始连续工作8小时。现在要求选择一些申请者进行录用,使得任何一个时刻i,营业员数目都能大于等于R(i)。求出至少需要录用多少营业员。

我们用S[i]表示一天内前i+1个小时录用的人员,

当i>=7时,我们需要满足s[i]-s[i-8]>=R[i]

当0<=i<7,经过推倒不难发现,我们需要满足s[i]+s[23]-s[i+16]>=R[i]

同时,因为题目中人数的限制,我们还需要满足0<=s[i]-s[i-1]<=b[i]

这样这道题就看起来可做了

但是我们的第二个式子左边有三项,不过还好s[23]是个常数项,我们可以二分解决

因为是求最小,所以我们按照套路连边,求最长路

代码语言:javascript
复制
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1e8+10
using namespace std;
const int MAXN=1e5+10;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
char buf[MAXN],*p1=buf,*p2=buf;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int R[MAXN],N,pep[MAXN],dis[MAXN],vis[MAXN];
struct node
{
    int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=1;
inline void AddEdge(int x,int y,int z)
{
    edge[num].u=x;
    edge[num].v=y;
    edge[num].w=z;
    edge[num].nxt=head[x];
    head[x]=num++;
}
void PRE()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    num=1;
}
int SPFA(int val)
{
    memset(dis,-0x7f,sizeof(dis));
    queue<int>q;
    dis[0]=0;
    q.push(0);
    while(q.size()!=0)
    {
        int p=q.front();q.pop();
        vis[p]=0;
        if(p==24&&dis[p]>val) return 0;
        for(int i=head[p];i!=-1;i=edge[i].nxt)
        {
            if(dis[edge[i].v]<dis[p]+edge[i].w)
            {
                dis[edge[i].v]=dis[p]+edge[i].w;
                if(!vis[edge[i].v])    vis[edge[i].v]=1,q.push(edge[i].v);
            }
        }
    }
    return dis[24]<=val?1:0;
    
}
int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #else
    #endif
    int QWQ=read();
    while(QWQ--)
    {    
        memset(pep,0,sizeof(pep));
        for(int i=0;i<=23;i++) R[i]=read();
        N=read();
        for(int i=1;i<=N;i++) 
            pep[read()]++;
        int r=N+1,l=0;
        int ans=INF;
         while(l<=r)
        {
            PRE();
            int mid=l+r>>1;
            for(int i=0;i<=23;i++) AddEdge(i,i+1,0),AddEdge(i+1,i,-pep[i]);
            for(int i=7;i<=23;i++) AddEdge(i-7,i+1,R[i]); 
            AddEdge(0,24,mid);AddEdge(24,0,-mid);
            for(int i=0;i<7;i++) AddEdge(i+17,i+1,R[i]-mid);
            if(SPFA(mid)) ans=mid,r=mid-1;
            else l=mid+1;
        }
        if(ans>N) printf("No Solution\n");
        else printf("%d\n",ans);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
  • Sample Input
  • Sample Output
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档