Express Mail Taking

Express Mail Taking

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(1≤T≤100),the number of testcases.
For each test cases,the first line contains three integer n,m,k(1≤k≤n≤109,1≤m<min(n,106))
The next line contains m integer,the i-th stand for ai(1≤ai≤n,ai≠k).
The input guarantees that ∑m≤2×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.

Output

2
10 2 5
6 7
10 2 5
3 4

Sample Output

14
10

我在Dev上运行的都没有问题,在杭电的官网总是交不成功,哪位帮忙看看呀
代码:

#include<iostream>
#include<algorithm>
#define M 10000
using namespace std;
int main()
{
	int T;
	cin>>T;
	long long inarray[M]={0},array[M]={0},ans[M]={0},k=1;
	while(T--)
	{
		for(int i=1;i<=3;i++)
			cin>>inarray[i];
		for(int j=1;j<=inarray[2];j++)
		{
			int temp;
			cin>>temp;
			array[temp]=temp;			
		}
		int sum=inarray[3]-1; 
		int maxk=0;
		for(int i=1;i<=inarray[1];i++)
		{
			if(array[i]!=0)
			{
				sum=sum+abs(inarray[3]-array[i]);
				if(maxk<abs(inarray[3]-array[i]))
				{
					maxk=array[i];
				}
			} 
		}
		sum=sum+(maxk-1);
		for(int i=1;i<=inarray[1];i++)
		{
			if(array[i]!=0&&i!=maxk)
				sum=sum+abs(inarray[3]-array[i]);
		}			
		ans[k++]=sum;	
		for(int i=1;i<=inarray[1];i++)
			array[i]=0;			
	}
	for(int i=1;ans[i]!=0;i++)
		cout<<ans[i]<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值