Submission #9815160


Source Code Expand

unittest
{
	assert( [ "5 7", "11 13 17 19 23" ].parse.expand.solve == 5 );
	assert( [ "4 10", "0 3 6 10" ].parse.expand.solve == 0 );
	assert( [ "6 36", "0 5 32 48 69 71" ].parse.expand.solve == 4 );
	assert( [ "6 405885562", "133510576 158828561 245133494 461153833 840383806 867039395" ].parse.expand.solve == 6 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nd = input.front.split.to!( long[] );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( nd[ 1 ], as );
}

auto solve( long d, long[] as )
{
	// 都市iから徒歩で移動できる都市の数を計算
	auto bs = new long[ as.length ];
	for( auto i = 0L, j = 1L; i < as.length; i++ )
	{
		while( j < as.length && as[ j ] - as[ i ] <= d ) j++;
		bs[ i ] = j - i - 1;
	}
	
	// bsの累積和表を作成
	auto bts = new long[ 1 + as.length ];
	foreach( i, b; bs )
	{
		bts[ i + 1 ] = bts[ i ] + b;
	}
	bts.popFront;
	
	// 徒歩で都市i->j->k かつ 電車で都市i->k と移動するパターンの数を計算
	auto ans = 0L;
	foreach( i, b; bs )
	{
		auto bt = bts[ i + b ] - bts[ i ];		// 徒歩で都市i->j->k と移動するパターンの数
		auto rt = b.combination( 2 );			// 徒歩で都市i->k と移動するパターンの数
		ans += bt - rt;
	}
	return ans;
}

// 組み合わせ数
auto combination( long n, long k )
{
	if( n < k ) return 0;
	auto r = 1L;
	for( auto d = 1L; d <= k; d++, n-- )
	{
		r *= n;
		r /= d;
	}
	return r;
}

Submission Info

Submission Time
Task C - 徒歩圏内
User KouMikage
Language D (DMD64 v2.070.1)
Score 400
Code Size 1714 Byte
Status AC
Exec Time 29 ms
Memory 8116 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 16
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
Case Name Status Exec Time Memory
01.txt AC 29 ms 6452 KB
02.txt AC 26 ms 7476 KB
03.txt AC 28 ms 7220 KB
04.txt AC 29 ms 8116 KB
05.txt AC 28 ms 7348 KB
06.txt AC 29 ms 7092 KB
07.txt AC 23 ms 6772 KB
08.txt AC 21 ms 5392 KB
09.txt AC 1 ms 256 KB
10.txt AC 28 ms 6312 KB
11.txt AC 29 ms 7988 KB
12.txt AC 29 ms 7476 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB
sample-04.txt AC 1 ms 256 KB