UVa 210 Concurrency Simulator(双端队列)

【日常学习】【双端队列】 Uva - 210 Concurrency Simulator题解 ruka也的确是个小恶魔一般的东西,双端队列竟然只提了一句让我们自己去查TUT比人痛心疾首学习了众多网上神犇们的文章,终于知道了个大概。有几篇整理得非常精妙的文章,在这里给大家分享一下。 http://blog.163.com/zhoumhan_0351/blog/static/399542272010358491361/这篇文章相对有些生硬,但是基本系统的介绍了一下双端队列。 http: 阅读详情

题意  模拟程序并行运行  

STL队列 双端队列 的应用  用双端队列维护即将执行的程序  再用个队列维护等待变量释放的程序   用lock表示变量锁定状态
先将所有程序依次放到执行队列中  每次取出队首程序运行不超过lim时间  未运行完又放到执行队列队尾  
遇到lock时  若当前锁定状态为false就将锁定状态变为true  否则将当前程序放到等待队列队尾并结束运行  
遇到unlock时  若等待队列有程序  就将等待队列队首程序放到执行队列队首

遇到end时 退出当前执行(不再进队尾)

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
bool lock;
deque<int> qr;//执行队列
queue<int> qb;//等待队列
vector<string> prg[N];
string s;
int t[N], p[N], var[26], lim;

void run(int i)
{
    int rt = lim, v;
    string cur;
    while(rt > 0)
    {
        cur = prg[i][p[i]];
        if(cur[2] == '=')  // 赋值
        {
            rt -= t[0];
            v = cur[4] - '0';
            if(cur.size() == 6) v = v * 10 + cur[5] - '0';
            var[cur[0] - 'a'] = v;
        }
        else if(cur[2] == 'i')   //print
        {
            rt -= t[1];
            printf("%d: %d\n", i, var[cur[6] - 'a']);
        }
        else if(cur[2] == 'c')   //lock
        {
            rt -= t[2];
            if(lock)
            {
                qb.push(i);
                return;
            }
            else lock = true;
        }
        else if(cur[2] == 'l')  //unlock
        {
            lock = false;
            rt -= t[3];
            if(!qb.empty())
            {
                v = qb.front();
                qb.pop();
                qr.push_front(v);
            }
        }
        else return;  //end
        ++p[i];
    }
    qr.push_back(i);
}

int main()
{
    int cas, n;
    scanf("%d", &cas);
    while(cas--)
    {
        scanf("%d", &n);
        for(int i = 0; i < 5; ++i)
            scanf("%d", &t[i]);
        scanf("%d", &lim);

        for(int i = 1; i <= n; ++i)
        {
            prg[i].clear();
            while(getline(cin, s))
            {
                if(s == "") continue;
                prg[i].push_back(s);
                if(prg[i].back() == "end") break;
            }
            qr.push_back(i);
        }

        memset(p, 0, sizeof(p));
        memset(var, 0, sizeof(var));
        while(!qr.empty())
        {
            int cur = qr.front();
            qr.pop_front();
            run(cur);
        }
        if(cas) puts("");
    }
    return 0;
}


 Concurrency Simulator 

Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some number of instructions from each program before switching to the next. You are to simulate the concurrent execution of up to ten programs on such a system and determine the output that they will produce.

The program that is currently being executed is said to be running, while all programs awaiting execution are said to be ready. A program consists of a sequence of no more than 25 statements, one per line, followed by an end statement. The statements available are listed below.

tex2html_wrap66 tex2html_wrap68

Each statement requires an integral number of time units to execute. The running program is permitted to continue executing instructions for a period of time called its quantum. When a program�s time quantum expires, another ready program will be selected to run. Any instruction currently being executed when the time quantum expires will be allowed to complete.

Programs are queued first-in-first-out for execution in a ready queue. The initial order of the ready queue corresponds to the original order of the programs in the input file. This order can change, however, as a result of the execution of lock and unlock statements.

