HDU1529:Cashier Employment(差分约束 & 二分)

Cashier Employment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1292    Accepted Submission(s): 584


Problem 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
  
  
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

题意:一家商店老板要求0~23时各至少要有r[i]人值班,现有N分简历,每个应聘者有个开始工作时间,且开始工作就连续工作8个小时,问至少招聘多少人满足老板的要求。

思路:差分约束,先找固定的条件,即每个小时的应聘者有几个人,Ti-Ti-1 <= Sum(Ti),Ti-1 - Ti >= 0,然后是老板的条件,i - i-8 >= Ri(i>=8),  24-(16+i)+i >= Ri(i<8),因为这里不等式左边是区间下标,三个项不符合差分约束的要求,于是二分“24”,即二分0~24招聘了几个人。参考资料:点击打开链接

# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
struct node
{
    int e, w, next;
}edge[10000];
int n, m, tot;
int num[26], Next[26], dis[26],vis[26], sum[26], in[26], q[10000];
void add_edge(int u, int v, int w)
{
    edge[tot] = node{v, w, Next[u]};
    Next[u] = tot++;
}

bool spfa()
{
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    int r = 0;
    q[r++] = 0;
    dis[0] = 0;
    in[0] = vis[0] = 1;
    while(r)
    {
        int u = q[--r];
        vis[u] = 0;
        for(int i=Next[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].e, w = edge[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!vis[v])
                {
                    vis[v] = 1;
                    q[r++] = v;
                    if(++in[v] > 25)
                        return false;
                }
            }
        }
    }
    return true;
}
bool check(int x)
{
    tot = 0;
    memset(Next, -1, sizeof(Next));
    for(int i=1; i<=24; ++i)
    {
        add_edge(i-1, i, 0);
        add_edge(i, i-1, sum[i]);
        if(i >= 8) add_edge(i-8, i, -num[i]);
        else add_edge(16+i, i, x-num[i]);
    }
    add_edge(24, 0, x);
    add_edge(0, 24, -x);
    return spfa();
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum, 0, sizeof(sum));
        memset(num, 0, sizeof(num));
        for(int i=1; i<=24; ++i)
            scanf("%d",&num[i]);
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
        {
            scanf("%d",&m);
            ++sum[m+1];
        }
        int l=0, r=n, ans=-1;
        while(l<=r)
        {
            int mid = l+r>>1;
            if(check(mid))
            {
                ans = mid;
                r = mid-1;
            }
            else l = mid+1;
        }
        if(ans == -1) puts("No Solution");
        else printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值