c_cpp http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

#define N 100000

typedef struct process {
   char name[20];
   int time;
} Process;

typedef struct queue {
  int head;
  int size;
  Process data[N];
} Queue;

void initialize(Queue*);
void enqueue(Queue*, Process);
Process dequeue(Queue*);
int is_empty(Queue*);

int main(){
    int n, q, current, time, i;
    char name[5];
    Queue *queue = malloc(sizeof(struct queue));

    initialize(queue);

    scanf("%d %d", &n, &q);

    for(i=0; i<n; i++){
        Process p;
        scanf("%s %d", p.name, &p.time);
        enqueue(queue, p);
    }

    current = 0;
    while(!is_empty(queue)){
        Process p = dequeue(queue);
        if(p.time <= q){
            current += p.time;
            printf("%s %d\n", p.name, current);
        }else{
            current += q;
            p.time -= q;
            enqueue(queue, p);
        }
    }

    free(queue);

    return 0;
}

void initialize(Queue *queue){
    queue->head = 0;
    queue->size = 0;
}

void enqueue(Queue* queue, Process process){
    queue->data[(queue->head + queue->size) % N] = process;
    queue->size++;
}

Process dequeue(Queue* queue){
    Process p = queue->data[queue->head];
    queue->head = (queue->head + 1) % N;
    queue->size--;

    return p;
}

int is_empty(Queue *queue){
    return queue->size == 0;
}

以上是关于c_cpp http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp

搜索的应用-分配货物

计几模板模板整理

最小成本排序 挑战程序设计

AOJ 0118: Property Distribution (简单DFS)

AOJ 2266 Cache Strategy(费用流)