The lock and unlock statements are used whenever a program wishes to claim mutually exclusive access to the variables it is manipulating. These statements always occur in pairs, bracketing one or more other statements. A lock will always precede an unlock, and these statements will never be nested. Once a program successfully executes a lock statement, no other program may successfully execute a lock statement until the locking program runs and executes the corresponding unlockstatement. Should a running program attempt to execute a lock while one is already in effect, this program will be placed at the end of the blocked queue. Programs blocked in this fashion lose any of their current time quantum remaining. When an unlock is executed, any program at the head of the blocked queue is moved to the head of the ready queue. The first statement this program will execute when it runs will be the lock statement that previously failed. Note that it is up to the programs involved to enforce the mutual exclusion protocol through correct usage of lock andunlock statements. (A renegade program with no lock/unlock pair could alter any variables it wished, despite the proper use of lock/unlock by the other programs.)

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input file consists of seven integers separated by spaces. These integers specify (in order): the number of programs which follow, the unit execution times for each of the five statements (in the order given above), and the number of time units comprising the time quantum. The remainder of the input consists of the programs, which are correctly formed from statements according to the rules described above.

All program statements begin in the first column of a line. Blanks appearing in a statement should be ignored. Associated with each program is an identification number based upon its location in the input data (the first program has ID = 1, the second has ID = 2, etc.).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Your output will contain of the output generated by the print statements as they occur during the simulation. When a print statement is executed, your program should display the program ID, a colon, a space, and the value of the selected variable. Output from separate print statements should appear on separate lines.

A sample input and correct output are shown below.

Sample Input

1

3 1 1 1 1 1 1
a = 4
print a
lock
b = 9
print b
unlock
print b
end
a = 3
print a
lock
b = 8
print b 
unlock
print b
end
b = 5
a = 17
print a
print b
lock
b = 21
print b
unlock
print b
end

Sample Output

1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21


低代码Agent开发框架使用指南(二)—Coze平台核心功能概览 本篇分享系统介绍了字节跳动推出的低代码智能体开发平台Coze。Coze以免费易用、多模型支持和便捷的跨平台发布为核心优势,显著降低了智能体开发门槛。文章详细解析了平台的六大核心功能:扩展能力边界的插件系统、增强认知的知识库、结构化数据管理的数据库、可视化图像处理的图像流、实现复杂逻辑编排的工作流,以及处理复杂任务的多智能体协作机制,全面展现了Coze平台的强大能力。 阅读详情

相关推荐

模拟IC设计避坑:手把手教你用Cadence仿真恒定跨导轨到轨运放(附完整工艺库文件)

本文详细解析了使用Cadence仿真恒定跨导轨到轨运放的全流程,涵盖电路原理、工艺库配置、尺寸设计及仿真分析。通过1:3电流镜补偿技术实现跨导稳定,提供CSMC 0.25um工艺下的实战案例,帮助工程师规避设计陷阱,提升模拟IC设计效率。

weixin_30952535的博客 503

【ACM菜逼解题报告】Concurrency Simulator

Concurrency Simulator  Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some nu

yzxin95的专栏 497

DeepLearningTool-深度学习分类-和HALCON的详细过程

损失函数:衡量预测误差,用于模型优化。混淆矩阵:评估分类模型性能,提供详细预测结果分析。两者结合使用,可以全面评估和优化模型。过拟合:模型过于复杂,捕捉了噪声,表现为训练集误差低,测试集误差高。欠拟合:模型过于简单,无法捕捉数据模式,表现为训练集和测试集误差都高。调整参数过拟合:简化模型、增加数据、使用正则化、早停、Dropout。欠拟合:增加模型复杂度、特征工程、增加训练时间、调整学习率、使用集成方法。通过合理调整参数和方法,可以有效避免过拟合和欠拟合,提升模型性能。

weixin_36672036的博客 6210

数据结构--双端队列

双端队列(Double-ended Queue,简称Deque)是一种具有队列和栈特性的数据结构,可以在队列的两端进行插入和删除操作。双端队列允许从前端和后端同时进行插入和删除操作,因此可以称为“两端都可以进出的队列”。双端队列的实现方式有多种,包括使用数组、链表等数据结构。具体的实现方式可以根据不同的需求和场景选择合适的方式。

m0_60593173的博客 9865

多尺度Transformer+特征融合新范式,算力减半精度猛增

在大模型时代,多尺度特征融合与Transformer架构的结合正成为研究热点。多尺度特征融合能够捕捉数据在不同尺度上的关键信息,而Transformer架构凭借其强大的并行处理能力和对长距离依赖的建模能力,为复杂数据处理提供了新的思路。这种结合在处理多模态数据时展现出巨大潜力。

Tulingxueshu的博客 1065

ENVI SARscape安装全攻略:从下载到汉化一步到位(附最新试用申请链接)

本文提供ENVI SARscape从下载安装到汉化的完整指南,特别针对InSAR(合成孔径雷达干涉测量)分析环境搭建。详细介绍了系统要求、分步安装流程、汉化优化方法及硬件加速配置,并分享数据处理验证和常见问题解决方案,帮助用户高效搭建遥感数据分析平台。

rnn9storyteller的博客 1427

UVa210 Concurrency Simulator

Concurrency Simulator  Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, execu

alpc_paul的专栏 3421

从FER2013到AffectNet:表情识别数据集十年演进史与你的项目选型攻略

本文回顾了从FER2013到AffectNet等主流人脸表情识别数据集的十年演进历程,详细分析了各数据集的特点、适用场景及技术突破。针对不同项目需求,提供了从移动端应用到高精度研究的选型策略,特别强调AffectNet在现代FER项目中的核心价值,并给出混合数据集构建的实战方案。

weixin_26808439的博客 523

数据结构与算法(八)——双端队列

双端队列(Double-ended Queue,简称Deque)是一种具有队列和栈特性的数据结构,可以在队列的两端进行插入和删除操作。双端队列允许从前端和后端同时进行插入和删除操作,因此可以称为“两端都可以进出的队列”。双端队列有以下几个特点:1、可以在队列的头部和尾部进行插入和删除的操作。2、元素的插入和删除操作可以分别称作入队和出队操作。3、可以进行先进先出(FIFO)和先进后出(LIFO)两种操作方式。4、可以实现栈和队列以及其他需要在两端进行插入和删除操作的场景。

shsbsns的博客 4672

052、联发科Imagiq HyperEngine架构适配:天玑9000/9200的ISP多核并行调度与Tuning Toolkit调优案例

摘要: 项目组在适配联发科天玑9200的Imagiq 890 ISP架构时,遇到多摄预览功耗过高、帧率不稳等问题。通过分析多核调度机制,调整辅核QoS通道、优化AI-ISP协同模式(采用FREERUN+运动补偿),并利用Tuning Toolkit的Pipeline Viewer动态分配负载,最终降低功耗30mA、提升画质。关键经验包括:避免多核带宽冲突、合理配置动态时钟策略,以及统一校准参数版本管理。天玑9000/9200的HyperEngine架构需优先理解多核协同逻辑,再针对性调优,否则易引发场景兼容

zzfmlxs的博客 39

《Python星球日记》 第73天:情感分析与主题建模

《Python星球日记》 第73天:情感分析与主题建模,今天我们将探索两个自然语言处理领域的重要技术:情感分析和主题建模。这两项技术在社交媒体分析、用户反馈处理、市场研究等多个领域有着广泛应用。

Code_流苏:在代码中寻诗意,在实践中觅真知 921

641,双端队列

