比较经典的差分约束

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

题目大意

直接挂loj的翻译算了……

题目分析

算是差分约束类型有难度并且挺经典的题目。

设$s_i$为$i$时刻能够开始工作的人数;$x_i$为$i$时刻实际雇佣的人数。于是有$x_i≤num_i$。设$a_i$为$i$时刻至少需要工作的人数。有:

$x_{i-7}+x_{i-6}+...+x_{i-1}+x_i≥a_i$

设$t_i=x_1+x_2+...+x_i$,则得到

$0≤t_i-t_{i-1}≤s_i,0≤i≤23,$

$t_i-t_{i-8}≥a_i,8≤i≤23,$

$t_{23}+t_i-t_{i+16}≥a_i,0≤i≤7$

那么在建出约束关系之后,就是枚举$t_{23}$.

之后就是处理的细节需要注意一下。

 #include<cstdio>
#include<cctype>
#include<cstring>
const int maxn = ;
const int maxm = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxm];
int T,n,ans,a[],s[maxn],dis[maxn];
int edgeTot,head[maxn],nxt[maxm];
bool vis[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void init()
{
edgeTot = ;
memset(dis, -0x3f3f3f3f, sizeof dis);
// memset(dis, 0, sizeof dis);
memset(vis, , sizeof vis);
memset(head, -, sizeof head);
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
}
bool dfs(int x)
{
vis[x] = ;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].y, w = edges[i].val;
if (dis[v] < dis[x]+w){
dis[v] = dis[x]+w;
if (vis[v]||dfs(v)) return ;
}
}
vis[x] = ;
return ;
}
bool check(int w)
{
init(), dis[] = ;
// for (int i=1; i<=23; i++) addedge(i-1, i, 0);addedge(23, 0, 0);
// for (int i=1; i<=23; i++) addedge(i, i-1, -w);addedge(0, 23, -w);
for (int i=; i<=; i++) addedge(i-, i, ), addedge(i, i-, -s[i]);
// for (int i=8; i<=23; i++) addedge(i-8, i, a[i]);
// for (int i=0; i<=7; i++) addedge(i+16, i, s[i]-w);    //注意细节处理
for (int i=; i<=; i++) addedge(i-, i, a[i]);
for (int i=; i<=; i++) addedge(i+, i, a[i]-w);
addedge(, , w);
return dfs();
}
int main()
{
T = read();
while (T--)
{
memset(s, , sizeof s);
for (int i=; i<=; i++) a[i] = read();
n = read(), ans = -;
for (int i=; i<=n; i++) s[read()+]++;
for (int i=; i<=n; i++)
if (!check(i)){
ans = i;
break;
}
if (ans==-) puts("No Solution");
else printf("%d\n",ans);
}
return ;
}

END

【差分约束】poj1275Cashier Employment的更多相关文章

  1. 【POJ1275】Cashier Employment 差分约束

    [POJ1275]Cashier Employment 题意: 超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23).R(0)表示从午夜到凌晨1:00所需要 ...

  2. POJ 1275 Cashier Employment(差分约束)

    http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...

  3. POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作, ...

  4. POJ1275 Cashier Employment 二分、差分约束

    传送门 题意太长 为了叙述方便,将题意中的$0$点看作$1$点,$23$点看做$24$点 考虑二分答案(其实从小到大枚举也是可以的) 设$x_i$是我们选的雇员第$i$小时开始工作的人数,$s_i$是 ...

  5. POJ1275 Cashier Employment 【二分 + 差分约束】

    题目链接 POJ1275 题解 显然可以差分约束 我们记\(W[i]\)为\(i\)时刻可以开始工作的人数 令\(s[i]\)为前\(i\)个时刻开始工作的人数的前缀和 每个时刻的要求\(r[i]\) ...

  6. POJ 1275 Cashier Employment 挺难的差分约束题

    http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...

  7. hdu1529 Cashier Employment[差分约束+二分答案]

    这题是一个类似于区间选点,但是有一些不等式有三个未知量参与的情况. 依题意,套路性的,将小时数向右平移1个单位后,设$f_i$为前$i$小时工作的人数最少是多少,$f_{24}$即为所求.设$c_i$ ...

  8. ACM差分约束笔记

    https://www.cnblogs.com/31415926535x/p/10463112.html 很早之前学最短路的时候就看了一眼差分约束,,当时以为这种问题不怎么会出现,,而且当时为了只为了 ...

  9. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

随机推荐

  1. 关于setTimeout(fn,0)

    JS是单线程引擎:它把任务放到队列中,不会同步去执行,必须在完成一个任务后才开始另外一个任务. 浏览器的内核是多线程的,它们在内核制控下相互配合以保持同步,一个浏览器至少实现三个常驻线程:javasc ...

  2. 本机和虚拟机互联 设置静态IP vmware 虚拟网络 桥接 NAT 仅主机 自定义

  3. day6作业详解

    1.day6题目 1,老男孩好声⾳选秀⼤赛评委在打分的时候呢, 可以进⾏输入. 假设, 老男孩有10个评委. 让10个评委进⾏打分, 要求, 分数必须⼤于5分, 小于10分. 电影投票. 程序先给出⼀ ...

  4. window.location.origin兼容问题

    if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + ...

  5. csdn自动展开+去广告+净化剪切板+免登陆(如有侵权,立即删博)

    对于csdn的广告大家想必......又没钱充VIP,怎么办,下面是脚本源码: 重要的事说三遍:如有侵权,立即删除!如有侵权,立即删除!如有侵权,立即删除! // ==UserScript== // ...

  6. CodeForces - 1003-B-Binary String Constructing (规律+模拟)

    You are given three integers aa, bb and xx. Your task is to construct a binary string ssof length n= ...

  7. NET?.NET Framework?.NET Core?

    什么是.NET?什么是.NET Framework?什么是.NET Core? https://www.cnblogs.com/1996V/p/9037603.html 什么是.NET?什么是.NET ...

  8. java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this JAXP implementation or earlier: class gnu.xml.dom.JAXPFactory的解决办法(图文详解)

    不多说,直接上干货! 问题详情 java.lang.UnsupportedOperationException: setXIncludeAware is not supported on this J ...

  9. C#微信支付

    回归主题,16年1月初我对微信开发比较好奇,由于自己是一个比较喜欢钱的人,所以对支付功能颇为冲动,就用公司信息在微信平台申请了一个服务号,还给腾讯打赏了300大洋做了下认证,抽空看了下微信支付官方的文 ...

  10. 利用Vagrant and VirtualBox搭建core os环境

    利用Vagrant and VirtualBox搭建core os环境 系统环境 ubuntu 14.04 x64 vagrant 1.7.4 virtualbox 4.3.10 git 1.9.1 ...