【ACMclub周赛5】Problem A——购物停车

来源:点击打开链接

水题一道,由推断可知,如果停车点在首尾两个点的外边,那么会至少行进2*(Pi-P0)+K(i为最右面的商铺,K不定反正大于0),而设在内部,只需要行进最多2*(Pi-P0)的距离,而一个来回是一定要走的,关键是第二个来回怎么尽可能的省出来,数据不大,暴搜就行了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

int store[22];

int main()
{
	int testcase;
	cin>>testcase;
	while(testcase--)
	{
		memset(store,0,sizeof(store));
		int numofstore;
		cin>>numofstore;
		for(int i=0;i<numofstore;i++)
		{
			cin>>store[i];
		}
		int minn=0x3f3f3f3f,lf=0,rt=0,tmp=0;
		sort(store,store+numofstore);
		for(int p=store[0];p<=store[numofstore-1];p++)
		{
			tmp=store[numofstore-1]-store[0];
			lf=p-store[0];
			rt=store[numofstore-1]-p;
			tmp=tmp+lf+rt;
			if(tmp<minn)
				minn=tmp;
		}
		cout<<tmp<<endl;
	}
	return 0;
}


你可能感兴趣的:(【ACMclub周赛5】Problem A——购物停车)