一 思路 下面的函数不能直接用k,需要在init里定义个量然后注意长度的问题就可以完成了 二代码 class MyCircularDeque: def __init__(self, k: int): """ Initialize your data structure here. Set the size of the deque to be k. ...

weixin_45568848的博客 158

MATLAB应用实战系列(七十七)-基于长时间序列栅格数据的MK检验

1 MK检验介绍 MK检验是曼-肯德尔法,又称Mann—Kenddall 检验法,是一种气候诊断与预测技术,应用Mann-Kendall检验法可以判断气候序列中是否存在气候突变,如果存在,可确定出突变发生的时间。Mann-Kendall检验法也经常用于气候变化影响下的降水、干旱频次趋势检测。目前常用于长时间序列的栅格数据的显著性检验,在植被覆盖度,NDVI,NPP等方面尤为常见。 该检验功能强大,不需要样本遵从一定的分布,部分数据缺失不会对结果造成影响,不受少数异常值的干扰,适用性强。不但可以检验时

wenyusuran的专栏 6295

UVa210_Concurrency Simulator_双端队列的使用

原题链接 不得不说,看懂题真的很重要! 我也是别人的代码才看懂题在讲什么TAT #include #include #include #include #include using namespace std; const int maxn = 1000; const int times = 5; deque qr; //模拟运行的队

qq_40358998的博客 498

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Uva210 Concurrency Simulator 【双端队列deque】【例题6-1】

双端队列deque的应用

GuoZLH的博客 812

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

UVA - 210 - Concurrency Simulator <双端队列>

Concurrency Simulator  Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, execu

爆米 1213

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Concurrency Simulator UVA - 210(双端队列)

题意:本题需要你模拟一些简单程序,每一个程序有以下5种指:var = val,给变量赋值,简单起见保证变量名为一个字母,变量为所有进程共用,并且初始为0,保证val是不大于100的正整数;print var,输出变量var;lock对所有变量申请独占访问(不影响赋值和打印)unlock解除独占访问end结束程序以上指令分别耗时t1,t2,t3,t4,t5的时间,一开始进程按照输入顺序依次插入到等待...

GYH0730的博客 338

UVA - 210 Concurrency Simulator(双端队列)

点击打开题目链接 用一个双端队列deque存等待队列,一个普通队列存阻止队列,然后进行各项操作的模拟 一开始用的结构体存每个程序,然后队列里面存的也是结构体,T了。然后改用vector存程序,队列存每个程序的标号。 代码: #include using namespace std; const int maxn = 1000 + 5; queueint> stop; dequeint

Chook_lxk的博客 367

UVa 210 - Concurrency Simulator(模拟+双端队列)

模拟执行各种指令,很久之前的一道例题,书上给出了思路。 使用双端队列储存程序,根据相应的指令对队列进行调整。

wcr1996的专栏 2112

UVA ~ 210 ~ Concurrency Simulator (双端队列 + 队列)

题意:本题需要你模拟一些简单程序,每一个程序有以下5种指令:□var = val,给变量赋值,简单起见保证变量名为一个字母,变量为所有进程共用,并且初始为0,保证val是不大于100的正整数;□print var,输出变量var;□lock对所有变量申请独占访问(不影响赋值和打印)□unlock解除独占访问□end结束程序以上指令分别耗时t1,t2,t3,t4,t5的时间,一开始进程按照输入顺序依...

张松超的博客 403

UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列

Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some number of instructions fro...

祈的博客 389

UVa 210 Concurrency Simulator

本文详细说明了uvaoj 210题的实现思路。

2401_87372485的博客 626

例题6-1 UVA210 Concurrency Simulator(64行AC代码)

紫书刷题进行中,题解系列【GitHub|CSDN】 例题6-1 UVA210 Concurrency Simulator(64行AC代码) 题目大意 题目难懂,仔细读题 给定若干个程序,每个程序包含若干条指令,给出每个命令执行所需时间和每个程序的可用时间,模拟指令执行输出相应信息。 五条指令如下: variable = constant:赋值语句,变量variable为单字符(26个小写字母...

努力做个书呆儿 486
上一篇: UVa 12504 Updating a Dictionary(更新字典)
下一篇: UVa 10305 Ordering Tasks(拓扑排序)
virgoDd
博客等级 码龄12年 95粉丝 246原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址