排队打水C++代码

C排队打水
时间限制 : - MS   空间限制 : - KB 

评测说明 : 1s,256m

问题描述

有个人排队到个水龙头去打水,他们装满水桶的时间为整数,应如何安排他们的打水顺序才能使每个人花费的时间的总和最少?

输入格式

第一行
第二行为个人打水所用的时间

输出格式

一个整数,最少花费的总时间

样例输入 1

3 2
1 2 3

样例输出 1

7

样例输入 2

4 2                     
2 6 4 5

样例输出 2

23

提示

样例1说明:
第1,2两人在1号龙头接水,2排1后。
第1个人接水,耗时1
第2个人排队耗时1,接水耗时2,总耗时3

第2个人在2号龙头接水,耗时3

总耗时1+3+3=7

#include<bits/stdc++.h>
using namespace std;
int n,r,a[1005],sum[105],ans=0;
int main(){
	cin>>n>>r;
	for(int i=1;i<=n;i++)	cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1,j=0;i<=n;i++){
		j++;
		if(j==r+1)	j=1;
		sum[j]+=a[i];
		ans+=sum[j];
	}
	cout<<ans;
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行排队模型是一种常见的事件驱动模型,它可以用来模拟银行等服务场所的排队情况。以下是一个简单的C++代码实现: ``` #include <iostream> #include <queue> #include <cstdlib> #include <ctime> using namespace std; const int MAX_WINDOW_NUM = 10; // 最大窗口数 const int MAX_CUSTOMER_NUM = 100; // 最大顾客数 // 顾客类 class Customer { public: Customer(int arrive_time = 0, int service_time = 0) : arrive_time_(arrive_time), service_time_(service_time) {} int arrive_time() const { return arrive_time_; } int service_time() const { return service_time_; } private: int arrive_time_; // 到达时间 int service_time_; // 服务时间 }; // 窗口类 class Window { public: Window() : is_busy_(false), current_customer_(NULL) {} bool is_busy() const { return is_busy_; } void serveCustomer(Customer& customer) { is_busy_ = true; current_customer_ = &customer; } void finishService() { is_busy_ = false; current_customer_ = NULL; } Customer* current_customer() const { return current_customer_; } private: bool is_busy_; // 是否忙碌 Customer* current_customer_; // 当前服务的顾客 }; // 队列系统类 class QueueSystem { public: QueueSystem(int total_service_time, int window_num) : total_service_time_(total_service_time), window_num_(window_num), current_time_(0), customer_num_(0), total_customer_wait_time_(0) { srand(time(NULL)); for (int i = 0; i < window_num_; ++i) { windows_[i] = Window(); } } void simulate() { while (current_time_ < total_service_time_) { generateCustomer(); updateWindow(); ++current_time_; } printStatistics(); } private: void generateCustomer() { if (rand() % 3 == 0) { // 每秒钟有1/3的概率生成一个新顾客 Customer customer(current_time_, rand() % 5 + 1); customer_queue_.push(customer); ++customer_num_; } } void updateWindow() { for (int i = 0; i < window_num_; ++i) { if (windows_[i].is_busy()) { // 如果窗口正在服务顾客 Customer* customer = windows_[i].current_customer(); if (current_time_ - customer->arrive_time() >= customer->service_time()) { // 如果顾客服务结束 windows_[i].finishService(); total_customer_wait_time_ += current_time_ - customer->arrive_time() - customer->service_time(); } } if (!windows_[i].is_busy() && !customer_queue_.empty()) { // 如果窗口空闲且队列中有顾客 Customer customer = customer_queue_.front(); customer_queue_.pop(); windows_[i].serveCustomer(customer); } } } void printStatistics() { cout << "顾客总数:" << customer_num_ << endl; cout << "平均等待时间:" << (double)total_customer_wait_time_ / customer_num_ << endl; } private: int total_service_time_; // 总服务时间 int window_num_; // 窗口数 int current_time_; // 当前时间 int customer_num_; // 顾客总数 int total_customer_wait_time_; // 顾客总等待时间 Window windows_[MAX_WINDOW_NUM]; // 窗口数组 queue<Customer> customer_queue_; // 顾客队列 }; int main() { QueueSystem queue_system(60, 3); // 总服务时间为60秒,3个窗口 queue_system.simulate(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值