比较经典的差分约束

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. 编译最新linux内核(version 4.4.2)

    环境:centos6.4 内核版本为2.6.32 目标:编译4.4.2内核,升级到 4.4.2 准备工作: 安装开发库和ncurses库 # yum groupinstall "Develo ...

  2. [题解](双向bfs)hdu_3085_Nightmare Ⅱ

    发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向bfs,比较普通, 在判断是否碰到ghost时只要比较两点的曼哈顿距离大小和step*2(即ghost扩散的距离)即可,仔细思考也是可以想到的 ...

  3. 【手撸一个ORM】第九步、orm默认配置类 MyDbConfiguration,一次配置,简化实例化流程

    这个实现比较简单,事实上可配置的项目很多,如有需要,请读者自行扩展 using System; namespace MyOrm { public class MyDbConfiguration { p ...

  4. Core 事件总

    NET Core 事件总线,分布式事务解决方案:CAP 背景 相信前面几篇关于微服务的文章也介绍了那么多了,在构建微服务的过程中确实需要这么一个东西,即便不是在构建微服务,那么在构建分布式应用的过程中 ...

  5. 基于TCP协议网络编程

    1.TCP/IP是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路: 一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路来进行通信: 2.Java对基 ...

  6. SyntaxError: Use of const in strict mode.

    具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...

  7. 安卓下对SD卡文件的读写

    为SD下的操作文件,封装了一些类: package ujs.javawritedata; import java.io.File; import java.io.FileInputStream; im ...

  8. window下安装php调试工具xdebug

    1.https://xdebug.org/wizard.php在方框中输入本地phpinfo.php中的内容会提示对应要安装的版本, 2.https://xdebug.org/download.php ...

  9. 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式

    1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...

  10. ios 11 12以后下拉刷新不回位的解决方法

    原因:  iOS11弃用了automaticallyAdjustsScrollViewInsets属性,新增contentInsetAdjustmentBehavior来替代它 //解决方案 添加如下 ...