結果
| 問題 |
No.1391 ±1 Abs Sum
|
| コンテスト | |
| ユーザー |
eSeF
|
| 提出日時 | 2020-11-08 13:14:41 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,203 bytes |
| コンパイル時間 | 1,067 ms |
| コンパイル使用メモリ | 108,160 KB |
| 実行使用メモリ | 48,832 KB |
| 最終ジャッジ日時 | 2024-07-22 15:06:22 |
| 合計ジャッジ時間 | 5,211 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 TLE * 1 -- * 25 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using static System.Math;
namespace Solution
{
class AC
{
const long INF = long.MaxValue / 2;
static void Main(string[] args)
{
var (N, K) = Cin.Input<int, int>();
var A = Cin.ReadSplitLong();
long ans = INF;
var lst = new List<long>();
for (var i = 0; i < Min(N, 70); i++)
{
long s = 0;
lst.Clear();
for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
lst.Sort();
for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
ans = Min(ans, s);
}
for(var i = N - 1; i >= Max(0, N - 1 - 70); i--)
{
long s = 0;
lst.Clear();
for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
lst.Sort();
for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
ans = Min(ans, s);
}
for(var i = Max(0, N / 2 - 35); i < Min(N, N / 2 + 35); i++)
{
long s = 0;
lst.Clear();
for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
lst.Sort();
for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
ans = Min(ans, s);
}
Console.WriteLine(ans);
}
}
}
static class Cin
{
public static string Str() => Console.ReadLine();
public static int Int() => int.Parse(Cin.Str());
public static long Long() => long.Parse(Cin.Str());
public static double Double() => double.Parse(Cin.Str());
private static T Conv<T>(string value) => (T)Convert.ChangeType(value, typeof(T));
public static (T, U) ReadTuple<T, U>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]));
public static (T, U, V) ReadTuple<T, U, V>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]));
public static (T, U, V, W) ReadTuple<T, U, V, W>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]), Conv<W>(input[3]));
public static (T, U, V, W, X) ReadTuple<T, U, V, W, X>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]), Conv<W>(input[3]), Conv<X>(input[4]));
public static (T, U) Input<T, U>() => ReadTuple<T, U>(ReadSplit());
public static (T, U, V) Input<T, U, V>() => ReadTuple<T, U, V>(ReadSplit());
public static (T, U, V, W) Input<T, U, V, W>() => ReadTuple<T, U, V, W>(ReadSplit());
public static (T, U, V, W, X) Input<T, U, V, W, X>() => ReadTuple<T, U, V, W, X>(ReadSplit());
public static string[] ReadSplit() => Console.ReadLine().Split();
public static string[] ReadSplit(char separator) => Console.ReadLine().Split(separator);
public static int[] ReadSplitInt() => Array.ConvertAll(ReadSplit(), int.Parse);
public static long[] ReadSplitLong() => Array.ConvertAll(ReadSplit(), long.Parse);
public static double[] ReadSplit_Double() => Array.ConvertAll(ReadSplit(), double.Parse);
}
eSeF