joj 2431: Shift and Increment (模板队列与数组模拟队列的对比练习)

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
5s 16384K 1226 196 Standard

Shift and increment is the basic operations of the ALU (Arithmetic Logical Unit) in CPU. One number can be transform to any other number by these operations. Your task is to find the shortest way from x to 0 using the shift (*=2) and increment (+=1) operations. All operations are restricted in 0..n, that is if the result x is greater than n, it should be replace as x%n.

Input and Output

There are two integer x, n (n <=1000000)

Sample Input

2 4
3 9

Sample Output

1
3
//两种模拟队列,模板库的是0.24S,模拟数组是0.14s,明显的优势啊。这题优化的关键是标记父亲节点,bfs题,貌似有数学方法。
//先贴STL模板
#include <cstdio>
#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
const int maxn=1000010;
struct state
{
    bool flag;
    int step;
}a[maxn];
int x,n,i;
int main ()
{
    while (scanf("%d%d",&x,&n)==2)
    {
        for(  i=0 ; i<n ; i++)
        {
            a[i].flag=1;
            a[i].step=-1;
        }
        int t;
        queue<int>q ;
        a[x].step=0;
        q.push(x);
        while (!q.empty())
        {
            int t;
            t=q.front();
            int t1=t*2%n;
            int t2=(t+1)%n;
            if(a[t].flag)
            {
                if(a[t1].flag)
                {
                    if(a[t1].step==-1)
                     a[t1].step=a[t].step+1;
                    q.push(t1);//先放入队列,若以搜索到结果则从队尾取。
                    //printf("t=%d/n",t);
                }
                    if(t1==0)break;
                if(a[t2].flag)
                {
                    if(a[t2].step==-1)
                     a[t2].step=a[t].step+1;
                    q.push(t2);
                    //printf("t+1=%d/n",(t+1)%n);
                }
                    if(t2==0)break;
            }
            a[t].flag=0;
            q.pop();
        }
        printf("%d/n",a[q.back()].step);;
    }
    return 0;
}

//数组模拟队列方法,时效大大地明显
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1000010;
struct state
{
    bool flag;
    int step;
}a[maxn];
int q[1500000];
int x,n,i;
int main ()
{
    while (scanf("%d%d",&x,&n)==2)
    {
        for(  i=0 ; i<n ; i++)
        {
            a[i].flag=1;
            a[i].step=-1;
        }
        int t;
        int rear=1,head=0;
        a[x].step=0;
        q[head]=x;
        while (rear>=head)
        {
            int t;
            t=q[head];
            int t1=t*2%n;
            int t2=(t+1)%n;
            if(a[t].flag)
            {
                if(a[t1].flag)
                {
                    if(a[t1].step==-1)
                     a[t1].step=a[t].step+1;
                    q[rear++]=t1;//先放入队列,若以搜索到结果则从队尾取。
                    //printf("t=%d/n",t);
                    if(t1==0)break;
                }
                if(a[t2].flag)
                {
                    if(a[t2].step==-1)
                     a[t2].step=a[t].step+1;
                    q[rear++]=t2;
                    //printf("t+1=%d/n",(t+1)%n);
                    if(t2==0)break;
                }
            }
            a[t].flag=0;
            head++;
        }
        printf("%d/n",a[q[rear-1]].step);;
    }
    return 0;
}

你可能感兴趣的:(joj 2431: Shift and Increment (模板队列与数组模拟队列的对比练习))