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

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
5s16384K1226196Standard

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值