当前位置:网站首页>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;
}
边栏推荐
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Learning practice: comprehensive application of cycle and branch structure (I)
- 【SQL注入点】注入点出现位置、判断
- Market status and development prospect forecast of global button dropper industry in 2022
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- How to retrieve the password for opening word files
- Library management system based on SSM
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
- Arthas watch grabs a field / attribute of the input parameter
- The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
猜你喜欢
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
Youdao cloud notes
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
Number of uniform strings of leetcode simple problem
Uipath practice (08) - selector
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
stm32逆向入门
Apache MPM model and ab stress test
Library management system based on SSM
随机推荐
Prefix and (continuously updated)
Leetcode simple question: check whether the string is an array prefix
Current market situation and development prospect forecast of the global fire boots industry in 2022
FFMpeg example
[USACO 2009 Dec S]Music Notes
General undergraduate college life pit avoidance Guide
2.14 summary
论文阅读_中文医疗模型_ eHealth
Review the old and know the new: Notes on Data Science
Bugku CTF daily question baby_ flag. txt
[set theory] binary relation (example of binary relation on a | binary relation on a)
Leetcode simple question: the key with the longest key duration
Jincang KFS data bidirectional synchronization scenario deployment
Reptile exercise 03
论文阅读_ICD编码_MSMN
Writing skills of multi plate rotation strategy -- strategy writing learning materials
关于开学的准备与专业认知
Preparation for school and professional cognition
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement