当前位置:网站首页>Hire cashier (differential constraint)
Hire cashier (differential constraint)
2022-07-03 04:44:00 【Snow_ raw】
Hire a cashier
Link
The question :
A supermarket needs to 24 24 24 Hours open , In order to meet business needs , Need to hire a large number of cashiers . It is known that the number of cashiers required in different time periods is different , In order to be able to hire as few people as possible , To reduce costs , The manager of this supermarket asked you to help with some advice . The manager provides you with a list of the minimum number of cashiers required in each time period R ( 0 ) , R ( 1 ) , R ( 2 ) , … , R ( 23 ) . R(0),R(1),R(2),…,R(23). R(0),R(1),R(2),…,R(23). R ( 0 ) R(0) R(0) It means midnight 00 : 00 00:00 00:00 Into the morning 01 : 00 01:00 01:00 Minimum required quantity of , R ( 1 ) R(1) R(1) Morning Express 01 : 00 01:00 01:00 Into the morning 02 : 00 02:00 02:00 Minimum required quantity of , And so on . Altogether N N N A qualified applicant applies for a position , The first i i i Applicants can choose from t i ti ti Start working continuously all the time 8 8 8 Hours . There is no replacement between cashiers , Will work completely 8 8 8 Hours , There must be enough cash registers . Now give your cashier's demand list , Please calculate the minimum number of cashiers you need to hire .
Input format :
Each group of data outputs a result , Each result takes up one line . If there is no arrangement to meet the needs , Output
No Solution.
Data range :
0 ≤ R ( 0 ) ≤ 1000 , 0 \le R(0) \le 1000, 0≤R(0)≤1000,
0 ≤ N ≤ 1000 , 0 \le N \le 1000, 0≤N≤1000,
0 ≤ t i ≤ 23 0 \le t_i \le 23 0≤ti≤23
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
Ideas :
There are many restrictions on this topic , Inequality equation , for example : Applicants must work continuously 8 8 8 Hours , Cashier's for each time period Minimum required quantity . And the minimum number of cashiers required by the question , Consider the direction to the direction of the difference constraint .
The topic requires the minimum required quantity , namely The longest run .
Find the inequality relation equation : Because we need to restrain the applicant 8 8 8 Hours , Each time period is a point in the graph , Therefore, the prefix and , S [ i ] S[i] S[i] Express 1 1 1~ i i i Number of all cashiers in .
1 : 1: 1: S [ i ] − S [ i − 1 ] > = 0 S[i]-S[i-1]>=0 S[i]−S[i−1]>=0 * \implies * S [ i ] > = S [ i − 1 ] S[i]>=S[i-1] S[i]>=S[i−1]
2 : 2: 2: S [ i ] − S [ i − 1 ] < = p e r s o n [ i ] ( p e r s o n [ i ] yes i rank paragraph Shen please people Count The amount ) S[i]-S[i-1]<=person[i](person[i] yes i Number of stage applicants ) S[i]−S[i−1]<=person[i](person[i] yes i rank paragraph Shen please people Count The amount ) * \implies * S [ i − 1 ] > = S [ i ] − p e r s o n [ i ] S[i-1]>=S[i]-person[i] S[i−1]>=S[i]−person[i]Because there is a ring, that is 23 − 24 23-24 23−24 And 1 − 7 1-7 1−7 Will disconnect , So classified discussion .
n u m [ i ] yes finger When front when between paragraph most Less Need to be want Of closed silver member num[i] It refers to the minimum number of cashiers needed in the current time period num[i] yes finger When front when between paragraph most Less Need to be want Of closed silver member
3.1 : i < = 7 when , S [ i ] + S [ 24 ] − S [ 16 + i ] > = n u m [ i ] 3.1:i<=7 when ,S[i]+S[24]−S[16+i]>=num[i] 3.1:i<=7 when ,S[i]+S[24]−S[16+i]>=num[i] * \implies * S [ i ] > = S [ 16 + i ] − S [ 24 ] + n u m [ i ] S[i]>=S[16+i]-S[24]+num[i] S[i]>=S[16+i]−S[24]+num[i]
3.2 : i > = 8 when , S [ i ] − S [ i − 8 ] > = n u m [ i ] 3.2:i>=8 when ,S[i]−S[i−8]>=num[i] 3.2:i>=8 when ,S[i]−S[i−8]>=num[i] * \implies * S [ i ] > = S [ i − 8 ] + R [ i ] S[i]>=S[i-8]+R[i] S[i]>=S[i−8]+R[i]But we found that 3.1 3.1 3.1 There are two variables in S [ 16 + i ] S[16+i] S[16+i] And S [ 24 ] . S[24]. S[24]. Because at most 1000 1000 1000 Individual application cashier , So we can enumerate violence S [ 24 ] S[24] S[24] From small to large , The first satisfaction is the smallest . And because the answer is monotonous , That is, the more cashiers, the more satisfied . So you can also use Two points c h e c k . check. check.
We need to pay attention to c h e c k check check Two constraints also need to be added to some drawings under construction
1 : a d d ( 0 , 24 , x ) 1:add(0,24,x) 1:add(0,24,x)
2 : a d d ( 24 , 0 , − x ) 2:add(24,0,-x) 2:add(24,0,−x)
Because we already have S [ 24 ] S[24] S[24] Take regular , therefore S [ 24 ] S[24] S[24] The value of should also be constrained .The violence enumeration and dichotomy are attached below c h e c k check check Code for :
Code 1: Violence enumeration + Difference constraint
#include<bits/stdc++.h>
using namespace std;
int t;
int n;
const int N = 30;
const int M = 100;
int e[M],ne[M],idx,w[M];
int h[N];
int num[N];
int person[N];
int dist[N];
bool st[N];
int cnt[N];
void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
void build(int x){
memset(h,-1,sizeof h);
idx=0;
for(int i=1;i<=24;i++){
add(i-1,i,0);
add(i,i-1,-person[i]);
}
for(int i=8;i<=24;i++){
add(i-8,i,num[i]);
}
for(int i=1;i<=7;i++){
add(i+16,i,-x+num[i]);
}
add(0,24,x);
add(24,0,-x);
}
bool spfa(int x){
// Judge whether the current number of cashiers is sufficient
build(x);// Drawing
memset(st,false,sizeof st);
memset(dist,-0x3f,sizeof dist);
memset(cnt,0,sizeof cnt);
queue<int>q;
q.push(0);
st[0]=true;
dist[0]=0;
while(q.size()){
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(dist[j]<dist[t]+w[i]){
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=25)return false;
if(!st[j]){
st[j]=true;
q.push(j);
}
}
}
}
return true;
}
signed main(){
cin>>t;
while(t--){
memset(person,0,sizeof person);
for(int i=1;i<=24;i++)cin>>num[i];
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
x++;//0 Leave it to the super source
person[x]++;
}
bool success=false;
for(int i=0;i<=n;i++){
if(spfa(i)){
success=true;
cout<<i<<endl;
break;
}
}
if(!success)cout<<"No Solution"<<endl;
}
return 0;
}
Code 2: Two points check+ Difference constraint
#include<bits/stdc++.h>
using namespace std;
int t;
int n;
const int N = 30;
const int M = 100;
int e[M],ne[M],idx,w[M];
int h[N];
int num[N];
int person[N];
int dist[N];
bool st[N];
int cnt[N];
void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
void build(int x){
memset(h,-1,sizeof h);
idx=0;
for(int i=1;i<=24;i++){
add(i-1,i,0);
add(i,i-1,-person[i]);
}
for(int i=8;i<=24;i++){
add(i-8,i,num[i]);
}
for(int i=1;i<=7;i++){
add(i+16,i,-x+num[i]);
}
add(0,24,x);
add(24,0,-x);
}
bool spfa(int x){
// Judge whether the current number of cashiers is sufficient
build(x);// Drawing
memset(st,false,sizeof st);
memset(dist,-0x3f,sizeof dist);
memset(cnt,0,sizeof cnt);
queue<int>q;
q.push(0);
st[0]=true;
dist[0]=0;
while(q.size()){
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(dist[j]<dist[t]+w[i]){
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=25)return false;
if(!st[j]){
st[j]=true;
q.push(j);
}
}
}
}
return true;
}
signed main(){
cin>>t;
while(t--){
memset(person,0,sizeof person);
for(int i=1;i<=24;i++)cin>>num[i];
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
x++;//0 Leave it to the super source
person[x]++;
}
bool success=false;
int l=0,r=n;
while(l<r){
int mid=l+r>>1;
if(spfa(mid)){
success=true;
r=mid;
}
else l=mid+1;
}
if(spfa(l))success=true;
if(success)cout<<l<<endl;
else cout<<"No Solution"<<endl;
}
return 0;
}
边栏推荐
- Games101 Lesson 9 shading 3 Notes
- Number of uniform strings of leetcode simple problem
- Web - Information Collection
- The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
- Pyqt control part (II)
- Arthas watch grabs a field / attribute of the input parameter
- 【工具跑SQL盲注】
- C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
- [tools run SQL blind note]
- 2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
猜你喜欢

Sdl2 + OpenGL glsl practice (Continued)

2022 P cylinder filling test content and P cylinder filling simulation test questions

FuncS sh file not found when using the benchmarksql tool to test kingbases

Preparation for school and professional cognition

Employee attendance management system based on SSM

FFMpeg filter

Review the old and know the new: Notes on Data Science

论文阅读_中文医疗模型_ eHealth

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination

FISCO bcos zero knowledge proof Fiat Shamir instance source code
随机推荐
[tools run SQL blind note]
C primre plus Chapter 10 question 6 inverted array
论文阅读_中文NLP_ELECTRA
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
[set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
C Primer Plus Chapter 10, question 14 3 × 5 array
Current market situation and development prospect forecast of the global fire boots industry in 2022
AWS VPC
Function introduction of member points mall system
[USACO 2009 Dec S]Music Notes
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
I stepped on a foundation pit today
Pyqt control part (II)
【XSS绕过-防护策略】理解防护策略,更好的绕过
Number of 1 in binary (simple difficulty)
Hj35 serpentine matrix
Market status and development prospects of the global IOT active infrared sensor industry in 2022
【SQL注入】联合查询(最简单的注入方法)
Internationalization and localization, dark mode and dark mode in compose
Matplotlib -- save graph