結果

問題 No.370 道路の掃除
ユーザー fjafjafjafjafjafja
提出日時 2017-09-12 21:27:46
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,498 bytes
コンパイル時間 4,595 ms
コンパイル使用メモリ 79,760 KB
実行使用メモリ 47,352 KB
最終ジャッジ日時 2024-04-25 07:10:31
合計ジャッジ時間 9,469 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
45,672 KB
testcase_01 AC 129 ms
41,192 KB
testcase_02 AC 129 ms
41,108 KB
testcase_03 AC 116 ms
39,956 KB
testcase_04 AC 130 ms
41,204 KB
testcase_05 AC 130 ms
41,092 KB
testcase_06 AC 131 ms
41,448 KB
testcase_07 AC 119 ms
40,228 KB
testcase_08 AC 117 ms
40,100 KB
testcase_09 AC 133 ms
41,224 KB
testcase_10 AC 143 ms
41,484 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class N370
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner (System.in);
		int N=sc.nextInt(),M=sc.nextInt();
		Integer[] d=new Integer[M];
		Integer[] pbuf=new Integer[M];
		Integer[] nbuf=new Integer[M];

		for(int i=0;i<M;i++)
		{
			pbuf[i]=10001;
			nbuf[i]=-10001;
		}

		int pos=0,neg=0;
		for(int i=0;i<M;i++)
		{
			d[i]=sc.nextInt();
			if(d[i]==0){N--;}
			if(d[i]>0){pbuf[pos++]=d[i];}
			if(d[i]<0){nbuf[neg++]=d[i];}
		}
		Arrays.sort(d);
		Arrays.sort(pbuf,Comparator.naturalOrder());
		Arrays.sort(nbuf,Comparator.reverseOrder());

		Integer[] Ph=new Integer[N];
		for(int i=0;i<N;i++){Ph[i]=pbuf[i];}
		Integer[] Nh=new Integer[N];
		for(int i=0;i<N;i++){Nh[i]=nbuf[i];}

		int pp=0;
		int np=0;
		int now=0;
		int end=0;
		int ans=0;
		int min=Integer.MAX_VALUE;

		for(int i=0;i<(int)Math.pow(2,N);i++)
		{
			now=0;
			ans=0;
			pp=0;
			np=0;
			end=0;
			for(int j=0;j<N;j++)
			{
				if(end==1){break;}

				if((i&(int)Math.pow(2,j))!=0) 
				{
					if(Ph[pp]==10001)
					{
						end++;
					}
					else
					{
						ans+=((int)Math.abs(Ph[pp]-now));
						now=Ph[pp];
						pp++;
					}
				}

				else
				{
					if(Nh[np]==-10001)
					{
						end++;
					}
					else
					{
						ans+=((int)Math.abs(Nh[np]-now));
						now=Nh[np];
						np++;
					}
				}
			}
			if(end!=1&&min>ans){min=ans;}
			
		}


		System.out.println(min);




	}

}
0