From 0 to learn algorithm - a data structure (queue 2.3)

As the name suggests: a queue like the same sort, the first line of the first people to get treatment

Similar queues and Stack: is a FIFO queue table

First, consider an array of analog, if linear array simulation, will lead to too much space, and why? Analog array stack encounter this problem?

Because the queue is a FIFO table, such as adding five elements occupy the position in the array is No. 0-4 of the table, delete the two elements this time, No. 0-1 location. In order to maintain this structure will lead to queue No. 0-1 location occupied but not utilized.

Remove the stack of elements in the array is the tail, so it will not take up extra space.

Optimization: "array into a ring" (meaning the mold).

If we each element is added to the table under the modulo the array, the entire array becomes circular, there is no problem taking up extra space in the

Example: scheduling algorithm simulation loop array

5 task every time you perform a task up to 100 minutes, if not complete execution put it in the tail, if the output of finished executing the time

0:150-80-200-350-20

1:80-200-350-20-50

2; 200-350-20-50 output 180

3:。。。。。。。

Output:

5 100

150 p1

p2 80

p3 200

p4 350

P5 20

Output:

p2 180

P5 400

450 p1

p3 550

p4 800

Code:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<time.h>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define LEN 100005

typedef struct pp{
    char name[100];
    int t;
}P;

PQ [LEN];

int head,tail,n;

void enqueue(P x){
    Q[tail]=x;
    tail=(tail+1)%LEN;
}

P dequeue{
    P x=Q[head];
    head=(head+1)%LEN;
    return x;
}


int main(){
    int elaps=0,c;
    int i,q;
    P u;
    scanf("%d%d",&n,&q);
    
    for(int i=0;i<n;i++){
        scanf("%s",Q[i].name);
        scanf("%d",&Q[i].t);
    }
    head=1;tail=n+1;
    while(head!=tail){
        u=dequeue();
        c=min(q,u.t);
        u.t-=c;
        elaps+=c;
        if(u.t>0){
            enqueue(u);
        }else{
            printf("%s %d\n",u.name,elaps);
        }
    }
    return 0;
}

c ++ Standard Library

head File

#include<queue>

definition

queue<int>que;

Was added to the tail element x

que.push(x);

Delete the head elements

que.pop ();

Visit the head elements but does not delete

que.front ();

Example Two: queue can solve problems breadth-first search

And m and n you three operations, to n + 1, -1, * 2, several operations can be asked minimum m

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<time.h>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>

using namespace std;

struct aa{
    int x,i;
};

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    queue<aa>que;
    aa a;
    a.i=0;
    a.x=n;
    que.push (a);
    int num;
    while(!que.empty()){
        a=que.front();
        if(a.x==m){
            num=a.i;
            break;
        }
        //+
        a=que.front();
        a.x++;
        a.i++;
        que.push (a);
        //-
        a=que.front();
        a.x--;
        a.i++;
        que.push (a);
        //*
        a=que.front();
        a.x*=2;
        a.i++;
        que.push (a);
        
        que.pop ();
    }
    printf("%d\n",num);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wz-archer/p/11711774.html
Recommended