2017年中南大学复试-走路还是坐公交

2017年中南大学复试机试题解

走路还是坐公交

题目来源: http://www.pipioj.online/problem.php?id=1024.
题目描述
巨人国的小学生放假了,老师要给小朋友们排队了。可是这个老师有强迫症,一定要路队上的小朋友按照身高从高到矮排序(也就是排在前面的不能比后面的矮)。小朋友呢也很调皮,一旦老师给他排好队就不愿意动了。这个时候小朋友们一个一个的从教室里出来了,每个小朋友一出来老师就要给小朋友安排好位置。请问老师最少要给小朋友排几条路队呢?
输入
对于每组数据,第一行两个数n,表示小朋友总数量(1<=n<=100000)
第二行n个整数,表示小朋友身高,身高不超过30000(我也觉得很吓人)
输出
对于每组数据,输出一个整数,表示最少的路队数
样例输入
8
389 207 155 300 299 170 158 65
样例输出
2
提示
最少要排两条路队,其中一种方案是398-207-155-65 和 300-299-170-158

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

int vis[100000+100];

struct node{
    int pos,dis;
    friend bool operator < (node a,node b)
    {
        return a.dis > b.dis;
    }
};

priority_queue<node>q;

int main()
{
    int n,k;
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        if(n>=k) {printf("%d\n",n-k);continue;}
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();

        node tmp,next;
        tmp.pos=n;
        tmp.dis=0;
        q.push(tmp);
        vis[n]=1;
        while(!q.empty())
        {
            tmp=q.top();q.pop();
            if(tmp.pos==k) {break;}
            for(int i=-1;i<=1;i++)
            {
                if(i==0) next.pos = tmp.pos * 2;
                else next.pos = tmp.pos + i;
                if(tmp.pos==0&&i==0) next.dis = tmp.dis;
                else next.dis = tmp.dis + 1;
                if(next.pos>=0&&next.pos<=k+100&&vis[next.pos]==0)
                {
                    q.push(next);
                    vis[next.pos]=1;
                }
            }
        }
        printf("%d\n",tmp.dis);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值