1003 Express Mail Taking

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1003&cid=909

Express Mail Taking

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 10439    Accepted Submission(s): 2786

Problem Description
Besides on the traditional classes,Baby Volcano also needs to learn how to take the express mails.
Usually express mails are stored in cabinets. In Baby Volcano's school,there are  n cabinets in a row,numbered by 1 to n. The distance between two adjacent cabinets is 1, and the entrance is at the cabinet 1. Among all n cabinets,the one numbered k is special and it is used to enter the code and open the cabinet door.
Baby Volcano has m express mails to take,the i-th is in the cabinet ai.
Two express mails will not be stored in the same cabinet. Also there is no express mail in the cabinet k.
To prevent expresses from being stolen, Baby Volcano have to take these express mails one by one, starting at the entrance. Generally, if he wants to take the express mail i, he have to walk to cabinet k first to enter the code, and then walks to cabinet ai. After taking the last one,he walks to the entrance.
There are so many express mails to take, so Baby Volcano wants to find a taking order which minimize the distance he walks.
Input
The first line contains one integer  T(1T100),the number of testcases.
For each test cases,the first line contains three integer n,m,k(1kn109,1m<min(n,106))
The next line contains m integer,the i-th stand for ai(1ain,aik).
The input guarantees that m2×106
**Note:Because of the large input,it is prefered to use scanf instead of cin.**
Output
For each test case,Output a single line contains one integer,representing for the minimal walking distance.
Sample Input
2 10 2 5 6 7 10 2 5 3 4
Sample Output
14 10
题意:
  给定n个邮箱到起点的距离,指定k号邮箱为输密码的位置(不在k号位置取件
  从起点出发,
  先输密码,再取件,规划得最短路径长度
思路:
  先到k输密码,对以后的每一次取件,取完回到k(一个来回
  不分先后顺序走完所有来回。
  如果距离起点最近的邮箱位置在k之前:
    则取完后直接回到起点,不经过k
    总距离减去k点到最小点的来回,加上k到起点距离
  否则
    直接加上k到起点距离
ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

int main()
{
    int t = 0;
    cin >> t;
    ll m , n , k;
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&m,&k);
        ll min = 1e9;
        ll sum = k-1;
        ll temp = 0;
        for(ll i = 0;i<m;i++)
        {
            scanf("%lld",&temp);
            sum += abs(temp-k)*2;
            if(temp < min)min = temp;
        }
        if(min < k)
        {
            sum = sum - 2*(k-min) + k-1;
        }
        else
        {
            sum += k-1;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值