队列(裸题)

Queue   Aizu - ALDS1_3_B

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)

First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)

Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)

Your task is to write a program which simulates the round-robin scheduling .

Input

n q

name1 time1

name2 time2

...

namen timen

In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given.
namei and timei are separated by a single space.

Output

For each process, prints its name and the time the process finished in order.

Constraints

  • 1 ≤ n ≤ 100000
  • 1 ≤ q ≤ 1000
  • 1 ≤ timei ≤ 50000
  • 1 ≤ length of namei ≤ 10
  • 1 ≤ Sum of timei ≤ 1000000

Sample Input 1

5 100
p1 150
p2 80
p3 200
p4 350
p5 20

Sample Output 1

p2 180
p5 400
p1 450
p3 550
p4 800

Notes

Template in C

题目也是一个很裸的队列,就是支持插入和删除的操作,只不过做成了环,减少了极大的空间花费。同样数组模拟队列。做成环的环的话要注意取模问题。

 1     #include <iostream>
 2     #include <cstdio>
 3     using namespace std;
 4
 5     const int maxn = 100000 + 5;
 6
 7     struct node{
 8       char my_name[10];
 9       int my_time;
10     };
11
12     int n,q;
13     int head,tail;
14     int q_time = 0;
15     node que[maxn];
16
17     void push_back(node& k){
18       que[tail++] = k;
19       tail %= (n+5);
20     }
21
22     node pop_front(){
23       node k = que[head++];
24       head %= (n+5);
25       return k;
26     }
27
28     int main(){
29       scanf("%d%d",&n,&q);
30       head = 0;
31       tail = 0;
32       for(int i = 1;i <= n; i++){
33         node Q;
34         scanf("%s%d",Q.my_name,&Q.my_time);
35         push_back(Q);
36       }
37       while(head != tail){
38         node k = pop_front();
39         if(k.my_time - q > 0){
40           k.my_time -= q;
41           push_back(k);
42           q_time += q;
43         }
44         else{
45           q_time += k.my_time;
46           printf("%s %d\n",k.my_name,q_time);
47         }
48       }
49     }  
时间: 2024-08-29 01:18:18

队列(裸题)的相关文章

poj 2823 单调队列裸题

两个队列分别维持最大和最小 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; struct node { int mi,ma; }num[1000010]; int sax[1000010],sin[1000010]; int ida[1000010],idi[1000010]; int main() { int n,m,i,j,k,a; while(~scanf(&

SZOJ 167 Lca裸题

一道.......一道我改了一周的裸题 无根树建双向边 无根树建双向边 无根树建双向边 重要的事情说三遍(微笑) 还有要开longlong 还有双向边不是双倍边(微笑) 我真是,能把自己气吐血10次就不把自己气吐血9次 [问题描述] 已知一棵nn个点的树,点从1开始标号,树上每条边都有一个正整数边权. 有qq个询问,每个询问由type,u,vtype,u,v三个正整数构成. 当type=1type=1时,询问uu到vv路径上所有边权的二进制异或和. 当type=2type=2时,询问uu到vv路

hdu1159 poj1458 LCS裸题

HDU 1159 题意:找LCS 思路:裸题 n*m的写法,我的写法好像比较奇怪...用一个ci保存s2第i位可以做为s1的公共子序列的最大值,s1的每一位遍历s2,遍历的时候记录前面出现过的ci的最大值,ci一定是一个连序的上升序列,我的好像不是正经的LCS的算法,改天还是要学习一个的 AC代码: #include "iostream" #include "string.h" #include "stack" #include "qu

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

HDOJ-1232 畅通工程【并查集裸题】

题目传送门 : http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集的裸题 AC code: #include <iostream> #define MAXN 1050 using namespace std; int pre[MAXN]; int Find(int pos) { int r = pos; while (r != pre[r]) r = pre[r]; int i = pos; while (i != r) { int t = p

【不可能的任务22/200】【填坑】bzoj3224 splay裸题

人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 1 #include <cstdio> 2 int root,N=0,n,p,q; 3 int fa[100001],c[100001][2],size[100001],sp[100001]; 4 void rot(int x) 5 { 6 int y=fa[x],k=(c[y][0]==x); 7 size[y]=size[c[y][k]]+size[c[x][k]]+1;size[x]=size[c[x][!k]]+

HDU 1102 最小生成树裸题,kruskal,prim

1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using na

贴一下WC总结里提到的那道裸题吧。。。

[bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a . 操作 3 :询问某个节点 x 到根的路径中所有点的点权和. 输入 第一行包含两个整数 N, M .表示点数和操作数. 接下来一行 N 个整数,表示树中节点的初始权值. 接下来 N-1 行每行三个正整数 fr, to , 表示该树中存在一条边

tarjan讲解(用codevs1332(tarjan的裸题)讲解)

主要借助这道比较裸的题来讲一下tarjan这种算法 tarjan是一种求解有向图强连通分量的线性时间的算法.(用dfs来实现) 如果两个顶点可以相互通达,则称两个顶点强连通.如果有向图G的每两个顶点都强连通,称G是一个强连通图.有向图的极大强连通子图,称为强连通分量. 在上面这张有向图中1,2,3,4形成了一个强连通分量,而1,2,4,和1,3,4并不是(因为它们并不是极大强连通子图). tarjan是用dfs来实现的(用了tarjan后我们就可以对图进行缩点(当然这道裸题用不到)) 这道题只要