列表

详情


NC51299. Cashier Employment

描述

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 , 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. 

输入描述

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 , after which come N lines each containing one . There are no blank lines between test cases.

输出描述

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.

示例1

输入:

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

输出:

1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 4ms, 内存消耗: 384K, 提交时间: 2022-10-17 17:53:19

#include<bits/stdc++.h>
using namespace std;
#define N 105
#define int long long
int a[30],b[30];
int tot=0;
int h[30],ne[N],e[N],v[N];
int d[30];
int st[30];
int c[30];
int n;
int t;
void add(int x,int y,int z){
	tot++;
	e[tot]=y;
	v[tot]=z;
	ne[tot]=h[x];
	h[x]=tot;
}
int spfa(){
	queue<int> q;
	//st[0]=1;
	d[0]=0;
	q.push(0);
	while(!q.empty()){
		int t=q.front();
		q.pop();
		st[t]=0;
		for(int i=h[t];i!=0;i=ne[i]){
			int j=e[i];
			if(d[j]<d[t]+v[i]){
				d[j]=d[t]+v[i];
				c[j]=c[t]+1;
				if(c[j]>=25) return 0;
				if(st[j]==0){
					st[j]=1;
					q.push(j);
				}
			}
		}
	}
	//cout<<"hb"<<endl;
	return 1;
}
int ch(int x){
	tot=0;
    for(int i=0;i<30;i++){
	 	h[i]=0;
	 	d[i]=-0x3f3f3f3f;
	 	c[i]=0;
	 	st[i]=0;
	}
	for(int i=0;i<=tot;i++){
		ne[i]=e[i]=v[i]=0;
	}
	for(int i=1;i<=24;i++){
		add(i-1,i,0);
		add(i,i-1,-a[i]);
		if(i>=8) add(i-8,i,b[i]);
		else add(16+i,i,-x+b[i]);
	}
	add(0,24,x);
	add(24,0,-x);
	if(spfa()==0) return 0;
	else return 1;
}
signed main(){
	cin>>t;
	while(t--){
		memset(a,0,sizeof(a));
		for(int i=1;i<=24;i++){
			cin>>b[i];
		}
		cin>>n;
		for(int i=0;i<n;i++){
			int k;
			cin>>k;
			a[++k]++;
		}
		int l=0;
		int r=n+1;
		while(l<r){
			int mid=(l+r)>>1;
		//	cout<<"mid="<<mid<<endl;
			if(ch(mid)==1){
		//		cout<<"ikbh"<<endl;
				r=mid;
			}else{
		//		cout<<"h"<<endl;
				l=mid+1;
			}
		}
		if(l==n+1) cout<<"No Solution"<<endl;
		else cout<<l<<endl;
	}
}

上一题