1process.stdin.resume(); 2process.stdin.setEncoding('utf-8'); 3let input = ''; 4process.stdin.on('data', (data) => { 5 input += data; 6}); 7process.stdin.on('end', () => { 8 let inputArray = input.split('\n'); 9 10 let n = parseInt(inputArray[0], 10); 11 let servers = inputArray[1].split(' ').map(Number); 12 13 function findMinLatency() { 14 let minLatency = Infinity; 15 16 for (let i = 0; i < n; i++) { 17 let currentServer = servers[i]; 18 let totalLatency = 0; 19 20 for (let j = 0; j < n; j++) { 21 totalLatency += Math.abs(currentServer - servers[j]); 22 } 23 24 if (totalLatency < minLatency) { 25 minLatency = totalLatency; 26 } 27 } 28 29 return minLatency; 30 } 31 32 console.log(findMinLatency()); 33 process.exit(); 34});
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语