buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
change_count
int64
0
100
using System; using System.Linq; class Program { static void Main() { int[] nm = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int[] l = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); if (nm[1] == 1) { Console.Write("0"); return; } int[] a = new int[nm[1] - 1]; Array.Sort(l); int len = l[nm[1] - 1] - l[0]; for (int i = 1; i < nm[1]; i++) { a[i - 1] = l[i] - l[i - 1]; } Array.Sort(a); for (int i = nm[1] - nm[0]; i < a.Length; i++) { len -= a[i]; } Console.Write(len); } }
using System; using System.Linq; class Program { static void Main() { int[] nm = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int[] l = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); if (nm[0] >= nm[1]) { Console.Write("0"); return; } int[] a = new int[nm[1] - 1]; Array.Sort(l); int len = l[nm[1] - 1] - l[0]; for (int i = 1; i < nm[1]; i++) { a[i - 1] = l[i] - l[i - 1]; } Array.Sort(a); for (int i = nm[1] - nm[0]; i < a.Length; i++) { len -= a[i]; } Console.Write(len); } }
[["-", 15, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 20], ["+", 8, 196, 0, 57, 15, 16, 12, 204, 205, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 70], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 73]]
8
235
7
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { var input1 = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); var N = input1[0]; var M = input1[1]; var points = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); points.Sort(); if (points.First() < 0) { points = points.Select(x => -1 * points.First() + x).ToList(); } else { points = points.Select(x => x - points.First()).ToList(); } if (M == 1) { Console.WriteLine(0); return; } if (N == 1) { Console.WriteLine(points.Last()); return; } var distance = new List<int>(); for (int i = 0; i < points.Count; i++) { if (i == 0) { distance.Add(points[1]); continue; } distance.Add(points[i] - points[i - 1]); } distance.Sort((x, y) => y - x); Console.WriteLine(distance.Skip(N).Sum()); } }
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { var input1 = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); var N = input1[0]; var M = input1[1]; var points = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList(); points.Sort(); if (points.First() < 0) { points = points.Select(x => -1 * points.First() + x).ToList(); } else { points = points.Select(x => x - points.First()).ToList(); } if (M == 1) { Console.WriteLine(0); return; } if (N == 1) { Console.WriteLine(points.Last()); return; } var distance = new List<int>(); for (int i = 0; i < points.Count - 1; i++) { distance.Add(points[i + 1] - points[i]); } distance.Sort((x, y) => y - x); Console.WriteLine(distance.Skip(N - 1).Sum()); } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 25], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 45], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 31, 22], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["-", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 8, 196, 0, 57, 64, 196, 0, 116, 0, 117], ["-", 8, 196, 0, 57, 64, 196, 0, 116, 0, 35], ["-", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["-", 12, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["-", 12, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 205, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["+", 205, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
307
28
using System; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Collections; namespace AtCoder { class Program { static void Main(string[] args) { var ir = new Resolver(new ConsoleReader()); Console.WriteLine(ir.Resolve()); } } public class Resolver { IInputReader InputReader { get; } public List<string> Messages { get; set; } = new List<string>(); int[] rule; int[] input; public void ParseLines() { rule = InputReader.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); input = InputReader.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); } public string Resolve() { var arranged = input.ToList().OrderBy(x => x).ToList(); var diff = arranged .Select((x, i) => { if (i == 0) return 0; return arranged[i] - arranged[i - 1]; }) .ToList(); var diff_ = diff.OrderBy(x => -x) .ToList() .GetRange(rule[0] - 1, rule[1] - rule[0]) .ToList(); return diff_.Sum().ToString(); // return diff_.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b); } public Resolver(IInputReader inputReader) { InputReader = inputReader; ParseLines(); } } public interface IInputReader { string ReadLine(); } class ConsoleReader : IInputReader { string IInputReader.ReadLine() { return Console.ReadLine(); } } }
using System; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Collections; namespace AtCoder { class Program { static void Main(string[] args) { var ir = new Resolver(new ConsoleReader()); Console.WriteLine(ir.Resolve()); } } public class Resolver { IInputReader InputReader { get; } public List<string> Messages { get; set; } = new List<string>(); int[] rule; int[] input; public void ParseLines() { rule = InputReader.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); input = InputReader.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); } public string Resolve() { var arranged = input.ToList().OrderBy(x => x).ToList(); var diff = arranged .Select((x, i) => { if (i == 0) return 0; return arranged[i] - arranged[i - 1]; }) .ToList(); if (rule[1] <= rule[0]) return "0"; var diff_ = diff.OrderBy(x => -x) .ToList() .GetRange(rule[0] - 1, rule[1] - rule[0]) .ToList(); return diff_.Sum().ToString(); // return diff_.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b); } public Resolver(IInputReader inputReader) { InputReader = inputReader; ParseLines(); } } public interface IInputReader { string ReadLine(); } class ConsoleReader : IInputReader { string IInputReader.ReadLine() { return Console.ReadLine(); } } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 70], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 8, 196, 0, 57, 15, 16, 12, 204, 205, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 70], ["+", 15, 16, 12, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 73], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 37, 0, 5, 0, 62], ["+", 8, 196, 0, 57, 64, 37, 0, 5, 0, 222], ["+", 0, 195, 8, 196, 0, 57, 64, 37, 0, 35]]
8
350
17
using System.Linq; using System; public class Hello { public static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var m = int.Parse(line[1]); if (m == 1) { Console.WriteLine(0); goto exit; } line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.Parse); Array.Sort(a); var b = new int[m - 1]; for (int i = 0; i < m - 1; i++) b[i] = a[i + 1] - a[i]; Array.Sort(b); Array.Reverse(b); var bsum = b.Sum(); var t = 0; for (int i = 0; i < n - 1; i++) t += b[i]; var ans = bsum - t; Console.WriteLine(ans); exit:; } }
using System.Linq; using System; public class Hello { public static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var m = int.Parse(line[1]); if (m == 1) { Console.WriteLine(0); goto exit; } if (n >= m) { Console.WriteLine(0); goto exit; } line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.Parse); Array.Sort(a); var b = new int[m - 1]; for (int i = 0; i < m - 1; i++) b[i] = a[i + 1] - a[i]; Array.Sort(b); Array.Reverse(b); var bsum = b.Sum(); var t = 0; for (int i = 0; i < n - 1; i++) t += b[i]; var ans = bsum - t; Console.WriteLine(ans); exit:; } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 20], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 22], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 126, 0, 127], ["+", 8, 196, 0, 57, 64, 196, 0, 126, 0, 22], ["+", 8, 196, 0, 57, 64, 196, 0, 126, 0, 35], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46]]
8
239
18
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { List<long> nm = Console.ReadLine().Split(' ').Select(long.Parse).ToList(); long n = nm[0]; long k = nm[1]; List<long> a = Console.ReadLine().Split(' ').Select(long.Parse).ToList(); long x = 0; long t = 1; while (t <= k) { long count0 = 0; long count1 = 0; for (int i = 0; i < n; i++) { if ((a[i] & t) != 0) { ++count1; } else { ++count0; } } if (count0 >= count1) { x |= t; } t *= 2; } long[] anss = new long[40]; for (int i = 0; i < 40; i++) { long xx = 0; for (int j = 0; j < 40; j++) { if (j > i) { xx |= k & ~((long)1 << j); } else if (j == i) { } else { xx |= x & ~((long)1 << j); } } for (int j = 0; j < n; j++) { anss[i] += xx ^ a[j]; } } Console.WriteLine(anss.Max()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { List<long> nm = Console.ReadLine().Split(' ').Select(long.Parse).ToList(); long n = nm[0]; long k = nm[1]; List<long> a = Console.ReadLine().Split(' ').Select(long.Parse).ToList(); long x = 0; long t = 1; while (t <= k) { long count0 = 0; long count1 = 0; for (int i = 0; i < n; i++) { if ((a[i] & t) != 0) { ++count1; } else { ++count0; } } if (count0 >= count1) { x |= t; } t *= 2; } long[] anss = new long[40]; for (int i = 0; i < 40; i++) { long xx = 0; for (int j = 0; j < 40; j++) { if (j > i) { xx |= k & ((long)1 << j); } else if (j == i) { } else { xx |= x & ((long)1 << j); } } xx = Math.Min(xx, k); for (int j = 0; j < n; j++) { anss[i] += xx ^ a[j]; } } Console.WriteLine(anss.Max()); } } }
[["-", 0, 1, 0, 11, 12, 16, 12, 241, 0, 92], ["+", 0, 7, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 205, 22], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 0, 131], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 141, 22], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 0, 11, 12, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
342
13
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime.CompilerServices; using System.Text; using System.Diagnostics; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; using static Template; using Pi = Pair<int, int>; class Solver { public void Solve(Scanner sc) { int N; long K; sc.Make(out N, out K); var A = sc.ArrLong; var res = 0L; var sum = Create(2, () => new long[50]); var ok = false; for (int i = 49; i >= 0; i--) { ok |= (1 & K >> i) == 1; for (var j = 0; j < N; j++) if ((1 & A[j] >> i) == 1) sum[0][i] += 1L << i; else if (ok) sum[1][i] += 1L << i; } for (var i = 0; i < 50; i++) if ((1 & K >> i) == 1) { res += Max(sum[0][i], sum[1][i]); } else res += sum[0][i]; for (int i = 50; i > 0; i--) { if ((1 & K >> i) == 0) continue; var calc = 0L; for (int j = 0; j < i; j++) calc += Max(sum[0][j], sum[1][j]); for (var j = i + 1; j < 50; j++) calc += sum[(1 & K >> j) ^ 1][j]; chmax(ref res, calc); } WriteLine(res); } } #region Template public static class Template { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver().Solve(new Scanner()); Console.Out.Flush(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == 1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == -1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; } public static T[] Shuffle<T>(this IList<T> A) { T[] rt = A.ToArray(); Random rnd = new Random(); for (int i = rt.Length - 1; i >= 1; i--) swap(ref rt[i], ref rt[rnd.Next(i + 1)]); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; } public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); } } public class Scanner { public string Str => Console.ReadLine().Trim(); public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray(); public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray(); public char[][] Grid(int n) => Create(n, () => Str.ToCharArray()); public int[] ArrInt1D(int n) => Create(n, () => Int); public long[] ArrLong1D(int n) => Create(n, () => Long); public int[][] ArrInt2D(int n) => Create(n, () => ArrInt); public long[][] ArrLong2D(int n) => Create(n, () => ArrLong); private Queue<string> q = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); } public void Make<T1>(out T1 v1) => v1 = Next<T1>(); public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); } // public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, // v2); } public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, // out T3 v3); return (v1, v2, v3); } public (T1, T2, T3, T4) Make<T1, T2, T3, // T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, // v3, v4); } } public class Pair<T1, T2> : IComparable<Pair<T1, T2>> { public T1 v1; public T2 v2; public Pair() {} public Pair(T1 v1, T2 v2) { this.v1 = v1; this.v2 = v2; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2> p) { var c = Comparer<T1>.Default.Compare(v1, p.v1); if (c == 0) c = Comparer<T2>.Default.Compare(v2, p.v2); return c; } public override string ToString() => $"{v1.ToString()} {v2.ToString()}"; public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; } } public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>> { public T3 v3; public Pair() : base() {} public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2) { this.v3 = v3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2, T3> p) { var c = base.CompareTo(p); if (c == 0) c = Comparer<T3>.Default.Compare(v3, p.v3); return c; } public override string ToString() => $"{base.ToString()} {v3.ToString()}"; public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; } } #endregion
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime.CompilerServices; using System.Text; using System.Diagnostics; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; using static Template; using Pi = Pair<int, int>; class Solver { public void Solve(Scanner sc) { int N; long K; sc.Make(out N, out K); var A = sc.ArrLong; var res = 0L; var sum = Create(2, () => new long[50]); var ok = false; for (int i = 49; i >= 0; i--) { ok |= (1 & K >> i) == 1; for (var j = 0; j < N; j++) if ((1 & A[j] >> i) == 1) sum[0][i] += 1L << i; else if (ok) sum[1][i] += 1L << i; } for (var i = 0; i < 50; i++) if ((1 & K >> i) == 1) { res += Max(sum[0][i], sum[1][i]); } else res += sum[0][i]; for (int i = 50; i > 0; i--) { if ((1 & K >> i) == 0) continue; var calc = sum[0][i]; for (int j = 0; j < i; j++) calc += Max(sum[0][j], sum[1][j]); for (var j = i + 1; j < 50; j++) if ((1 & K >> j) == 1) calc += Max(sum[0][j], sum[1][j]); else calc += sum[0][j]; chmax(ref res, calc); } WriteLine(res); } } #region Template public static class Template { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver().Solve(new Scanner()); Console.Out.Flush(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == 1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == -1) { a = b; return true; } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; } public static T[] Shuffle<T>(this IList<T> A) { T[] rt = A.ToArray(); Random rnd = new Random(); for (int i = rt.Length - 1; i >= 1; i--) swap(ref rt[i], ref rt[rnd.Next(i + 1)]); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; } public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); } } public class Scanner { public string Str => Console.ReadLine().Trim(); public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray(); public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray(); public char[][] Grid(int n) => Create(n, () => Str.ToCharArray()); public int[] ArrInt1D(int n) => Create(n, () => Int); public long[] ArrLong1D(int n) => Create(n, () => Long); public int[][] ArrInt2D(int n) => Create(n, () => ArrInt); public long[][] ArrLong2D(int n) => Create(n, () => ArrLong); private Queue<string> q = new Queue<string>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); } public void Make<T1>(out T1 v1) => v1 = Next<T1>(); public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); } // public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, // v2); } public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, // out T3 v3); return (v1, v2, v3); } public (T1, T2, T3, T4) Make<T1, T2, T3, // T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, // v3, v4); } } public class Pair<T1, T2> : IComparable<Pair<T1, T2>> { public T1 v1; public T2 v2; public Pair() {} public Pair(T1 v1, T2 v2) { this.v1 = v1; this.v2 = v2; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2> p) { var c = Comparer<T1>.Default.Compare(v1, p.v1); if (c == 0) c = Comparer<T2>.Default.Compare(v2, p.v2); return c; } public override string ToString() => $"{v1.ToString()} {v2.ToString()}"; public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; } } public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>> { public T3 v3; public Pair() : base() {} public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2) { this.v3 = v3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(Pair<T1, T2, T3> p) { var c = base.CompareTo(p); if (c == 0) c = Comparer<T3>.Default.Compare(v3, p.v3); return c; } public override string ToString() => $"{base.ToString()} {v3.ToString()}"; public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; } } #endregion
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 200, 0, 212, 0, 204, 205, 204, 205, 22], ["+", 0, 212, 0, 204, 205, 204, 206, 207, 0, 70], ["+", 0, 204, 205, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 212, 0, 204, 205, 204, 206, 207, 0, 73], ["+", 0, 200, 0, 212, 0, 204, 206, 207, 0, 70], ["+", 0, 212, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 200, 0, 212, 0, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 7, 8, 1, 0, 11, 31, 22], ["-", 0, 7, 8, 1, 0, 11, 0, 202, 0, 107], ["-", 8, 1, 0, 11, 12, 204, 205, 204, 205, 22], ["-", 0, 11, 12, 204, 205, 204, 206, 207, 0, 70], ["+", 0, 7, 8, 196, 0, 7, 8, 57, 0, 121], ["+", 0, 7, 8, 196, 0, 7, 8, 57, 0, 24], ["-", 205, 204, 206, 207, 0, 28, 0, 16, 17, 140], ["+", 8, 196, 0, 7, 8, 57, 15, 16, 17, 60], ["+", 8, 196, 0, 7, 8, 57, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 8, 57, 0, 25], ["+", 0, 7, 8, 57, 64, 1, 0, 11, 31, 22], ["+", 8, 57, 64, 1, 0, 11, 0, 202, 0, 107], ["+", 8, 57, 64, 1, 0, 11, 12, 213, 63, 22], ["+", 64, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 204, 205, 204, 205, 22], ["+", 0, 28, 0, 204, 205, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 205, 204, 206, 207, 0, 73], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 64, 1, 0, 11, 12, 213, 3, 4, 0, 21], ["+", 64, 1, 0, 11, 12, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 7, 8, 57, 64, 1, 0, 35], ["+", 0, 7, 8, 196, 0, 7, 8, 57, 0, 95], ["+", 0, 7, 8, 57, 75, 1, 0, 11, 31, 22], ["+", 8, 57, 75, 1, 0, 11, 0, 202, 0, 107], ["+", 75, 1, 0, 11, 12, 204, 205, 204, 205, 22], ["+", 0, 11, 12, 204, 205, 204, 206, 207, 0, 70], ["+", 12, 204, 205, 204, 206, 207, 0, 28, 0, 203]]
8
1,867
44
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using static kyoupuro.Input; using static kyoupuro.MyMath; using static kyoupuro.Modular; namespace kyoupuro { class MainClass { public static void Main() { long N = NextLong, K = NextLong; var A = LongList; var array = new int[40]; foreach (var item in A) { long tmp = item; for (int i = 0; i < 40; i++) { if ((tmp & 1) == 1) { array[i]++; } tmp = tmp >> 1; } } long value = 0, sum = 0, bit = 1; for (int i = 0; i < 40; i++) { if (array[i] * 2 < N && value + bit <= K) { value += bit; sum += (N - array[i]) * bit; } else { sum += array[i] * bit; } bit = bit << 1; } Console.WriteLine(sum); } } class MyMath { public static long Gcd(long a, long b) { return b == 0 ? a : Gcd(b, a % b); } public static long Sqrt(long n) { if (n < 0) return 0; long a = 0, tmp = 0, b = 0; for (int i = 62; i >= 0; i -= 2) { tmp = (b << 1) + 1 <= (n >> i) ? 1 : 0; a = a << 1 | tmp; n -= (((b << 1) + 1) * tmp) << i; b = (b << 1) + tmp + tmp; } return a; } } class Modular { public const int mod = 1000000007; public readonly long value; public Modular(long value) { this.value = value; } public static implicit operator Modular(long a) { var m = a % mod; return new Modular((m < 0) ? m + mod : m); } public static Modular operator +(Modular a, Modular b) { return a.value + b.value; } public static Modular operator -(Modular a, Modular b) { return a.value - b.value; } public static Modular operator *(Modular a, Modular b) { return a.value * b.value; } private static Modular Pow(Modular a, int n) { switch (n) { case 0: return 1; case 1: return a; default: var p = Pow(a, n / 2); return p * p * Pow(a, n % 2); } } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, mod - 2); } private static readonly List<int> facs = new List<int> { 1 }; private static Modular Fac(int n) { for (int i = facs.Count; i <= n; ++i) { facs.Add((int)(Math.BigMul(facs.Last(), i) % mod)); } return facs[n]; } public static Modular Ncr(int n, int r) { return (n < r) ? 0 : (n == r) ? 1 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(Modular a) { return (int)a.value; } } public class PriorityQueue<T> { readonly List<T> _heap = new List<T>(); readonly Comparison<T> _comparison; public PriorityQueue(Comparison<T> comparison) { _comparison = comparison; } public PriorityQueue() : this(Comparer<T>.Default.Compare) {} public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) {} public void Enqueue(T item) { _heap.Add(item); int childID = _heap.Count - 1; int parentID = (childID - 1) / 2; while (childID > 0 && _comparison(_heap[parentID], _heap[childID]) > 0) { Swap(parentID, childID); childID = parentID; parentID = (parentID - 1) / 2; } } public T Dequeue() { var first = _heap.First(); _heap[0] = _heap.Last(); _heap.RemoveAt(_heap.Count - 1); int parentID = 0; int childID = parentID * 2 + 2; if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--; while (childID < _heap.Count && _comparison(_heap[parentID], _heap[childID]) > 0) { Swap(parentID, childID); parentID = childID; childID = parentID * 2 + 2; if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--; } return first; } void Swap(int parent, int child) { var tmp = _heap[parent]; _heap[parent] = _heap[child]; _heap[child] = tmp; } public T Peek() { return _heap[0]; } public int Count => _heap.Count; public bool Any() { return _heap.Any(); } public List<T> Data => _heap; } public class UnionFind { public int[] Parents { get; set; } public UnionFind(int size) { Parents = Enumerable.Repeat(-1, size).ToArray(); } public int Find(int num) { if (Parents[num] < 0) return num; Parents[num] = Find(Parents[num]); return Parents[num]; } public int Size(int num) { return -Parents[Find(num)]; } public bool Same(int a, int b) { return Find(a) == Find(b); } public void Unite(int a, int b) { int x = Find(a), y = Find(b); if (Size(x) > Size(y)) { Parents[x] += Parents[y]; Parents[y] = x; } else { Parents[y] += Parents[x]; Parents[x] = y; } } } class Input { static IEnumerator<string> enumerator = new string[] {}.AsEnumerable().GetEnumerator(); public static string Line => Console.ReadLine(); public static string[] StrArr => Line.Split(' '); public static int NextInt => int.Parse(NextWord()); public static long NextLong => long.Parse(NextWord()); public static List<int> IntList => StrArr.Select(int.Parse).ToList(); public static List<long> LongList => StrArr.Select(long.Parse).ToList(); public static string NextWord() { while (!enumerator.MoveNext()) { enumerator = StrArr.AsEnumerable().GetEnumerator(); } return enumerator.Current; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using static kyoupuro.Input; using static kyoupuro.MyMath; using static kyoupuro.Modular; namespace kyoupuro { class MainClass { public static void Main() { long N = NextLong, K = NextLong; var A = LongList; var array = new int[40]; foreach (var item in A) { long tmp = item; for (int i = 0; i < 40; i++) { if ((tmp & 1) == 1) { array[i]++; } tmp = tmp >> 1; } } long value = 0, sum = 0, bit = (1L << 39); foreach (var count in array.Reverse()) { if (count * 2 < N && value + bit <= K) { value += bit; sum += (N - count) * bit; } else { sum += count * bit; } bit = bit >> 1; } Console.WriteLine(sum); } } class MyMath { public static long Gcd(long a, long b) { return b == 0 ? a : Gcd(b, a % b); } public static long Sqrt(long n) { if (n < 0) return 0; long a = 0, tmp = 0, b = 0; for (int i = 62; i >= 0; i -= 2) { tmp = (b << 1) + 1 <= (n >> i) ? 1 : 0; a = a << 1 | tmp; n -= (((b << 1) + 1) * tmp) << i; b = (b << 1) + tmp + tmp; } return a; } } class Modular { public const int mod = 1000000007; public readonly long value; public Modular(long value) { this.value = value; } public static implicit operator Modular(long a) { var m = a % mod; return new Modular((m < 0) ? m + mod : m); } public static Modular operator +(Modular a, Modular b) { return a.value + b.value; } public static Modular operator -(Modular a, Modular b) { return a.value - b.value; } public static Modular operator *(Modular a, Modular b) { return a.value * b.value; } private static Modular Pow(Modular a, int n) { switch (n) { case 0: return 1; case 1: return a; default: var p = Pow(a, n / 2); return p * p * Pow(a, n % 2); } } public static Modular operator /(Modular a, Modular b) { return a * Pow(b, mod - 2); } private static readonly List<int> facs = new List<int> { 1 }; private static Modular Fac(int n) { for (int i = facs.Count; i <= n; ++i) { facs.Add((int)(Math.BigMul(facs.Last(), i) % mod)); } return facs[n]; } public static Modular Ncr(int n, int r) { return (n < r) ? 0 : (n == r) ? 1 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(Modular a) { return (int)a.value; } } public class PriorityQueue<T> { readonly List<T> _heap = new List<T>(); readonly Comparison<T> _comparison; public PriorityQueue(Comparison<T> comparison) { _comparison = comparison; } public PriorityQueue() : this(Comparer<T>.Default.Compare) {} public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) {} public void Enqueue(T item) { _heap.Add(item); int childID = _heap.Count - 1; int parentID = (childID - 1) / 2; while (childID > 0 && _comparison(_heap[parentID], _heap[childID]) > 0) { Swap(parentID, childID); childID = parentID; parentID = (parentID - 1) / 2; } } public T Dequeue() { var first = _heap.First(); _heap[0] = _heap.Last(); _heap.RemoveAt(_heap.Count - 1); int parentID = 0; int childID = parentID * 2 + 2; if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--; while (childID < _heap.Count && _comparison(_heap[parentID], _heap[childID]) > 0) { Swap(parentID, childID); parentID = childID; childID = parentID * 2 + 2; if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--; } return first; } void Swap(int parent, int child) { var tmp = _heap[parent]; _heap[parent] = _heap[child]; _heap[child] = tmp; } public T Peek() { return _heap[0]; } public int Count => _heap.Count; public bool Any() { return _heap.Any(); } public List<T> Data => _heap; } public class UnionFind { public int[] Parents { get; set; } public UnionFind(int size) { Parents = Enumerable.Repeat(-1, size).ToArray(); } public int Find(int num) { if (Parents[num] < 0) return num; Parents[num] = Find(Parents[num]); return Parents[num]; } public int Size(int num) { return -Parents[Find(num)]; } public bool Same(int a, int b) { return Find(a) == Find(b); } public void Unite(int a, int b) { int x = Find(a), y = Find(b); if (Size(x) > Size(y)) { Parents[x] += Parents[y]; Parents[y] = x; } else { Parents[y] += Parents[x]; Parents[x] = y; } } } class Input { static IEnumerator<string> enumerator = new string[] {}.AsEnumerable().GetEnumerator(); public static string Line => Console.ReadLine(); public static string[] StrArr => Line.Split(' '); public static int NextInt => int.Parse(NextWord()); public static long NextLong => long.Parse(NextWord()); public static List<int> IntList => StrArr.Select(int.Parse).ToList(); public static List<long> LongList => StrArr.Select(long.Parse).ToList(); public static string NextWord() { while (!enumerator.MoveNext()) { enumerator = StrArr.AsEnumerable().GetEnumerator(); } return enumerator.Current; } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 88], ["-", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["-", 8, 196, 0, 7, 10, 198, 0, 200, 141, 22], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 35], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 200, 0, 212, 0, 23, 0, 16, 31, 203], ["+", 0, 200, 0, 212, 0, 23, 0, 16, 17, 151], ["+", 0, 200, 0, 212, 0, 23, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 23, 0, 25], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 22], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 0, 266], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 0, 24], ["+", 0, 195, 8, 196, 0, 210, 39, 216, 0, 217], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 31, 22], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 0, 267], ["+", 8, 196, 0, 210, 12, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 210, 12, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 210, 12, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 210, 12, 213, 3, 4, 0, 24], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 0, 25], ["-", 15, 16, 31, 16, 31, 16, 31, 204, 205, 22], ["-", 31, 16, 31, 16, 31, 204, 206, 207, 0, 70], ["-", 31, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 31, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 57, 15, 16, 31, 16, 31, 16, 31, 22], ["-", 12, 16, 31, 23, 0, 16, 12, 204, 205, 22], ["-", 31, 23, 0, 16, 12, 204, 206, 207, 0, 70], ["-", 0, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["-", 31, 23, 0, 16, 12, 204, 206, 207, 0, 73], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 12, 22], ["-", 0, 1, 0, 11, 12, 16, 31, 204, 205, 22], ["-", 0, 11, 12, 16, 31, 204, 206, 207, 0, 70], ["-", 12, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 11, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 75, 196, 0, 1, 0, 11, 12, 16, 31, 22], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 151], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 152]]
8
1,485
44
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace AtCoder { public class ABC { static int mod = 1000000007; // 10^9+7 static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); // int n = int.Parse(Console.ReadLine()); // string s = Console.ReadLine(); // int[] vs = Console.ReadLine().Split().Select(int.Parse).ToArray(); long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray(); long n = vs[0]; long k = vs[1]; long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray(); int[] bit = new int[40]; long b = 1; for (int i = 0; i < bit.Length; i++) { foreach (var j in a) { if ((j & b) != 0) { bit[i]++; } } b *= 2; } System.Diagnostics.Debug.WriteLine(string.Join(" ", bit)); long x = 0; b = 0x8000000000; for (int i = bit.Length - 1; i >= 0; i--) { if (n - bit[i] > bit[i] && (x | b) < k) { x |= b; } b /= 2; } System.Diagnostics.Debug.WriteLine(x); long ans = 0; foreach (var i in a) { ans += i ^ x; } Console.WriteLine(ans); Console.Out.Flush(); } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace AtCoder { public class ABC { static int mod = 1000000007; // 10^9+7 static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); // int n = int.Parse(Console.ReadLine()); // string s = Console.ReadLine(); // int[] vs = Console.ReadLine().Split().Select(int.Parse).ToArray(); long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray(); long n = vs[0]; long k = vs[1]; long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray(); int[] bit = new int[40]; long b = 1; for (int i = 0; i < bit.Length; i++) { foreach (var j in a) { if ((j & b) != 0) { bit[i]++; } } b *= 2; } // System.Diagnostics.Debug.WriteLine(string.Join(" ", bit)); long x = 0; b = 0x8000000000; for (int i = bit.Length - 1; i >= 0; i--) { if (n - bit[i] > bit[i] && (x | b) <= k) { x |= b; } b /= 2; } // System.Diagnostics.Debug.WriteLine($"{x}"); long ans = 0; foreach (var i in a) { ans += i ^ x; } Console.WriteLine(ans); Console.Out.Flush(); } } }
[["-", 0, 213, 63, 214, 205, 214, 205, 214, 205, 22], ["-", 0, 213, 63, 214, 205, 214, 205, 214, 0, 131], ["-", 0, 213, 63, 214, 205, 214, 205, 214, 141, 22], ["-", 0, 1, 0, 213, 63, 214, 205, 214, 0, 131], ["-", 0, 1, 0, 213, 63, 214, 205, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 199], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 62], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["-", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 18], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 19], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22]]
8
342
33
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(); int N = int.Parse(str[0]); int M = int.Parse(str[1]); int[] A = new int[M]; string[] str2 = Console.ReadLine().Split(); for (var i = 0; i < M; i++) { A[i] = int.Parse(str2[i]); } int[] E = new int[8]; int[] EM = new int[8]; for (var i = 0; i < 8; i++) { E[i] = 99999; } int[] G = new int[10] { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; for (var i = 0; i < M; i++) { if (A[i] == 1) { E[2] = 2; EM[2] = A[i]; } else if (A[i] == 7) { E[3] = 3; EM[3] = A[i]; } else if (A[i] == 4) { E[4] = 4; EM[4] = A[i]; } else if (A[i] == 2 || A[i] == 3 || A[i] == 5) { E[5] = 5; EM[5] = Math.Max(EM[5], A[i]); } else if (A[i] == 6 || A[i] == 9) { E[6] = 6; EM[6] = Math.Max(EM[6], A[i]); } else if (A[i] == 8) { E[7] = 7; EM[7] = A[i]; } } int[] DP = new int[N + 1]; int[,] DA = new int[N + 1, 8]; for (var i = 1; i <= N; i++) { DP[i] = -1; } for (var i = 1; i <= N; i++) { for (var j = 7; j >= 2; j--) { if (i - j >= 0 && DP[i - j] != -1) { if (E[j] < 8) { bool flag = false; if (DP[i] == -1 || DP[i - j] + 1 > DP[i]) { flag = true; } else if (DP[i - j] + 1 == DP[i]) { for (var k = 9; k >= 1; k--) { if (E[G[k]] < 8) { int ta = 0; if (G[k] == j) { ta = 1; } if (DA[i - j, G[k]] + ta > DA[i, G[k]]) { flag = true; break; } else if (DA[i - j, G[k]] + ta < DA[i, G[k]]) { flag = false; break; } } } } if (flag) { for (var k = 0; k < 8; k++) { DA[i, k] = DA[i - j, k]; } DP[i] = DP[i - j] + 1; DA[i, j] += 1; } } } } } // for(var i=0;i<=N;i++){ // for(var j=0;j<8;j++){ // Console.Write(DA[i,j]); // } // Console.WriteLine(" "+DP[i]); //} var list = new List<int>(); int dn = N; for (var i = 0; i < DP[N]; i++) { for (var j = 9; j >= 1; j--) { if (E[G[j]] < 8) { if (dn - E[G[j]] >= 0 && DP[dn - E[G[j]]] == DP[dn] - 1) { list.Add(j); dn -= E[G[j]]; break; } } } } // for(var i=0;i<8;i++){ // for(var j=0;j<DA[N,i];j++){ // list.Add(EM[i]); // } //} // list.Sort((x,y)=>y-x); for (var i = 0; i < list.Count; i++) { Console.Write(list[i]); } } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(); int N = int.Parse(str[0]); int M = int.Parse(str[1]); int[] A = new int[M]; string[] str2 = Console.ReadLine().Split(); for (var i = 0; i < M; i++) { A[i] = int.Parse(str2[i]); } int[] E = new int[8]; int[] EM = new int[8]; for (var i = 0; i < 8; i++) { E[i] = 99999; } int[] G = new int[10] { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; for (var i = 0; i < M; i++) { if (A[i] == 1) { E[2] = 2; EM[2] = A[i]; } else if (A[i] == 7) { E[3] = 3; EM[3] = A[i]; } else if (A[i] == 4) { E[4] = 4; EM[4] = A[i]; } else if (A[i] == 2 || A[i] == 3 || A[i] == 5) { E[5] = 5; EM[5] = Math.Max(EM[5], A[i]); } else if (A[i] == 6 || A[i] == 9) { E[6] = 6; EM[6] = Math.Max(EM[6], A[i]); } else if (A[i] == 8) { E[7] = 7; EM[7] = A[i]; } } int[] DP = new int[N + 1]; int[,] DA = new int[N + 1, 8]; for (var i = 1; i <= N; i++) { DP[i] = -1; } for (var i = 1; i <= N; i++) { for (var j = 7; j >= 2; j--) { if (i - j >= 0 && DP[i - j] != -1) { if (E[j] < 8) { bool flag = false; if (DP[i] == -1 || DP[i - j] + 1 > DP[i]) { flag = true; } else if (DP[i - j] + 1 == DP[i]) { for (var k = 9; k >= 1; k--) { if (E[G[k]] < 8) { int ta = 0; if (G[k] == j) { ta = 1; } if (DA[i - j, G[k]] + ta > DA[i, G[k]]) { flag = true; break; } else if (DA[i - j, G[k]] + ta < DA[i, G[k]]) { flag = false; break; } } } } if (flag) { for (var k = 0; k < 8; k++) { DA[i, k] = DA[i - j, k]; } DP[i] = DP[i - j] + 1; DA[i, j] += 1; } } } } } // for(var i=0;i<=N;i++){ // for(var j=0;j<8;j++){ // Console.Write(DA[i,j]); // } // Console.WriteLine(" "+DP[i]); //} var list = new List<int>(); int dn = N; for (var i = 0; i < DP[N]; i++) { for (var j = 9; j >= 1; j--) { if (EM[G[j]] == j && E[G[j]] < 8) { if (dn - E[G[j]] >= 0 && DP[dn - E[G[j]]] == DP[dn] - 1) { list.Add(j); dn -= G[j]; break; } } } } for (var i = 0; i < list.Count; i++) { Console.Write(list[i]); } } }
[["+", 0, 57, 15, 16, 31, 16, 31, 204, 205, 22], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 204, 205, 22], ["+", 206, 207, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 206, 207, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 60], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 64, 196, 0, 1, 0, 11, 12, 204, 205, 22], ["-", 0, 1, 0, 11, 12, 204, 206, 207, 0, 70], ["-", 206, 207, 0, 28, 0, 204, 206, 207, 0, 73]]
8
940
13
using System; namespace AtCoderBeginnerContest118_4 { class Program { public static int[] act = null; static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]); int M = int.Parse(s[1]); s = Console.ReadLine().Split(' '); bool[] number = new bool[6]; int[] buf = new int[6]; int[] A = new int[M]; for (int i = 0; i < M; i++) { A[i] = int.Parse(s[i]); } Array.Sort(A); for (int i = 0; i < M; i++) { switch (A[i]) { case 1: number[0] = true; buf[0] = A[i]; break; case 7: number[1] = true; buf[1] = A[i]; break; case 4: number[2] = true; buf[2] = A[i]; break; case 2: case 3: case 5: number[3] = true; buf[3] = A[i]; break; case 6: case 9: number[4] = true; buf[4] = A[i]; break; case 8: number[5] = true; buf[5] = A[i]; break; } } int tmp = 0; for (int i = 0; i < 6; i++) { if (number[i]) tmp++; } act = new int[tmp]; tmp = 0; for (int i = 0; i < 6; i++) { if (number[i]) { act[tmp] = i + 2; tmp++; } } int[] real = new int[act.Length]; int[] lag = new int[act.Length]; for (int i = 0; i < act.Length; i++) { real[i] = buf[act[i] - 2]; lag[i] = buf[act[i] - 2]; } Array.Reverse(lag); int digits = check(N, 0); int rem = N; for (int i = 0; i < digits; i++) { for (int j = 0; j < lag.Length; j++) { if (digits - i - 1 == check(rem - getNuber(lag[j]), 0)) { Console.Write(lag[j]); rem -= getNuber(lag[j]); break; } } } } private static int getNuber(int i) { switch (i) { case 1: return 2; case 7: return 3; case 4: return 4; case 2: case 3: case 5: return 5; case 6: case 9: return 6; case 8: return 7; } return 0; } private static int check(int rem, int x) { for (int i = rem / act[x]; i >= 0; i--) { int rem2 = rem - i * act[x]; if (rem2 == 0) { return i; } if (x == act.Length - 1) return -1; int res = check(rem2, x + 1); if (res > 0) { return i + res; } } return -1; } } }
using System; namespace AtCoderBeginnerContest118_4 { class Program { public static int[] act = null; static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]); int M = int.Parse(s[1]); s = Console.ReadLine().Split(' '); bool[] number = new bool[6]; int[] buf = new int[6]; int[] A = new int[M]; for (int i = 0; i < M; i++) { A[i] = int.Parse(s[i]); } Array.Sort(A); for (int i = 0; i < M; i++) { switch (A[i]) { case 1: number[0] = true; buf[0] = A[i]; break; case 7: number[1] = true; buf[1] = A[i]; break; case 4: number[2] = true; buf[2] = A[i]; break; case 2: case 3: case 5: number[3] = true; buf[3] = A[i]; break; case 6: case 9: number[4] = true; buf[4] = A[i]; break; case 8: number[5] = true; buf[5] = A[i]; break; } } int tmp = 0; for (int i = 0; i < 6; i++) { if (number[i]) tmp++; } act = new int[tmp]; tmp = 0; for (int i = 0; i < 6; i++) { if (number[i]) { act[tmp] = i + 2; tmp++; } } int[] real = new int[act.Length]; int[] lag = new int[act.Length]; for (int i = 0; i < act.Length; i++) { real[i] = buf[act[i] - 2]; lag[i] = buf[act[i] - 2]; } Array.Sort(lag); Array.Reverse(lag); int digits = check(N, 0); int rem = N; for (int i = 0; i < digits; i++) { for (int j = 0; j < lag.Length; j++) { if (digits - i - 1 == check(rem - getNuber(lag[j]), 0)) { Console.Write(lag[j]); rem -= getNuber(lag[j]); break; } } } } private static int getNuber(int i) { switch (i) { case 1: return 2; case 7: return 3; case 4: return 4; case 2: case 3: case 5: return 5; case 6: case 9: return 6; case 8: return 7; } return 0; } private static int check(int rem, int x) { for (int i = rem / act[x]; i >= 0; i--) { int rem2 = rem - i * act[x]; if (rem2 == 0) { return i; } if (x == act.Length - 1) return -1; int res = check(rem2, x + 1); if (res > 0) { return i + res; } } return -1; } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131]]
8
748
7
using System; using System.IO; using System.Linq; using System.Collections.Generic; using Cons = System.Console; class PriorityQueue<T> { private readonly List<T> m_list; private readonly Func<T, T, int> m_compare; private int m_count; public PriorityQueue(int capacity, Func<T, T, int> compare) { m_list = new List<T>(capacity); m_compare = compare; m_count = 0; } private int Add(T value) { if (m_count == m_list.Count) { m_list.Add(value); } else { m_list[m_count] = value; } return m_count++; } private void Swap(int a, int b) { T tmp = m_list[a]; m_list[a] = m_list[b]; m_list[b] = tmp; } public void Enqueue(T value) { int c = Add(value); while (c > 0) { int p = (c - 1) / 2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } c = p; } } public T Dequeue() { T value = m_list[0]; m_list[0] = m_list[--m_count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= m_count) { break; } int c = (c2 >= m_count || m_compare(m_list[c1], m_list[c2]) < 0) ? c1 : c2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } p = c; } return value; } } class Program { static int Main(string[] args) { var sw = new StreamWriter(Cons.OpenStandardOutput()) { AutoFlush = false }; Cons.SetOut(sw); #if DBG int ret = Solve(); Cons.Out.Flush(); return ret; #else Solve(); return 0; #endif } static int Solve() { List<int> list = Console.ReadLine().Split(' ').Select(str => int.Parse(str)).ToList(); if ((list[1] * 2 - 1) > list[0]) { Cons.WriteLine("NO"); } else { Cons.WriteLine("YES"); } return 0; } }
using System; using System.IO; using System.Linq; using System.Collections.Generic; using Cons = System.Console; class PriorityQueue<T> { private readonly List<T> m_list; private readonly Func<T, T, int> m_compare; private int m_count; public PriorityQueue(int capacity, Func<T, T, int> compare) { m_list = new List<T>(capacity); m_compare = compare; m_count = 0; } private int Add(T value) { if (m_count == m_list.Count) { m_list.Add(value); } else { m_list[m_count] = value; } return m_count++; } private void Swap(int a, int b) { T tmp = m_list[a]; m_list[a] = m_list[b]; m_list[b] = tmp; } public void Enqueue(T value) { int c = Add(value); while (c > 0) { int p = (c - 1) / 2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } c = p; } } public T Dequeue() { T value = m_list[0]; m_list[0] = m_list[--m_count]; int p = 0; while (true) { int c1 = p * 2 + 1; int c2 = p * 2 + 2; if (c1 >= m_count) { break; } int c = (c2 >= m_count || m_compare(m_list[c1], m_list[c2]) < 0) ? c1 : c2; if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; } p = c; } return value; } } class Program { static int Main(string[] args) { var sw = new StreamWriter(Cons.OpenStandardOutput()) { AutoFlush = false }; Cons.SetOut(sw); int ret = Solve(); Cons.Out.Flush(); return ret; } static int Solve() { List<int> list = Console.ReadLine().Split(' ').Select(str => int.Parse(str)).ToList(); if ((list[1] * 2 - 1) > list[0]) { Cons.WriteLine("NO"); } else { Cons.WriteLine("YES"); } return 0; } }
[["-", 0, 208, 0, 235, 8, 201, 0, 195, 8, 196], ["-", 0, 235, 8, 201, 0, 195, 8, 196, 0, 197], ["-", 8, 201, 0, 195, 8, 196, 0, 197, 0, 198], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 216], ["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 200], ["-", 0, 197, 0, 198, 39, 216, 0, 200, 0, 1], ["-", 0, 198, 39, 216, 0, 200, 0, 1, 0, 213], ["-", 39, 216, 0, 200, 0, 1, 0, 213, 63, 214], ["-", 0, 200, 0, 1, 0, 213, 63, 214, 3, 4], ["-", 0, 1, 0, 213, 63, 214, 3, 4, 0, 301], ["-", 0, 213, 63, 214, 3, 4, 0, 301, 0, 22], ["-", 63, 214, 3, 4, 0, 301, 0, 22, 0, 197], ["-", 3, 4, 0, 301, 0, 22, 0, 197, 0, 198], ["-", 0, 301, 0, 22, 0, 197, 0, 198, 39, 199], ["-", 0, 22, 0, 197, 0, 198, 39, 199, 0, 200], ["-", 0, 197, 0, 198, 39, 199, 0, 200, 0, 1], ["-", 0, 198, 39, 199, 0, 200, 0, 1, 0, 213], ["-", 39, 199, 0, 200, 0, 1, 0, 213, 63, 214], ["-", 0, 1, 0, 213, 63, 214, 3, 4, 0, 37], ["-", 0, 213, 63, 214, 3, 4, 0, 37, 0, 22], ["-", 63, 214, 3, 4, 0, 37, 0, 22, 0, 302], ["-", 3, 4, 0, 37, 0, 22, 0, 302, 0, 1], ["-", 0, 37, 0, 22, 0, 302, 0, 1, 0, 213], ["-", 0, 22, 0, 302, 0, 1, 0, 213, 63, 22], ["-", 0, 302, 0, 1, 0, 213, 63, 22, 3, 4], ["-", 0, 1, 0, 213, 63, 22, 3, 4, 0, 37], ["-", 0, 213, 63, 22, 3, 4, 0, 37, 0, 203], ["-", 63, 22, 3, 4, 0, 37, 0, 203, 0, 303], ["-", 8, 201, 0, 195, 8, 196, 0, 301, 0, 121], ["-", 8, 201, 0, 195, 8, 196, 0, 301, 0, 22], ["-", 8, 201, 0, 195, 8, 196, 0, 37, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 302, 0, 95], ["-", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 37, 0, 38], ["-", 8, 201, 0, 195, 8, 196, 0, 37, 0, 203], ["-", 0, 235, 8, 201, 0, 195, 8, 196, 0, 303]]
8
532
14
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput input) { var n = input.ReadInt; var k = input.ReadInt; if ((1 + n) / 2 < k) WriteLine("YES"); else WriteLine("NO"); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput input) { var n = input.ReadInt; var k = input.ReadInt; WriteLine((1 + n) / 2 >= k ? "YES" : "NO"); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 3, 4, 0, 28, 0, 41, 15, 16, 17, 20], ["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["-", 8, 196, 0, 57, 64, 1, 0, 213, 63, 22], ["-", 0, 57, 64, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 101], ["-", 0, 57, 64, 1, 0, 213, 3, 4, 0, 25], ["-", 0, 195, 8, 196, 0, 57, 64, 1, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 95], ["-", 8, 196, 0, 57, 75, 1, 0, 213, 63, 22], ["-", 0, 57, 75, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 102]]
8
466
14
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var input = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); if (input[1] == 0 || input[1] == 1) { Console.WriteLine("YES"); } else if ((input[0] + 1 / 2) >= input[1]) { Console.WriteLine("YES"); } else { Console.WriteLine("NO"); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var input = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); if (input[1] == 1) { Console.WriteLine("YES"); } else if ((input[0] + 1) / 2 >= input[1]) { Console.WriteLine("YES"); } else { Console.WriteLine("NO"); } } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 106], ["-", 0, 57, 15, 16, 12, 16, 31, 204, 205, 22], ["-", 15, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["-", 15, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 60], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203], ["+", 75, 57, 15, 16, 31, 16, 31, 23, 0, 25], ["-", 0, 57, 75, 57, 15, 16, 31, 23, 0, 25]]
8
140
9
using System; using System.Diagnostics; using System.IO; namespace AtCoder.MinPuro2019 { class A { static void Main() => CustomMain(); static void Solve() { int n, k; ReadMulti(out n, out k); WriteAnswer(k <= n / 2 ? "YES" : "NO"); } static void CustomMain() { #if DEBUG // ファイルからテストケースを読み込む global::Common.ChangeStdIn(); // 時間計測 var stopwatch = new Stopwatch(); stopwatch.Start(); #endif // 出力高速化 var streamWriter = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(streamWriter); // 回答 Solve(); #if DEBUG stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); #endif Console.Out.Flush(); } static int ReadInt() => int.Parse(Console.ReadLine()); static long ReadLong() => long.Parse(Console.ReadLine()); static float ReadFloat() => float.Parse(Console.ReadLine()); static double ReadDouble => double.Parse(Console.ReadLine()); static string ReadString() => Console.ReadLine(); static int[] ReadIntArray() => Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); static long[] ReadLongArray() => Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); static float[] ReadFloatArray() => Array.ConvertAll(Console.ReadLine().Split(' '), float.Parse); static double[] ReadDoubleArray() => Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); static string[] ReadStringArray() => Console.ReadLine().Split(' '); static T TypeConversion<T>(string s) { if (typeof(T) == typeof(int)) { return (T)Convert.ChangeType(int.Parse(s), typeof(T)); } else if (typeof(T) == typeof(long)) { return (T)Convert.ChangeType(long.Parse(s), typeof(T)); } else if (typeof(T) == typeof(double)) { return (T)Convert.ChangeType(double.Parse(s), typeof(T)); } else { return (T)Convert.ChangeType(s, typeof(T)); } } static void ReadMulti<T, U>(out T a, out U b) { var str = ReadStringArray(); a = TypeConversion<T>(str[0]); b = TypeConversion<U>(str[1]); } static void ReadMulti<T, U, V>(out T a, out U b, out V c) { var str = ReadStringArray(); a = TypeConversion<T>(str[0]); b = TypeConversion<U>(str[1]); c = TypeConversion<V>(str[2]); } static void WriteAnswer(object ans) => Console.WriteLine(ans); } }
using System; using System.Diagnostics; using System.IO; namespace AtCoder.MinPuro2019 { class A { static void Main() => CustomMain(); static void Solve() { int n, k; ReadMulti(out n, out k); if (n % 2 == 1) { WriteAnswer(k <= n / 2 + 1 ? "YES" : "NO"); } else { WriteAnswer(k <= n / 2 ? "YES" : "NO"); } } static void CustomMain() { #if DEBUG // ファイルからテストケースを読み込む global::Common.ChangeStdIn(); // 時間計測 var stopwatch = new Stopwatch(); stopwatch.Start(); #endif // 出力高速化 var streamWriter = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(streamWriter); // 回答 Solve(); #if DEBUG stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); #endif Console.Out.Flush(); } static int ReadInt() => int.Parse(Console.ReadLine()); static long ReadLong() => long.Parse(Console.ReadLine()); static float ReadFloat() => float.Parse(Console.ReadLine()); static double ReadDouble => double.Parse(Console.ReadLine()); static string ReadString() => Console.ReadLine(); static int[] ReadIntArray() => Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); static long[] ReadLongArray() => Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); static float[] ReadFloatArray() => Array.ConvertAll(Console.ReadLine().Split(' '), float.Parse); static double[] ReadDoubleArray() => Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); static string[] ReadStringArray() => Console.ReadLine().Split(' '); static T TypeConversion<T>(string s) { if (typeof(T) == typeof(int)) { return (T)Convert.ChangeType(int.Parse(s), typeof(T)); } else if (typeof(T) == typeof(long)) { return (T)Convert.ChangeType(long.Parse(s), typeof(T)); } else if (typeof(T) == typeof(double)) { return (T)Convert.ChangeType(double.Parse(s), typeof(T)); } else { return (T)Convert.ChangeType(s, typeof(T)); } } static void ReadMulti<T, U>(out T a, out U b) { var str = ReadStringArray(); a = TypeConversion<T>(str[0]); b = TypeConversion<U>(str[1]); } static void ReadMulti<T, U, V>(out T a, out U b, out V c) { var str = ReadStringArray(); a = TypeConversion<T>(str[0]); b = TypeConversion<U>(str[1]); c = TypeConversion<V>(str[2]); } static void WriteAnswer(object ans) => Console.WriteLine(ans); } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 109], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 57, 64, 196, 0, 1, 0, 213, 63, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 41, 15, 16, 31, 22], ["+", 3, 4, 0, 28, 0, 41, 15, 16, 17, 19], ["+", 0, 41, 15, 16, 12, 16, 31, 16, 31, 22], ["+", 0, 41, 15, 16, 12, 16, 31, 16, 17, 85], ["+", 0, 41, 15, 16, 12, 16, 31, 16, 12, 203], ["+", 0, 28, 0, 41, 15, 16, 12, 16, 17, 72], ["+", 0, 28, 0, 41, 15, 16, 12, 16, 12, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 101], ["+", 3, 4, 0, 28, 0, 41, 64, 5, 0, 62], ["+", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 102], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 62], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 95], ["+", 0, 195, 8, 196, 0, 57, 75, 196, 0, 45], ["+", 0, 195, 8, 196, 0, 57, 75, 196, 0, 46]]
8
661
32
using System; using System.Collections.Generic; using System.Linq; namespace MP2019A { class MainClass { static int[] s; static int[] n; public static void Main(string[] args) { s = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse); if ((s[0] + 1) / 2 <= s[1]) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } return; } } }
using System; using System.Collections.Generic; using System.Linq; namespace MP2019A { class MainClass { static int[] s; public static void Main(string[] args) { s = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse); if ((s[0] + 1) / 2 >= s[1]) { Console.WriteLine("YES"); } else { Console.WriteLine("NO"); } return; } } }
[["-", 0, 235, 8, 201, 0, 124, 0, 251, 0, 115], ["-", 8, 201, 0, 124, 0, 198, 39, 224, 39, 199], ["-", 0, 124, 0, 198, 39, 224, 225, 226, 0, 70], ["-", 0, 124, 0, 198, 39, 224, 225, 226, 0, 73], ["-", 8, 201, 0, 124, 0, 198, 0, 200, 141, 22], ["-", 8, 201, 0, 235, 8, 201, 0, 124, 0, 35], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 20], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
115
12
using System; using System.Collections.Generic; using System.Linq; using static Input; public class Prog { private const int INF = 1000000007; public static void Main() { long K = NextInt(); long k = K; int A = NextInt(); int B = NextInt(); long C = 1; //最初は手動 long c = A - C + 2; K -= c; C += B - 1; // 1回ごとに得られる差 int r = B - A; //あまりを増やし続けながらK + 2 で変換だけする long dc = K / 2; if (K - dc * 2 == 1) C++; C += dc * r; Console.WriteLine(Math.Max(C, 1 + k)); } } public class Input { private static Queue<string> queue = new Queue<string>(); private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); } public static string NextString() { Confirm(); return queue.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } }
using System; using System.Collections.Generic; using System.Linq; using static Input; public class Prog { private const int INF = 1000000007; public static void Main() { long K = NextInt(); long k = K; int A = NextInt(); int B = NextInt(); long C = 1; //最初は手動 long c = A - C + 2; K -= c; if (K < 0) { Console.WriteLine(k + 1); return; } C += B - 1; // 1回ごとに得られる差 int r = B - A; //あまりを増やし続けながらK + 2 で変換だけする long dc = K / 2; if (K - dc * 2 == 1) C++; C += dc * r; Console.WriteLine(Math.Max(C, 1 + k)); } } public class Input { private static Queue<string> queue = new Queue<string>(); private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); } public static string NextString() { Confirm(); return queue.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); } public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); } public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46]]
8
427
19
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; class Z { static void Main() => new K(); } class K { int F => int.Parse(Str); long FL => long.Parse(Str); int[] G => Strs.Select(int.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); const int MOD = 998244353; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(Solve()); Out.Flush(); } long Solve() { var N = F; var A = new int[N]; for (var i = 0; i < N; i++) A[i] = F; // 0 1 2 3 4 // null even odd even null (どの区間も長さ 0 になりうる) // dp[i,s] = A[0,i) まで見て, 状態 s まで来ているときの最小値 // 答えは dp[N,4] // dp[i,s] => dp[i,s+1] // dp[i,s] => dp[i+1,s] var dp = new long[N + 1, 5]; for (var i = 1; i <= N; i++) for (var s = 0; s < 5; s++) dp[i, s] = long.MaxValue / 10; for (var i = 0; i <= N; i++) for (var s = 0; s < 5; s++) { if (s < 4) dp[i, s + 1] = Min(dp[i, s + 1], dp[i, s]); if (i < N) { // dp[i,s] => dp[i+1,s] (A[i] をどうするか) if (s == 0 || s == 4) dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + A[i]); else if (s == 2) { dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + (A[i] + 1) % 2); } else dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + A[i] % 2); } } // for (var i = 0; i <= N; i++) for (var s = 0; s < 5; s++) // WriteLine($"dp[{i}, {s}] = {dp[i, s]}"); return dp[N, 4]; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; class Z { static void Main() => new K(); } class K { int F => int.Parse(Str); long FL => long.Parse(Str); int[] G => Strs.Select(int.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); const int MOD = 998244353; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(Solve()); Out.Flush(); } long Solve() { var N = F; var A = new int[N]; for (var i = 0; i < N; i++) A[i] = F; // 0 1 2 3 4 // null even odd even null (どの区間も長さ 0 になりうる) // dp[i,s] = A[0,i) まで見て, 状態 s まで来ているときの最小値 // 答えは dp[N,4] // dp[i,s] => dp[i,s+1] // dp[i,s] => dp[i+1,s] var dp = new long[N + 1, 5]; for (var i = 1; i <= N; i++) for (var s = 0; s < 5; s++) dp[i, s] = long.MaxValue / 10; for (var i = 0; i <= N; i++) for (var s = 0; s < 5; s++) { if (s < 4) dp[i, s + 1] = Min(dp[i, s + 1], dp[i, s]); if (i < N) { // dp[i,s] => dp[i+1,s] (A[i] をどうするか) if (s == 0 || s == 4) dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + A[i]); else if (s == 2) { dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + (A[i] + 1) % 2); } else dp[i + 1, s] = Min(dp[i + 1, s], dp[i, s] + (A[i] == 0 ? 2 : A[i] % 2)); } } // for (var i = 0; i <= N; i++) for (var s = 0; s < 5; s++) // WriteLine($"dp[{i}, {s}] = {dp[i, s]}"); return dp[N, 4]; } }
[["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 0, 16, 12, 23, 0, 41, 15, 16, 17, 60], ["+", 0, 16, 12, 23, 0, 41, 15, 16, 12, 203], ["+", 0, 28, 0, 16, 12, 23, 0, 41, 0, 101], ["+", 0, 28, 0, 16, 12, 23, 0, 41, 64, 203], ["+", 0, 28, 0, 16, 12, 23, 0, 41, 0, 102], ["+", 12, 23, 0, 41, 75, 16, 31, 204, 205, 22], ["+", 0, 41, 75, 16, 31, 204, 206, 207, 0, 70], ["+", 75, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 41, 75, 16, 31, 204, 206, 207, 0, 73], ["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
516
11
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; class Z { static void Main() => new K(); } class K { int F => int.Parse(Str); long FL => long.Parse(Str); int[] G => Strs.Select(int.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); const int MOD = 998244353; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(Solve()); Out.Flush(); } Random rand = new Random(); long Solve() { var I = G; int Y = I[0], X = I[1]; var mat = new bool [Y][]; for (var y = 0; y < Y; y++) { mat[y] = new bool[X]; // I = G; // for (var x = 0; x < X; x++) mat[y][x] = I[x] != 0; for (var x = 0; x < X; x++) mat[y][x] = rand.Next(6) == 0; } var r = 0; for (var c = 0; c < X; c++) { int y = r; while (y < Y && !mat[y][c]) y++; if (y == Y) continue; if (y != r) AddTo(mat[r], mat[y]); for (y = r + 1; y < Y; y++) if (mat[y][c]) AddTo(mat[y], mat[r]); r++; } WriteLine($"rank = {r}"); foreach (var m in mat) Print(m); return (Pow(2, X + Y - 1, MOD) - Pow(2, X + Y - r - 1, MOD) + MOD) % MOD; } void Print(bool[] a) => WriteLine(string.Join("", a.Select(b => b ? 1 : 0))); public static long Pow(long a, long b, long mod) { var p = 1L; var x = a; while (b > 0) { if ((b & 1) == 1) p = (p * x) % mod; b >>= 1; x = (x * x) % mod; } return p; } // a += b void AddTo(bool[] a, bool[] b) { var n = a.Length; for (var i = 0; i < n; i++) a[i] = a[i] ^ b[i]; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; class Z { static void Main() => new K(); } class K { int F => int.Parse(Str); long FL => long.Parse(Str); int[] G => Strs.Select(int.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); const int MOD = 998244353; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); WriteLine(Solve()); Out.Flush(); } Random rand = new Random(); long Solve() { var I = G; int Y = I[0], X = I[1]; var mat = new bool [Y][]; for (var y = 0; y < Y; y++) { mat[y] = new bool[X]; I = G; for (var x = 0; x < X; x++) mat[y][x] = I[x] != 0; } var r = 0; for (var c = 0; c < X; c++) { int y = r; while (y < Y && !mat[y][c]) y++; if (y == Y) continue; if (y != r) AddTo(mat[r], mat[y]); for (y = r + 1; y < Y; y++) if (mat[y][c]) AddTo(mat[y], mat[r]); r++; } return (Pow(2, X + Y - 1, MOD) - Pow(2, X + Y - r - 1, MOD) + MOD) % MOD; } void Print(bool[] a) => WriteLine(string.Join("", a.Select(b => b ? 1 : 0))); public static long Pow(long a, long b, long mod) { var p = 1L; var x = a; while (b > 0) { if ((b & 1) == 1) p = (p * x) % mod; b >>= 1; x = (x * x) % mod; } return p; } // a += b void AddTo(bool[] a, bool[] b) { var n = a.Length; for (var i = 0; i < n; i++) a[i] = a[i] ^ b[i]; } }
[["+", 0, 7, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 0, 7, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35], ["-", 0, 11, 12, 16, 31, 213, 63, 214, 205, 22], ["-", 0, 11, 12, 16, 31, 213, 63, 214, 0, 131], ["-", 0, 11, 12, 16, 31, 213, 63, 214, 141, 22], ["-", 0, 11, 12, 16, 31, 213, 3, 4, 0, 24], ["-", 12, 16, 31, 213, 3, 4, 0, 28, 0, 203], ["-", 0, 11, 12, 16, 31, 213, 3, 4, 0, 25], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 17, 60], ["+", 8, 1, 0, 11, 12, 16, 31, 204, 205, 22], ["+", 0, 11, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 12, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 11, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 7, 8, 1, 0, 11, 12, 16, 17, 79], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 46], ["-", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 283, 0, 295], ["-", 0, 213, 3, 4, 0, 28, 0, 283, 0, 296], ["-", 3, 4, 0, 28, 0, 283, 0, 284, 0, 45], ["-", 3, 4, 0, 28, 0, 283, 0, 284, 0, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 283, 0, 62], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 266], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 24], ["-", 0, 195, 8, 196, 0, 210, 39, 216, 0, 217], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 31, 22], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 267], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 12, 22], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 25], ["-", 8, 196, 0, 210, 8, 1, 0, 213, 63, 22], ["-", 0, 210, 8, 1, 0, 213, 3, 4, 0, 24], ["-", 8, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 0, 210, 8, 1, 0, 213, 3, 4, 0, 25], ["-", 0, 195, 8, 196, 0, 210, 8, 1, 0, 35]]
8
635
38
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace AtTest.ABC_117 { class D { static void Main(string[] args) { Method(args); Console.ReadLine(); } static void Method(string[] args) { long[] nk = ReadLongs(); var array = ReadLongs(); Array.Sort(array); long n = nk[0]; long k = nk[1]; int aDigit = 0; long aa = array[n - 1]; while (aa > 1) { aDigit++; aa /= 2; } int kDigit = 0; long kk = k; while (kk > 1) { kDigit++; kk /= 2; } long res = 0; for (int i = kDigit + 1; i >= 0; i--) { long val = 0; bool down = (k & (long)Math.Pow(2, i)) > 0; for (int j = Math.Max(aDigit, kDigit); j >= 0; j--) { long div = (long)Math.Pow(2, j); long cnt = 0; for (int l = 0; l < n; l++) { if ((array[l] & div) > 0) cnt++; } if (cnt >= n - cnt) { val += div * cnt; } else { if (down) { if (j == i || (j > i && (k & div) == 0)) val += div * cnt; else val += div * (n - cnt); } else { if ((k & div) == 0) val += div * cnt; else val += div * (n - cnt); } } // Console.WriteLine(cnt+" "+val); } res = Math.Max(res, val); } // Console.WriteLine(res); } private static string Read() { return Console.ReadLine(); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } } }
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace AtTest.ABC_117 { class D { static void Main(string[] args) { Method(args); Console.ReadLine(); } static void Method(string[] args) { long[] nk = ReadLongs(); var array = ReadLongs(); Array.Sort(array); long n = nk[0]; long k = nk[1]; int aDigit = 0; long aa = array[n - 1]; while (aa > 1) { aDigit++; aa /= 2; } int kDigit = 0; long kk = k; while (kk > 1) { kDigit++; kk /= 2; } long res = 0; for (int i = kDigit + 1; i >= 0; i--) { long val = 0; bool down = (k & (long)Math.Pow(2, i)) > 0; for (int j = Math.Max(aDigit, kDigit); j >= 0; j--) { long div = (long)Math.Pow(2, j); long cnt = 0; for (int l = 0; l < n; l++) { if ((array[l] & div) > 0) cnt++; } if (cnt >= n - cnt) { val += div * cnt; } else { if (down) { if (j == i || (j > i && (k & div) == 0)) val += div * cnt; else val += div * (n - cnt); } else { if ((k & div) == 0) val += div * cnt; else val += div * (n - cnt); } } // Console.WriteLine(cnt+" "+val); } res = Math.Max(res, val); } Console.WriteLine(res); } private static string Read() { return Console.ReadLine(); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
539
7
using System; using System.Linq; using System.Collections.Generic; class Dmondai { static void Main() { var line = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); var N = line[0]; var K = line[1]; var A = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); if (K == 0) { Console.WriteLine(A.Sum(x => x)); return; } var length = (long)Math.Log(K, 2); var count = new int[length + 1]; var bit = 1L; for (int i = 0; i <= length; ++i) { for (long j = 0; j < N; ++j) if ((bit & A[j]) != 0) count[i]++; bit <<= 1; } bit = 1L; var num = 0L; var temp = 0L; for (int i = 0; i <= length; ++i) { if (count[i] < ((N + 1) / 2)) temp |= bit; if (temp <= K) num = temp; else temp = num; bit <<= 1; } var ans = 0L; for (int i = 0; i < N; ++i) ans += (num ^ A[i]); Console.WriteLine(ans); } }
using System; using System.Linq; using System.Collections.Generic; class Dmondai { static void Main() { var line = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); var N = line[0]; var K = line[1]; var A = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); if (K == 0) { Console.WriteLine(A.Sum(x => x)); return; } var length = (long)Math.Log(K, 2); var count = new int[length + 1]; var bit = 1L; for (int i = 0; i <= length; ++i) { for (long j = 0; j < N; ++j) if ((bit & A[j]) != 0) count[i]++; bit <<= 1; } bit = 1L; bit <<= (int)length; var num = 0L; var temp = 0L; for (long i = length; 0 <= i; --i) { if (count[i] < ((N + 1) / 2)) temp |= bit; if (temp <= K) num = temp; else temp = num; bit >>= 1; } var ans = 0L; for (int i = 0; i < N; ++i) ans += (num ^ A[i]); Console.WriteLine(ans); } }
[["+", 0, 195, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 177], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 0, 24], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 39, 199], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 0, 25], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 51, 22], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["-", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 22], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 31, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22], ["-", 0, 195, 8, 196, 0, 7, 26, 241, 0, 29], ["+", 0, 195, 8, 196, 0, 7, 26, 241, 0, 68], ["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 177], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 176]]
8
321
19
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(); int N = int.Parse(str[0]); long K = long.Parse(str[1]); string[] str2 = Console.ReadLine().Split(); long[] A = new long[N]; for (var i = 0; i < N; i++) { A[i] = long.Parse(str2[i]); } int[] B1 = new int[40]; int[] B2 = new int[40]; for (var i = 0; i < N; i++) { long t = 1; for (var j = 0; j < 40; j++) { long x = A[i] / t % 2; if (x == 0) { B1[j]++; } else { B2[j]++; } t *= 2; } } long t2 = (long)Math.Pow(2, 39); long[] D1 = new long[40]; long[] D2 = new long[40]; long ansf = 0; long ans = 0; bool flag = false; for (var i = 39; i >= 0; i--) { if (flag) { if (B1[i] > B2[i]) { D1[i] = D1[i + 1] + B1[i] * t2; if (K >= t2 + ansf) { ansf += t2; ans += B1[i] * t2; } else { ans += B2[i] * t2; } } else { D1[i] = D1[i + 1] + B2[i] * t2; ans += B2[i] * t2; } D2[i] = ans; } else if (K >= t2) { flag = true; if (B1[i] > B2[i]) { ansf += t2; ans += B1[i] * t2; } else { ans += B2[i] * t2; } D2[i] = ans; D1[i] = B2[i] * t2; } t2 /= 2; } Console.WriteLine(Math.Max(D1[0], ans)); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(); int N = int.Parse(str[0]); long K = long.Parse(str[1]); string[] str2 = Console.ReadLine().Split(); long[] A = new long[N]; for (var i = 0; i < N; i++) { A[i] = long.Parse(str2[i]); } int[] B1 = new int[40]; int[] B2 = new int[40]; for (var i = 0; i < N; i++) { long t = 1; for (var j = 0; j < 40; j++) { long x = A[i] / t % 2; if (x == 0) { B1[j]++; } else { B2[j]++; } t *= 2; } } long t2 = (long)Math.Pow(2, 39); long[] D1 = new long[40]; long[] D2 = new long[40]; long ansf = 0; long ans = 0; bool flag = false; for (var i = 39; i >= 0; i--) { if (flag) { if (B1[i] > B2[i]) { D1[i] = D1[i + 1] + B1[i] * t2; if (K >= t2 + ansf) { ansf += t2; ans += B1[i] * t2; } else { ans += B2[i] * t2; } } else { D1[i] = D1[i + 1] + B2[i] * t2; ans += B2[i] * t2; } D2[i] = ans; } else if (K >= t2) { D1[i] = ans + B2[i] * t2; flag = true; if (B1[i] > B2[i]) { ansf += t2; ans += B1[i] * t2; } else { ans += B2[i] * t2; } D2[i] = ans; } else { ans += B2[i] * t2; D2[i] = ans; } t2 /= 2; } Console.WriteLine(Math.Max(D1[0], ans)); } }
[["+", 64, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 31, 22], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 0, 11, 12, 16, 12, 16, 31, 204, 205, 22], ["+", 12, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 12, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 12, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 12, 22], ["+", 0, 57, 75, 57, 64, 196, 0, 1, 0, 35], ["-", 64, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["-", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 196, 0, 57, 75, 57, 64, 196, 0, 46], ["+", 0, 7, 8, 196, 0, 57, 75, 57, 0, 95], ["+", 8, 196, 0, 57, 75, 57, 75, 196, 0, 45], ["+", 75, 57, 75, 196, 0, 1, 0, 11, 31, 22], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 107], ["+", 0, 57, 75, 57, 75, 196, 0, 1, 0, 35], ["+", 75, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 75, 57, 75, 196, 0, 1, 0, 11, 12, 22]]
8
493
31
using System; using System.IO; using System.Collections.Generic; using System.Linq; namespace ABC117D { public class Program { void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- const int MAX = 50; var N = ss.Next(int.Parse); var K = ss.Next(long.Parse); var A = ss.Next(long.Parse, N); var pops = new long[MAX]; foreach (var a in A) { for (long i = 0, t = a; t > 0; i++, t >>= 1) { if ((t & 1) == 1) pops[i]++; } } var memo = new long[MAX]; for (long i = 0, t = 1; i < MAX; i++, t <<= 1) { memo[i] += Math.Max(N - pops[i], pops[i]) * t; if (i > 0) memo[i] += memo[i - 1]; } var ans = 0L; var calc = 0L; var kBase2 = Convert.ToString(K, 2); var kBase2Rev = (new string('0', MAX - kBase2.Length) + kBase2) .Select(c => c - '0') .Reverse() .ToArray(); for (long i = MAX - 1, t = 1L << (MAX - 1); i >= 0; i--, t >>= 1) { if (kBase2Rev[i] == 0) { calc += pops[i] * t; } else { ans = Math.Max(ans, calc + memo[i]); calc += (N - pops[i]) * t; } } ans = Math.Max(ans, calc); sw.WriteLine(ans); //--------------------------------- } static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; new Program().Solve(ss, sw); sw.Flush(); } static readonly Func<string, string> String = s => s; } public class StreamScanner { static readonly char[] Sep = { ' ' }; readonly Queue<string> buffer = new Queue<string>(); readonly TextReader textReader; public StreamScanner(TextReader textReader) { this.textReader = textReader; } public T Next<T>(Func<string, T> parser) { if (buffer.Count != 0) return parser(buffer.Dequeue()); var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries); foreach (var nextString in nextStrings) buffer.Enqueue(nextString); return Next(parser); } public T[] Next<T>(Func<string, T> parser, int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next(parser); return ret; } public T[][] Next<T>(Func<string, T> parser, int x, int y) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next(parser, x); return ret; } } }
using System; using System.IO; using System.Collections.Generic; using System.Linq; namespace ABC117D { public class Program { void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- const int MAX = 50; var N = ss.Next(int.Parse); var K = ss.Next(long.Parse); var A = ss.Next(long.Parse, N); var pops = new long[MAX]; foreach (var a in A) { for (long i = 0, t = a; t > 0; i++, t >>= 1) { if ((t & 1) == 1) pops[i]++; } } var memo = new long[MAX]; for (long i = 0, t = 1; i < MAX; i++, t <<= 1) { memo[i] += Math.Max(N - pops[i], pops[i]) * t; if (i > 0) memo[i] += memo[i - 1]; } var ans = 0L; var calc = 0L; var kBase2 = Convert.ToString(K, 2); var kBase2Rev = (new string('0', MAX - kBase2.Length) + kBase2) .Select(c => c - '0') .Reverse() .ToArray(); for (long i = MAX - 1, t = 1L << (MAX - 1); i >= 0; i--, t >>= 1) { if (kBase2Rev[i] == 0) { calc += pops[i] * t; } else { if (i > 0) ans = Math.Max(ans, calc + pops[i] * t + memo[i - 1]); calc += (N - pops[i]) * t; } } ans = Math.Max(ans, calc); sw.WriteLine(ans); //--------------------------------- } static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; new Program().Solve(ss, sw); sw.Flush(); } static readonly Func<string, string> String = s => s; } public class StreamScanner { static readonly char[] Sep = { ' ' }; readonly Queue<string> buffer = new Queue<string>(); readonly TextReader textReader; public StreamScanner(TextReader textReader) { this.textReader = textReader; } public T Next<T>(Func<string, T> parser) { if (buffer.Count != 0) return parser(buffer.Dequeue()); var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries); foreach (var nextString in nextStrings) buffer.Enqueue(nextString); return Next(parser); } public T[] Next<T>(Func<string, T> parser, int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next(parser); return ret; } public T[][] Next<T>(Func<string, T> parser, int x, int y) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next(parser, x); return ret; } } }
[["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 24], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 25], ["+", 0, 16, 31, 16, 12, 16, 31, 204, 205, 22], ["+", 31, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 12, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 31, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 28, 0, 16, 31, 16, 12, 16, 17, 48], ["+", 0, 28, 0, 16, 31, 16, 12, 16, 12, 22], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
706
15
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoderBeginnerContest117Fourth { class Program { static void Main(string[] args) { // 整数の入力 // int N = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input1 = Console.ReadLine().Split(' '); int N = int.Parse(input1[0]); long K = long.Parse(input1[1]); int[] column = new int[40]; // 各桁の1の数を記録 string[] A = new string[N]; long[] numA = new long[N]; string[] input2 = Console.ReadLine().Split(' '); for (var i = 0; i < N; i++) { numA[i] = long.Parse(input2[i]); string tmp = Convert.ToString(long.Parse(input2[i]), 2); // 2進数へ A[i] = string.Join("", tmp.Reverse()); // 桁表示を反転 for (var j = 0; j < A[i].Length; j++) { string s = A[i].Substring(j, 1); if (s == "1") { column[j]++; // 各桁の1の数 } } } /* for(var k = 0; k < N; k++) { Console.Write(A[k]+" "); } Console.WriteLine(); for(var k = 0; k < column.Length; k++) { Console.WriteLine(k+":"+column[k]); } */ // long X = 0; for (var k = 0; k < column.Length; k++) { if (column[k] < N - column[k]) { X += (long)Math.Pow(2, k); if (X > K) { X -= (long)Math.Pow(2, k); break; } } } // Console.WriteLine(X); long ans = 0; foreach (long num in numA) { ans += X ^ num; // ans = X ^ num; // Console.WriteLine(ans); } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace D_XXOR { class Program { static void Main(string[] args) { // 整数の入力 // int N = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input1 = Console.ReadLine().Split(' '); int N = int.Parse(input1[0]); long K = long.Parse(input1[1]); int[] column = new int[40]; // 各桁の1の数を記録 string[] A = new string[N]; long[] numA = new long[N]; string[] input2 = Console.ReadLine().Split(' '); for (var i = 0; i < N; i++) { numA[i] = long.Parse(input2[i]); string tmp = Convert.ToString(long.Parse(input2[i]), 2); // 2進数へ A[i] = string.Join("", tmp.Reverse()); // 桁表示を反転 for (var j = 0; j < A[i].Length; j++) { string s = A[i].Substring(j, 1); if (s == "1") { column[j]++; // 各桁の1の数 } } } /* for(var k = 0; k < N; k++) { Console.Write(A[k]+" "); } Console.WriteLine(); for(var k = 0; k < column.Length; k++) { Console.WriteLine(k+":"+column[k]); } */ // long X = 0; for (var k = column.Length - 1; k >= 0; k--) { if (column[k] < N - column[k]) { X += (long)Math.Pow(2, k); if (X > K) { X -= (long)Math.Pow(2, k); } } } // Console.WriteLine(X); long ans = 0; foreach (long num in numA) { ans += X ^ num; // ans = X ^ num; // Console.WriteLine(ans); } Console.WriteLine(ans); } } }
[["-", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 35], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 7, 0, 35], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 20], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 0, 195, 8, 196, 0, 7, 26, 223, 0, 68], ["-", 64, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 64, 196, 0, 57, 64, 196, 0, 93, 0, 94]]
8
379
16
using System; using System.IO; using System.Linq; namespace Contest { class Scanner { public Scanner() { _stream = new StreamReader(Console.OpenStandardInput()); _pos = 0; _line = new string[0]; _separator = ' '; } private char _separator; private StreamReader _stream; private int _pos; private string[] _line; #region get a element public string Next() { if (_pos >= _line.Length) { _line = _stream.ReadLine().Split(_separator); _pos = 0; } return _line[_pos++]; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } #endregion #region convert array private int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion #region get row elements #region get array public string[] Array() { if (_pos >= _line.Length) _line = _stream.ReadLine().Split(_separator); _pos = _line.Length; return _line; } public int[] IntArray() { return ToIntArray(Array()); } public long[] LongArray() { return ToLongArray(Array()); } public double[] DoubleArray() { return ToDoubleArray(Array()); } #endregion #region get 2 ~4 elements public void GetRow(out string a, out string b) { a = Next(); b = Next(); } public void GetRow(out string a, out string b, out string c) { a = Next(); b = Next(); c = Next(); } public void GetRow(out string a, out string b, out string c, out string d) { a = Next(); b = Next(); c = Next(); d = Next(); } public void GetIntRow(out int a, out int b) { a = NextInt(); b = NextInt(); } public void GetIntRow(out int a, out int b, out int c) { a = NextInt(); b = NextInt(); c = NextInt(); } public void GetIntRow(out int a, out int b, out int c, out int d) { a = NextInt(); b = NextInt(); c = NextInt(); d = NextInt(); } public void GetLongRow(out long a, out long b) { a = NextLong(); b = NextLong(); } public void GetLongRow(out long a, out long b, out long c) { a = NextLong(); b = NextLong(); c = NextLong(); } public void GetLongRow(out long a, out long b, out long c, out long d) { a = NextLong(); b = NextLong(); c = NextLong(); d = NextLong(); } public void GetDoubleRow(out double a, out double b) { a = NextDouble(); b = NextDouble(); } public void GetDoubleRow(out double a, out double b, out double c) { a = NextDouble(); b = NextDouble(); c = NextDouble(); } public void GetDoubleRow(out double a, out double b, out double c, out double d) { a = NextDouble(); b = NextDouble(); c = NextDouble(); d = NextDouble(); } #endregion #endregion #region get 2 ~4 column elements public void GetColumn(int n, out string[] a) { a = new string[n]; for (int i = 0; i < n; i++) { a[i] = Next(); } } public void GetColumn(int n, out string[] a, out string[] b) { a = new string[n]; b = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i]); } } public void GetColumn(int n, out string[] a, out string[] b, out string[] c) { a = new string[n]; b = new string[n]; c = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i], out c[i]); } } public void GetColumn(int n, out string[] a, out string[] b, out string[] c, out string[] d) { a = new string[n]; b = new string[n]; c = new string[n]; d = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i], out c[i], out d[i]); } } public void GetIntColumn(int n, out int[] a) { string[] sa; GetColumn(n, out sa); a = ToIntArray(sa); } public void GetIntColumn(int n, out int[] a, out int[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToIntArray(sa); b = ToIntArray(sb); } public void GetIntColumn(int n, out int[] a, out int[] b, out int[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToIntArray(sa); b = ToIntArray(sb); c = ToIntArray(sc); } public void GetIntColumn(int n, out int[] a, out int[] b, out int[] c, out int[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToIntArray(sa); b = ToIntArray(sb); c = ToIntArray(sc); d = ToIntArray(sd); } public void GetLongColumn(int n, out long[] a) { string[] sa; GetColumn(n, out sa); a = ToLongArray(sa); } public void GetLongColumn(int n, out long[] a, out long[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToLongArray(sa); b = ToLongArray(sb); } public void GetLongColumn(int n, out long[] a, out long[] b, out long[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToLongArray(sa); b = ToLongArray(sb); c = ToLongArray(sc); } public void GetLongColumn(int n, out long[] a, out long[] b, out long[] c, out long[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToLongArray(sa); b = ToLongArray(sb); c = ToLongArray(sc); d = ToLongArray(sd); } public void GetDoubleColumn(int n, out double[] a) { string[] sa; GetColumn(n, out sa); a = ToDoubleArray(sa); } public void GetDoubleColumn(int n, out double[] a, out double[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToDoubleArray(sa); b = ToDoubleArray(sb); } public void GetDoubleColumn(int n, out double[] a, out double[] b, out double[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToDoubleArray(sa); b = ToDoubleArray(sb); c = ToDoubleArray(sc); } public void GetDoubleColumn(int n, out double[] a, out double[] b, out double[] c, out double[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToDoubleArray(sa); b = ToDoubleArray(sb); c = ToDoubleArray(sc); d = ToDoubleArray(sd); } #endregion #region get matrix string[][] GetMatrix(int h) { string[][] result = new string[h][]; for (int i = 0; i < h; i++) { result[i] = Array(); } return result; } int[][] GetIntMatrix(int h) { int[][] result = new int [h][]; for (int i = 0; i < h; i++) { result[i] = IntArray(); } return result; } long[][] GetLongMatrix(int h) { long[][] result = new long [h][]; for (int i = 0; i < h; i++) { result[i] = LongArray(); } return result; } double[][] GetDoubleMatrix(int h) { double[][] result = new double [h][]; for (int i = 0; i < h; i++) { result[i] = DoubleArray(); } return result; } char[][] GetCharMatrix(int h) { char[][] result = new char [h][]; for (int i = 0; i < h; i++) { result[i] = Next().ToCharArray(); } return result; } #endregion } class Program { public void Solve() { var s = new Scanner(); var N = s.NextInt(); var K = s.NextLong(); var A = s.LongArray(); var dp1 = new long[40]; var dp2 = new long[40]; for (int i = 0; i < 40; i++) { int one = 0; int zero = 0; for (int j = 0; j < N; j++) { if (A[j] % 2 == 0) zero++; else one++; A[j] >>= 1; } var bit = 1L << i; var bdp1 = i > 0 ? dp1[i - 1] : 0; var bdp2 = i > 0 ? dp2[i - 1] : 0; if (K % 2 == 1) { dp1[i] = bdp1 + bit * Math.Max(one, zero); dp2[i] = Math.Max(bdp1 + bit * one, bdp2 + bit * zero); } else { dp1[i] = bdp2 + bit * Math.Max(one, zero); dp2[i] = bdp2 + bit * one; } K >>= 1; } Console.WriteLine(dp2[39]); } static void Main(string[] args) => new Program().Solve(); } }
using System; using System.IO; using System.Linq; namespace Contest { class Scanner { public Scanner() { _stream = new StreamReader(Console.OpenStandardInput()); _pos = 0; _line = new string[0]; _separator = ' '; } private char _separator; private StreamReader _stream; private int _pos; private string[] _line; #region get a element public string Next() { if (_pos >= _line.Length) { _line = _stream.ReadLine().Split(_separator); _pos = 0; } return _line[_pos++]; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } #endregion #region convert array private int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion #region get row elements #region get array public string[] Array() { if (_pos >= _line.Length) _line = _stream.ReadLine().Split(_separator); _pos = _line.Length; return _line; } public int[] IntArray() { return ToIntArray(Array()); } public long[] LongArray() { return ToLongArray(Array()); } public double[] DoubleArray() { return ToDoubleArray(Array()); } #endregion #region get 2 ~4 elements public void GetRow(out string a, out string b) { a = Next(); b = Next(); } public void GetRow(out string a, out string b, out string c) { a = Next(); b = Next(); c = Next(); } public void GetRow(out string a, out string b, out string c, out string d) { a = Next(); b = Next(); c = Next(); d = Next(); } public void GetIntRow(out int a, out int b) { a = NextInt(); b = NextInt(); } public void GetIntRow(out int a, out int b, out int c) { a = NextInt(); b = NextInt(); c = NextInt(); } public void GetIntRow(out int a, out int b, out int c, out int d) { a = NextInt(); b = NextInt(); c = NextInt(); d = NextInt(); } public void GetLongRow(out long a, out long b) { a = NextLong(); b = NextLong(); } public void GetLongRow(out long a, out long b, out long c) { a = NextLong(); b = NextLong(); c = NextLong(); } public void GetLongRow(out long a, out long b, out long c, out long d) { a = NextLong(); b = NextLong(); c = NextLong(); d = NextLong(); } public void GetDoubleRow(out double a, out double b) { a = NextDouble(); b = NextDouble(); } public void GetDoubleRow(out double a, out double b, out double c) { a = NextDouble(); b = NextDouble(); c = NextDouble(); } public void GetDoubleRow(out double a, out double b, out double c, out double d) { a = NextDouble(); b = NextDouble(); c = NextDouble(); d = NextDouble(); } #endregion #endregion #region get 2 ~4 column elements public void GetColumn(int n, out string[] a) { a = new string[n]; for (int i = 0; i < n; i++) { a[i] = Next(); } } public void GetColumn(int n, out string[] a, out string[] b) { a = new string[n]; b = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i]); } } public void GetColumn(int n, out string[] a, out string[] b, out string[] c) { a = new string[n]; b = new string[n]; c = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i], out c[i]); } } public void GetColumn(int n, out string[] a, out string[] b, out string[] c, out string[] d) { a = new string[n]; b = new string[n]; c = new string[n]; d = new string[n]; for (int i = 0; i < n; i++) { GetRow(out a[i], out b[i], out c[i], out d[i]); } } public void GetIntColumn(int n, out int[] a) { string[] sa; GetColumn(n, out sa); a = ToIntArray(sa); } public void GetIntColumn(int n, out int[] a, out int[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToIntArray(sa); b = ToIntArray(sb); } public void GetIntColumn(int n, out int[] a, out int[] b, out int[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToIntArray(sa); b = ToIntArray(sb); c = ToIntArray(sc); } public void GetIntColumn(int n, out int[] a, out int[] b, out int[] c, out int[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToIntArray(sa); b = ToIntArray(sb); c = ToIntArray(sc); d = ToIntArray(sd); } public void GetLongColumn(int n, out long[] a) { string[] sa; GetColumn(n, out sa); a = ToLongArray(sa); } public void GetLongColumn(int n, out long[] a, out long[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToLongArray(sa); b = ToLongArray(sb); } public void GetLongColumn(int n, out long[] a, out long[] b, out long[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToLongArray(sa); b = ToLongArray(sb); c = ToLongArray(sc); } public void GetLongColumn(int n, out long[] a, out long[] b, out long[] c, out long[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToLongArray(sa); b = ToLongArray(sb); c = ToLongArray(sc); d = ToLongArray(sd); } public void GetDoubleColumn(int n, out double[] a) { string[] sa; GetColumn(n, out sa); a = ToDoubleArray(sa); } public void GetDoubleColumn(int n, out double[] a, out double[] b) { string[] sa, sb; GetColumn(n, out sa, out sb); a = ToDoubleArray(sa); b = ToDoubleArray(sb); } public void GetDoubleColumn(int n, out double[] a, out double[] b, out double[] c) { string[] sa, sb, sc; GetColumn(n, out sa, out sb, out sc); a = ToDoubleArray(sa); b = ToDoubleArray(sb); c = ToDoubleArray(sc); } public void GetDoubleColumn(int n, out double[] a, out double[] b, out double[] c, out double[] d) { string[] sa, sb, sc, sd; GetColumn(n, out sa, out sb, out sc, out sd); a = ToDoubleArray(sa); b = ToDoubleArray(sb); c = ToDoubleArray(sc); d = ToDoubleArray(sd); } #endregion #region get matrix string[][] GetMatrix(int h) { string[][] result = new string[h][]; for (int i = 0; i < h; i++) { result[i] = Array(); } return result; } int[][] GetIntMatrix(int h) { int[][] result = new int [h][]; for (int i = 0; i < h; i++) { result[i] = IntArray(); } return result; } long[][] GetLongMatrix(int h) { long[][] result = new long [h][]; for (int i = 0; i < h; i++) { result[i] = LongArray(); } return result; } double[][] GetDoubleMatrix(int h) { double[][] result = new double [h][]; for (int i = 0; i < h; i++) { result[i] = DoubleArray(); } return result; } char[][] GetCharMatrix(int h) { char[][] result = new char [h][]; for (int i = 0; i < h; i++) { result[i] = Next().ToCharArray(); } return result; } #endregion } class Program { public void Solve() { var s = new Scanner(); var N = s.NextInt(); var K = s.NextLong(); var A = s.LongArray(); var dp1 = new long[40]; var dp2 = new long[40]; for (int i = 0; i < 40; i++) { int one = 0; int zero = 0; for (int j = 0; j < N; j++) { if (A[j] % 2 == 0) zero++; else one++; A[j] >>= 1; } var bit = 1L << i; var bdp1 = i > 0 ? dp1[i - 1] : 0; var bdp2 = i > 0 ? dp2[i - 1] : 0; dp1[i] = bdp1 + bit * Math.Max(one, zero); if (K % 2 == 1) { dp2[i] = Math.Max(bdp1 + bit * one, bdp2 + bit * zero); } else { dp2[i] = bdp2 + bit * one; } K >>= 1; } Console.WriteLine(dp2[39]); } static void Main(string[] args) => new Program().Solve(); } }
[["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 109], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25], ["-", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 109], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["-", 75, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["-", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 75, 196, 0, 1, 0, 11, 12, 16, 31, 22], ["-", 75, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 31, 22], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 17, 48], ["-", 12, 16, 12, 16, 12, 213, 63, 214, 205, 22], ["-", 12, 16, 12, 16, 12, 213, 63, 214, 0, 131], ["-", 12, 16, 12, 16, 12, 213, 63, 214, 141, 22], ["-", 12, 16, 12, 16, 12, 213, 3, 4, 0, 24], ["-", 12, 16, 12, 213, 3, 4, 0, 28, 0, 22], ["-", 12, 16, 12, 16, 12, 213, 3, 4, 0, 21], ["-", 12, 16, 12, 16, 12, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 57, 75, 196, 0, 1, 0, 35]]
8
2,524
36
using System; using System.Linq; using System.Collections.Generic; using E = System.Linq.Enumerable; public class Program { public static void Main() { var nk = Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); var n = nk[0]; var k = nk[1]; var ts = Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); if (k == 0) { Console.WriteLine(ts.Sum()); return; } var f = 0L; for (int i = 0; i < 40; i++) { var d = 1L << i; var one = ts.Sum(x => x >> i & 1); if (k >= d && one < n - one) { k -= d; f += (n - one) * d; } else { f += one * d; } } Console.WriteLine(f); } }
using System; using System.Linq; using System.Collections.Generic; using E = System.Linq.Enumerable; public class Program { public static void Main() { var nk = Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); var n = nk[0]; var k = nk[1]; var ts = Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); var f = 0L; for (int i = 39; i >= 0; i--) { var d = 1L << i; var one = ts.Sum(x => x >> i & 1); if (k >= d && one < n - one) { k -= d; f += (n - one) * d; } else { f += one * d; } } Console.WriteLine(f); } }
[["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 60], ["-", 0, 195, 8, 196, 0, 57, 15, 16, 12, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["-", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25], ["-", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["-", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35], ["-", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 20], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 0, 195, 8, 196, 0, 7, 26, 223, 0, 68]]
8
220
29
using System; using System.Collections.Generic; public class Test { public static void Main() { // while(true){ string[] str1 = Console.ReadLine().Split(' '); int n = int.Parse(str1[0]); ulong m = ulong.Parse(str1[1]); // if(n == 0) break; ulong[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, ulong>(s => ulong.Parse(s))); // string ss = Console.ReadLine(); // int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), new //Converter<string,int>(s => int.Parse(s))); int[] c = //Array.ConvertAll(Console.ReadLine().Split(' '), new //Converter<string,int>(s => int.Parse(s))); Console.WriteLine("改行あり N = // {0} P = {1} Ai = {2}", n, p, a); ulong k = 0; ulong f = 0; int keta = (int)Math.Log((double)m, 2.0); if (keta < 0) keta = -1; for (int i = 0; i < keta + 1; i++) { int ka = 0; int kb = 0; for (int j = 0; j < n; j++) { byte bit = (byte)((a[j] >> i) & 1); ka += (bit ^ 0); kb += (bit ^ 1); } ulong tk = k; if (ka < kb) tk += ((ulong)1 << i); if (m < k) break; k = tk; } for (int i = 0; i < n; i++) f += (k ^ a[i]); Console.WriteLine(f); // } } public static int calc(int n, int m, int[] a, int[] b, int[] c) { return 0; } }
using System; using System.Collections.Generic; public class Test { public static void Main() { // while(true){ string[] str1 = Console.ReadLine().Split(' '); int n = int.Parse(str1[0]); ulong m = ulong.Parse(str1[1]); // if(n == 0) break; ulong[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string, ulong>(s => ulong.Parse(s))); // string ss = Console.ReadLine(); // int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), new //Converter<string,int>(s => int.Parse(s))); int[] c = //Array.ConvertAll(Console.ReadLine().Split(' '), new //Converter<string,int>(s => int.Parse(s))); Console.WriteLine("改行あり N = // {0} P = {1} Ai = {2}", n, p, a); ulong k = 0; ulong f = 0; int keta = (int)Math.Log((double)m, 2.0) + 1; if (keta < 0) keta = 0; for (int i = keta; 0 <= i; i--) { int ka = 0; int kb = 0; for (int j = 0; j < n; j++) { byte bit = (byte)((a[j] >> i) & 1); ka += (bit ^ 0); kb += (bit ^ 1); } ulong tk = k; if (ka < kb) tk += ((ulong)1 << i); if (m < tk) continue; k = tk; } for (int i = 0; i < n; i++) f += (k ^ a[i]); Console.WriteLine(f); // } } public static int calc(int n, int m, int[] a, int[] b, int[] c) { return 0; } }
[["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["-", 0, 57, 64, 1, 0, 11, 12, 241, 0, 33], ["-", 0, 57, 64, 1, 0, 11, 12, 241, 0, 203], ["+", 8, 196, 0, 57, 64, 1, 0, 11, 12, 203], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 31, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 31, 22], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 17, 72], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 0, 195, 8, 196, 0, 7, 26, 223, 0, 68], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 22], ["-", 0, 7, 8, 196, 0, 57, 64, 93, 0, 94], ["+", 0, 7, 8, 196, 0, 57, 64, 116, 0, 117]]
8
332
19
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace B { public class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } public static void Solve() { var s = long.Parse(Console.ReadLine()); Func<long, long> f = n => n % 2 == 0 ? n / 2 : 3 * n + 1; var answer = 0L; var i = 0L; var flag = false; while (true) { if (s == 4) { if (flag) { answer = i + 1; break; } else { flag = true; } } s = f(s); i++; } Console.WriteLine(answer); } } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace B { public class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } public static void Solve() { var s = long.Parse(Console.ReadLine()); Func<long, long> f = n => n % 2 == 0 ? n / 2 : 3 * n + 1; var answer = 0L; var i = 0L; var flag = false; var loop = s <= 2 ? s : 4; while (true) { if (s == loop) { if (flag) { answer = i + 1; break; } else { flag = true; } } s = f(s); i++; } Console.WriteLine(answer); } } }
[["+", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 200, 0, 212, 0, 41, 15, 16, 31, 22], ["+", 0, 200, 0, 212, 0, 41, 15, 16, 17, 19], ["+", 0, 200, 0, 212, 0, 41, 15, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 41, 0, 101], ["+", 0, 198, 0, 200, 0, 212, 0, 41, 64, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 41, 0, 102], ["+", 0, 198, 0, 200, 0, 212, 0, 41, 75, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 0, 52, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 12, 22]]
8
197
13
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp61 { class Program { static void Main(string[] args) { int s = int.Parse(Console.ReadLine()); var ansList = new List<int>(); int cnt = 1; while (true) { if (s % 2 == 0) { s /= 2; } else { s = 3 * s + 1; } cnt++; if (ansList.Contains(s)) { Console.WriteLine(cnt); return; } ansList.Add(s); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp61 { class Program { static void Main(string[] args) { int s = int.Parse(Console.ReadLine()); var ansList = new List<int>(); ansList.Add(s); int cnt = 1; while (true) { if (s % 2 == 0) { s /= 2; } else { s = 3 * s + 1; } cnt++; if (ansList.Contains(s)) { Console.WriteLine(cnt); return; } ansList.Add(s); } } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
137
7
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var S = sc.ReadInt(); var map = new HashMap<int, int>(); map[1] = S; var before = S; var ans = 0; for (int i = 2; i <= 1000000; i++) { var x = F(before); if (map[x] == 1) { ans = i; break; } map[x]++; before = x; } Console.WriteLine(ans); } static int F(int n) { return n % 2 == 0 ? n / 2 : 3 * n + 1; ; } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Scanner sc = new Scanner(); } class HashMap<K, V> : Dictionary<K, V> where V : new() { private V DefaltValue = new V(); public HashMap() {} public HashMap(V defaultValue) { this.DefaltValue = new V(); } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = this.DefaltValue; } set { base[i] = value; } } } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } } class Debug { public static void Dump<T>(T[,] array) { #if !DEBUG return; #endif Console.WriteLine(); var h = array.GetLength(0); var w = array.GetLength(1); for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { Console.Write(array[r, c]); Console.Write(" "); } Console.WriteLine(); } Console.WriteLine(); } public static void Dump<T>(T[] array) { #if !DEBUG return; #endif Console.WriteLine(); Console.WriteLine(string.Join(" ", array)); Console.WriteLine(); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var S = sc.ReadInt(); var map = new HashMap<int, int>(); map[S] = 1; var ai = S; var ans = 0; for (int i = 2; i <= 1000000; i++) { ai = F(ai); map[ai]++; if (map[ai] > 1) { ans = i; break; } } Console.WriteLine(ans); } static int F(int n) { return n % 2 == 0 ? n / 2 : 3 * n + 1; ; } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Scanner sc = new Scanner(); } class HashMap<K, V> : Dictionary<K, V> where V : new() { private V DefaltValue = new V(); public HashMap() {} public HashMap(V defaultValue) { this.DefaltValue = new V(); } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = this.DefaltValue; } set { base[i] = value; } } } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } } class Debug { public static void Dump<T>(T[,] array) { #if !DEBUG return; #endif Console.WriteLine(); var h = array.GetLength(0); var w = array.GetLength(1); for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { Console.Write(array[r, c]); Console.Write(" "); } Console.WriteLine(); } Console.WriteLine(); } public static void Dump<T>(T[] array) { #if !DEBUG return; #endif Console.WriteLine(); Console.WriteLine(string.Join(" ", array)); Console.WriteLine(); } }
[["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 7, 8, 196, 0, 1, 0, 11, 31, 22], ["-", 0, 212, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 11, 12, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 223, 0, 204, 205, 22], ["+", 0, 1, 0, 223, 0, 204, 206, 207, 0, 70], ["+", 0, 223, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 223, 0, 204, 206, 207, 0, 73], ["+", 0, 7, 8, 196, 0, 1, 0, 223, 0, 29], ["+", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35], ["-", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["-", 8, 196, 0, 1, 0, 223, 0, 204, 205, 22], ["-", 0, 1, 0, 223, 0, 204, 206, 207, 0, 70], ["-", 0, 223, 0, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 223, 0, 204, 206, 207, 0, 73], ["-", 0, 7, 8, 196, 0, 1, 0, 223, 0, 29], ["-", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35], ["-", 0, 7, 8, 196, 0, 1, 0, 11, 31, 22], ["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 0, 7, 8, 196, 0, 1, 0, 11, 12, 22]]
8
970
31
using System; using System.Linq; using System.Collections.Generic; using System.IO; class Program { public static long Read() { return long.Parse(Console.ReadLine()); } public static long[] Reads() { return Console.ReadLine().Split().Select(long.Parse).ToArray(); } public static void Main() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); var s = Read(); long count = 1; if (s == 1) Console.WriteLine(5); else if (s == 2) Console.WriteLine(4); else { while (s != 4) { count++; if (s % 2 == 0) s = s / 2; else s = 3 * s + 1; } Console.WriteLine(count + 3); } Console.Out.Flush(); } }
using System; using System.Linq; using System.Collections.Generic; using System.IO; class Program { public static long Read() { return long.Parse(Console.ReadLine()); } public static long[] Reads() { return Console.ReadLine().Split().Select(long.Parse).ToArray(); } public static void Main() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); var s = Read(); long count = 1; if (s == 1 || s == 2) Console.WriteLine(4); else { while (s != 4) { count++; if (s % 2 == 0) s = s / 2; else s = 3 * s + 1; } Console.WriteLine(count + 3); } Console.Out.Flush(); } }
[["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["-", 0, 57, 64, 1, 0, 213, 63, 214, 205, 22], ["-", 0, 57, 64, 1, 0, 213, 63, 214, 0, 131], ["-", 0, 57, 64, 1, 0, 213, 63, 214, 141, 22], ["-", 0, 57, 64, 1, 0, 213, 3, 4, 0, 24], ["-", 64, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["-", 0, 57, 64, 1, 0, 213, 3, 4, 0, 25], ["-", 0, 195, 8, 196, 0, 57, 64, 1, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 57, 0, 95], ["-", 0, 195, 8, 196, 0, 57, 75, 57, 0, 121], ["-", 0, 195, 8, 196, 0, 57, 75, 57, 0, 24], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 106]]
8
202
12
using System; using System.Linq; class Procon { public Procon() {} static void Main(string[] agrs) { new Procon().Do(); } Scanner cin; public void Do() { cin = new Scanner(); var s = cin.nextInt(); var isCalced = new bool[1000000]; var a = new int[1000000]; for (int i = 0; i < 1000000; i++) { isCalced[i] = false; } a[0] = s; for (int i = 1; i < 1000000; i++) { a[i] = f(a[i - 1]); if (isCalced[a[i]] == false) { isCalced[a[i]] = true; } else { Console.WriteLine(i + 1); return; } } } int f(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } } class Scanner { string[] s; int i; int max_i; public Scanner() { s = new string[0]; max_i = 0; i = 0; } public string nextString() { if (i >= s.Length) { s = Console.ReadLine().Split(' '); max_i = s.Length; i = 0; if (max_i == 0) { return ""; } return s[i++]; } else { return s[i++]; } } public int nextInt() { return int.Parse(nextString()); } public long nextLong() { return long.Parse(nextString()); } public double nextDouble() { return double.Parse(nextString()); } public string[] arrayString() { return Console.ReadLine().Split(' '); } public int[] arrayInt() { string[] str = Console.ReadLine().Split(' '); int[] array = new int[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = int.Parse(str[i]); } return array; } public long[] arrayLong() { string[] str = Console.ReadLine().Split(' '); long[] array = new long[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = long.Parse(str[i]); } return array; } public double[] arrayDouble() { string[] str = Console.ReadLine().Split(' '); double[] array = new double[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = double.Parse(str[i]); } return array; } }
using System; using System.Linq; class Procon { public Procon() {} static void Main(string[] agrs) { new Procon().Do(); } Scanner cin; public void Do() { cin = new Scanner(); var s = cin.nextInt(); var isCalced = new bool[1000000]; var a = new int[1000000]; for (int i = 0; i < 1000000; i++) { isCalced[i] = false; } a[0] = s; isCalced[a[0]] = true; for (int i = 1; i < 1000000; i++) { a[i] = f(a[i - 1]); if (isCalced[a[i]] == false) { isCalced[a[i]] = true; } else { Console.WriteLine(i + 1); return; } } } int f(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } } class Scanner { string[] s; int i; int max_i; public Scanner() { s = new string[0]; max_i = 0; i = 0; } public string nextString() { if (i >= s.Length) { s = Console.ReadLine().Split(' '); max_i = s.Length; i = 0; if (max_i == 0) { return ""; } return s[i++]; } else { return s[i++]; } } public int nextInt() { return int.Parse(nextString()); } public long nextLong() { return long.Parse(nextString()); } public double nextDouble() { return double.Parse(nextString()); } public string[] arrayString() { return Console.ReadLine().Split(' '); } public int[] arrayInt() { string[] str = Console.ReadLine().Split(' '); int[] array = new int[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = int.Parse(str[i]); } return array; } public long[] arrayLong() { string[] str = Console.ReadLine().Split(' '); long[] array = new long[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = long.Parse(str[i]); } return array; } public double[] arrayDouble() { string[] str = Console.ReadLine().Split(' '); double[] array = new double[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = double.Parse(str[i]); } return array; } }
[["+", 8, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 204, 205, 22], ["+", 206, 207, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["+", 206, 207, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 196, 0, 1, 0, 11, 12, 211, 0, 146], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
624
10
using System; using System.Collections.Generic; class Plogram { static void Main() { var num = int.Parse(Console.ReadLine()); var list = new List<int>(); list.Add(num); for (int i = 0; i < 100; i++) { if (list[i] % 2 == 0) { num = num / 2; } else { num = num * 3 + 1; } if (list.Contains(num)) { break; } else { list.Add(num); } } Console.WriteLine(list.Count + 1); } }
using System; using System.Collections.Generic; class Plogram { static void Main() { var num = int.Parse(Console.ReadLine()); var list = new List<int>(); list.Add(num); while (true) { if (num % 2 == 0) { num = num / 2; } else { num = num * 3 + 1; } if (list.Contains(num)) { break; } else { list.Add(num); } } Console.WriteLine(list.Count + 1); } }
[["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 88], ["+", 8, 201, 0, 195, 8, 196, 0, 52, 0, 89], ["-", 0, 195, 8, 196, 0, 7, 10, 198, 39, 199], ["-", 8, 196, 0, 7, 10, 198, 0, 200, 141, 22], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 35], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 22], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 0, 195, 8, 196, 0, 52, 15, 211, 0, 146], ["-", 0, 57, 15, 16, 31, 16, 31, 204, 205, 22], ["-", 15, 16, 31, 16, 31, 204, 206, 207, 0, 70], ["-", 31, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 15, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22]]
8
133
19
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace At { class Program { static void Main(string[] args) { var count = 0; var s = ReadInt(); while (true) { count++; if (s == 4) { break; } s = f(s); } WriteLine(count + 3); } static int f(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } private static string Read() { return ReadLine(); } private static string[] Reads() { return (Read().Split()); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } //最大公約数 public static int Gcd(int a, int b) { if (a < b) { return Gcd(b, a); } while (b != 0) { var reminder = a % b; a = b; b = reminder; } return a; } //最小公倍数 public static int Lcm(int a, int b) { return a * b / Gcd(a, b); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace At { class Program { static void Main(string[] args) { var count = 0; var s = ReadInt(); while (true) { count++; if (s == 1 || s == 2 || s == 4) { WriteLine(count + 3); break; } s = f(s); } } static int f(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } private static string Read() { return ReadLine(); } private static string[] Reads() { return (Read().Split()); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } //最大公約数 public static int Gcd(int a, int b) { if (a < b) { return Gcd(b, a); } while (b != 0) { var reminder = a % b; a = b; b = reminder; } return a; } //最小公倍数 public static int Lcm(int a, int b) { return a * b / Gcd(a, b); } } }
[["+", 0, 57, 15, 16, 31, 16, 31, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 106], ["+", 0, 57, 15, 16, 31, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 16, 31, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 16, 31, 16, 12, 16, 12, 203], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 106], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 60], ["+", 0, 57, 64, 196, 0, 1, 0, 213, 63, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
384
22
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { public static void Main(string[] args) { int s = int.Parse(Console.ReadLine()); int n = 1; int[] a = new int[1000001]; a[s] = 1; for (int i = 0; i < 1000001; i++) { a[i] = 0; } while (n <= 1000000) { n++; s = f(s); if (a[s] == 1) { break; } a[s] = 1; } Console.WriteLine("{0}", n); } public static int f(int n) { return ((n % 2) == 0) ? n / 2 : 3 * n + 1; } } }
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { public static void Main(string[] args) { int s = int.Parse(Console.ReadLine()); int n = 1; int[] a = new int[1000001]; for (int i = 0; i < 1000001; i++) { a[i] = 0; } a[s] = 1; while (n <= 1000000) { n++; s = f(s); if (a[s] == 1) { break; } a[s] = 1; } Console.WriteLine("{0}", n); } public static int f(int n) { return ((n % 2) == 0) ? n / 2 : 3 * n + 1; } } }
[["-", 8, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
175
14
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput cin) { var S = cin.ReadInt; var ai = new int[1000000]; bool flg = false; for (int i = 1; i < 1000000; i++) { if (i == 1) { ai[i] = S; } else { ai[i] = problemFunction(ai[i - 1]); } if (ai[i] == 4) { if (!flg) { flg = true; continue; } Console.Write(i); return; } } } public int problemFunction(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCoder { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput cin) { var S = cin.ReadInt; var ai = new int[1000000]; for (int i = 1; i < 1000000; i++) { if (i == 1) { ai[i] = S; } else { ai[i] = problemFunction(ai[i - 1]); } for (int j = 0; j < i - 1; j++) { if (ai[j] == ai[i]) { Console.Write(i); return; } } } } public int problemFunction(int n) { if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 198, 0, 200, 0, 212, 0, 211, 0, 147], ["-", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 7, 8, 196, 0, 7, 0, 88], ["-", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["-", 0, 57, 15, 16, 31, 204, 206, 207, 0, 70], ["+", 0, 7, 8, 196, 0, 7, 10, 198, 39, 199], ["+", 8, 196, 0, 7, 10, 198, 0, 200, 141, 22], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 8, 196, 0, 7, 8, 196, 0, 7, 0, 35], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 31, 22], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 57, 15, 16, 31, 204, 206, 207, 0, 73], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 26, 223, 0, 22], ["+", 0, 7, 8, 196, 0, 7, 26, 223, 0, 29], ["-", 0, 57, 64, 196, 0, 57, 15, 241, 0, 111], ["-", 0, 57, 64, 196, 0, 57, 15, 241, 0, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 70], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 8, 196, 0, 57, 15, 16, 12, 204, 205, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 70], ["+", 15, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 73], ["-", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["-", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 64, 196, 0, 1, 0, 11, 12, 211, 0, 146], ["-", 64, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 64, 196, 0, 57, 64, 196, 0, 116, 0, 117], ["-", 64, 196, 0, 57, 64, 196, 0, 116, 0, 35], ["-", 0, 57, 64, 196, 0, 57, 64, 196, 0, 46], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
561
43
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { long A = long.Parse(Console.ReadLine()); int ct = 0; while (true) { ct++; if (A == 1) { ct++; break; } else if (A % 2 == 1) { A = A * 3 + 1; } else { A /= 2; } } Console.WriteLine(ct); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { long A = long.Parse(Console.ReadLine()); int ct = 0; while (true) { ct++; if (A == 1) { ct++; break; } else if (A % 2 == 1) { A = A * 3 + 1; } else { A /= 2; } } Console.WriteLine(Math.Max(4, ct)); } }
[["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["+", 0, 28, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25]]
8
99
7
using System; using System.Collections.Generic; using System.Linq; public class Hello { private const int INF = 1000000007; public static void Main() { int s = Input.Cin<int>(); HashSet<int> hs = new HashSet<int>(); int c = 1; while (true) { int box = (s % 2 == 0 ? s / 2 : 3 * s + 1); if (hs.Contains(box)) break; hs.Add(box); s = box; c++; } Console.WriteLine(c + 1); } } public class Input { private static Queue<string> queue = new Queue<string>(); public static T Cin<T>() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); if (typeof(T) == typeof(int)) return (T)(object) int.Parse(queue.Dequeue()); else if (typeof(T) == typeof(long)) return (T)(object) long.Parse(queue.Dequeue()); else if (typeof(T) == typeof(string)) return (T)(object)(queue.Dequeue()); else if (typeof(T) == typeof(double)) return (T)(object) double.Parse(queue.Dequeue()); else return (T)(object)(0); } public static T Line<T>() { foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); int c = queue.Count(); if (typeof(T) == typeof(int[])) { int[] ret = new int[c]; for (int i = 0; i < c; i++) ret[i] = Cin<int>(); return (T)(object)ret; } else if (typeof(T) == typeof(long[])) { long[] ret = new long[c]; for (int i = 0; i < c; i++) ret[i] = Cin<long>(); return (T)(object)ret; } else if (typeof(T) == typeof(string[])) { string[] ret = new string[c]; for (int i = 0; i < c; i++) ret[i] = Cin<string>(); return (T)(object)ret; } else if (typeof(T) == typeof(double[])) { double[] ret = new double[c]; for (int i = 0; i < c; i++) ret[i] = Cin<double>(); return (T)(object)ret; } else return (T)(object)(0); } }
using System; using System.Collections.Generic; using System.Linq; public class Hello { private const int INF = 1000000007; public static void Main() { int s = Input.Cin<int>(); HashSet<int> hs = new HashSet<int>(); hs.Add(s); while (true) { int box = (s % 2 == 0 ? s / 2 : 3 * s + 1); if (hs.Contains(box)) break; hs.Add(box); s = box; } Console.WriteLine(hs.Count() + 1); } } public class Input { private static Queue<string> queue = new Queue<string>(); public static T Cin<T>() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); if (typeof(T) == typeof(int)) return (T)(object) int.Parse(queue.Dequeue()); else if (typeof(T) == typeof(long)) return (T)(object) long.Parse(queue.Dequeue()); else if (typeof(T) == typeof(string)) return (T)(object)(queue.Dequeue()); else if (typeof(T) == typeof(double)) return (T)(object) double.Parse(queue.Dequeue()); else return (T)(object)(0); } public static T Line<T>() { foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); int c = queue.Count(); if (typeof(T) == typeof(int[])) { int[] ret = new int[c]; for (int i = 0; i < c; i++) ret[i] = Cin<int>(); return (T)(object)ret; } else if (typeof(T) == typeof(long[])) { long[] ret = new long[c]; for (int i = 0; i < c; i++) ret[i] = Cin<long>(); return (T)(object)ret; } else if (typeof(T) == typeof(string[])) { string[] ret = new string[c]; for (int i = 0; i < c; i++) ret[i] = Cin<string>(); return (T)(object)ret; } else if (typeof(T) == typeof(double[])) { double[] ret = new double[c]; for (int i = 0; i < c; i++) ret[i] = Cin<double>(); return (T)(object)ret; } else return (T)(object)(0); } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 0, 52, 8, 196, 0, 1, 0, 223, 0, 22], ["-", 0, 52, 8, 196, 0, 1, 0, 223, 0, 29], ["-", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["+", 0, 28, 0, 16, 31, 213, 63, 214, 205, 22], ["+", 0, 28, 0, 16, 31, 213, 63, 214, 0, 131], ["+", 0, 28, 0, 16, 31, 213, 63, 214, 141, 22], ["+", 0, 28, 0, 16, 31, 213, 3, 4, 0, 24], ["+", 0, 28, 0, 16, 31, 213, 3, 4, 0, 25]]
8
632
19
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace PaizaSkillCheck { public class Program { public static int[] A = new int[1000000]; public static void Main(string[] args) { // string[] line = Console.ReadLine().Split(' '); int s = int.Parse(Console.ReadLine()); A[1] = s; Dictionary<int, bool> dic = new Dictionary<int, bool>(); for (int i = 2; i < A.Length; i++) { int v = f(A[i - 1]); A[i] = v; if (dic.ContainsKey(v)) { Console.WriteLine(i); return; } dic.Add(v, true); } } public static int f(int n) { bool isEven = n % 2 == 0; return isEven ? (n / 2) : (3 * n + 1); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace PaizaSkillCheck { public class Program { public static int[] A = new int[1000000]; public static void Main(string[] args) { // string[] line = Console.ReadLine().Split(' '); int s = int.Parse(Console.ReadLine()); A[1] = s; Dictionary<int, bool> dic = new Dictionary<int, bool>(); dic.Add(s, true); for (int i = 2; i < A.Length; i++) { int v = f(A[i - 1]); A[i] = v; if (dic.ContainsKey(v)) { Console.WriteLine(i); return; } dic.Add(v, true); } } public static int f(int n) { bool isEven = n % 2 == 0; return isEven ? (n / 2) : (3 * n + 1); } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 21], ["+", 0, 213, 3, 4, 0, 28, 0, 211, 0, 146], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
209
9
using System.Collections.Generic; using System; public class Hello { public static void Main() { var s = long.Parse(Console.ReadLine().Trim()); var hs = new HashSet<long>(); var pre = s; var w = 0L; for (int i = 2; i <= 1000000; i++) { if (pre % 2 == 0) { w = pre / 2; if (!hs.Add(w)) { Console.WriteLine(i); break; } } else { w = pre * 3 + 1; if (!hs.Add(w)) { Console.WriteLine(i); break; } } pre = w; } } }
using System.Collections.Generic; using System; public class Hello { public static void Main() { var s = long.Parse(Console.ReadLine().Trim()); var hs = new HashSet<long>(); var pre = s; hs.Add(s); var w = 0L; for (int i = 2; i <= 1000000; i++) { if (pre % 2 == 0) { w = pre / 2; if (!hs.Add(w)) { Console.WriteLine(i); break; } } else { w = pre * 3 + 1; if (!hs.Add(w)) { Console.WriteLine(i); break; } } pre = w; } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
151
7
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Numerics; using System.IO; using System.Runtime.InteropServices; using static System.Math; using static Math2; using static io; using static Utils; public class CompetitiveProgramming { public void Solve() { var N = Int1; var h = Ints; int ans = 0; int i = 0; while (true) { if (i > N - 1) i = 0; if (h.All(x => x == 0)) break; while (i < N && h[i] == 0) ++i; if (i > N - 1) i = 0; ++ans; while (i < N && h[i] != 0) { --h[i]; ++i; } } Write(ans); } } public static class Graph {} public static class Math2 { public const int INF = 1 << 29; public const long INFL = 1L << 60; public const int MOD = 1000000007; public const int MOD2 = 998244353; public static long Pow(long i, long N, long MOD = 1000000007) { long res = 1; while (N > 0) { if ((N & 1) != 0) res = res * i % MOD; i = i * i % MOD; N >>= 1; } return res; } public static long GCD(long i, long N) { if (i > N) Swap(ref i, ref N); if (i == 0) return N; while (N != 0) { var r = i % N; i = N; N = r; } return i; } public static long LCM(long i, long N) => i * N / GCD(i, N); public static long Comb(long N, long R, int MOD = 1000000007) { long ret = 1; long x = 1; for (long i = N; i >= N - R + 1; --i) { ret = ret / x * i; ret %= MOD; x++; } return ret; } public static long Comb2(long N, long R) { long Nume = 1; long Deno = 1; if (R > N - R) Swap(ref N, ref R); for (long i = 1; i <= R; ++i) { Deno *= i; Nume *= N - i + 1; } return Deno / Nume; } public static Dictionary<long, int> PrimeFactorization(long N) { var ret = new Dictionary<long, int>(); for (long i = 2; i * i <= N; ++i) { int cnt = 0; while (N % i == 0) { cnt++; N /= i; } if (cnt != 0) ret[i] = cnt; } if (N >= 2) ret[N] = 1; return ret; } public static List<long> DivisorEnumrate(long N) { var ret = new List<long>(); for (long i = 1; i * i <= N; ++i) { if (N % i == 0) { ret.Add(i); ret.Add(N / i); } } return ret; } } public static class Utils { public static void Swap<T>(ref T A, ref T B) { T x = A; A = B; B = x; } public static int DigitSum(int N) { string s = N.ToString(); int ret = 0; for (int i = 0; i < s.Length; ++i) ret += s[i] - '0'; return ret; } } class Program { static void Main(string[] args) { var CompetitiveProgramming = new CompetitiveProgramming(); CompetitiveProgramming.Solve(); } } public static class io { public static string Str => Console.ReadLine(); public static string[] Strs => Str.Split(' '); public static long[] Longs => Strs.Select(long.Parse).ToArray(); public static int[] Ints => Strs.Select(int.Parse).ToArray(); public static char[] Chars => Str.ToArray(); public static double[] Doubles => Strs.Select(double.Parse).ToArray(); public static long Long1 => Longs[0]; public static int Int1 => Ints[0]; public static char Char1 => Chars[0]; public static double Double1 => Doubles[0]; public static long[] HorizontalRead(int N) { long[] A = new long[N]; for (int i = 0; i < N; ++i) A[i] = Long1; return A; } public static void Write(string a) => Console.WriteLine(a); public static void Write(params object[] i) => Write(string.Join(" ", i)); public static void Write<T>(IEnumerable<T> a) => Write(string.Join(" ", a)); public static void Horizontalwrite<T>(IEnumerable<T> a) { foreach (var z in a) Write(z); } public static void YN(bool i) { if (i) Write("Yes"); else Write("No"); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Numerics; using System.IO; using System.Runtime.InteropServices; using static System.Math; using static Math2; using static io; using static Utils; public class CompetitiveProgramming { public void Solve() { var N = Int1; var h = Ints; int ans = 0; int i = 0; while (true) { if (h.All(x => x == 0)) break; if (i > N - 1) i = 0; if (h.All(x => x == 0)) break; while (i < N && h[i] == 0) ++i; if (i > N - 1) continue; ++ans; while (i < N && h[i] > 0) { --h[i]; ++i; } } Write(ans); } } public static class Graph {} public static class Math2 { public const int INF = 1 << 29; public const long INFL = 1L << 60; public const int MOD = 1000000007; public const int MOD2 = 998244353; public static long Pow(long i, long N, long MOD = 1000000007) { long res = 1; while (N > 0) { if ((N & 1) != 0) res = res * i % MOD; i = i * i % MOD; N >>= 1; } return res; } public static long GCD(long i, long N) { if (i > N) Swap(ref i, ref N); if (i == 0) return N; while (N != 0) { var r = i % N; i = N; N = r; } return i; } public static long LCM(long i, long N) => i * N / GCD(i, N); public static long Comb(long N, long R, int MOD = 1000000007) { long ret = 1; long x = 1; for (long i = N; i >= N - R + 1; --i) { ret = ret / x * i; ret %= MOD; x++; } return ret; } public static long Comb2(long N, long R) { long Nume = 1; long Deno = 1; if (R > N - R) Swap(ref N, ref R); for (long i = 1; i <= R; ++i) { Deno *= i; Nume *= N - i + 1; } return Deno / Nume; } public static Dictionary<long, int> PrimeFactorization(long N) { var ret = new Dictionary<long, int>(); for (long i = 2; i * i <= N; ++i) { int cnt = 0; while (N % i == 0) { cnt++; N /= i; } if (cnt != 0) ret[i] = cnt; } if (N >= 2) ret[N] = 1; return ret; } public static List<long> DivisorEnumrate(long N) { var ret = new List<long>(); for (long i = 1; i * i <= N; ++i) { if (N % i == 0) { ret.Add(i); ret.Add(N / i); } } return ret; } } public static class Utils { public static void Swap<T>(ref T A, ref T B) { T x = A; A = B; B = x; } public static int DigitSum(int N) { string s = N.ToString(); int ret = 0; for (int i = 0; i < s.Length; ++i) ret += s[i] - '0'; return ret; } } class Program { static void Main(string[] args) { var CompetitiveProgramming = new CompetitiveProgramming(); CompetitiveProgramming.Solve(); } } public static class io { public static string Str => Console.ReadLine(); public static string[] Strs => Str.Split(' '); public static long[] Longs => Strs.Select(long.Parse).ToArray(); public static int[] Ints => Strs.Select(int.Parse).ToArray(); public static char[] Chars => Str.ToArray(); public static double[] Doubles => Strs.Select(double.Parse).ToArray(); public static long Long1 => Longs[0]; public static int Int1 => Ints[0]; public static char Char1 => Chars[0]; public static double Double1 => Doubles[0]; public static long[] HorizontalRead(int N) { long[] A = new long[N]; for (int i = 0; i < N; ++i) A[i] = Long1; return A; } public static void Write(string a) => Console.WriteLine(a); public static void Write(params object[] i) => Write(string.Join(" ", i)); public static void Write<T>(IEnumerable<T> a) => Write(string.Join(" ", a)); public static void Horizontalwrite<T>(IEnumerable<T> a) { foreach (var z in a) Write(z); } public static void YN(bool i) { if (i) Write("Yes"); else Write("No"); } }
[["+", 8, 196, 0, 57, 15, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 57, 15, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 57, 15, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 57, 15, 213, 3, 4, 0, 24], ["+", 0, 28, 0, 218, 54, 219, 0, 220, 141, 22], ["+", 15, 213, 3, 4, 0, 28, 0, 218, 0, 221], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 31, 22], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 17, 60], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 25], ["+", 0, 52, 8, 196, 0, 57, 64, 93, 0, 94], ["+", 0, 52, 8, 196, 0, 57, 64, 93, 0, 35], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 24], ["-", 8, 196, 0, 57, 64, 1, 0, 11, 31, 22], ["-", 0, 57, 64, 1, 0, 11, 0, 202, 0, 32], ["-", 8, 196, 0, 57, 64, 1, 0, 11, 12, 203], ["+", 0, 52, 8, 196, 0, 57, 64, 116, 0, 117], ["-", 8, 196, 0, 52, 15, 16, 12, 16, 17, 79], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 17, 47]]
8
1,146
21
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int ans = n; while (true) { ++ans; int count = 0; for (int i = 0; i < n; i++) { if (a[i] > 0) { a[i]--; ++count; } else { if (count < 0) { break; } } } if (count == 0) { break; } } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int ans = 0; while (true) { int count = 0; for (int i = 0; i < n; i++) { if (a[i] > 0) { a[i]--; ++count; } else { if (count > 0) { break; } } } if (count == 0) { break; } ++ans; } Console.WriteLine(ans); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 52, 8, 196, 0, 1, 0, 241, 0, 29], ["-", 0, 52, 8, 196, 0, 1, 0, 241, 0, 22], ["-", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35], ["-", 0, 57, 75, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 52, 8, 196, 0, 1, 0, 241, 0, 29], ["+", 0, 52, 8, 196, 0, 1, 0, 241, 0, 22], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
8
175
10
using System; using System.Linq; namespace CSharp { class Program { static void Main(string[] args) { var N = Console.ReadLine().Split().Select(int.Parse).ToArray()[0]; var H = Console.ReadLine().Split().Select(int.Parse).ToArray(); // var N = 4; // var H = new int[] { 1, 2, 2, 1 }; // 1 2 2 1 var res = 0; while (true) { if (H.Max() <= 0) break; var i = 0; res++; while (i < N) { if (H[i] == 0) i++; while (i < N) { H[i]--; i++; } } } Console.WriteLine(res); } } } /* 4 1 2 2 1 */
using System; using System.Linq; namespace CSharp { class Program { static void Main(string[] args) { // var N = 5; // var H = new int[] { 3, 1, 2, 3, 1 }; // // 5 // // 3 1 2 3 1 // // 1 2 2 1 var N = Console.ReadLine().Split().Select(int.Parse).ToArray()[0]; var H = Console.ReadLine().Split().Select(int.Parse).ToArray(); var res = 0; while (true) { if (H.Max() <= 0) break; var i = 0; res++; while (i < N) { if (H[i] == 0) { i++; } else { break; } } while (i < N && H[i] != 0) { H[i]--; i++; } } Console.WriteLine(res); } } }
[["+", 0, 52, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 52, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 95], ["+", 0, 52, 8, 196, 0, 57, 75, 196, 0, 45], ["+", 8, 196, 0, 57, 75, 196, 0, 93, 0, 94], ["+", 0, 52, 8, 196, 0, 57, 75, 196, 0, 46], ["+", 0, 52, 8, 196, 0, 52, 8, 196, 0, 46], ["+", 0, 52, 8, 196, 0, 52, 15, 16, 17, 98], ["+", 0, 52, 15, 16, 12, 16, 31, 204, 205, 22], ["+", 15, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 12, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 15, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 17, 79], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 12, 203], ["-", 0, 52, 8, 196, 0, 52, 8, 196, 0, 46]]
8
157
16
using System; using System.Linq; namespace train { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); string strFlowers = Console.ReadLine(); string[] flower = strFlowers.Split(' '); int[] flowers = flower.Select(f => int.Parse(f)).ToArray(); int water = 0; do { int min = -1; int max = -1; int lastIndex = flowers.Length - 1; for (int n = 0; n < flowers.Length; n++) { if (min == -1) { if (flowers[n] > 0) { min = n; if (n == lastIndex) max = n; } } else { if (flowers[n] == 0) max = n - 1; else if (n == lastIndex) max = n; } if (min >= 0 && max >= min) break; } if (min >= 0 && max >= 0) for (int n = min; n <= max; n++) flowers[n]--; water++; } while (flowers.Sum() > 0); Console.WriteLine(water); } } }
using System; using System.Linq; namespace train { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); string strFlowers = Console.ReadLine(); string[] flower = strFlowers.Split(' '); int[] flowers = flower.Select(f => int.Parse(f)).ToArray(); int water = 0; while (flowers.Sum() > 0) { int min = -1; int max = -1; int lastIndex = flowers.Length - 1; for (int n = 0; n < flowers.Length; n++) { if (min == -1) { if (flowers[n] > 0) { min = n; if (n == lastIndex) max = n; } } else { if (flowers[n] == 0) max = n - 1; else if (n == lastIndex) max = n; } if (min >= 0 && max >= min) break; } if (min >= 0 && max >= 0) { water++; for (int n = min; n <= max; n++) flowers[n]--; } } Console.WriteLine(water); } } }
[["-", 8, 201, 0, 195, 8, 196, 0, 82, 0, 170], ["+", 8, 201, 0, 195, 8, 196, 0, 52, 0, 89], ["+", 8, 201, 0, 195, 8, 196, 0, 52, 0, 24], ["+", 0, 52, 15, 16, 31, 213, 63, 214, 205, 22], ["+", 0, 52, 15, 16, 31, 213, 63, 214, 0, 131], ["+", 0, 52, 15, 16, 31, 213, 63, 214, 141, 22], ["+", 0, 52, 15, 16, 31, 213, 3, 4, 0, 24], ["+", 0, 52, 15, 16, 31, 213, 3, 4, 0, 25], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 17, 47], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 52, 0, 25], ["+", 0, 52, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 223, 0, 29], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["-", 8, 196, 0, 57, 64, 7, 8, 1, 0, 35], ["-", 0, 82, 8, 196, 0, 1, 0, 223, 0, 22], ["-", 0, 82, 8, 196, 0, 1, 0, 223, 0, 29], ["-", 8, 201, 0, 195, 8, 196, 0, 82, 0, 89], ["-", 8, 201, 0, 195, 8, 196, 0, 82, 0, 24], ["-", 0, 82, 15, 16, 31, 213, 63, 214, 205, 22], ["-", 0, 82, 15, 16, 31, 213, 63, 214, 0, 131], ["-", 0, 82, 15, 16, 31, 213, 63, 214, 141, 22], ["-", 0, 82, 15, 16, 31, 213, 3, 4, 0, 24], ["-", 0, 82, 15, 16, 31, 213, 3, 4, 0, 25], ["-", 0, 195, 8, 196, 0, 82, 15, 16, 17, 47], ["-", 0, 195, 8, 196, 0, 82, 15, 16, 12, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 82, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 82, 0, 35], ["+", 0, 195, 8, 196, 0, 52, 8, 196, 0, 46]]
8
260
30
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { Console.ReadLine(); int[] input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); bool isUP = true; int before = 0; int result = 0; foreach (int n in input) { if (n == before) { continue; } bool difIsUP = before < n; if (isUP & difIsUP == false) { result += before; } else if (!isUP && difIsUP == true) { result -= before; } before = n; isUP = difIsUP; } Console.WriteLine(result); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { Console.ReadLine(); int[] input = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); bool isUP = true; int before = 0; int result = 0; foreach (int n in input) { if (n == before) { continue; } bool difIsUP = before < n; if (isUP & difIsUP == false) { result += before; } else if (!isUP && difIsUP == true) { result -= before; } before = n; isUP = difIsUP; } result += isUP ? before : 0; Console.WriteLine(result); } }
[["+", 0, 195, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 107], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 15, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 0, 101], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 64, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 0, 102], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 75, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
168
8
using System; using System.Linq; namespace ABC116_C { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split().Select(int.Parse).ToArray(); long cnt = 0; var min = 0; for (int i = 0; i < N - 1; i++) { if (h[i] <= h[i + 1]) continue; cnt += h[i] - min; min = h[i + 1]; } if (h[N - 2] <= h[N - 1]) { cnt += h[N - 1] - min; } Console.WriteLine(cnt); } } }
using System; using System.Linq; namespace ABC116_C { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split().Select(int.Parse).ToArray(); if (h.Length == 1) { Console.WriteLine(h[0]); return; } long cnt = 0; var min = 0; for (int i = 0; i < N - 1; i++) { if (h[i] <= h[i + 1]) continue; cnt += h[i] - min; min = h[i + 1]; } if (h[N - 2] <= h[N - 1]) { cnt += h[N - 1] - min; } Console.WriteLine(cnt); } } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 31, 214, 205, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 214, 0, 131], ["+", 8, 196, 0, 57, 15, 16, 31, 214, 141, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46]]
8
163
22
using Yorukatsu051.Questions; using Yorukatsu051.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu051.Questions { /// <summary> /// https://atcoder.jp/contests/abc116/tasks/abc116_d /// </summary> public class QuestionF : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var nk = inputStream.ReadIntArray(); var sushiCount = nk[0]; var toEatCount = nk[1]; var sushis = new PriorityQueue<Sushi>(true); for (int i = 0; i < sushiCount; i++) { var td = inputStream.ReadIntArray(); sushis.Enqueue(new Sushi(td[0], td[1])); } var bestSushis = new Dictionary<int, Sushi>(); var duplicated = new PriorityQueue<Sushi>(false); long maxPoint = 0; for (int i = 0; i < toEatCount; i++) { var sushi = sushis.Dequeue(); maxPoint += sushi.Deliciousness; if (bestSushis.ContainsKey(sushi.Type)) { if (bestSushis[sushi.Type].CompareTo(sushi) >= 0) { duplicated.Enqueue(sushi); } else { duplicated.Enqueue(bestSushis[sushi.Type]); bestSushis[sushi.Type] = sushi; } } else { bestSushis[sushi.Type] = sushi; } } maxPoint += (long)bestSushis.Count * bestSushis.Count; var point = maxPoint; while (sushis.Count > 0) { var sushi = sushis.Dequeue(); if (bestSushis.ContainsKey(sushi.Type)) { continue; } point += sushi.Deliciousness; point -= duplicated.Dequeue().Deliciousness; var variety = bestSushis.Count; point += (variety + 1L) * (variety + 1L) - (long)variety * variety; bestSushis[sushi.Type] = sushi; if (point > maxPoint) { maxPoint = point; } } yield return maxPoint; } struct Sushi : IComparable<Sushi> { public int Type { get; } public long Deliciousness { get; } public Sushi(int type, long deliciousness) { Type = type; Deliciousness = deliciousness; } public int CompareTo(Sushi other) => Deliciousness.CompareTo(other.Deliciousness); } public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T> { private List<T> _heap = new List<T>(); private readonly int _reverseFactor; public int Count => _heap.Count; public bool IsDescending => _reverseFactor == 1; public PriorityQueue(bool descending) : this(descending, null) {} public PriorityQueue(bool descending, IEnumerable<T> collection) { _reverseFactor = descending ? 1 : -1; _heap = new List<T>(); if (collection != null) { foreach (var item in collection) { Enqueue(item); } } } public void Enqueue(T item) { _heap.Add(item); UpHeap(); } public T Dequeue() { var item = _heap[0]; DownHeap(); return item; } public T Peek() => _heap[0]; private void UpHeap() { var child = Count - 1; while (child > 0) { int parent = (child - 1) / 2; if (Compare(_heap[child], _heap[parent]) > 0) { SwapAt(child, parent); child = parent; } else { break; } } } private void DownHeap() { _heap[0] = _heap[Count - 1]; _heap.RemoveAt(Count - 1); var parent = 0; while (true) { var leftChild = 2 * parent + 1; if (leftChild > Count - 1) { break; } var target = (leftChild < Count - 1) && (Compare(_heap[leftChild], _heap[leftChild + 1]) < 0) ? leftChild + 1 : leftChild; if (Compare(_heap[parent], _heap[target]) < 0) { SwapAt(parent, target); } else { break; } parent = target; } } private int Compare(T a, T b) => _reverseFactor * a.CompareTo(b); private void SwapAt(int n, int m) { var temp = _heap[n]; _heap[n] = _heap[m]; _heap[m] = temp; } public IEnumerator<T> GetEnumerator() { var copy = new List<T>(_heap); try { while (Count > 0) { yield return Dequeue(); } } finally { _heap = copy; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); } } } namespace Yorukatsu051 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionF(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu051.Questions { public interface IAtCoderQuestion { IEnumerable<object> Solve(string input); IEnumerable<object> Solve(TextReader inputStream); } public abstract class AtCoderQuestionBase : IAtCoderQuestion { public IEnumerable<object> Solve(string input) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(input)); var reader = new StreamReader(stream, Encoding.Unicode); return Solve(reader); } public abstract IEnumerable<object> Solve(TextReader inputStream); } } #endregion #region Extensions namespace Yorukatsu051.Extensions { internal static class TextReaderExtensions { internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader)); internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader)); internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader)); internal static string ReadString(this TextReader reader) => reader.ReadLine(); internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray(); internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray(); internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray(); internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator); } } #endregion
using Yorukatsu051.Questions; using Yorukatsu051.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu051.Questions { /// <summary> /// https://atcoder.jp/contests/abc116/tasks/abc116_d /// </summary> public class QuestionF : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var nk = inputStream.ReadIntArray(); var sushiCount = nk[0]; var toEatCount = nk[1]; var sushis = new PriorityQueue<Sushi>(true); for (int i = 0; i < sushiCount; i++) { var td = inputStream.ReadIntArray(); sushis.Enqueue(new Sushi(td[0], td[1])); } var bestSushis = new Dictionary<int, Sushi>(); var duplicated = new PriorityQueue<Sushi>(false); long maxPoint = 0; for (int i = 0; i < toEatCount; i++) { var sushi = sushis.Dequeue(); maxPoint += sushi.Deliciousness; if (bestSushis.ContainsKey(sushi.Type)) { if (bestSushis[sushi.Type].CompareTo(sushi) >= 0) { duplicated.Enqueue(sushi); } else { duplicated.Enqueue(bestSushis[sushi.Type]); bestSushis[sushi.Type] = sushi; } } else { bestSushis[sushi.Type] = sushi; } } maxPoint += (long)bestSushis.Count * bestSushis.Count; var point = maxPoint; while (sushis.Count > 0 && duplicated.Count > 0) { var sushi = sushis.Dequeue(); if (bestSushis.ContainsKey(sushi.Type)) { continue; } point += sushi.Deliciousness; point -= duplicated.Dequeue().Deliciousness; long variety = bestSushis.Count; point += (variety + 1) * (variety + 1) - variety * variety; bestSushis[sushi.Type] = sushi; if (point > maxPoint) { maxPoint = point; } } yield return maxPoint; } struct Sushi : IComparable<Sushi> { public int Type { get; } public long Deliciousness { get; } public Sushi(int type, long deliciousness) { Type = type; Deliciousness = deliciousness; } public int CompareTo(Sushi other) => Deliciousness.CompareTo(other.Deliciousness); } public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T> { private List<T> _heap = new List<T>(); private readonly int _reverseFactor; public int Count => _heap.Count; public bool IsDescending => _reverseFactor == 1; public PriorityQueue(bool descending) : this(descending, null) {} public PriorityQueue(bool descending, IEnumerable<T> collection) { _reverseFactor = descending ? 1 : -1; _heap = new List<T>(); if (collection != null) { foreach (var item in collection) { Enqueue(item); } } } public void Enqueue(T item) { _heap.Add(item); UpHeap(); } public T Dequeue() { var item = _heap[0]; DownHeap(); return item; } public T Peek() => _heap[0]; private void UpHeap() { var child = Count - 1; while (child > 0) { int parent = (child - 1) / 2; if (Compare(_heap[child], _heap[parent]) > 0) { SwapAt(child, parent); child = parent; } else { break; } } } private void DownHeap() { _heap[0] = _heap[Count - 1]; _heap.RemoveAt(Count - 1); var parent = 0; while (true) { var leftChild = 2 * parent + 1; if (leftChild > Count - 1) { break; } var target = (leftChild < Count - 1) && (Compare(_heap[leftChild], _heap[leftChild + 1]) < 0) ? leftChild + 1 : leftChild; if (Compare(_heap[parent], _heap[target]) < 0) { SwapAt(parent, target); } else { break; } parent = target; } } private int Compare(T a, T b) => _reverseFactor * a.CompareTo(b); private void SwapAt(int n, int m) { var temp = _heap[n]; _heap[n] = _heap[m]; _heap[m] = temp; } public IEnumerator<T> GetEnumerator() { var copy = new List<T>(_heap); try { while (Count > 0) { yield return Dequeue(); } } finally { _heap = copy; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); } } } namespace Yorukatsu051 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionF(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu051.Questions { public interface IAtCoderQuestion { IEnumerable<object> Solve(string input); IEnumerable<object> Solve(TextReader inputStream); } public abstract class AtCoderQuestionBase : IAtCoderQuestion { public IEnumerable<object> Solve(string input) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(input)); var reader = new StreamReader(stream, Encoding.Unicode); return Solve(reader); } public abstract IEnumerable<object> Solve(TextReader inputStream); } } #endregion #region Extensions namespace Yorukatsu051.Extensions { internal static class TextReaderExtensions { internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader)); internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader)); internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader)); internal static string ReadString(this TextReader reader) => reader.ReadLine(); internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray(); internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray(); internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray(); internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator); } } #endregion
[["+", 0, 195, 8, 196, 0, 52, 15, 16, 17, 98], ["+", 0, 52, 15, 16, 12, 16, 31, 214, 205, 22], ["+", 0, 52, 15, 16, 12, 16, 31, 214, 0, 131], ["+", 0, 52, 15, 16, 12, 16, 31, 214, 141, 22], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 17, 47], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 12, 203], ["-", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 0, 52, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 12, 16, 31, 16, 31, 23, 0, 16, 12, 203], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 203], ["-", 12, 16, 31, 16, 12, 23, 0, 16, 12, 203], ["+", 12, 16, 31, 16, 12, 23, 0, 16, 12, 203], ["-", 0, 11, 12, 16, 12, 16, 31, 74, 0, 24], ["-", 0, 11, 12, 16, 12, 16, 31, 74, 39, 199], ["-", 0, 11, 12, 16, 12, 16, 31, 74, 0, 25]]
8
1,353
15
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using static ABC116D.Input; namespace ABC116D { static class Input { /// <summary> /// 1行の入力をTに応じてリストに変換する関数を返す /// </summary> static Func<string, List<T>> Cast<T>() => _ => _.Split(' ').Select(Convert<T>()).ToList(); /// <summary> /// 1行の入力をTに応じて変換する関数を返す /// </summary> static Func<string, T> Convert<T>() { Type t = typeof(T); if (t == typeof(string)) return _ => (T)(object)_; if (t == typeof(int)) return _ => (T)(object) int.Parse(_); if (t == typeof(long)) return _ => (T)(object) long.Parse(_); if (t == typeof(double)) return _ => (T)(object) double.Parse(_); if (t == typeof(List<string>)) return _ => (T)(object)Cast<string>()(_); if (t == typeof(List<int>)) return _ => (T)(object)Cast<int>()(_); if (t == typeof(List<long>)) return _ => (T)(object)Cast<long>()(_); if (t == typeof(List<double>)) return _ => (T)(object)Cast<double>()(_); throw new NotSupportedException(t + "is not supported."); } static string String() => Console.ReadLine(); static List<string> String(long rowNumber) => new bool [rowNumber].Select(_ => String()).ToList(); /// <summary> /// 1行の値の入力 /// </summary> public static void @in<T>(out T a) => a = Convert<List<T>>()(String())[0]; public static void @in<T>(out T a1, out T a2) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; } public static void @in<T>(out T a1, out T a2, out T a3) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4, out T a5) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; a5 = v[4]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4, out T a5, out T a6) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; a5 = v[4]; a6 = v[5]; } /// <summary> /// 複数行の値の入力 /// </summary> public static void @in<T>(long rowNumber, out List<T> l) => l = String(rowNumber).Select(Convert<T>()).ToList(); public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2) { l1 = new List<T>(); l2 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); } } public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2, out List<T> l3) { l1 = new List<T>(); l2 = new List<T>(); l3 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); l3.Add(l[2]); } } public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2, out List<T> l3, out List<T> l4) { l1 = new List<T>(); l2 = new List<T>(); l3 = new List<T>(); l4 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); l3.Add(l[2]); l4.Add(l[3]); } } /// <summary> /// 1行に書かれた複数の値の入力 /// </summary> public static void @in<T>(out List<T> lArr) => lArr = Convert<List<T>>()(String()); /// <summary> /// h行の行列の入力 /// </summary> public static void @in<T>(long h, out List<List<T>> map) => map = String(h).Select(Convert<List<T>>()).ToList(); } class Program { public static void Main(string[] args) { StreamWriter streamWriter = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(streamWriter); new Solver().Solve(); Console.Out.Flush(); } public static void Debug() { new Solver().Solve(); } } class Solver { private long N; private long K; private List<long> t; private List<long> d; public void Solve() { @in(out N, out K); @in(N, out t, out d); Pair[] sushi = new Pair[N]; for (int i = 0; i < N; i++) { sushi[i] = new Pair(t[i], d[i], i); } Array.Sort(sushi, new PairComperator()); //現在のもの var s = new PriorityQueue<Pair>(new PairComperator()); //残り var k = new PriorityQueue<Pair>(new PairComperator(), false); long sum = 0; Dictionary<long, int> count = new Dictionary<long, int>(); for (int i = 0; i < K; i++) { sum += sushi[i].B; if (!count.ContainsKey(sushi[i].A)) count.Add(sushi[i].A, 0); count[sushi[i].A]++; s.Enqueue(sushi[i]); } long kinds = count.Count; for (long i = K; i < N; i++) { k.Enqueue(sushi[i]); if (!count.ContainsKey(sushi[i].A)) count.Add(sushi[i].A, 0); } long ans = sum + kinds * kinds; while (k.Count > 0 && s.Count > 0) { // 2個以上持ってるのを外す while (s.Count > 0) { var o = s.Dequeue(); if (count[o.A] > 1) { count[o.A]--; sum -= o.B; break; } } //持ってないのを入れる while (k.Count > 0) { var c = k.Dequeue(); if (count[c.A] > 0) continue; count[c.A]++; sum += c.B; kinds++; break; } ans = Math.Max(ans, sum + kinds * kinds); } Console.WriteLine(ans); } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> data = new List<T>(); private readonly IComparer<T> comparer; private readonly bool isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { this.comparer = comparer; this.isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { data.Add(item); var childIndex = data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(data[childIndex], data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { if (Count < 1) { throw new Exception("キューに値が入っていません"); } var lastIndex = data.Count - 1; var firstItem = data[0]; data[0] = data[lastIndex]; data.RemoveAt(lastIndex); lastIndex--; var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) { break; } var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(data[rightChild], data[childIndex]) < 0) { childIndex = rightChild; } if (Compare(data[parentIndex], data[childIndex]) <= 0) { break; } Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return data[0]; } private void Swap(int a, int b) { var tmp = data[a]; data[a] = data[b]; data[b] = tmp; } private int Compare(T a, T b) { return isDescending ? comparer.Compare(b, a) : comparer.Compare(a, b); } public int Count => data.Count; public IEnumerator<T> GetEnumerator() { return data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } /// <summary> /// 値のペア ソート/出力用のindexも持つ /// </summary> class Pair { public long A; public long B; public long I; public Pair(long a, long b, long i) { A = a; B = b; I = i; } public override string ToString() { return "i: " + I + " A: " + A + " B: " + B; } } class PairComperator : IComparer<Pair> { int IComparer<Pair>.Compare(Pair a, Pair b) { return (int)b.B - (int)a.B; } } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using static ABC116D.Input; namespace ABC116D { static class Input { /// <summary> /// 1行の入力をTに応じてリストに変換する関数を返す /// </summary> static Func<string, List<T>> Cast<T>() => _ => _.Split(' ').Select(Convert<T>()).ToList(); /// <summary> /// 1行の入力をTに応じて変換する関数を返す /// </summary> static Func<string, T> Convert<T>() { Type t = typeof(T); if (t == typeof(string)) return _ => (T)(object)_; if (t == typeof(int)) return _ => (T)(object) int.Parse(_); if (t == typeof(long)) return _ => (T)(object) long.Parse(_); if (t == typeof(double)) return _ => (T)(object) double.Parse(_); if (t == typeof(List<string>)) return _ => (T)(object)Cast<string>()(_); if (t == typeof(List<int>)) return _ => (T)(object)Cast<int>()(_); if (t == typeof(List<long>)) return _ => (T)(object)Cast<long>()(_); if (t == typeof(List<double>)) return _ => (T)(object)Cast<double>()(_); throw new NotSupportedException(t + "is not supported."); } static string String() => Console.ReadLine(); static List<string> String(long rowNumber) => new bool [rowNumber].Select(_ => String()).ToList(); /// <summary> /// 1行の値の入力 /// </summary> public static void @in<T>(out T a) => a = Convert<List<T>>()(String())[0]; public static void @in<T>(out T a1, out T a2) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; } public static void @in<T>(out T a1, out T a2, out T a3) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4, out T a5) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; a5 = v[4]; } public static void @in<T>(out T a1, out T a2, out T a3, out T a4, out T a5, out T a6) { var v = Convert<List<T>>()(String()); a1 = v[0]; a2 = v[1]; a3 = v[2]; a4 = v[3]; a5 = v[4]; a6 = v[5]; } /// <summary> /// 複数行の値の入力 /// </summary> public static void @in<T>(long rowNumber, out List<T> l) => l = String(rowNumber).Select(Convert<T>()).ToList(); public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2) { l1 = new List<T>(); l2 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); } } public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2, out List<T> l3) { l1 = new List<T>(); l2 = new List<T>(); l3 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); l3.Add(l[2]); } } public static void @in<T>(long rowNumber, out List<T> l1, out List<T> l2, out List<T> l3, out List<T> l4) { l1 = new List<T>(); l2 = new List<T>(); l3 = new List<T>(); l4 = new List<T>(); foreach (List<T> l in String(rowNumber).Select(Convert<List<T>>())) { l1.Add(l[0]); l2.Add(l[1]); l3.Add(l[2]); l4.Add(l[3]); } } /// <summary> /// 1行に書かれた複数の値の入力 /// </summary> public static void @in<T>(out List<T> lArr) => lArr = Convert<List<T>>()(String()); /// <summary> /// h行の行列の入力 /// </summary> public static void @in<T>(long h, out List<List<T>> map) => map = String(h).Select(Convert<List<T>>()).ToList(); } class Program { public static void Main(string[] args) { StreamWriter streamWriter = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(streamWriter); new Solver().Solve(); Console.Out.Flush(); } public static void Debug() { new Solver().Solve(); } } class Solver { private long N; private long K; private List<long> t; private List<long> d; public void Solve() { @in(out N, out K); @in(N, out t, out d); Pair[] sushi = new Pair[N]; for (int i = 0; i < N; i++) { sushi[i] = new Pair(t[i], d[i], i); } Array.Sort(sushi, new PairComperator()); //現在のもの var s = new PriorityQueue<Pair>(new PairComperator()); //残り var k = new PriorityQueue<Pair>(new PairComperator(), false); long sum = 0; Dictionary<long, int> count = new Dictionary<long, int>(); for (int i = 0; i < K; i++) { sum += sushi[i].B; if (!count.ContainsKey(sushi[i].A)) count.Add(sushi[i].A, 0); count[sushi[i].A]++; s.Enqueue(sushi[i]); } long kinds = count.Count; for (long i = K; i < N; i++) { k.Enqueue(sushi[i]); if (!count.ContainsKey(sushi[i].A)) count.Add(sushi[i].A, 0); } long ans = sum + kinds * kinds; while (k.Count > 0 && s.Count > 0) { // 2個以上持ってるのを外す var couldnt = true; while (s.Count > 0) { var o = s.Dequeue(); if (count[o.A] > 1) { count[o.A]--; sum -= o.B; couldnt = false; break; } } if (couldnt) break; //持ってないのを入れる couldnt = true; while (k.Count > 0) { var c = k.Dequeue(); if (count[c.A] > 0) continue; count[c.A]++; sum += c.B; kinds++; couldnt = false; break; } if (couldnt) break; ans = Math.Max(ans, sum + kinds * kinds); } Console.WriteLine(ans); } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> data = new List<T>(); private readonly IComparer<T> comparer; private readonly bool isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { this.comparer = comparer; this.isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { data.Add(item); var childIndex = data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(data[childIndex], data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { if (Count < 1) { throw new Exception("キューに値が入っていません"); } var lastIndex = data.Count - 1; var firstItem = data[0]; data[0] = data[lastIndex]; data.RemoveAt(lastIndex); lastIndex--; var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) { break; } var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(data[rightChild], data[childIndex]) < 0) { childIndex = rightChild; } if (Compare(data[parentIndex], data[childIndex]) <= 0) { break; } Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return data[0]; } private void Swap(int a, int b) { var tmp = data[a]; data[a] = data[b]; data[b] = tmp; } private int Compare(T a, T b) { return isDescending ? comparer.Compare(b, a) : comparer.Compare(a, b); } public int Count => data.Count; public IEnumerator<T> GetEnumerator() { return data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } /// <summary> /// 値のペア ソート/出力用のindexも持つ /// </summary> class Pair { public long A; public long B; public long I; public Pair(long a, long b, long i) { A = a; B = b; I = i; } public override string ToString() { return "i: " + I + " A: " + A + " B: " + B; } } class PairComperator : IComparer<Pair> { int IComparer<Pair>.Compare(Pair a, Pair b) { return (int)b.B - (int)a.B; } } }
[["+", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 198, 0, 200, 0, 212, 0, 211, 0, 146], ["+", 8, 196, 0, 52, 8, 196, 0, 197, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 64, 196, 0, 1, 0, 11, 12, 211, 0, 147], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 15, 22], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 25], ["+", 0, 52, 8, 196, 0, 57, 64, 93, 0, 94], ["+", 0, 52, 8, 196, 0, 57, 64, 93, 0, 35], ["+", 0, 52, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 196, 0, 1, 0, 11, 12, 211, 0, 146], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 1, 0, 11, 12, 211, 0, 147]]
8
2,391
29
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { ABC116.D(); } class ABC116 { public static void A() { Console.WriteLine(Read.Int() * Read.Int() / 2); } public static void B() { var s = Read.Int(); HashSet<int> hash = new HashSet<int>(); while (true) { hash.Add(s); s = s % 2 == 0 ? s / 2 : s * 3 + 1; if (hash.Contains(s)) { break; } } Console.WriteLine(hash.Count + 1); } public static void C() { var N = Read.Int(); var H = Read.IntN(); int count = H[N - 1]; for (int i = 0; i < N - 1; ++i) { count += Math.Max(0, H[i] - H[i + 1]); } Console.WriteLine(count); } public static void D() { var N = Read.Int(); var K = Read.Int(); var TD = Read.TupleLong(N); TD = TD.OrderBy(x => -x.Item2).ToArray(); long[] SumD = Enumerable.Range(0, N + 1).Select(i => -10000000000).ToArray(); HashSet<long> ts = new HashSet<long>(); var candRemove = new Util.MultiSet<long>(); var candInsert = new Util.MultiSet<long>(); for (int i = 0; i < N; ++i) { var td = TD[i]; if (i < K) { if (ts.Contains(td.Item1)) { candRemove.Insert(td.Item2); } else { ts.Add(td.Item1); } } else { if (!ts.Contains(td.Item1)) { candInsert.Insert(td.Item2); } } } SumD[ts.Count] = TD.Take(K).Sum(x => x.Item2); int idx = ts.Count + 1; while (candRemove.Count() > 0 && candInsert.Count() > 0) { var max = candInsert.ElementAt(candInsert.Count() - 1); var min = candRemove.ElementAt(0); SumD[idx] = SumD[idx - 1] + max - min; candInsert.Remove(max); candRemove.Remove(min); idx++; } long maxV = 0; for (int i = 0; i <= N; ++i) { long v = (long)i * i + SumD[i]; maxV = Math.Max(maxV, v); } Console.WriteLine(maxV); } } class Util { public class BST<T> where T : IComparable { public class Node { public T V; public Node L, R; public int Count; public Node(T v) { V = v; Count = 1; } } static Random _rnd = new Random(); public static int Count(Node t) { return t == null ? 0 : t.Count; } static Node Update(Node t) { t.Count = Count(t.L) + Count(t.R) + 1; return t; } public static Node Merge(Node l, Node r) { if (l == null || r == null) return l == null ? r : l; if ((double)Count(l) / (double)(Count(l) + Count(r)) > _rnd.NextDouble()) { l.R = Merge(l.R, r); return Update(l); } else { r.L = Merge(l, r.L); return Update(r); } } public static Tuple<Node, Node> Split(Node t, int k) { if (t == null) return new Tuple<Node, Node>(null, null); if (k <= Count(t.L)) { var s = Split(t.L, k); t.L = s.Item2; return new Tuple<Node, Node>(s.Item1, Update(t)); } else { var s = Split(t.R, k - Count(t.L) - 1); t.R = s.Item1; return new Tuple<Node, Node>(Update(t), s.Item2); } } public static Node Remove(Node t, T v) { if (Find(t, v) == null) return t; return RemoveAt(t, LowerBound(t, v)); } public static Node RemoveAt(Node t, int k) { var s = Split(t, k); var s2 = Split(s.Item2, 1); return Merge(s.Item1, s2.Item2); } public static bool Contains(Node t, T v) { return Find(t, v) != null; } public static Node Find(Node t, T v) { while (t != null) { var cmp = t.V.CompareTo(v); if (cmp > 0) t = t.L; else if (cmp < 0) t = t.R; else break; } return t; } public static Node FindByIndex(Node t, int idx) { if (t == null) return null; var currentIdx = Count(t) - Count(t.R) - 1; while (t != null) { if (currentIdx == idx) return t; if (currentIdx > idx) { t = t.L; currentIdx -= (Count(t == null ? null : t.R) + 1); } else { t = t.R; currentIdx += (Count(t == null ? null : t.L) + 1); } } return null; } public static int UpperBound(Node t, T v) { var torg = t; if (t == null) return -1; var ret = Int32.MaxValue; var idx = Count(t) - Count(t.R) - 1; while (t != null) { var cmp = t.V.CompareTo(v); if (cmp > 0) { ret = Math.Min(ret, idx); t = t.L; idx -= (Count(t == null ? null : t.R) + 1); } else if (cmp <= 0) { t = t.R; idx += (Count(t == null ? null : t.L) + 1); } } return ret == Int32.MaxValue ? Count(torg) : ret; } public static int LowerBound(Node t, T v) { var torg = t; if (t == null) return -1; var idx = Count(t) - Count(t.R) - 1; var ret = Int32.MaxValue; while (t != null) { var cmp = t.V.CompareTo(v); if (cmp >= 0) { if (cmp == 0) ret = Math.Min(ret, idx); t = t.L; if (t == null) ret = Math.Min(ret, idx); idx -= t == null ? 0 : (Count(t.R) + 1); } else if (cmp < 0) { t = t.R; idx += (Count(t == null ? null : t.L) + 1); if (t == null) return idx; } } return ret == Int32.MaxValue ? Count(torg) : ret; } public static Node Insert(Node t, T v) { var ub = LowerBound(t, v); return InsertByIdx(t, ub, v); } static Node InsertByIdx(Node t, int k, T v) { var s = Split(t, k); return Merge(Merge(s.Item1, new Node(v)), s.Item2); } public static IEnumerable<T> Enumerate(Node t) { var ret = new List<T>(); Enumerate(t, ret); return ret; } static void Enumerate(Node t, List<T> ret) { if (t == null) return; Enumerate(t.L, ret); ret.Add(t.V); Enumerate(t.R, ret); } } public class Set<T> where T : IComparable { protected BST<T>.Node _root; public T this[int idx] { get { return ElementAt(idx); } } public int Count() { return BST<T>.Count(_root); } public virtual void Insert(T v) { if (_root == null) _root = new BST<T>.Node(v); else { if (BST<T>.Find(_root, v) != null) return; _root = BST<T>.Insert(_root, v); } } public void Clear() { _root = null; } public void Remove(T v) { _root = BST<T>.Remove(_root, v); } public bool Contains(T v) { return BST<T>.Contains(_root, v); } public T ElementAt(int k) { var node = BST<T>.FindByIndex(_root, k); if (node == null) throw new IndexOutOfRangeException(); return node.V; } public int Count(T v) { return BST<T>.UpperBound(_root, v) - BST<T>.LowerBound(_root, v); } public int LowerBound(T v) { return BST<T>.LowerBound(_root, v); } public int UpperBound(T v) { return BST<T>.UpperBound(_root, v); } public Tuple<int, int> EqualRange(T v) { if (!Contains(v)) return new Tuple<int, int>(-1, -1); return new Tuple<int, int>(BST<T>.LowerBound(_root, v), BST<T>.UpperBound(_root, v) - 1); } public List<T> ToList() { return new List<T>(BST<T>.Enumerate(_root)); } } public class MultiSet<T> : Set<T> where T : IComparable { public override void Insert(T v) { if (_root == null) { _root = new BST<T>.Node(v); } else { _root = BST<T>.Insert(_root, v); } } } } static class Read { static Queue<string> queue = new Queue<string>(); static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } } public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); } public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); } public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); } public static int[] IntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } public static long[] LongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } public static string[] StrN() { return Console.ReadLine().Split(' '); } public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } } } }
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { ABC116.D(); } class ABC116 { public static void A() { Console.WriteLine(Read.Int() * Read.Int() / 2); } public static void B() { var s = Read.Int(); HashSet<int> hash = new HashSet<int>(); while (true) { hash.Add(s); s = s % 2 == 0 ? s / 2 : s * 3 + 1; if (hash.Contains(s)) { break; } } Console.WriteLine(hash.Count + 1); } public static void C() { var N = Read.Int(); var H = Read.IntN(); int count = H[N - 1]; for (int i = 0; i < N - 1; ++i) { count += Math.Max(0, H[i] - H[i + 1]); } Console.WriteLine(count); } public static void D() { var N = Read.Int(); var K = Read.Int(); var TD = Read.TupleLong(N); TD = TD.OrderBy(x => -x.Item2).ToArray(); long[] SumD = Enumerable.Range(0, N + 1).Select(i => -10000000000).ToArray(); HashSet<long> ts = new HashSet<long>(); HashSet<long> tsALL = new HashSet<long>(); var candRemove = new Util.MultiSet<long>(); var candInsert = new Util.MultiSet<long>(); for (int i = 0; i < N; ++i) { var td = TD[i]; if (i < K) { if (ts.Contains(td.Item1)) { candRemove.Insert(td.Item2); } else { ts.Add(td.Item1); } } else { if (!ts.Contains(td.Item1) && !tsALL.Contains(td.Item1)) { candInsert.Insert(td.Item2); tsALL.Add(td.Item1); } } } SumD[ts.Count] = TD.Take(K).Sum(x => x.Item2); int idx = ts.Count + 1; while (candRemove.Count() > 0 && candInsert.Count() > 0) { var max = candInsert.ElementAt(candInsert.Count() - 1); var min = candRemove.ElementAt(0); SumD[idx] = SumD[idx - 1] + max - min; candInsert.Remove(max); candRemove.Remove(min); idx++; } long maxV = 0; for (int i = 0; i <= N; ++i) { long v = (long)i * i + SumD[i]; maxV = Math.Max(maxV, v); } Console.WriteLine(maxV); } } class Util { public class BST<T> where T : IComparable { public class Node { public T V; public Node L, R; public int Count; public Node(T v) { V = v; Count = 1; } } static Random _rnd = new Random(); public static int Count(Node t) { return t == null ? 0 : t.Count; } static Node Update(Node t) { t.Count = Count(t.L) + Count(t.R) + 1; return t; } public static Node Merge(Node l, Node r) { if (l == null || r == null) return l == null ? r : l; if ((double)Count(l) / (double)(Count(l) + Count(r)) > _rnd.NextDouble()) { l.R = Merge(l.R, r); return Update(l); } else { r.L = Merge(l, r.L); return Update(r); } } public static Tuple<Node, Node> Split(Node t, int k) { if (t == null) return new Tuple<Node, Node>(null, null); if (k <= Count(t.L)) { var s = Split(t.L, k); t.L = s.Item2; return new Tuple<Node, Node>(s.Item1, Update(t)); } else { var s = Split(t.R, k - Count(t.L) - 1); t.R = s.Item1; return new Tuple<Node, Node>(Update(t), s.Item2); } } public static Node Remove(Node t, T v) { if (Find(t, v) == null) return t; return RemoveAt(t, LowerBound(t, v)); } public static Node RemoveAt(Node t, int k) { var s = Split(t, k); var s2 = Split(s.Item2, 1); return Merge(s.Item1, s2.Item2); } public static bool Contains(Node t, T v) { return Find(t, v) != null; } public static Node Find(Node t, T v) { while (t != null) { var cmp = t.V.CompareTo(v); if (cmp > 0) t = t.L; else if (cmp < 0) t = t.R; else break; } return t; } public static Node FindByIndex(Node t, int idx) { if (t == null) return null; var currentIdx = Count(t) - Count(t.R) - 1; while (t != null) { if (currentIdx == idx) return t; if (currentIdx > idx) { t = t.L; currentIdx -= (Count(t == null ? null : t.R) + 1); } else { t = t.R; currentIdx += (Count(t == null ? null : t.L) + 1); } } return null; } public static int UpperBound(Node t, T v) { var torg = t; if (t == null) return -1; var ret = Int32.MaxValue; var idx = Count(t) - Count(t.R) - 1; while (t != null) { var cmp = t.V.CompareTo(v); if (cmp > 0) { ret = Math.Min(ret, idx); t = t.L; idx -= (Count(t == null ? null : t.R) + 1); } else if (cmp <= 0) { t = t.R; idx += (Count(t == null ? null : t.L) + 1); } } return ret == Int32.MaxValue ? Count(torg) : ret; } public static int LowerBound(Node t, T v) { var torg = t; if (t == null) return -1; var idx = Count(t) - Count(t.R) - 1; var ret = Int32.MaxValue; while (t != null) { var cmp = t.V.CompareTo(v); if (cmp >= 0) { if (cmp == 0) ret = Math.Min(ret, idx); t = t.L; if (t == null) ret = Math.Min(ret, idx); idx -= t == null ? 0 : (Count(t.R) + 1); } else if (cmp < 0) { t = t.R; idx += (Count(t == null ? null : t.L) + 1); if (t == null) return idx; } } return ret == Int32.MaxValue ? Count(torg) : ret; } public static Node Insert(Node t, T v) { var ub = LowerBound(t, v); return InsertByIdx(t, ub, v); } static Node InsertByIdx(Node t, int k, T v) { var s = Split(t, k); return Merge(Merge(s.Item1, new Node(v)), s.Item2); } public static IEnumerable<T> Enumerate(Node t) { var ret = new List<T>(); Enumerate(t, ret); return ret; } static void Enumerate(Node t, List<T> ret) { if (t == null) return; Enumerate(t.L, ret); ret.Add(t.V); Enumerate(t.R, ret); } } public class Set<T> where T : IComparable { protected BST<T>.Node _root; public T this[int idx] { get { return ElementAt(idx); } } public int Count() { return BST<T>.Count(_root); } public virtual void Insert(T v) { if (_root == null) _root = new BST<T>.Node(v); else { if (BST<T>.Find(_root, v) != null) return; _root = BST<T>.Insert(_root, v); } } public void Clear() { _root = null; } public void Remove(T v) { _root = BST<T>.Remove(_root, v); } public bool Contains(T v) { return BST<T>.Contains(_root, v); } public T ElementAt(int k) { var node = BST<T>.FindByIndex(_root, k); if (node == null) throw new IndexOutOfRangeException(); return node.V; } public int Count(T v) { return BST<T>.UpperBound(_root, v) - BST<T>.LowerBound(_root, v); } public int LowerBound(T v) { return BST<T>.LowerBound(_root, v); } public int UpperBound(T v) { return BST<T>.UpperBound(_root, v); } public Tuple<int, int> EqualRange(T v) { if (!Contains(v)) return new Tuple<int, int>(-1, -1); return new Tuple<int, int>(BST<T>.LowerBound(_root, v), BST<T>.UpperBound(_root, v) - 1); } public List<T> ToList() { return new List<T>(BST<T>.Enumerate(_root)); } } public class MultiSet<T> : Set<T> where T : IComparable { public override void Insert(T v) { if (_root == null) { _root = new BST<T>.Node(v); } else { _root = BST<T>.Insert(_root, v); } } } } static class Read { static Queue<string> queue = new Queue<string>(); static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } } public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); } public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); } public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); } public static int[] IntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } public static long[] LongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } public static string[] StrN() { return Console.ReadLine().Split(' '); } public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } } } }
[["+", 8, 196, 0, 197, 0, 198, 39, 236, 141, 22], ["+", 0, 197, 0, 198, 39, 236, 237, 238, 0, 18], ["+", 0, 197, 0, 198, 39, 236, 237, 238, 0, 199], ["+", 0, 197, 0, 198, 39, 236, 237, 238, 0, 47], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 198, 0, 200, 0, 212, 0, 230, 0, 228], ["+", 0, 200, 0, 212, 0, 230, 39, 236, 141, 22], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 18], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 47], ["+", 0, 200, 0, 212, 0, 230, 3, 4, 0, 24], ["+", 0, 200, 0, 212, 0, 230, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 17, 98], ["+", 75, 196, 0, 57, 15, 16, 12, 241, 0, 111], ["+", 15, 16, 12, 241, 0, 213, 63, 214, 205, 22], ["+", 15, 16, 12, 241, 0, 213, 63, 214, 0, 131], ["+", 15, 16, 12, 241, 0, 213, 63, 214, 141, 22], ["+", 15, 16, 12, 241, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 205, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 0, 131], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 141, 22], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 25], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 75, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24]]
8
3,125
33
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; using System.Linq; using E = System.Linq.Enumerable; internal partial class Solver { struct Sushi { public int Type, Point; } public void Run() { var n = ni(); var k = ni(); var t = new int[n]; var d = new int[n]; var sushi = new List<Sushi>(); for (int i = 0; i < n; i++) { t[i] = ni() - 1; d[i] = ni(); sushi.Add(new Sushi { Type = t[i], Point = d[i] }); } sushi.Sort((s1, s2) => -s1.Point.CompareTo(s2.Point)); long sum = 0; for (int i = 0; i < k; i++) { sum += sushi[i].Point; } long ans = 0; var used = new bool[n]; var choose = new bool[n]; int typeNum = 0; int last = k - 1; for (int i = 0; i < n; i++) { if (used[sushi[i].Type]) continue; used[sushi[i].Type] = true; choose[i] = true; typeNum++; if (typeNum > k) break; if (i < k) { } else { while (choose[last]) { last--; } sum -= sushi[last].Point; sum += sushi[i].Point; } ans = Math.Max(ans, sum + 1L * typeNum * typeNum); } cout.WriteLine(ans); } } // PREWRITEN CODE BEGINS FROM HERE internal partial class Solver : Scanner { public static void Main(string[] args) { #if LOCAL byte[] inputBuffer = new byte[1000000]; var inputStream = Console.OpenStandardInput(inputBuffer.Length); using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) { Console.SetIn(reader); new Solver(Console.In, Console.Out).Run(); } #else Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver(Console.In, Console.Out).Run(); Console.Out.Flush(); #endif } #pragma warning disable IDE0052 private readonly TextReader cin; private readonly TextWriter cout; #pragma warning restore IDE0052 public Solver(TextReader reader, TextWriter writer) : base(reader) { cin = reader; cout = writer; } public Solver(string input, TextWriter writer) : this(new StringReader(input), writer) {} #pragma warning disable IDE1006 #pragma warning disable IDE0051 private int ni() { return NextInt(); } private int[] ni(int n) { return NextIntArray(n); } private long nl() { return NextLong(); } private long[] nl(int n) { return NextLongArray(n); } private double nd() { return NextDouble(); } private double[] nd(int n) { return NextDoubleArray(n); } private string ns() { return Next(); } private string[] ns(int n) { return NextArray(n); } #pragma warning restore IDE1006 #pragma warning restore IDE0051 } internal static class LinqPadExtension { [Conditional("DEBUG")] public static void Dump<T>(this T obj) { #if DEBUG LINQPad.Extensions.Dump(obj); #endif } } public class Scanner { private readonly TextReader Reader; private readonly Queue<string> TokenQueue = new Queue<string>(); private readonly CultureInfo ci = CultureInfo.InvariantCulture; public Scanner() : this(Console.In) {} public Scanner(TextReader reader) { Reader = reader; } public int NextInt() { return int.Parse(Next(), ci); } public long NextLong() { return long.Parse(Next(), ci); } public double NextDouble() { return double.Parse(Next(), ci); } public string[] NextArray(int size) { string[] array = new string[size]; for (int i = 0; i < size; i++) { array[i] = Next(); } return array; } public int[] NextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = NextInt(); } return array; } public long[] NextLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = NextLong(); } return array; } public double[] NextDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = NextDouble(); } return array; } public string Next() { if (TokenQueue.Count == 0) { if (!StockTokens()) { throw new InvalidOperationException(); } } return TokenQueue.Dequeue(); } public bool HasNext() { if (TokenQueue.Count > 0) { return true; } return StockTokens(); } private static readonly char[] _separator = new[] { ' ', '\t' }; private bool StockTokens() { while (true) { string line = Reader.ReadLine(); if (line == null) { return false; } string[] tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { continue; } foreach (string token in tokens) { TokenQueue.Enqueue(token); } return true; } } }
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; using System.Linq; using E = System.Linq.Enumerable; internal partial class Solver { struct Sushi { public int Type, Point; } public void Run() { var n = ni(); var k = ni(); var t = new int[n]; var d = new int[n]; var sushi = new List<Sushi>(); for (int i = 0; i < n; i++) { t[i] = ni() - 1; d[i] = ni(); sushi.Add(new Sushi { Type = t[i], Point = d[i] }); } sushi.Sort((s1, s2) => -s1.Point.CompareTo(s2.Point)); long sum = 0; for (int i = 0; i < k; i++) { sum += sushi[i].Point; } long ans = 0; var used = new bool[n]; var choose = new bool[n]; int typeNum = 0; int last = k - 1; for (int i = 0; i < n; i++) { if (used[sushi[i].Type]) continue; used[sushi[i].Type] = true; choose[i] = true; typeNum++; if (typeNum > k) break; if (i < k) { } else { while (choose[last]) { last--; } sum -= sushi[last].Point; sum += sushi[i].Point; choose[last] = true; } ans = Math.Max(ans, sum + 1L * typeNum * typeNum); } cout.WriteLine(ans); } } // PREWRITEN CODE BEGINS FROM HERE internal partial class Solver : Scanner { public static void Main(string[] args) { #if LOCAL byte[] inputBuffer = new byte[1000000]; var inputStream = Console.OpenStandardInput(inputBuffer.Length); using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) { Console.SetIn(reader); new Solver(Console.In, Console.Out).Run(); } #else Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver(Console.In, Console.Out).Run(); Console.Out.Flush(); #endif } #pragma warning disable IDE0052 private readonly TextReader cin; private readonly TextWriter cout; #pragma warning restore IDE0052 public Solver(TextReader reader, TextWriter writer) : base(reader) { cin = reader; cout = writer; } public Solver(string input, TextWriter writer) : this(new StringReader(input), writer) {} #pragma warning disable IDE1006 #pragma warning disable IDE0051 private int ni() { return NextInt(); } private int[] ni(int n) { return NextIntArray(n); } private long nl() { return NextLong(); } private long[] nl(int n) { return NextLongArray(n); } private double nd() { return NextDouble(); } private double[] nd(int n) { return NextDoubleArray(n); } private string ns() { return Next(); } private string[] ns(int n) { return NextArray(n); } #pragma warning restore IDE1006 #pragma warning restore IDE0051 } internal static class LinqPadExtension { [Conditional("DEBUG")] public static void Dump<T>(this T obj) { #if DEBUG LINQPad.Extensions.Dump(obj); #endif } } public class Scanner { private readonly TextReader Reader; private readonly Queue<string> TokenQueue = new Queue<string>(); private readonly CultureInfo ci = CultureInfo.InvariantCulture; public Scanner() : this(Console.In) {} public Scanner(TextReader reader) { Reader = reader; } public int NextInt() { return int.Parse(Next(), ci); } public long NextLong() { return long.Parse(Next(), ci); } public double NextDouble() { return double.Parse(Next(), ci); } public string[] NextArray(int size) { string[] array = new string[size]; for (int i = 0; i < size; i++) { array[i] = Next(); } return array; } public int[] NextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = NextInt(); } return array; } public long[] NextLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = NextLong(); } return array; } public double[] NextDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = NextDouble(); } return array; } public string Next() { if (TokenQueue.Count == 0) { if (!StockTokens()) { throw new InvalidOperationException(); } } return TokenQueue.Dequeue(); } public bool HasNext() { if (TokenQueue.Count > 0) { return true; } return StockTokens(); } private static readonly char[] _separator = new[] { ' ', '\t' }; private bool StockTokens() { while (true) { string line = Reader.ReadLine(); if (line == null) { return false; } string[] tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { continue; } foreach (string token in tokens) { TokenQueue.Enqueue(token); } return true; } } }
[["+", 75, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 75, 196, 0, 1, 0, 11, 12, 211, 0, 146], ["+", 8, 196, 0, 57, 75, 196, 0, 1, 0, 35]]
8
1,254
7
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using static System.Math; using static System.Array; using static AtCoder.Tool; namespace AtCoder { class AC { const int MOD = 1000000007; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 2; const double EPS = 1e-8; static readonly int[] dx = { 0, 1, 0, -1 }; static readonly int[] dy = { 1, 0, -1, 0 }; static void Main(string[] args) { // var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = // false }; Console.SetOut(sw); var cin = new Scanner(); var input = cin.SplitRead(); int N = input[0]; int K = input[1]; var dic = new Dictionary<int, int>(); long type = 0; long dsum = 0; var s = new PriorityQueue<Sushi>(true); for (var i = 0; i < N; i++) { var td = cin.SplitReadL(); s.Enqueue(new Sushi((int)td[0], td[1])); } var chout = new PriorityQueue<Sushi>(false); for (var i = 0; i < K; i++) { var su = s.Dequeue(); if (!dic.ContainsKey(su.t)) { dic[su.t] = 1; type++; } else { dic[su.t]++; chout.Enqueue(su); } dsum += su.d; } var ans = new List<long>(); ans.Add(dsum + (type * type)); for (var i = 0; i < N - K; i++) { var su = s.Dequeue(); if (dic.ContainsKey(su.t)) { continue; } var sout = chout.Dequeue(); dic[su.t] = 1; type++; dsum += su.d - sout.d; ans.Add(dsum + (type * type)); } Console.WriteLine(ans.Max()); // Console.Out.Flush(); } struct Sushi : IComparable<Sushi> { public int t; public long d; public Sushi(int ti, long di) { t = ti; d = di; } public int CompareTo(Sushi other) { return d.CompareTo(other.d); } } } public class PriorityQueue<T> where T : IComparable<T> { private List<T> Q; private readonly int r; public PriorityQueue(bool ascending) { if (ascending) { r = 1; } else { r = -1; } Q = new List<T>(); } private void PushHeap(List<T> list, T item) { int n = list.Count(); list.Add(item); while (n != 0) { int pIndex = (n - 1) / 2; if (list[n].CompareTo(list[pIndex]) * r > 0) { Swap(Q, n, pIndex); } else { break; } n = pIndex; } } private void PopHeap(List<T> list) { int n = list.Count() - 1; list[0] = list[n]; list.RemoveAt(n); int cur = 0; int comp; while (2 * cur + 1 <= n - 1) { int c1 = 2 * cur + 1; int c2 = 2 * (cur + 1); if (c1 == n - 1) { comp = c1; } else { comp = list[c1].CompareTo(list[c2]) * r > 0 ? c1 : c2; } if (list[cur].CompareTo(list[comp]) * r < 0) { Swap(Q, cur, comp); } else { break; } cur = comp; } } private void Swap(List<T> list, int a, int b) { T keep = list[a]; list[a] = list[b]; list[b] = keep; } public void Enqueue(T value) { PushHeap(Q, value); } public T Dequeue() { T ret = Q[0]; PopHeap(Q); return ret; } public T Peek() { return Q[0]; } public int CountItem() { return Q.Count(); } } public class Scanner { public int[] SplitRead() { int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); return array; } public long[] SplitReadL() { long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray(); return array; } } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { for (var i = 0; i < array.Length; i++) { array[i] = initialvalue; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using static System.Math; using static System.Array; using static AtCoder.Tool; namespace AtCoder { class AC { const int MOD = 1000000007; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 2; const double EPS = 1e-8; static readonly int[] dx = { 0, 1, 0, -1 }; static readonly int[] dy = { 1, 0, -1, 0 }; static void Main(string[] args) { // var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = // false }; Console.SetOut(sw); var cin = new Scanner(); var input = cin.SplitRead(); int N = input[0]; int K = input[1]; var dic = new Dictionary<int, int>(); long type = 0; long dsum = 0; var s = new PriorityQueue<Sushi>(true); for (var i = 0; i < N; i++) { var td = cin.SplitReadL(); s.Enqueue(new Sushi((int)td[0], td[1])); } var chout = new PriorityQueue<Sushi>(false); for (var i = 0; i < K; i++) { var su = s.Dequeue(); if (!dic.ContainsKey(su.t)) { dic[su.t] = 1; type++; } else { dic[su.t]++; chout.Enqueue(su); } dsum += su.d; } var ans = new List<long>(); ans.Add(dsum + (type * type)); for (var i = 0; i < N - K; i++) { if (chout.CountItem() == 0) { break; } var su = s.Dequeue(); if (dic.ContainsKey(su.t)) { continue; } var sout = chout.Dequeue(); dic[su.t] = 1; type++; dsum += su.d - sout.d; ans.Add(dsum + (type * type)); } Console.WriteLine(ans.Max()); // Console.Out.Flush(); } struct Sushi : IComparable<Sushi> { public int t; public long d; public Sushi(int ti, long di) { t = ti; d = di; } public int CompareTo(Sushi other) { return d.CompareTo(other.d); } } } public class PriorityQueue<T> where T : IComparable<T> { private List<T> Q; private readonly int r; public PriorityQueue(bool ascending) { if (ascending) { r = 1; } else { r = -1; } Q = new List<T>(); } private void PushHeap(List<T> list, T item) { int n = list.Count(); list.Add(item); while (n != 0) { int pIndex = (n - 1) / 2; if (list[n].CompareTo(list[pIndex]) * r > 0) { Swap(Q, n, pIndex); } else { break; } n = pIndex; } } private void PopHeap(List<T> list) { int n = list.Count() - 1; list[0] = list[n]; list.RemoveAt(n); int cur = 0; int comp; while (2 * cur + 1 <= n - 1) { int c1 = 2 * cur + 1; int c2 = 2 * (cur + 1); if (c1 == n - 1) { comp = c1; } else { comp = list[c1].CompareTo(list[c2]) * r > 0 ? c1 : c2; } if (list[cur].CompareTo(list[comp]) * r < 0) { Swap(Q, cur, comp); } else { break; } cur = comp; } } private void Swap(List<T> list, int a, int b) { T keep = list[a]; list[a] = list[b]; list[b] = keep; } public void Enqueue(T value) { PushHeap(Q, value); } public T Dequeue() { T ret = Q[0]; PopHeap(Q); return ret; } public T Peek() { return Q[0]; } public int CountItem() { return Q.Count(); } } public class Scanner { public int[] SplitRead() { int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); return array; } public long[] SplitReadL() { long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray(); return array; } } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { for (var i = 0; i < array.Length; i++) { array[i] = initialvalue; } } } }
[["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 205, 22], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 0, 131], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 141, 22], ["+", 0, 57, 15, 16, 31, 213, 3, 4, 0, 24], ["+", 0, 57, 15, 16, 31, 213, 3, 4, 0, 25], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 94], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 35], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
1,064
14
using System; using System.Collections.Generic; using System.Linq; namespace CSharpSample01 { struct Sushi { public int t; //ネタID public int d; //おいしさ public Sushi(int neta, int oisisa) { t = neta; d = oisisa; } } // Sushiノードの優先度付きキュー //おいしさdが対象 class PriorityQueue_Sushi { int H; //高速に要素にアクセスするためにリストでなく配列 Sushi[] heap; const int MAX = 2000000; bool is_maxheap; // f:最大ヒープにするか(true:Max, false:Min) public PriorityQueue_Sushi(bool f) { H = 0; heap = new Sushi[MAX]; is_maxheap = f; } public PriorityQueue_Sushi(int size, bool f) { H = size; heap = new Sushi[H + 1]; is_maxheap = f; } //上のuから下までずーと(入れ替えられなくなるor限界Hまで) // p:=MaxHeapfyを始めるノード番号 public void MaxHeapfy(int p) { while (2 * p <= H) { int son_n = 2 * p; Sushi son = heap[2 * p]; if (2 * p + 1 <= H && son.d < heap[2 * p + 1].d) { son = heap[2 * p + 1]; son_n++; } if (son.d > heap[p].d) { Sushi tmp = heap[p]; heap[p] = son; heap[son_n] = tmp; } else { break; } p = son_n; } } public void MinHeapfy(int p) { while (2 * p <= H) { int son_n = 2 * p; Sushi son = heap[2 * p]; if (2 * p + 1 <= H && son.d > heap[2 * p + 1].d) { son = heap[2 * p + 1]; son_n++; } if (son.d < heap[p].d) { Sushi tmp = heap[p]; heap[p] = son; heap[son_n] = tmp; } else { break; } p = son_n; } } public void BuildMaxHeap() { for (int s = H / 2; s >= 1; s--) { MaxHeapfy(s); } } public void BuildMinHeap() { for (int s = H / 2; s >= 1; s--) { MinHeapfy(s); } } public void Insert(Sushi key) { //下から上にずーとヒープ H++; heap[H] = key; int son = H; while (son != 1) { int p = son / 2; if (is_maxheap && heap[son].d > heap[p].d) { Sushi tmp = heap[p]; heap[p] = heap[son]; heap[son] = tmp; son /= 2; } else if (!is_maxheap && heap[son].d < heap[p].d) { Sushi tmp = heap[p]; heap[p] = heap[son]; heap[son] = tmp; son /= 2; } else { break; } } } public Sushi Extract() { Sushi max = heap[1]; heap[1] = heap[H]; H--; if (is_maxheap) MaxHeapfy(1); else MinHeapfy(1); return max; } public int Count() { return H; } } class Program { static int N; static int K; static PriorityQueue_Sushi stomach; static PriorityQueue_Sushi not_eat; // t_cnt[i] := ネタiを何個食べたか static int[] t_cnt; // t_max[i] := ネタiのすしのおいしさdの最大値 static int[] t_max; //入力されたすしを格納するsushi_list static List<Sushi> sushi_list; //ソートしたsushi_listの中のおいしさ上位K個のeat_sushi_list static List<Sushi> eat_sushi_list; static long max_point; static long d_sum; static long now_type_cnt; static void Create_stomach() { //最初に食べるK個のすしに対して // stamach生成(d_sumとnow_type_cntの更新) // Console.WriteLine("eat_sushi_list"); for (int i = 0; i < K; i++) { Sushi sushi = eat_sushi_list[i]; // Console.WriteLine($"t:{sushi.t} d:{sushi.d}"); d_sum += sushi.d; if (t_cnt[sushi.t] >= 1) { stomach.Insert(sushi); } if (t_cnt[sushi.t] == 0) { now_type_cnt++; } t_cnt[sushi.t]++; } } // not_eat生成 static void Create_not_eat() { // t_max更新 // Console.WriteLine("not_eat_list"); for (int i = K; i < N; i++) { Sushi sushi = sushi_list[i]; // Console.WriteLine($"t:{sushi.t} d:{sushi.d}"); if (t_cnt[sushi.t] == 0) { t_max[sushi.t] = Math.Max(t_max[sushi.t], sushi.d); } } // t_max > 0のネタをnot_eatに入れる for (int i = 1; i <= N; i++) { if (t_max[i] > 0) not_eat.Insert(new Sushi(i, t_max[i])); } } // max_pointを更新 static void Updata_max_point() { while (true) { if (now_type_cnt == K || stomach.Count() == 0) break; Sushi sushi = stomach.Extract(); d_sum -= sushi.d; sushi = not_eat.Extract(); d_sum += sushi.d; ++now_type_cnt; max_point = Math.Max(max_point, d_sum + now_type_cnt * now_type_cnt); } } static void Main(string[] args) { // N, Kの入力 string[] input = Console.ReadLine().Split(' '); N = int.Parse(input[0]); K = int.Parse(input[1]); t_cnt = new int[N + 1]; t_max = new int[N + 1]; sushi_list = new List<Sushi>(); eat_sushi_list = new List<Sushi>(); stomach = new PriorityQueue_Sushi(false); // Minヒープ not_eat = new PriorityQueue_Sushi(true); // Maxヒープ //すし(ネタ, おいしさ)を入力 for (int i = 0; i < N; i++) { input = Console.ReadLine().Split(' '); int t = int.Parse(input[0]); int d = int.Parse(input[1]); sushi_list.Add(new Sushi(t, d)); } // sushi_listをdでソート sushi_list = sushi_list.OrderByDescending(x => x.d).ToList(); for (int i = 0; i < K; i++) eat_sushi_list.Add(sushi_list[i]); eat_sushi_list = eat_sushi_list.OrderBy(x => x.t).ThenByDescending(x => x.d).ToList(); /* Console.WriteLine("eat_sushi_list"); foreach (var s in eat_sushi_list) Console.WriteLine($"t:{s.t} d:{s.d}"); */ max_point = 0; d_sum = 0; now_type_cnt = 0; // stomach生成 Create_stomach(); // max_pointの初期化 max_point = d_sum + now_type_cnt * now_type_cnt; // Console.WriteLine($"max_point初期値:{max_point}"); // not_eat生成 Create_not_eat(); // max_pointを求める Updata_max_point(); Console.WriteLine(max_point); } } }
using System; using System.Collections.Generic; using System.Linq; namespace CSharpSample01 { struct Sushi { public int t; //ネタID public int d; //おいしさ public Sushi(int neta, int oisisa) { t = neta; d = oisisa; } } // Sushiノードの優先度付きキュー //おいしさdが対象 class PriorityQueue_Sushi { int H; //高速に要素にアクセスするためにリストでなく配列 Sushi[] heap; const int MAX = 2000000; bool is_maxheap; // f:最大ヒープにするか(true:Max, false:Min) public PriorityQueue_Sushi(bool f) { H = 0; heap = new Sushi[MAX]; is_maxheap = f; } public PriorityQueue_Sushi(int size, bool f) { H = size; heap = new Sushi[H + 1]; is_maxheap = f; } //上のuから下までずーと(入れ替えられなくなるor限界Hまで) // p:=MaxHeapfyを始めるノード番号 public void MaxHeapfy(int p) { while (2 * p <= H) { int son_n = 2 * p; Sushi son = heap[2 * p]; if (2 * p + 1 <= H && son.d < heap[2 * p + 1].d) { son = heap[2 * p + 1]; son_n++; } if (son.d > heap[p].d) { Sushi tmp = heap[p]; heap[p] = son; heap[son_n] = tmp; } else { break; } p = son_n; } } public void MinHeapfy(int p) { while (2 * p <= H) { int son_n = 2 * p; Sushi son = heap[2 * p]; if (2 * p + 1 <= H && son.d > heap[2 * p + 1].d) { son = heap[2 * p + 1]; son_n++; } if (son.d < heap[p].d) { Sushi tmp = heap[p]; heap[p] = son; heap[son_n] = tmp; } else { break; } p = son_n; } } public void BuildMaxHeap() { for (int s = H / 2; s >= 1; s--) { MaxHeapfy(s); } } public void BuildMinHeap() { for (int s = H / 2; s >= 1; s--) { MinHeapfy(s); } } public void Insert(Sushi key) { //下から上にずーとヒープ H++; heap[H] = key; int son = H; while (son != 1) { int p = son / 2; if (is_maxheap && heap[son].d > heap[p].d) { Sushi tmp = heap[p]; heap[p] = heap[son]; heap[son] = tmp; son /= 2; } else if (!is_maxheap && heap[son].d < heap[p].d) { Sushi tmp = heap[p]; heap[p] = heap[son]; heap[son] = tmp; son /= 2; } else { break; } } } public Sushi Extract() { Sushi max = heap[1]; heap[1] = heap[H]; H--; if (is_maxheap) MaxHeapfy(1); else MinHeapfy(1); return max; } public int Count() { return H; } } class Program { static int N; static int K; static PriorityQueue_Sushi stomach; static PriorityQueue_Sushi not_eat; // t_cnt[i] := ネタiを何個食べたか static int[] t_cnt; // t_max[i] := ネタiのすしのおいしさdの最大値 static int[] t_max; //入力されたすしを格納するsushi_list static List<Sushi> sushi_list; //ソートしたsushi_listの中のおいしさ上位K個のeat_sushi_list static List<Sushi> eat_sushi_list; static long max_point; static long d_sum; static long now_type_cnt; static void Create_stomach() { //最初に食べるK個のすしに対して // stamach生成(d_sumとnow_type_cntの更新) // Console.WriteLine("eat_sushi_list"); for (int i = 0; i < K; i++) { Sushi sushi = eat_sushi_list[i]; // Console.WriteLine($"t:{sushi.t} d:{sushi.d}"); d_sum += sushi.d; if (t_cnt[sushi.t] >= 1) { stomach.Insert(sushi); } if (t_cnt[sushi.t] == 0) { now_type_cnt++; } t_cnt[sushi.t]++; } } // not_eat生成 static void Create_not_eat() { // t_max更新 // Console.WriteLine("not_eat_list"); for (int i = K; i < N; i++) { Sushi sushi = sushi_list[i]; // Console.WriteLine($"t:{sushi.t} d:{sushi.d}"); if (t_cnt[sushi.t] == 0) { t_max[sushi.t] = Math.Max(t_max[sushi.t], sushi.d); } } // t_max > 0のネタをnot_eatに入れる for (int i = 1; i <= N; i++) { if (t_max[i] > 0) not_eat.Insert(new Sushi(i, t_max[i])); } } // max_pointを更新 static void Updata_max_point() { while (true) { if (now_type_cnt == K || stomach.Count() == 0 || not_eat.Count() == 0) break; Sushi sushi = stomach.Extract(); d_sum -= sushi.d; sushi = not_eat.Extract(); d_sum += sushi.d; ++now_type_cnt; max_point = Math.Max(max_point, d_sum + now_type_cnt * now_type_cnt); } } static void Main(string[] args) { // N, Kの入力 string[] input = Console.ReadLine().Split(' '); N = int.Parse(input[0]); K = int.Parse(input[1]); t_cnt = new int[N + 1]; t_max = new int[N + 1]; sushi_list = new List<Sushi>(); eat_sushi_list = new List<Sushi>(); stomach = new PriorityQueue_Sushi(false); // Minヒープ not_eat = new PriorityQueue_Sushi(true); // Maxヒープ //すし(ネタ, おいしさ)を入力 for (int i = 0; i < N; i++) { input = Console.ReadLine().Split(' '); int t = int.Parse(input[0]); int d = int.Parse(input[1]); sushi_list.Add(new Sushi(t, d)); } // sushi_listをdでソート sushi_list = sushi_list.OrderByDescending(x => x.d).ToList(); //上位K個を選ぶ for (int i = 0; i < K; i++) eat_sushi_list.Add(sushi_list[i]); eat_sushi_list = eat_sushi_list.OrderBy(x => x.t).ThenByDescending(x => x.d).ToList(); /* Console.WriteLine("eat_sushi_list"); foreach (var s in eat_sushi_list) Console.WriteLine($"t:{s.t} d:{s.d}"); */ max_point = 0; d_sum = 0; now_type_cnt = 0; // stomach生成 Create_stomach(); // max_pointの初期化 max_point = d_sum + now_type_cnt * now_type_cnt; // Console.WriteLine($"max_point初期値:{max_point}"); // not_eat生成 Create_not_eat(); // max_pointを求める Updata_max_point(); Console.WriteLine(max_point); } } }
[["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 106], ["+", 15, 16, 12, 16, 31, 213, 63, 214, 205, 22], ["+", 15, 16, 12, 16, 31, 213, 63, 214, 0, 131], ["+", 15, 16, 12, 16, 31, 213, 63, 214, 141, 22], ["+", 15, 16, 12, 16, 31, 213, 3, 4, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 60], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 52, 8, 196, 0, 57, 0, 25]]
8
1,272
8
using System; using System.Linq; using System.Collections.Generic; class Dmondai { static void Main() { var line = Console.ReadLine().Split(' ').Select(x => Int32.Parse(x)).ToArray(); var N = line[0]; var K = line[1]; var list = new List<long[]>(); var netaAllCount = 0L; for (int i = 0; i < N; ++i) { var line1 = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); list.Add(new long[2] { line1[0], line1[1] }); } list = list.OrderBy(x => x[0]).ThenByDescending(x => x[1]).ToList(); for (int i = N - 1; 0 < i; --i) { if (list[i][0] != list[i - 1][0]) { list[i][0] = 1; netaAllCount++; } else list[i][0] = 0; } list[0][0] = 1; list = list.OrderByDescending(x => x[1]).ThenByDescending(x => x[0]).ToList(); var selectedList = list.Take(K).ToList(); var selectedNeta = 0L; var selectedOishisa = 0L; var stack = new Stack<long>(); for (int i = 0; i < K; ++i) { selectedNeta += selectedList[i][0]; selectedOishisa += selectedList[i][1]; if (selectedList[i][0] == 0) stack.Push(selectedList[i][1]); } var ans = selectedOishisa + selectedNeta * selectedNeta; for (int i = K; i < N; ++i) { if (list[i][0] == 0) continue; if (K <= selectedNeta || stack.Count() < 1 || netaAllCount <= selectedNeta) break; selectedNeta += list[i][0]; selectedOishisa = selectedOishisa + list[i][1] - stack.Pop(); var temp = selectedOishisa + selectedNeta * selectedNeta; ans = temp < ans ? ans : temp; } Console.WriteLine(ans); } }
using System; using System.Linq; using System.Collections.Generic; class Dmondai { static void Main() { var line = Console.ReadLine().Split(' ').Select(x => Int32.Parse(x)).ToArray(); var N = line[0]; var K = line[1]; var list = new List<long[]>(); var netaAllCount = 0L; for (int i = 0; i < N; ++i) { var line1 = Console.ReadLine().Split(' ').Select(x => Int64.Parse(x)).ToArray(); list.Add(new long[2] { line1[0], line1[1] }); } list = list.OrderBy(x => x[0]).ThenByDescending(x => x[1]).ToList(); for (int i = N - 1; 0 < i; --i) { if (list[i][0] != list[i - 1][0]) { list[i][0] = 1; netaAllCount++; } else list[i][0] = 0; } list[0][0] = 1; list = list.OrderByDescending(x => x[1]).ThenByDescending(x => x[0]).ToList(); var selectedList = list.Take(K).ToList(); var selectedNeta = 0L; var selectedOishisa = 0L; var stack = new Stack<long>(); for (int i = 0; i < K; ++i) { selectedNeta += selectedList[i][0]; selectedOishisa += selectedList[i][1]; if (selectedList[i][0] == 0) stack.Push(selectedList[i][1]); } var ans = selectedOishisa + selectedNeta * selectedNeta; for (int i = K; i < N; ++i) { if (list[i][0] == 0) continue; if (/*K <= selectedNeta ||*/ stack.Count() < 1 /*|| netaAllCount <= selectedNeta*/) break; selectedNeta += list[i][0]; selectedOishisa = selectedOishisa + list[i][1] - stack.Pop(); var temp = selectedOishisa + selectedNeta * selectedNeta; ans = temp < ans ? ans : temp; } Console.WriteLine(ans); } }
[["-", 0, 57, 15, 16, 31, 16, 31, 16, 31, 22], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 17, 19], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 12, 22], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 106], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 106], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 19], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 12, 22]]
8
501
8
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC116 { class Sushi { public long d; public int t; public bool top; } class D { static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]), K = int.Parse(s[1]); List<Sushi> list = new List<Sushi>(); for (int i = 0; i < N; i++) { string[] s0 = Console.ReadLine().Split(' '); int t = int.Parse(s0[0]); long d = long.Parse(s0[1]); Sushi sushi = new Sushi(); sushi.d = d; sushi.t = t; list.Add(sushi); } list = list.OrderByDescending(x => x.d).ToList(); //各t毎に最大のdを持つSushiをTopとして特別扱い HashSet<int> tset = new HashSet<int>(); for (int i = 0; i < N; i++) { Sushi sushi = list[i]; if (!tset.Contains(sushi.t)) { sushi.top = true; tset.Add(sushi.t); } } int indexA = K - 1; int indexB = K; var tlist = list.Take(K); long countType = tlist.GroupBy(x => x.t).Count(); long sum = tlist.Sum(x => x.d); long sd = sum + countType * countType; long smax = sd; while (true) { while (list[indexA].top) { indexA--; if (indexA < 0) goto Fin; } while (!list[indexB].top) { indexB++; if (indexB >= N) goto Fin; } sum += list[indexB].d - list[indexA].d; countType++; long sdn = sum + countType * countType; if (sdn > smax) smax = sdn; indexA--; indexB++; } Fin: Console.WriteLine(smax); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC116 { class Sushi { public long d; public int t; public bool top; } class D { static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]), K = int.Parse(s[1]); List<Sushi> list = new List<Sushi>(); for (int i = 0; i < N; i++) { string[] s0 = Console.ReadLine().Split(' '); int t = int.Parse(s0[0]); long d = long.Parse(s0[1]); Sushi sushi = new Sushi(); sushi.d = d; sushi.t = t; list.Add(sushi); } list = list.OrderByDescending(x => x.d).ToList(); //各t毎に最大のdを持つSushiをTopとして特別扱い HashSet<int> tset = new HashSet<int>(); for (int i = 0; i < N; i++) { Sushi sushi = list[i]; if (!tset.Contains(sushi.t)) { sushi.top = true; tset.Add(sushi.t); } } int indexA = K - 1; int indexB = K; var tlist = list.Take(K); long countType = tlist.GroupBy(x => x.t).Count(); long sum = tlist.Sum(x => x.d); long sd = sum + countType * countType; long smax = sd; while (indexA >= 0 && indexB < N) { while (list[indexA].top) { indexA--; if (indexA < 0) goto Fin; } while (!list[indexB].top) { indexB++; if (indexB >= N) goto Fin; } sum += list[indexB].d - list[indexA].d; countType++; long sdn = sum + countType * countType; if (sdn > smax) smax = sdn; indexA--; indexB++; } Fin: Console.WriteLine(smax); } } }
[["-", 0, 195, 8, 196, 0, 52, 15, 211, 0, 146], ["+", 8, 196, 0, 52, 15, 16, 31, 16, 31, 22], ["+", 8, 196, 0, 52, 15, 16, 31, 16, 17, 20], ["+", 8, 196, 0, 52, 15, 16, 31, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 17, 98], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 31, 22], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 17, 18], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 12, 22]]
8
465
8
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace AtCoder { class Program { private static string Read() { return Console.ReadLine(); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } struct Sushi { public readonly int t; public readonly long d; public Sushi(long[] V) { t = (int)V[0]; d = V[1]; } } static long Solve(int N, int K, Sushi[] sushis) { long sum = 0; var dic = new Dictionary<int, int>(); Array.Sort(sushis, (a, b) => b.d.CompareTo(a.d)); for (int i = 0; i < K; ++i) { if (!dic.ContainsKey(sushis[i].t)) { dic[sushis[i].t] = 0; } ++dic[sushis[i].t]; sum += sushis[i].d; } long max = sum + Math.BigMul(dic.Count, dic.Count); int p = K - 1, q = K; while (true) { while (p >= 0 && dic[sushis[p].t] == 1) { --p; } if (p < 0) { break; } sum -= sushis[p].d; --dic[sushis[p].t]; while (q < N && dic.ContainsKey(sushis[q].t)) { ++q; } if (q >= N) { break; } sum += sushis[q].d; dic[sushis[q].t] = 1; max = Math.Max(sum + Math.BigMul(dic.Count, dic.Count), max); } return max; } static void Main(string[] args) { int[] V = ReadInts(); int N = V[0], K = V[1]; var sushis = new Sushi[N]; for (int i = 0; i < N; ++i) { sushis[i] = new Sushi(ReadLongs()); } Console.WriteLine(Solve(N, K, sushis)); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace AtCoder { class Program { private static string Read() { return Console.ReadLine(); } private static int ReadInt() { return int.Parse(Read()); } private static long ReadLong() { return long.Parse(Read()); } private static double ReadDouble() { return double.Parse(Read()); } private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); } private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); } private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); } struct Sushi { public readonly int t; public readonly long d; public Sushi(long[] V) { t = (int)V[0]; d = V[1]; } } static long Solve(int N, int K, Sushi[] sushis) { long sum = 0; var dic = new Dictionary<int, int>(); Array.Sort(sushis, (a, b) => b.d.CompareTo(a.d)); for (int i = 0; i < K; ++i) { if (!dic.ContainsKey(sushis[i].t)) { dic[sushis[i].t] = 0; } ++dic[sushis[i].t]; sum += sushis[i].d; } long max = sum + Math.BigMul(dic.Count, dic.Count); int p = K - 1, q = K; while (true) { while (p >= 0 && dic[sushis[p].t] < 2) { --p; } if (p < 0) { break; } sum -= sushis[p].d; --dic[sushis[p].t]; --p; while (q < N && dic.ContainsKey(sushis[q].t)) { ++q; } if (q >= N) { break; } sum += sushis[q].d; dic[sushis[q].t] = 1; ++q; max = Math.Max(sum + Math.BigMul(dic.Count, dic.Count), max); } return max; } static void Main(string[] args) { int[] V = ReadInts(); int N = V[0], K = V[1]; var sushis = new Sushi[N]; for (int i = 0; i < N; ++i) { sushis[i] = new Sushi(ReadLongs()); } Console.WriteLine(Solve(N, K, sushis)); } } }
[["-", 8, 196, 0, 52, 15, 16, 12, 16, 17, 60], ["-", 8, 196, 0, 52, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 17, 18], ["+", 8, 196, 0, 52, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35], ["+", 0, 52, 8, 196, 0, 1, 0, 241, 0, 68], ["+", 0, 52, 8, 196, 0, 1, 0, 241, 0, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 241, 0, 29]]
8
607
10
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Console.WriteLine(Solve()); return; } public static string Solve() { string[] str = Console.ReadLine().Split(' '); int[] numbers = new int[4]; for (int i = 0; i < 4; i++) { numbers[i] = int.Parse(str[i]); } Array.Sort(numbers); Console.WriteLine("[{0}]", string.Join(", ", numbers)); if (numbers[0] == 1 && numbers[1] == 4 && numbers[2] == 7 && numbers[3] == 9) { return "YES"; } return "NO"; } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Console.WriteLine(Solve()); return; } public static string Solve() { string[] str = Console.ReadLine().Split(' '); int[] numbers = new int[4]; for (int i = 0; i < 4; i++) { numbers[i] = int.Parse(str[i]); } Array.Sort(numbers); // Console.WriteLine("[{0}]", string.Join(", ", numbers)); if (numbers[0] == 1 && numbers[1] == 4 && numbers[2] == 7 && numbers[3] == 9) { return "YES"; } return "NO"; } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 62], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 21], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 199], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["-", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
174
20
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { var keyence = "keyence"; var S = sc.ReadStr(); var ok = S.StartsWith(keyence) || S.EndsWith(keyence); if (ok) { Console.WriteLine("YES"); return; } for (int i = 1; i <= 6; i++) { var s = S.Substring(0, i); var e = S.Substring(S.Length - (7 - i), 7 - i); ok = S.StartsWith(s) && S.EndsWith(e); if (ok) break; } Console.WriteLine(ok ? "YES" : "NO"); } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Scanner sc = new Scanner(); } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { var keyence = "keyence"; var S = sc.ReadStr(); var ok = S.StartsWith(keyence) || S.EndsWith(keyence); if (ok) { Console.WriteLine("YES"); return; } for (int i = 1; i <= 6; i++) { var s = keyence.Substring(0, i); var e = keyence.Substring(i, 7 - i); ok = S.StartsWith(s) && S.EndsWith(e); if (ok) break; } Console.WriteLine(ok ? "YES" : "NO"); } static int MOD = (int)1e9 + 7; static int INF = (int)1e7 + 1; static long LINF = (long)1e13 + 1; static Scanner sc = new Scanner(); } class Scanner { string[] S = new string[0]; int Index = 0; char[] Separators = new char[] { ' ' }; public string Next() { if (this.Index < this.S.Length) return this.S[this.Index++]; var line = ""; while (line == "") line = Console.ReadLine(); this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries); if (this.S.Length == 0) return this.Next(); this.Index = 0; return this.S[this.Index++]; } public string ReadStr() { return this.Next(); } public char ReadChar() { return this.Next()[0]; } public int ReadInt() { return int.Parse(this.Next()); } public uint ReadUInt() { return uint.Parse(this.Next()); } public long ReadLong() { return long.Parse(this.Next()); } public double ReadDouble() { return double.Parse(this.Next()); } public int[] ReadIntArray(int n) { var array = new int[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadInt(); } return array; } public long[] ReadLongArray(int n) { var array = new long[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadLong(); } return array; } public double[] ReadDoubleArray(int n) { var array = new double[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadDouble(); } return array; } public char[] ReadCharArray(int n) { var array = new char[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadChar(); } return array; } public string[] ReadStrArray(int n) { var array = new string[n]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadStr(); } return array; } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 205, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 205, 22], ["-", 0, 200, 0, 212, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 16, 31, 214, 205, 22], ["-", 3, 4, 0, 28, 0, 16, 31, 214, 0, 131], ["-", 3, 4, 0, 28, 0, 16, 31, 214, 141, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["-", 0, 28, 0, 16, 12, 23, 0, 16, 31, 203], ["-", 0, 28, 0, 16, 12, 23, 0, 16, 17, 33], ["-", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
720
12
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(In)); } public long Min(params long[] a) { var min = a[0]; for (int i = 1; i < a.Length; i++) { if (min > a[i]) min = a[i]; } return min; } public long Max(params long[] a) { var max = a[0]; for (int i = 1; i < a.Length; i++) { if (max < a[i]) max = a[i]; } return max; } public void Solve(ConsoleInput input) { var s = input.Read.ToCharArray(); var ks = "keyence".ToCharArray(); for (var i = 0; i < s.Length; i++) { for (var j = 0; j < s.Length - i; j++) { var ss = s.ToList(); ss.RemoveRange(i, j); WriteLine(new string(ss.ToArray())); if (ss.SequenceEqual(ks)) { WriteLine("YES"); return; } } } WriteLine("NO"); // Solve(input); } private long Xxor(long x, long[] a) { long fx = 0; foreach (var ai in a) { fx += x ^ ai; } return fx; } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { new Program().Solve(new ConsoleInput(In)); } public long Min(params long[] a) { var min = a[0]; for (int i = 1; i < a.Length; i++) { if (min > a[i]) min = a[i]; } return min; } public long Max(params long[] a) { var max = a[0]; for (int i = 1; i < a.Length; i++) { if (max < a[i]) max = a[i]; } return max; } public void Solve(ConsoleInput input) { var s = input.Read.ToCharArray(); var ks = "keyence".ToCharArray(); for (var i = 0; i < s.Length; i++) { for (var j = 0; j < s.Length - i; j++) { var ss = s.ToList(); ss.RemoveRange(i, j); // WriteLine(new string(ss.ToArray())); if (ss.SequenceEqual(ks)) { WriteLine("YES"); return; } } } WriteLine("NO"); // Solve(input); } private long Xxor(long x, long[] a) { long fx = 0; foreach (var ai in a) { fx += x ^ ai; } return fx; } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 0, 7, 8, 196, 0, 1, 0, 213, 63, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 230, 0, 228], ["-", 0, 213, 3, 4, 0, 28, 0, 230, 39, 199], ["-", 3, 4, 0, 28, 0, 230, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["-", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25], ["-", 3, 4, 0, 28, 0, 230, 3, 4, 0, 25], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
685
13
using System; using System.Collections.Generic; using System.Linq; namespace KPCB { static class MainClass { public static void Main(string[] args) { string S = Console.ReadLine(); string K = "keyence"; var Klst = new List<string>(7) { "k", "e", "y", "e", "n", "c", "e" }; string ans = "NO"; for (int i = 0; i <= S.Length; i++) { for (int j = 0; j <= S.Length - i; j++) { string fpart = S.Substring(0, i); string lpart = S.Substring(i + j, S.Length - i - j); Console.WriteLine(fpart + lpart + " i: " + i + " j: " + j); if (fpart + lpart == K) { Console.WriteLine("YES"); return; } } } /* if (S.IndexOf(K,0)==0 || S.IndexOf(K, S.Length - K.Length) >= 0) { Console.WriteLine("YES"); return; } for (int i = 0; i < Klst.Count(); i++) { int fstidx = S.IndexOf(K.Substring(0,i+1)); int residx = S.IndexOf(K.Substring(i+1, K.Length - (i + 1)), fstidx + i + 1); int lastidx = residx + K.Length - (i + 1); if (fstidx >= 0 && residx >= 0&& fstidx == 0 && lastidx == S.Length) { ans = "YES"; } } */ Console.WriteLine(ans); return; } public static long Gcd(long a, long b) { long tmp; // a<bにする if (a >= b) { tmp = a; a = b; b = tmp; } if (a == 0) { return b; } tmp = a; a = b % a; b = tmp; return Gcd(a, b); } public static IEnumerable<long> PF(long n) { long i = 2; long tmp = n; while (i * i <= n) //※1 { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; //最後の素数も返す } } }
using System; using System.Collections.Generic; using System.Linq; namespace KPCB { static class MainClass { public static void Main(string[] args) { string S = Console.ReadLine(); string K = "keyence"; var Klst = new List<string>(7) { "k", "e", "y", "e", "n", "c", "e" }; string ans = "NO"; for (int i = 0; i <= S.Length; i++) { for (int j = 0; j <= S.Length - i; j++) { string fpart = S.Substring(0, i); string lpart = S.Substring(i + j, S.Length - i - j); if (fpart + lpart == K) { Console.WriteLine("YES"); return; } } } /* if (S.IndexOf(K,0)==0 || S.IndexOf(K, S.Length - K.Length) >= 0) { Console.WriteLine("YES"); return; } for (int i = 0; i < Klst.Count(); i++) { int fstidx = S.IndexOf(K.Substring(0,i+1)); int residx = S.IndexOf(K.Substring(i+1, K.Length - (i + 1)), fstidx + i + 1); int lastidx = residx + K.Length - (i + 1); if (fstidx >= 0 && residx >= 0&& fstidx == 0 && lastidx == S.Length) { ans = "YES"; } } */ Console.WriteLine(ans); return; } public static long Gcd(long a, long b) { long tmp; // a<bにする if (a >= b) { tmp = a; a = b; b = tmp; } if (a == 0) { return b; } tmp = a; a = b % a; b = tmp; return Gcd(a, b); } public static IEnumerable<long> PF(long n) { long i = 2; long tmp = n; while (i * i <= n) //※1 { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; //最後の素数も返す } } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 31, 16, 31, 16, 31, 16, 31, 16, 31, 22], ["-", 31, 16, 31, 16, 31, 16, 31, 16, 17, 72], ["-", 31, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["-", 0, 16, 31, 16, 31, 16, 31, 16, 17, 72], ["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 62], ["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 222], ["-", 0, 28, 0, 16, 31, 16, 31, 16, 17, 72], ["-", 0, 28, 0, 16, 31, 16, 31, 16, 12, 22], ["-", 3, 4, 0, 28, 0, 16, 31, 16, 17, 72], ["-", 0, 28, 0, 16, 31, 16, 12, 5, 0, 62], ["-", 0, 28, 0, 16, 31, 16, 12, 5, 0, 222], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
361
21
using System; using System.Text; using System.Collections.Generic; using System.Collections; using System.Linq; using E = System.Linq.Enumerable; using System.IO.Pipes; class Program { static char[] abc = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); static long mod = 1000000007; static void Main(string[] args) { // var S = Console.ReadLine(); // var A = long.Parse(Console.ReadLine()); var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long ans = 0; long sum = 0; var pq = new PriorityQueue<long>(); var plus = 0; var pp = new long[N]; for (int i = 0; i < N; i++) { var C = A[i] - B[i]; if (C < 0) { ans++; sum += C; } else if (C > 0) { pp[plus] = C; plus++; } } Array.Sort(pp); while (plus > 0 && sum < 0) { ans++; plus--; var top = pp[plus]; sum += top; } Console.WriteLine(sum >= 0 ? ans : -1); } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }
using System; using System.Text; using System.Collections.Generic; using System.Collections; using System.Linq; using E = System.Linq.Enumerable; using System.IO.Pipes; class Program { static char[] abc = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); static long mod = 1000000007; static void Main(string[] args) { // var S = Console.ReadLine(); // var A = long.Parse(Console.ReadLine()); var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var B = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long ans = 0; long sum = 0; var pq = new PriorityQueue<long>(); var plus = 0; var pp = new long[N]; for (int i = 0; i < N; i++) { var C = A[i] - B[i]; if (C < 0) { ans++; sum += C; } else if (C > 0) { pp[plus] = C; plus++; } } Array.Sort(pp); Array.Reverse(pp); var now = 0; while (plus > 0 && sum < 0) { ans++; plus--; var top = pp[now]; now++; sum += top; } Console.WriteLine(sum >= 0 ? ans : -1); } } public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) {} public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) {} public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 0, 212, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 212, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 8, 196, 0, 52, 8, 196, 0, 197, 0, 35], ["+", 0, 52, 8, 196, 0, 1, 0, 223, 0, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 223, 0, 29]]
8
752
17
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Numerics; using System.Xml.Schema; using System.Threading; using System.Diagnostics.CodeAnalysis; namespace AtCoder { public static class Ex { public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); } public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); } public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); } public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); } public static int BuildBitCount(this int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } } partial class AtCoder { public string GetStr() { return Console.ReadLine().Trim(); } public char GetChar() { return Console.ReadLine().Trim()[0]; } public int GetInt() { return int.Parse(Console.ReadLine().Trim()); } public long GetLong() { return long.Parse(Console.ReadLine().Trim()); } public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); } public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); } public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); } public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H) .Select(s => Enumerable.Repeat(value, W).ToArray()) .ToArray(); } public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetIntArray().ToArray()) .ToArray(); } public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetLongArray().ToArray()) .ToArray(); } public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetStr().ToCharArray()) .ToArray(); } public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); } Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<int>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; dic[a].Add(b); if (isDirected == false) dic[b].Add(a); } return dic; } Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<Edge>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; var c = input[2]; dic[a].Add(new Edge(b, c)); if (isDirected == false) dic[b].Add(new Edge(a, c)); } return dic; } bool eq<T, U>() => typeof(T).Equals(typeof(U)); T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s)) : eq<T, long>() ? ct<T, long>(long.Parse(s)) : eq<T, double>() ? ct<T, double>(double.Parse(s)) : eq<T, char>() ? ct<T, char>(s[0]) : ct<T, string>(s); void Multi<T>(out T a) => a = cv<T>(GetStr()); void Multi<T, U>(out T a, out U b) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); } void Multi<T, U, V>(out T a, out U b, out V c) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); } void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]); } } class Edge { public Edge(int to, long length) { To = to; Length = length; } public int To { get; set; } public long Length { get; set; } } partial class Program { static void Main() { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new AtCoder().Solve(); Console.Out.Flush(); Console.Read(); } } public partial class AtCoder { public void Solve() { int N = GetInt(); var A = GetLongArray(); var B = GetLongArray(); if (B.Sum() > A.Sum()) { Console.WriteLine(-1); return; } var small = Enumerable.Range(0, N) .Select(s => B[s] - A[s]) .Where(w => w > 0) .ToArray(); var cnt = small.Length; var req = small.Sum(); var big = Enumerable.Range(0, N) .Select(s => A[s] - B[s]) .Where(w => w > 0) .ToArray(); foreach (var b in big) { if (req <= 0) break; req -= b; cnt++; } Console.WriteLine(cnt); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.IO; using System.Linq; using System.Numerics; using System.Xml.Schema; using System.Threading; using System.Diagnostics.CodeAnalysis; namespace AtCoder { public static class Ex { public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); } public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); } public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); } public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); } public static int BuildBitCount(this int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } } partial class AtCoder { public string GetStr() { return Console.ReadLine().Trim(); } public char GetChar() { return Console.ReadLine().Trim()[0]; } public int GetInt() { return int.Parse(Console.ReadLine().Trim()); } public long GetLong() { return long.Parse(Console.ReadLine().Trim()); } public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); } public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); } public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); } public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H) .Select(s => Enumerable.Repeat(value, W).ToArray()) .ToArray(); } public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetIntArray().ToArray()) .ToArray(); } public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetLongArray().ToArray()) .ToArray(); } public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N) .Select(s => GetStr().ToCharArray()) .ToArray(); } public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); } Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<int>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; dic[a].Add(b); if (isDirected == false) dic[b].Add(a); } return dic; } Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed) { var dic = new Dictionary<int, List<Edge>>(); foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); } for (int i = 0; i < M; i++) { var input = GetIntArray(); var a = isNode_0indexed ? input[0] : input[0] - 1; var b = isNode_0indexed ? input[1] : input[1] - 1; var c = input[2]; dic[a].Add(new Edge(b, c)); if (isDirected == false) dic[b].Add(new Edge(a, c)); } return dic; } bool eq<T, U>() => typeof(T).Equals(typeof(U)); T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s)) : eq<T, long>() ? ct<T, long>(long.Parse(s)) : eq<T, double>() ? ct<T, double>(double.Parse(s)) : eq<T, char>() ? ct<T, char>(s[0]) : ct<T, string>(s); void Multi<T>(out T a) => a = cv<T>(GetStr()); void Multi<T, U>(out T a, out U b) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); } void Multi<T, U, V>(out T a, out U b, out V c) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); } void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]); } } class Edge { public Edge(int to, long length) { To = to; Length = length; } public int To { get; set; } public long Length { get; set; } } partial class Program { static void Main() { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new AtCoder().Solve(); Console.Out.Flush(); Console.Read(); } } public partial class AtCoder { public void Solve() { int N = GetInt(); var A = GetLongArray(); var B = GetLongArray(); if (B.Sum() > A.Sum()) { Console.WriteLine(-1); return; } var small = Enumerable.Range(0, N) .Select(s => B[s] - A[s]) .Where(w => w > 0) .ToArray(); var cnt = small.Length; var req = small.Sum(); var big = Enumerable.Range(0, N) .Select(s => A[s] - B[s]) .Where(w => w > 0) .OrderByDescending(o => o) .ToArray(); foreach (var b in big) { if (req <= 0) break; req -= b; cnt++; } Console.WriteLine(cnt); } } }
[["+", 0, 213, 63, 214, 205, 213, 63, 214, 141, 22], ["+", 0, 213, 63, 214, 205, 213, 3, 4, 0, 24], ["+", 0, 28, 0, 218, 54, 219, 0, 220, 141, 22], ["+", 205, 213, 3, 4, 0, 28, 0, 218, 0, 221], ["+", 205, 213, 3, 4, 0, 28, 0, 218, 8, 22], ["+", 0, 213, 63, 214, 205, 213, 3, 4, 0, 25], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 0, 131]]
8
2,020
7
using System; using System.Linq; namespace KEYPC2019_C { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split().Select(long.Parse).ToList(); var B = Console.ReadLine().Split().Select(long.Parse).ToList(); var dif = new long[N]; var check = new int[N]; for (int i = 0; i < N; i++) { dif[i] = A[i] - B[i]; check[i] = A[i] - B[i] >= 0 ? 1 : 0; } if (check.Sum() == 0) { Console.WriteLine(0); return; } dif = dif.OrderByDescending(x => x).ToArray(); var r = dif.Where(x => x < 0).Sum(); var cnt = dif.Count(d => d < 0); foreach (var i in dif) { r += i; cnt++; if (r >= 0) break; } Console.WriteLine(cnt); } } }
using System; using System.Linq; namespace KEYPC2019_C { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Split().Select(long.Parse).ToList(); var B = Console.ReadLine().Split().Select(long.Parse).ToList(); var dif = new long[N]; var check = new int[N]; for (int i = 0; i < N; i++) { dif[i] = A[i] - B[i]; check[i] = A[i] - B[i] >= 0 ? 0 : 1; } if (check.Sum() == 0) { Console.WriteLine(0); return; } if (dif.Sum() < 0) { Console.WriteLine(-1); return; } dif = dif.OrderByDescending(x => x).ToArray(); var r = dif.Where(x => x < 0).Sum(); var cnt = dif.Count(d => d < 0); foreach (var i in dif) { r += i; cnt++; if (r >= 0) break; } Console.WriteLine(cnt); } } }
[["-", 8, 196, 0, 1, 0, 11, 12, 41, 64, 203], ["-", 8, 196, 0, 1, 0, 11, 12, 41, 0, 102], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 0, 102], ["+", 8, 196, 0, 1, 0, 11, 12, 41, 75, 203], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 64, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 205, 22], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 0, 131], ["+", 0, 57, 15, 16, 31, 213, 63, 214, 141, 22], ["+", 0, 57, 15, 16, 31, 213, 3, 4, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 64, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 241, 0, 33], ["+", 0, 213, 3, 4, 0, 28, 0, 241, 0, 203]]
8
258
26
using System; using System.Collections.Generic; using static Assistant.Input; using static Assistant.Debug; using System.Linq; using Assistant; namespace Keyence2019D { class Program { static void Main(string[] args) { var N = RInt; var M = RInt; var A = RInts.Distinct().OrderBy(x => x).ToArray(); var B = RInts.Distinct().OrderBy(x => x).ToArray(); if (A.Length != N || B.Length != M) { Console.WriteLine(0); return; } Mlong ans = 1; for (int i = 1; i <= N * M; i++) { int ai = LowerBound(A, i); int bi = LowerBound(B, i); if (ai == N) ans = 0; if (bi == M) ans = 0; if (A[ai] == i && B[bi] == i) { } else if (A[ai] == i) { ans *= M - bi; } else if (B[bi] == i) { ans *= N - ai; } else { ans *= (N - ai) * (M - bi) - (N * M - i); } } Console.WriteLine(ans); } public static int LowerBound<T>(T[] a, T v) { return LowerBound(a, v, Comparer<T>.Default); } public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp) { var l = 0; var r = a.Length - 1; while (l <= r) { var mid = l + (r - l) / 2; var res = cmp.Compare(a[mid], v); if (res == -1) l = mid + 1; else r = mid - 1; } return l; } } } namespace Assistant { static class Input { static List<string> line = new List<string>(); static int index = 0; static String RNext() { if (line.Count <= index) line.AddRange(Console.ReadLine().Split()); return line[index++]; } public static int RInt => int.Parse(RNext()); public static long RLong => long.Parse(RNext()); public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray(); public static string RString => RNext(); //以下未テスト public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray(); public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray(); public static char[][] RMap(int h) => Enumerable.Repeat(0, h) .Select(x => Console.ReadLine().ToCharArray()) .ToArray(); } public struct Mlong { long _v; const long mod = 1000000007; public Mlong(long n = 0) : this() { _v = n >= mod || n < 0 ? (n % mod + mod) % mod : n; } public static implicit operator Mlong(long _x) => new Mlong(_x); public static implicit operator long(Mlong _x) => _x._v; public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; } public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; } public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod; public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod; public static long ModPow(long a, long n) { if (n == 0) return 1; else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod; else return ModPow(a * a % mod, n / 2); } static Mlong[] fac, finv, inv; public static void nCkInit(int max) { fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < max; i++) { fac[i] = fac[i - 1] * i; inv[i] = mod - inv[mod % i] * (mod / i); finv[i] = finv[i - 1] * inv[i]; } } public static Mlong nCk(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } } static class Debug { public static void Draw<T>(T[] array, int mode = 0) { #if DEBUG Console.WriteLine(string.Join(" ", array)); #endif } static public void Draw<T>(T[,] map, int mode = 1) { #if DEBUG int W = map.GetLength(0); int H = map.GetLength(1); string[,] map2 = new string[W + 1, H + 1]; for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (i == 0 && j == 0) map2[i, j] = 0.ToString(); else if (i == 0) map2[i, j] = (j - 1).ToString(); else if (j == 0) map2[i, j] = (i - 1).ToString(); else map2[i, j] = map[i - 1, j - 1].ToString(); } } for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (mode == 0) Console.Write(map2[i, j].Last()); if (mode == 1) Console.Write(map2[i, j] + " "); } Console.WriteLine(); } Console.WriteLine(); #endif } } }
using System; using System.Collections.Generic; using static Assistant.Input; using static Assistant.Debug; using System.Linq; using Assistant; namespace Keyence2019D { class Program { static void Main(string[] args) { var N = RInt; var M = RInt; var A = RInts.Distinct().OrderBy(x => x).ToArray(); var B = RInts.Distinct().OrderBy(x => x).ToArray(); if (A.Length != N || B.Length != M) { Console.WriteLine(0); return; } Mlong ans = 1; for (int i = 1; i <= N * M; i++) { int ai = LowerBound(A, i); int bi = LowerBound(B, i); if (ai == N || bi == M) { ans = 0; break; } if (A[ai] == i && B[bi] == i) { } else if (A[ai] == i) { ans *= M - bi; } else if (B[bi] == i) { ans *= N - ai; } else { ans *= (N - ai) * (M - bi) - (N * M - i); } } Console.WriteLine(ans); } public static int LowerBound<T>(T[] a, T v) { return LowerBound(a, v, Comparer<T>.Default); } public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp) { var l = 0; var r = a.Length - 1; while (l <= r) { var mid = l + (r - l) / 2; var res = cmp.Compare(a[mid], v); if (res == -1) l = mid + 1; else r = mid - 1; } return l; } } } namespace Assistant { static class Input { static List<string> line = new List<string>(); static int index = 0; static String RNext() { if (line.Count <= index) line.AddRange(Console.ReadLine().Split()); return line[index++]; } public static int RInt => int.Parse(RNext()); public static long RLong => long.Parse(RNext()); public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray(); public static string RString => RNext(); //以下未テスト public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray(); public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray(); public static char[][] RMap(int h) => Enumerable.Repeat(0, h) .Select(x => Console.ReadLine().ToCharArray()) .ToArray(); } public struct Mlong { long _v; const long mod = 1000000007; public Mlong(long n = 0) : this() { _v = n >= mod || n < 0 ? (n % mod + mod) % mod : n; } public static implicit operator Mlong(long _x) => new Mlong(_x); public static implicit operator long(Mlong _x) => _x._v; public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; } public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; } public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod; public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod; public static long ModPow(long a, long n) { if (n == 0) return 1; else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod; else return ModPow(a * a % mod, n / 2); } static Mlong[] fac, finv, inv; public static void nCkInit(int max) { fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < max; i++) { fac[i] = fac[i - 1] * i; inv[i] = mod - inv[mod % i] * (mod / i); finv[i] = finv[i - 1] * inv[i]; } } public static Mlong nCk(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } } static class Debug { public static void Draw<T>(T[] array, int mode = 0) { #if DEBUG Console.WriteLine(string.Join(" ", array)); #endif } static public void Draw<T>(T[,] map, int mode = 1) { #if DEBUG int W = map.GetLength(0); int H = map.GetLength(1); string[,] map2 = new string[W + 1, H + 1]; for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (i == 0 && j == 0) map2[i, j] = 0.ToString(); else if (i == 0) map2[i, j] = (j - 1).ToString(); else if (j == 0) map2[i, j] = (i - 1).ToString(); else map2[i, j] = map[i - 1, j - 1].ToString(); } } for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (mode == 0) Console.Write(map2[i, j].Last()); if (mode == 1) Console.Write(map2[i, j] + " "); } Console.WriteLine(); } Console.WriteLine(); #endif } } }
[["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25], ["-", 8, 196, 0, 57, 64, 1, 0, 11, 31, 22], ["-", 0, 57, 64, 1, 0, 11, 0, 202, 0, 32], ["-", 8, 196, 0, 57, 64, 1, 0, 11, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 64, 1, 0, 35], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 106], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 94], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 35], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
1,473
12
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; static void Main(string[] args) { try { SetOut(sw); Solve.Answer(); Out.Flush(); } catch (Exception ex) { WriteLine(ex.ToString()); Out.Flush(); } } } public class Solve { public static void Answer() { var N = IO.RInt; var H = IO.RInt; var W = IO.RInt; WriteLine((H - N + 1) * (W - N + 1)); } } public class IO { #region Input public static int RInt => int.Parse(ReadLine()); public static long RLong => long.Parse(ReadLine()); public static double RDouble => double.Parse(ReadLine()); public static string[] RSString => ReadLine().Split(); public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray(); public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray(); public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO"); #endregion } public class Mod { #region private readonly static long M = 1000000007; public static long Add(long a, long b) => (a * b) % M; public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; public static long Multi(long a, long b) => (a * b) % M; #endregion } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static System.Console; using static System.Math; namespace Atcoder { class Program { static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false }; static void Main(string[] args) { try { SetOut(sw); Solve.Answer(); Out.Flush(); } catch (Exception ex) { WriteLine(ex.ToString()); Out.Flush(); } } } public class Solve { public static void Answer() { var N = IO.RInt; var H = IO.RInt; var W = IO.RInt; WriteLine((N - H + 1) * (N - W + 1)); } } public class IO { #region Input public static int RInt => int.Parse(ReadLine()); public static long RLong => long.Parse(ReadLine()); public static double RDouble => double.Parse(ReadLine()); public static string[] RSString => ReadLine().Split(); public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray(); public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray(); public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray(); #endregion #region Output public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list)); public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No"); public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO"); #endregion } public class Mod { #region private readonly static long M = 1000000007; public static long Add(long a, long b) => (a * b) % M; public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M; public static long Multi(long a, long b) => (a * b) % M; #endregion } }
[["-", 0, 16, 31, 23, 0, 16, 31, 16, 31, 22], ["-", 0, 16, 31, 23, 0, 16, 31, 16, 17, 33], ["+", 0, 16, 31, 23, 0, 16, 31, 16, 17, 33], ["+", 0, 16, 31, 23, 0, 16, 31, 16, 12, 22], ["-", 0, 16, 12, 23, 0, 16, 31, 16, 31, 22], ["-", 0, 16, 12, 23, 0, 16, 31, 16, 17, 33], ["+", 0, 16, 12, 23, 0, 16, 31, 16, 17, 33], ["+", 0, 16, 12, 23, 0, 16, 31, 16, 12, 22]]
8
510
8
using System; class Program { static void Main() { int N = int.Parse(Console.ReadLine()); int H = int.Parse(Console.ReadLine()); int W = int.Parse(Console.ReadLine()); if (H == N && W == N) Console.WriteLine(1); else if (H == N && W != N) Console.WriteLine(N - W + 1); else if (H != N && W == N) Console.WriteLine(N - H + 1); else Console.WriteLine((N - H + 1) + (N - W + 1)); } }
using System; class Program { static void Main() { int N = int.Parse(Console.ReadLine()); int H = int.Parse(Console.ReadLine()); int W = int.Parse(Console.ReadLine()); if (H == N && W == N) Console.WriteLine(1); else if (H == N) Console.WriteLine(N - W + 1); else if (W == N) Console.WriteLine(N - H + 1); else Console.WriteLine((N - H + 1) * (N - W + 1)); } }
[["-", 8, 196, 0, 57, 75, 57, 15, 16, 17, 98], ["-", 0, 57, 75, 57, 15, 16, 12, 16, 31, 22], ["-", 0, 57, 75, 57, 15, 16, 12, 16, 17, 79], ["-", 0, 57, 75, 57, 15, 16, 12, 16, 12, 22], ["-", 75, 57, 75, 57, 15, 16, 31, 16, 31, 22], ["-", 75, 57, 75, 57, 15, 16, 31, 16, 17, 79], ["-", 75, 57, 75, 57, 15, 16, 31, 16, 12, 22], ["-", 0, 57, 75, 57, 75, 57, 15, 16, 17, 98], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48]]
8
139
10
using System; using System.Collections.Generic; using System.IO; using System.Linq; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class AISING2019D { static public int numberOfRandomCases = 0; static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker) {} static public void Solve() { var N = NN; var Q = NN; var AList = NNList(N).ToList(); var XList = NNList(Q); var takahasiList = new List<long>(); for (var i = 0; i < N; i++) { if (i % 2 == 0) takahasiList.Add(AList[(int)N - i - 1]); } takahasiList.Reverse(); var ruisekiTakahasiList = new long[takahasiList.Count + 1]; for (var i = 1; i <= takahasiList.Count; i++) { ruisekiTakahasiList[i] = ruisekiTakahasiList[i - 1] + takahasiList[i - 1]; } var AListRevRuiseki = new long[N + 1]; var AListRev = AList.ToArray().Reverse().ToArray(); for (var i = 1; i <= N; i++) { AListRevRuiseki[i] = AListRevRuiseki[i - 1] + AListRev[i - 1]; } foreach (var item in XList) { var idx = AList.BinarySearch(item); if (idx < 0) { var tmp = ~idx; if (tmp == N) idx = (int)N - 1; else if (tmp == 0) idx = 0; else { var a = AList[tmp]; var b = AList[tmp - 1]; if (Abs(b - item) <= Abs(a - item)) idx = tmp - 1; else idx = tmp; } } var left = idx; var right = N; while (right - left > 1) { var mid = (right + left) / 2; var left2 = 0; var right2 = idx; while (right2 - left2 > 1) { var mid2 = (right2 + left2) / 2; if (AList[mid2] - item > item - AList[(int)mid]) left2 = mid2; else right2 = mid2; } if (mid - right2 + 1 >= N - mid) right = mid; else left = (int)mid; } var firstCnt = N - right; Console.WriteLine(AListRevRuiseki[firstCnt] + ruisekiTakahasiList[takahasiList.Count - firstCnt]); } } static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); } static class Console_ { static Queue<string> param = new Queue<string>(); public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); } } static long NN => long.Parse(Console_.NextString()); static double ND => double.Parse(Console_.NextString()); static string NS => Console_.NextString(); static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred); static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); static IEnumerable<long> Primes(long x) { if (x < 2) yield break; yield return 2; var halfx = x / 2; var table = new bool[halfx + 1]; var max = (long)(Math.Sqrt(x) / 2); for (long i = 1; i <= max; ++i) { if (table[i]) continue; var add = 2 * i + 1; yield return add; for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true; } for (long i = max + 1; i <= halfx; ++i) if (!table[i] && 2 * i + 1 <= x) yield return 2 * i + 1; } static IEnumerable<long> Factors(long x) { if (x < 2) yield break; while (x % 2 == 0) { x /= 2; yield return 2; } var max = (long)Math.Sqrt(x); for (long i = 3; i <= max; i += 2) while (x % i == 0) { x /= i; yield return i; } if (x != 1) yield return x; } static IEnumerable<long> Divisor(long x) { if (x < 1) yield break; var max = (long)Math.Sqrt(x); for (long i = 1; i <= max; ++i) { if (x % i != 0) continue; yield return i; if (i != x / i) yield return x / i; } } static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } } static IEnumerator<uint> _xsi = _xsc(); static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } static long GCD(long a, long b) { while (b > 0) { var tmp = b; b = a % b; a = tmp; } return a; } static long LCM(long a, long b) => a * b / GCD(a, b); static Mod Pow(Mod x, long y) { Mod a = 1; while (y != 0) { if ((y & 1) == 1) a.Mul(x); x.Mul(x); y >>= 1; } return a; } static long Pow(long x, long y) { long a = 1; while (y != 0) { if ((y & 1) == 1) a *= x; x *= x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static long Comb(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static long Perm(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } static Func<T1, TR> Lambda<T1, TR>(Func<T1, Func<T1, TR>, TR> f) { Func<T1, TR> t = x1 => default(TR); return t = x1 => f(x1, t); } static Func<T1, T2, TR> Lambda<T1, T2, TR>(Func<T1, T2, Func<T1, T2, TR>, TR> f) { Func<T1, T2, TR> t = (x1, x2) => default(TR); return t = (x1, x2) => f(x1, x2, t); } static Func<T1, T2, T3, TR> Lambda<T1, T2, T3, TR>(Func<T1, T2, T3, Func<T1, T2, T3, TR>, TR> f) { Func<T1, T2, T3, TR> t = (x1, x2, x3) => default(TR); return t = (x1, x2, x3) => f(x1, x2, x3, t); } static Func<T1, T2, T3, T4, TR> Lambda<T1, T2, T3, T4, TR>( Func<T1, T2, T3, T4, Func<T1, T2, T3, T4, TR>, TR> f) { Func<T1, T2, T3, T4, TR> t = (x1, x2, x3, x4) => default(TR); return t = (x1, x2, x3, x4) => f(x1, x2, x3, x4, t); } static List<T> LCS<T>(T[] s, T[] t) where T : IEquatable<T> { int sl = s.Length, tl = t.Length; var dp = new int[sl + 1, tl + 1]; for (var i = 0; i < sl; i++) for (var j = 0; j < tl; j++) dp[i + 1, j + 1] = s[i].Equals(t[j]) ? dp[i, j] + 1 : Max(dp[i + 1, j], dp[i, j + 1]); { var r = new List<T>(); int i = sl, j = tl; while (i > 0 && j > 0) if (s[--i].Equals(t[--j])) r.Add(s[i]); else if (dp[i, j + 1] > dp[i + 1, j]) ++j; else ++i; r.Reverse(); return r; } } static long LIS<T>(T[] array, bool strict) { var l = new List<T>(); foreach (var e in array) { var i = l.BinarySearch(e); if (i < 0) i = ~i; else if (!strict) ++i; if (i == l.Count) l.Add(e); else l[i] = e; } return l.Count; } class PQ<T> { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } public PQ(int cap, bool asc = true) : this(cap, Comparer<T>.Default.Compare, asc) {} public PQ(bool asc = true) : this(Comparer<T>.Default.Compare, asc) {} public void Push(T v) { var i = h.Count; h.Add(v); while (i > 0) { var ni = (i - 1) / 2; if (c(v, h[ni]) >= 0) break; h[i] = h[ni]; i = ni; } h[i] = v; } public T Pop() { var r = h[0]; var v = h[h.Count - 1]; h.RemoveAt(h.Count - 1); if (h.Count == 0) return r; var i = 0; while (i * 2 + 1 < h.Count) { var i1 = i * 2 + 1; var i2 = i * 2 + 2; if (i2 < h.Count && c(h[i1], h[i2]) > 0) i1 = i2; if (c(v, h[i1]) <= 0) break; h[i] = h[i1]; i = i1; } h[i] = v; return r; } } class PQ<TK, TV> { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; public PQ(int cap, Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } public PQ(int cap, bool asc = true) : this(cap, Comparer<TK>.Default.Compare, asc) {} public PQ(bool asc = true) : this(Comparer<TK>.Default.Compare, asc) {} public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; public UF(long s) { d = Repeat(-1L, s).ToArray(); } public bool Unite(long x, long y) { x = Root(x); y = Root(y); if (x != y) { if (d[y] < d[x]) { var t = y; y = x; x = t; } d[x] += d[y]; d[y] = x; } return x != y; } public bool IsSame(long x, long y) => Root(x) == Root(y); public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); public long Count(long x) => -d[Root(x)]; } struct Mod : IEquatable<Mod>, IEquatable<long> { static public long _mod = 1000000007; long v; public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; } static public implicit operator Mod(long x) => new Mod(x); static public implicit operator long(Mod x) => x.v; public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; } public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; } public void Mul(Mod x) => v = (v * x.v) % _mod; public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod; static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; } static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; } static public Mod operator *(Mod x, Mod y) => x.v * y.v; static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v); static public bool operator ==(Mod x, Mod y) => x.v == y.v; static public bool operator !=(Mod x, Mod y) => x.v != y.v; static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; } public bool Equals(Mod x) => v == x.v; public bool Equals(long x) => v == x; public override bool Equals(object x) => x == null ? false : Equals((Mod)x); public override int GetHashCode() => v.GetHashCode(); public override string ToString() => v.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } } class Tree { long N; int l; List<long>[] p; int[] d; long[][] pr; long r; Tuple<long, long, int>[] e; Tuple<long, long>[] b; bool lca; bool euler; bool bfs; public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } public Tuple<long, long>[] BFSRoot() { if (!bfs) { var nb = new List<Tuple<long, long>>(); var q = new Queue<long>(); var d = new bool[N]; nb.Add(Tuple.Create(r, -1L)); d[r] = true; q.Enqueue(r); while (q.Count > 0) { var w = q.Dequeue(); foreach (var i in p[w]) { if (d[i]) continue; d[i] = true; q.Enqueue(i); nb.Add(Tuple.Create(i, w)); } } b = nb.ToArray(); bfs = true; } return b; } public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); public Tuple<long, long, int>[] Euler() { if (!euler) { var ne = new List<Tuple<long, long, int>>(); var s = new Stack<Tuple<long, long>>(); var d = new bool[N]; d[r] = true; s.Push(Tuple.Create(r, -1L)); while (s.Count > 0) { var w = s.Peek(); var ad = true; foreach (var i in p[w.Item1]) { if (d[i]) continue; d[i] = true; ad = false; s.Push(Tuple.Create(i, w.Item1)); } if (!ad || p[w.Item1].Count == 1) ne.Add(Tuple.Create(w.Item1, w.Item2, 1)); if (ad) { s.Pop(); ne.Add(Tuple.Create(w.Item1, w.Item2, -1)); } } e = ne.ToArray(); euler = true; } return e; } public long LCA(long u, long v) { if (!lca) { l = 0; while (N > (1 << l)) l++; d = new int[N]; pr = Repeat(0, l).Select(_ => new long[N]).ToArray(); d[r] = 0; pr[0][r] = -1; var q = new Stack<long>(); q.Push(r); while (q.Count > 0) { var w = q.Pop(); foreach (var i in p[w]) { if (i == pr[0][w]) continue; q.Push(i); d[i] = d[w] + 1; pr[0][i] = w; } } for (var k = 0; k + 1 < l; k++) for (var w = 0; w < N; w++) if (pr[k][w] < 0) pr[k + 1][w] = -1; else pr[k + 1][w] = pr[k][pr[k][w]]; lca = true; } if (d[u] > d[v]) { var t = u; u = v; v = t; } for (var k = 0; k < l; k++) if ((((d[v] - d[u]) >> k) & 1) != 0) v = pr[k][v]; if (u == v) return u; for (var k = l - 1; k >= 0; k--) if (pr[k][u] != pr[k][v]) { u = pr[k][u]; v = pr[k][v]; } return pr[0][u]; } } class BT<T> where T : IComparable { class Node { public Node l; public Node r; public T v; public bool b; public int c; } Comparison<T> c; Node r; bool ch; T lm; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} bool R(Node n) => n != null && !n.b; bool B(Node n) => n != null && n.b; long C(Node n) => n?.c ?? 0; Node RtL(Node n) { Node m = n.r, t = m.l; m.l = n; n.r = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtR(Node n) { Node m = n.l, t = m.r; m.r = n; n.l = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } public void Add(T x) { r = A(r, x); r.b = true; } Node A(Node n, T x) { if (n == null) { ch = true; return new Node() { v = x, c = 1 }; } if (c(x, n.v) < 0) n.l = A(n.l, x); else n.r = A(n.r, x); n.c++; return Bl(n); } Node Bl(Node n) { if (!ch) return n; if (!B(n)) return n; if (R(n.l) && R(n.l.l)) { n = RtR(n); n.l.b = true; } else if (R(n.l) && R(n.l.r)) { n = RtLR(n); n.l.b = true; } else if (R(n.r) && R(n.r.l)) { n = RtRL(n); n.r.b = true; } else if (R(n.r) && R(n.r.r)) { n = RtL(n); n.r.b = true; } else ch = false; return n; } public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } Node Rm(Node n, T x) { if (n == null) { ch = false; return n; } n.c--; var r = c(x, n.v); if (r < 0) { n.l = Rm(n.l, x); return BlL(n); } if (r > 0) { n.r = Rm(n.r, x); return BlR(n); } if (n.l == null) { ch = n.b; return n.r; } n.l = RmM(n.l); n.v = lm; return BlL(n); } Node RmM(Node n) { n.c--; if (n.r != null) { n.r = RmM(n.r); return BlR(n); } lm = n.v; ch = n.b; return n.l; } Node BlL(Node n) { if (!ch) return n; if (B(n.r) && R(n.r.l)) { var b = n.b; n = RtRL(n); n.b = b; n.l.b = true; ch = false; } else if (B(n.r) && R(n.r.r)) { var b = n.b; n = RtL(n); n.b = b; n.r.b = true; n.l.b = true; ch = false; } else if (B(n.r)) { ch = n.b; n.b = true; n.r.b = false; } else { n = RtL(n); n.b = true; n.l.b = false; n.l = BlL(n.l); ch = false; } return n; } Node BlR(Node n) { if (!ch) return n; if (B(n.l) && R(n.l.r)) { var b = n.b; n = RtLR(n); n.b = b; n.r.b = true; ch = false; } else if (B(n.l) && R(n.l.l)) { var b = n.b; n = RtR(n); n.b = b; n.l.b = true; n.r.b = true; ch = false; } else if (B(n.l)) { ch = n.b; n.b = true; n.l.b = false; } else { n = RtR(n); n.b = true; n.r.b = false; n.r = BlR(n.r); ch = false; } return n; } public T this[long i] { get { return At(r, i); } } T At(Node n, long i) { if (n == null) return default(T); if (n.l == null) if (i == 0) return n.v; else return At(n.r, i - 1); if (n.l.c == i) return n.v; if (n.l.c > i) return At(n.l, i); return At(n.r, i - n.l.c - 1); } public bool Have(T x) { var t = FindUpper(x); return t < C(r) && c(At(r, t), x) == 0; } public long FindUpper(T x, bool allowSame = true) => allowSame ? FL(r, x) + 1 : FU(r, x); long FU(Node n, T x) { if (n == null) return 0; var r = c(x, n.v); if (r < 0) return FU(n.l, x); return C(n.l) + 1 + FU(n.r, x); } public long FindLower(T x, bool allowSame = true) { var t = FL(r, x); if (allowSame) return t + 1 < C(r) && c(At(r, t + 1), x) == 0 ? t + 1 : t < 0 ? C(r) : t; return t < 0 ? C(r) : t; } long FL(Node n, T x) { if (n == null) return -1; var r = c(x, n.v); if (r > 0) return C(n.l) + 1 + FL(n.r, x); return FL(n.l, x); } public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } public bool Any() => r != null; public long Count() => C(r); public IEnumerable<T> List() => L(r); IEnumerable<T> L(Node n) { if (n == null) yield break; foreach (var i in L(n.l)) yield return i; yield return n.v; foreach (var i in L(n.r)) yield return i; } } class Dict<K, V> : Dictionary<K, V> { Func<K, V> d; public Dict(Func<K, V> _d) { d = _d; } public Dict() : this(_ => default(V)) {} new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = d(i); } set { base[i] = value; } } public override int GetHashCode() { var ret = 0; for (var e = GetEnumerator(); e.MoveNext();) { ret ^= e.Current.Key.GetHashCode(); ret ^= e.Current.Value.GetHashCode(); } return ret; } public override bool Equals(object obj) { var o = (Dict<K, V>)obj; foreach (var kv in o) { if (!ContainsKey(kv.Key) || !this[kv.Key].Equals(kv.Value)) return false; } return o.Count == Count; } } class Deque<T> { T[] b; int o, c; public int Count; public T this[int i] { get { return b[gi(i)]; } set { b[gi(i)] = value; } } public Deque(int cap = 16) { b = new T[c = cap]; } int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } public T PopFront() { if (Count-- == 0) throw new Exception(); var r = b[o++]; if (o >= c) o -= c; return r; } public T Front => b[o]; public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; void e() { T[] nb = new T[c << 1]; if (o > c - Count) { var l = b.Length - o; Array.Copy(b, o, nb, 0, l); Array.Copy(b, 0, nb, l, Count - l); } else Array.Copy(b, o, nb, 0, Count); b = nb; o = 0; c <<= 1; } public void Insert(int i, T x) { if (i > Count) throw new Exception(); this.PushFront(x); for (int j = 0; j < i; j++) this[j] = this[j + 1]; this[i] = x; } public T RemoveAt(int i) { if (i < 0 || i >= Count) throw new Exception(); var r = this[i]; for (int j = i; j > 0; j--) this[j] = this[j - 1]; this.PopFront(); return r; } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; public SegTree(long _n, T _ti, Func<T, T, T> _f) { n = 1; while (n < _n) n <<= 1; ti = _ti; f = _f; dat = Repeat(ti, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public SegTree(List<T> l, T _ti, Func<T, T, T> _f) : this(l.Count, _ti, _f) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public void Update(long i, T v) { dat[i += n] = v; while ((i >>= 1) > 0) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public T Query(long l, long r) { var vl = ti; var vr = ti; for (long li = n + l, ri = n + r; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, dat[li++]); if ((ri & 1) == 1) vr = f(dat[--ri], vr); } return f(vl, vr); } public T this[long idx] { get { return dat[idx + n]; } set { Update(idx, value); } } } class LazySegTree<T, E> { int n, height; T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; T[] dat; E[] laz; public LazySegTree(long _n, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { n = 1; height = 0; while (n < _n) { n <<= 1; ++height; } ti = _ti; ei = _ei; f = _f; g = _g; h = _h; dat = Repeat(ti, n << 1).ToArray(); laz = Repeat(ei, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public LazySegTree(List<T> l, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) : this(l.Count, _ti, _ei, _f, _g, _h) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); void Eval(long i) { if (laz[i].Equals(ei)) return; laz[(i << 1) | 0] = h(laz[(i << 1) | 0], laz[i]); laz[(i << 1) | 1] = h(laz[(i << 1) | 1], laz[i]); dat[i] = Reflect(i); laz[i] = ei; } void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } public void Update(long l, long r, E v) { Thrust(l += n); Thrust(r += n - 1); for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) { laz[li] = h(laz[li], v); ++li; } if ((ri & 1) == 1) { --ri; laz[ri] = h(laz[ri], v); } } Recalc(l); Recalc(r); } public T Query(long l, long r) { Thrust(l += n); Thrust(r += n - 1); var vl = ti; var vr = ti; for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, Reflect(li++)); if ((ri & 1) == 1) vr = f(Reflect(--ri), vr); } return f(vl, vr); } public T this[long idx] { get { Thrust(idx += n); return dat[idx] = Reflect(idx); } set { Thrust(idx += n); dat[idx] = value; laz[idx] = ei; Recalc(idx); } } } class SlidingWindowAggregation<T> { T[][] l; T[][] a; int[] n; Func<T, T, T> f; public SlidingWindowAggregation(IEnumerable<T> ary, Func<T, T, T> _f) { l = new T[2][]; a = new T[2][]; n = new int[2]; f = _f; l[0] = new T[16]; a[0] = new T[16]; n[0] = 0; if (ary.Any()) { var t = ary.ToArray(); n[1] = t.Length; l[1] = new T[n[1]]; a[1] = new T[n[1]]; for (var i = 0; i < l[1].Length; i++) { l[1][i] = t[i]; if (i == 0) a[1][i] = t[i]; else a[1][i] = this.f(a[1][i - 1], t[i]); } } else { l[1] = new T[16]; a[1] = new T[16]; n[1] = 0; } } public SlidingWindowAggregation(Func<T, T, T> f) : this(new T[0], f) {} public int Count => n[0] + n[1]; void Push(int la, T v) { if (l[la].Length == n[la]) { var nar = new T[n[la] * 2]; var nag = new T[n[la] * 2]; Array.Copy(l[la], nar, n[la]); Array.Copy(a[la], nag, n[la]); l[la] = nar; a[la] = nag; } if (n[la] == 0) a[la][0] = v; else a[la][n[la]] = la == 0 ? f(v, a[la][n[la] - 1]) : f(a[la][n[la] - 1], v); l[la][n[la]++] = v; } public void PushBack(T val) => Push(1, val); public void PushFront(T val) => Push(0, val); public T Pop(int la) { var lb = 1 - la; if (n[la] == 0) { if (n[lb] == 0) throw new Exception(); var m = (n[lb] - 1) / 2; if (l[la].Length <= m) { l[la] = new T[m + 1]; a[la] = new T[m + 1]; } n[la] = 0; for (var i = m; i >= 0; i--) { if (n[la] == 0) a[la][n[la]] = l[lb][i]; else a[la][n[la]] = la == 0 ? f(l[lb][i], a[la][n[la] - 1]) : f(a[la][n[la] - 1], l[lb][i]); l[la][n[la]++] = l[lb][i]; } for (var i = m + 1; i < n[lb]; i++) { var j = i - m - 1; if (j == 0) a[lb][j] = l[lb][i]; else a[lb][j] = la == 0 ? f(l[lb][i], a[lb][j - 1]) : f(a[lb][j - 1], l[lb][i]); l[lb][j] = l[lb][i]; } n[lb] -= n[la]; } return l[la][--n[la]]; } public T PopBack() => Pop(1); public T PopFront() => Pop(0); public T Aggregate() { if (n[0] == 0 && n[1] == 0) throw new Exception(); else if (n[1] == 0) return a[0][n[0] - 1]; else if (n[0] == 0) return a[1][n[1] - 1]; else return f(a[0][n[0] - 1], a[1][n[1] - 1]); } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class AISING2019D { static public int numberOfRandomCases = 0; static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker) {} static public void Solve() { var N = NN; var Q = NN; var AList = NNList(N).ToList(); var XList = NNList(Q); var takahasiList = new List<long>(); for (var i = 0; i < N; i++) { if (i % 2 == 0) takahasiList.Add(AList[(int)N - i - 1]); } takahasiList.Reverse(); var ruisekiTakahasiList = new long[takahasiList.Count + 1]; for (var i = 1; i <= takahasiList.Count; i++) { ruisekiTakahasiList[i] = ruisekiTakahasiList[i - 1] + takahasiList[i - 1]; } var AListRevRuiseki = new long[N + 1]; var AListRev = AList.ToArray().Reverse().ToArray(); for (var i = 1; i <= N; i++) { AListRevRuiseki[i] = AListRevRuiseki[i - 1] + AListRev[i - 1]; } foreach (var item in XList) { var idx = AList.BinarySearch(item); if (idx < 0) { var tmp = ~idx; if (tmp == N) idx = (int)N - 1; else if (tmp == 0) idx = 0; else { var a = AList[tmp]; var b = AList[tmp - 1]; if (Abs(b - item) <= Abs(a - item)) idx = tmp - 1; else idx = tmp; } } var left = idx - 1; var right = N - 1; while (right - left > 1) { var mid = (right + left + 1) / 2; var left2 = 0; var right2 = idx + 1; while (right2 - left2 > 1) { var mid2 = (right2 + left2) / 2; if (Abs(AList[idx - mid2] - item) <= Abs(item - AList[(int)mid])) left2 = mid2; else right2 = mid2; } if (mid - idx + left2 + 1 >= N - mid) right = mid; else left = (int)mid; } var firstCnt = N - right; Console.WriteLine(AListRevRuiseki[firstCnt] + ruisekiTakahasiList[takahasiList.Count - firstCnt]); } } static public void Main(string[] args) { if (args.Length == 0) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); } static class Console_ { static Queue<string> param = new Queue<string>(); public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); } } static long NN => long.Parse(Console_.NextString()); static double ND => double.Parse(Console_.NextString()); static string NS => Console_.NextString(); static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred); static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); static IEnumerable<long> Primes(long x) { if (x < 2) yield break; yield return 2; var halfx = x / 2; var table = new bool[halfx + 1]; var max = (long)(Math.Sqrt(x) / 2); for (long i = 1; i <= max; ++i) { if (table[i]) continue; var add = 2 * i + 1; yield return add; for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true; } for (long i = max + 1; i <= halfx; ++i) if (!table[i] && 2 * i + 1 <= x) yield return 2 * i + 1; } static IEnumerable<long> Factors(long x) { if (x < 2) yield break; while (x % 2 == 0) { x /= 2; yield return 2; } var max = (long)Math.Sqrt(x); for (long i = 3; i <= max; i += 2) while (x % i == 0) { x /= i; yield return i; } if (x != 1) yield return x; } static IEnumerable<long> Divisor(long x) { if (x < 1) yield break; var max = (long)Math.Sqrt(x); for (long i = 1; i <= max; ++i) { if (x % i != 0) continue; yield return i; if (i != x / i) yield return x / i; } } static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } } static IEnumerator<uint> _xsi = _xsc(); static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } static long GCD(long a, long b) { while (b > 0) { var tmp = b; b = a % b; a = tmp; } return a; } static long LCM(long a, long b) => a * b / GCD(a, b); static Mod Pow(Mod x, long y) { Mod a = 1; while (y != 0) { if ((y & 1) == 1) a.Mul(x); x.Mul(x); y >>= 1; } return a; } static long Pow(long x, long y) { long a = 1; while (y != 0) { if ((y & 1) == 1) a *= x; x *= x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static long Comb(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static long Perm(long n, long k) { _B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } static Func<T1, TR> Lambda<T1, TR>(Func<T1, Func<T1, TR>, TR> f) { Func<T1, TR> t = x1 => default(TR); return t = x1 => f(x1, t); } static Func<T1, T2, TR> Lambda<T1, T2, TR>(Func<T1, T2, Func<T1, T2, TR>, TR> f) { Func<T1, T2, TR> t = (x1, x2) => default(TR); return t = (x1, x2) => f(x1, x2, t); } static Func<T1, T2, T3, TR> Lambda<T1, T2, T3, TR>(Func<T1, T2, T3, Func<T1, T2, T3, TR>, TR> f) { Func<T1, T2, T3, TR> t = (x1, x2, x3) => default(TR); return t = (x1, x2, x3) => f(x1, x2, x3, t); } static Func<T1, T2, T3, T4, TR> Lambda<T1, T2, T3, T4, TR>( Func<T1, T2, T3, T4, Func<T1, T2, T3, T4, TR>, TR> f) { Func<T1, T2, T3, T4, TR> t = (x1, x2, x3, x4) => default(TR); return t = (x1, x2, x3, x4) => f(x1, x2, x3, x4, t); } static List<T> LCS<T>(T[] s, T[] t) where T : IEquatable<T> { int sl = s.Length, tl = t.Length; var dp = new int[sl + 1, tl + 1]; for (var i = 0; i < sl; i++) for (var j = 0; j < tl; j++) dp[i + 1, j + 1] = s[i].Equals(t[j]) ? dp[i, j] + 1 : Max(dp[i + 1, j], dp[i, j + 1]); { var r = new List<T>(); int i = sl, j = tl; while (i > 0 && j > 0) if (s[--i].Equals(t[--j])) r.Add(s[i]); else if (dp[i, j + 1] > dp[i + 1, j]) ++j; else ++i; r.Reverse(); return r; } } static long LIS<T>(T[] array, bool strict) { var l = new List<T>(); foreach (var e in array) { var i = l.BinarySearch(e); if (i < 0) i = ~i; else if (!strict) ++i; if (i == l.Count) l.Add(e); else l[i] = e; } return l.Count; } class PQ<T> { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } public PQ(int cap, bool asc = true) : this(cap, Comparer<T>.Default.Compare, asc) {} public PQ(bool asc = true) : this(Comparer<T>.Default.Compare, asc) {} public void Push(T v) { var i = h.Count; h.Add(v); while (i > 0) { var ni = (i - 1) / 2; if (c(v, h[ni]) >= 0) break; h[i] = h[ni]; i = ni; } h[i] = v; } public T Pop() { var r = h[0]; var v = h[h.Count - 1]; h.RemoveAt(h.Count - 1); if (h.Count == 0) return r; var i = 0; while (i * 2 + 1 < h.Count) { var i1 = i * 2 + 1; var i2 = i * 2 + 2; if (i2 < h.Count && c(h[i1], h[i2]) > 0) i1 = i2; if (c(v, h[i1]) <= 0) break; h[i] = h[i1]; i = i1; } h[i] = v; return r; } } class PQ<TK, TV> { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; public PQ(int cap, Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>(cap, (x, y) => c(x.Item1, y.Item1), asc); } public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } public PQ(int cap, bool asc = true) : this(cap, Comparer<TK>.Default.Compare, asc) {} public PQ(bool asc = true) : this(Comparer<TK>.Default.Compare, asc) {} public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; public UF(long s) { d = Repeat(-1L, s).ToArray(); } public bool Unite(long x, long y) { x = Root(x); y = Root(y); if (x != y) { if (d[y] < d[x]) { var t = y; y = x; x = t; } d[x] += d[y]; d[y] = x; } return x != y; } public bool IsSame(long x, long y) => Root(x) == Root(y); public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); public long Count(long x) => -d[Root(x)]; } struct Mod : IEquatable<Mod>, IEquatable<long> { static public long _mod = 1000000007; long v; public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; } static public implicit operator Mod(long x) => new Mod(x); static public implicit operator long(Mod x) => x.v; public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; } public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; } public void Mul(Mod x) => v = (v * x.v) % _mod; public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod; static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; } static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; } static public Mod operator *(Mod x, Mod y) => x.v * y.v; static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v); static public bool operator ==(Mod x, Mod y) => x.v == y.v; static public bool operator !=(Mod x, Mod y) => x.v != y.v; static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; } public bool Equals(Mod x) => v == x.v; public bool Equals(long x) => v == x; public override bool Equals(object x) => x == null ? false : Equals((Mod)x); public override int GetHashCode() => v.GetHashCode(); public override string ToString() => v.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; } static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; } } class Tree { long N; int l; List<long>[] p; int[] d; long[][] pr; long r; Tuple<long, long, int>[] e; Tuple<long, long>[] b; bool lca; bool euler; bool bfs; public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } public Tuple<long, long>[] BFSRoot() { if (!bfs) { var nb = new List<Tuple<long, long>>(); var q = new Queue<long>(); var d = new bool[N]; nb.Add(Tuple.Create(r, -1L)); d[r] = true; q.Enqueue(r); while (q.Count > 0) { var w = q.Dequeue(); foreach (var i in p[w]) { if (d[i]) continue; d[i] = true; q.Enqueue(i); nb.Add(Tuple.Create(i, w)); } } b = nb.ToArray(); bfs = true; } return b; } public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); public Tuple<long, long, int>[] Euler() { if (!euler) { var ne = new List<Tuple<long, long, int>>(); var s = new Stack<Tuple<long, long>>(); var d = new bool[N]; d[r] = true; s.Push(Tuple.Create(r, -1L)); while (s.Count > 0) { var w = s.Peek(); var ad = true; foreach (var i in p[w.Item1]) { if (d[i]) continue; d[i] = true; ad = false; s.Push(Tuple.Create(i, w.Item1)); } if (!ad || p[w.Item1].Count == 1) ne.Add(Tuple.Create(w.Item1, w.Item2, 1)); if (ad) { s.Pop(); ne.Add(Tuple.Create(w.Item1, w.Item2, -1)); } } e = ne.ToArray(); euler = true; } return e; } public long LCA(long u, long v) { if (!lca) { l = 0; while (N > (1 << l)) l++; d = new int[N]; pr = Repeat(0, l).Select(_ => new long[N]).ToArray(); d[r] = 0; pr[0][r] = -1; var q = new Stack<long>(); q.Push(r); while (q.Count > 0) { var w = q.Pop(); foreach (var i in p[w]) { if (i == pr[0][w]) continue; q.Push(i); d[i] = d[w] + 1; pr[0][i] = w; } } for (var k = 0; k + 1 < l; k++) for (var w = 0; w < N; w++) if (pr[k][w] < 0) pr[k + 1][w] = -1; else pr[k + 1][w] = pr[k][pr[k][w]]; lca = true; } if (d[u] > d[v]) { var t = u; u = v; v = t; } for (var k = 0; k < l; k++) if ((((d[v] - d[u]) >> k) & 1) != 0) v = pr[k][v]; if (u == v) return u; for (var k = l - 1; k >= 0; k--) if (pr[k][u] != pr[k][v]) { u = pr[k][u]; v = pr[k][v]; } return pr[0][u]; } } class BT<T> where T : IComparable { class Node { public Node l; public Node r; public T v; public bool b; public int c; } Comparison<T> c; Node r; bool ch; T lm; public BT(Comparison<T> _c) { c = _c; } public BT() : this((x, y) => x.CompareTo(y)) {} bool R(Node n) => n != null && !n.b; bool B(Node n) => n != null && n.b; long C(Node n) => n?.c ?? 0; Node RtL(Node n) { Node m = n.r, t = m.l; m.l = n; n.r = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtR(Node n) { Node m = n.l, t = m.r; m.r = n; n.l = t; n.c -= m.c - (t?.c ?? 0); m.c += n.c - (t?.c ?? 0); return m; } Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } public void Add(T x) { r = A(r, x); r.b = true; } Node A(Node n, T x) { if (n == null) { ch = true; return new Node() { v = x, c = 1 }; } if (c(x, n.v) < 0) n.l = A(n.l, x); else n.r = A(n.r, x); n.c++; return Bl(n); } Node Bl(Node n) { if (!ch) return n; if (!B(n)) return n; if (R(n.l) && R(n.l.l)) { n = RtR(n); n.l.b = true; } else if (R(n.l) && R(n.l.r)) { n = RtLR(n); n.l.b = true; } else if (R(n.r) && R(n.r.l)) { n = RtRL(n); n.r.b = true; } else if (R(n.r) && R(n.r.r)) { n = RtL(n); n.r.b = true; } else ch = false; return n; } public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } Node Rm(Node n, T x) { if (n == null) { ch = false; return n; } n.c--; var r = c(x, n.v); if (r < 0) { n.l = Rm(n.l, x); return BlL(n); } if (r > 0) { n.r = Rm(n.r, x); return BlR(n); } if (n.l == null) { ch = n.b; return n.r; } n.l = RmM(n.l); n.v = lm; return BlL(n); } Node RmM(Node n) { n.c--; if (n.r != null) { n.r = RmM(n.r); return BlR(n); } lm = n.v; ch = n.b; return n.l; } Node BlL(Node n) { if (!ch) return n; if (B(n.r) && R(n.r.l)) { var b = n.b; n = RtRL(n); n.b = b; n.l.b = true; ch = false; } else if (B(n.r) && R(n.r.r)) { var b = n.b; n = RtL(n); n.b = b; n.r.b = true; n.l.b = true; ch = false; } else if (B(n.r)) { ch = n.b; n.b = true; n.r.b = false; } else { n = RtL(n); n.b = true; n.l.b = false; n.l = BlL(n.l); ch = false; } return n; } Node BlR(Node n) { if (!ch) return n; if (B(n.l) && R(n.l.r)) { var b = n.b; n = RtLR(n); n.b = b; n.r.b = true; ch = false; } else if (B(n.l) && R(n.l.l)) { var b = n.b; n = RtR(n); n.b = b; n.l.b = true; n.r.b = true; ch = false; } else if (B(n.l)) { ch = n.b; n.b = true; n.l.b = false; } else { n = RtR(n); n.b = true; n.r.b = false; n.r = BlR(n.r); ch = false; } return n; } public T this[long i] { get { return At(r, i); } } T At(Node n, long i) { if (n == null) return default(T); if (n.l == null) if (i == 0) return n.v; else return At(n.r, i - 1); if (n.l.c == i) return n.v; if (n.l.c > i) return At(n.l, i); return At(n.r, i - n.l.c - 1); } public bool Have(T x) { var t = FindUpper(x); return t < C(r) && c(At(r, t), x) == 0; } public long FindUpper(T x, bool allowSame = true) => allowSame ? FL(r, x) + 1 : FU(r, x); long FU(Node n, T x) { if (n == null) return 0; var r = c(x, n.v); if (r < 0) return FU(n.l, x); return C(n.l) + 1 + FU(n.r, x); } public long FindLower(T x, bool allowSame = true) { var t = FL(r, x); if (allowSame) return t + 1 < C(r) && c(At(r, t + 1), x) == 0 ? t + 1 : t < 0 ? C(r) : t; return t < 0 ? C(r) : t; } long FL(Node n, T x) { if (n == null) return -1; var r = c(x, n.v); if (r > 0) return C(n.l) + 1 + FL(n.r, x); return FL(n.l, x); } public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } public bool Any() => r != null; public long Count() => C(r); public IEnumerable<T> List() => L(r); IEnumerable<T> L(Node n) { if (n == null) yield break; foreach (var i in L(n.l)) yield return i; yield return n.v; foreach (var i in L(n.r)) yield return i; } } class Dict<K, V> : Dictionary<K, V> { Func<K, V> d; public Dict(Func<K, V> _d) { d = _d; } public Dict() : this(_ => default(V)) {} new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = d(i); } set { base[i] = value; } } public override int GetHashCode() { var ret = 0; for (var e = GetEnumerator(); e.MoveNext();) { ret ^= e.Current.Key.GetHashCode(); ret ^= e.Current.Value.GetHashCode(); } return ret; } public override bool Equals(object obj) { var o = (Dict<K, V>)obj; foreach (var kv in o) { if (!ContainsKey(kv.Key) || !this[kv.Key].Equals(kv.Value)) return false; } return o.Count == Count; } } class Deque<T> { T[] b; int o, c; public int Count; public T this[int i] { get { return b[gi(i)]; } set { b[gi(i)] = value; } } public Deque(int cap = 16) { b = new T[c = cap]; } int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } public T PopFront() { if (Count-- == 0) throw new Exception(); var r = b[o++]; if (o >= c) o -= c; return r; } public T Front => b[o]; public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; void e() { T[] nb = new T[c << 1]; if (o > c - Count) { var l = b.Length - o; Array.Copy(b, o, nb, 0, l); Array.Copy(b, 0, nb, l, Count - l); } else Array.Copy(b, o, nb, 0, Count); b = nb; o = 0; c <<= 1; } public void Insert(int i, T x) { if (i > Count) throw new Exception(); this.PushFront(x); for (int j = 0; j < i; j++) this[j] = this[j + 1]; this[i] = x; } public T RemoveAt(int i) { if (i < 0 || i >= Count) throw new Exception(); var r = this[i]; for (int j = i; j > 0; j--) this[j] = this[j - 1]; this.PopFront(); return r; } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; public SegTree(long _n, T _ti, Func<T, T, T> _f) { n = 1; while (n < _n) n <<= 1; ti = _ti; f = _f; dat = Repeat(ti, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public SegTree(List<T> l, T _ti, Func<T, T, T> _f) : this(l.Count, _ti, _f) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public void Update(long i, T v) { dat[i += n] = v; while ((i >>= 1) > 0) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public T Query(long l, long r) { var vl = ti; var vr = ti; for (long li = n + l, ri = n + r; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, dat[li++]); if ((ri & 1) == 1) vr = f(dat[--ri], vr); } return f(vl, vr); } public T this[long idx] { get { return dat[idx + n]; } set { Update(idx, value); } } } class LazySegTree<T, E> { int n, height; T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; T[] dat; E[] laz; public LazySegTree(long _n, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { n = 1; height = 0; while (n < _n) { n <<= 1; ++height; } ti = _ti; ei = _ei; f = _f; g = _g; h = _h; dat = Repeat(ti, n << 1).ToArray(); laz = Repeat(ei, n << 1).ToArray(); for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } public LazySegTree(List<T> l, T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) : this(l.Count, _ti, _ei, _f, _g, _h) { for (var i = 0; i < l.Count; i++) dat[n + i] = l[i]; for (var i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); void Eval(long i) { if (laz[i].Equals(ei)) return; laz[(i << 1) | 0] = h(laz[(i << 1) | 0], laz[i]); laz[(i << 1) | 1] = h(laz[(i << 1) | 1], laz[i]); dat[i] = Reflect(i); laz[i] = ei; } void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } public void Update(long l, long r, E v) { Thrust(l += n); Thrust(r += n - 1); for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) { laz[li] = h(laz[li], v); ++li; } if ((ri & 1) == 1) { --ri; laz[ri] = h(laz[ri], v); } } Recalc(l); Recalc(r); } public T Query(long l, long r) { Thrust(l += n); Thrust(r += n - 1); var vl = ti; var vr = ti; for (long li = l, ri = r + 1; li < ri; li >>= 1, ri >>= 1) { if ((li & 1) == 1) vl = f(vl, Reflect(li++)); if ((ri & 1) == 1) vr = f(Reflect(--ri), vr); } return f(vl, vr); } public T this[long idx] { get { Thrust(idx += n); return dat[idx] = Reflect(idx); } set { Thrust(idx += n); dat[idx] = value; laz[idx] = ei; Recalc(idx); } } } class SlidingWindowAggregation<T> { T[][] l; T[][] a; int[] n; Func<T, T, T> f; public SlidingWindowAggregation(IEnumerable<T> ary, Func<T, T, T> _f) { l = new T[2][]; a = new T[2][]; n = new int[2]; f = _f; l[0] = new T[16]; a[0] = new T[16]; n[0] = 0; if (ary.Any()) { var t = ary.ToArray(); n[1] = t.Length; l[1] = new T[n[1]]; a[1] = new T[n[1]]; for (var i = 0; i < l[1].Length; i++) { l[1][i] = t[i]; if (i == 0) a[1][i] = t[i]; else a[1][i] = this.f(a[1][i - 1], t[i]); } } else { l[1] = new T[16]; a[1] = new T[16]; n[1] = 0; } } public SlidingWindowAggregation(Func<T, T, T> f) : this(new T[0], f) {} public int Count => n[0] + n[1]; void Push(int la, T v) { if (l[la].Length == n[la]) { var nar = new T[n[la] * 2]; var nag = new T[n[la] * 2]; Array.Copy(l[la], nar, n[la]); Array.Copy(a[la], nag, n[la]); l[la] = nar; a[la] = nag; } if (n[la] == 0) a[la][0] = v; else a[la][n[la]] = la == 0 ? f(v, a[la][n[la] - 1]) : f(a[la][n[la] - 1], v); l[la][n[la]++] = v; } public void PushBack(T val) => Push(1, val); public void PushFront(T val) => Push(0, val); public T Pop(int la) { var lb = 1 - la; if (n[la] == 0) { if (n[lb] == 0) throw new Exception(); var m = (n[lb] - 1) / 2; if (l[la].Length <= m) { l[la] = new T[m + 1]; a[la] = new T[m + 1]; } n[la] = 0; for (var i = m; i >= 0; i--) { if (n[la] == 0) a[la][n[la]] = l[lb][i]; else a[la][n[la]] = la == 0 ? f(l[lb][i], a[la][n[la] - 1]) : f(a[la][n[la] - 1], l[lb][i]); l[la][n[la]++] = l[lb][i]; } for (var i = m + 1; i < n[lb]; i++) { var j = i - m - 1; if (j == 0) a[lb][j] = l[lb][i]; else a[lb][j] = la == 0 ? f(l[lb][i], a[lb][j - 1]) : f(a[lb][j - 1], l[lb][i]); l[lb][j] = l[lb][i]; } n[lb] -= n[la]; } return l[la][--n[la]]; } public T PopBack() => Pop(1); public T PopFront() => Pop(0); public T Aggregate() { if (n[0] == 0 && n[1] == 0) throw new Exception(); else if (n[1] == 0) return a[0][n[0] - 1]; else if (n[0] == 0) return a[1][n[1] - 1]; else return f(a[0][n[0] - 1], a[1][n[1] - 1]); } } } }
[["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 0, 212, 0, 16, 31, 23, 0, 16, 17, 72], ["+", 0, 212, 0, 16, 31, 23, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 8, 196, 0, 57, 15, 16, 31, 213, 63, 22], ["+", 0, 57, 15, 16, 31, 213, 3, 4, 0, 24], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["-", 0, 52, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 57, 15, 16, 31, 213, 3, 4, 0, 25], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 8, 196, 0, 57, 15, 16, 12, 213, 63, 22], ["+", 0, 57, 15, 16, 12, 213, 3, 4, 0, 24], ["+", 0, 57, 15, 16, 12, 213, 3, 4, 0, 25], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 12, 22], ["+", 15, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 57, 15, 16, 31, 16, 31, 16, 17, 72], ["+", 0, 57, 15, 16, 31, 16, 31, 16, 12, 22]]
8
9,971
22
using System; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); Int64[] dp = new Int64[n]; dp[0] = 0; dp[1] = Math.Abs(int.Parse(input[0]) - int.Parse(input[1])); for (int i = 2; i < n; i++) { int a = int.Parse(input[i]); int b = int.Parse(input[i - 1]); int c = int.Parse(input[i - 2]); if (Math.Abs(a - b) < Math.Abs(a - c)) dp[i] = dp[i - 1] + Math.Abs(a - b); else dp[i] = dp[i - 2] + Math.Abs(a - c); } Console.WriteLine(dp[n - 1]); } } }
using System; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(' '); Int64[] dp = new Int64[n]; dp[0] = 0; dp[1] = Math.Abs(int.Parse(input[0]) - int.Parse(input[1])); for (int i = 2; i < n; i++) { int a = int.Parse(input[i]); int b = int.Parse(input[i - 1]); int c = int.Parse(input[i - 2]); if (dp[i - 1] + Math.Abs(a - b) < dp[i - 2] + Math.Abs(a - c)) dp[i] = dp[i - 1] + Math.Abs(a - b); else dp[i] = dp[i - 2] + Math.Abs(a - c); } Console.WriteLine(dp[n - 1]); } } }
[["+", 0, 57, 15, 16, 31, 16, 31, 204, 205, 22], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 72], ["+", 0, 57, 15, 16, 12, 16, 31, 204, 205, 22], ["+", 15, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 15, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 72]]
8
236
14
using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("10 30 40 20"); // 30 } else if (InputPattern == "Input2") { WillReturn.Add("2"); WillReturn.Add("10 10"); // 0 } else if (InputPattern == "Input3") { WillReturn.Add("6"); WillReturn.Add("30 10 60 10 60 50"); // 40 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List<string> InputList = GetInputList(); int[] HArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); int UB = HArr.GetUpperBound(0); // 最小コスト[現在位置] なDP表 var DPDict = new Dictionary<int, int>(); DPDict[0] = 0; for (int I = 0; I <= UB; I++) { if (DPDict.ContainsKey(I) == false) continue; Action<int> wkAct = (pInt) => { int NewKey = I + pInt; if (NewKey > UB) return; int NewVal = DPDict[I] + Math.Abs(HArr[I] - HArr[NewKey]); if (DPDict.ContainsKey(NewKey)) { if (DPDict[NewKey] <= NewVal) { return; } } DPDict[NewKey] = NewVal; }; wkAct(I + 1); // 1つ先にジャンプする場合 wkAct(I + 2); // 2つ先にジャンプする場合 // Console.WriteLine("{0}回目のDPの結果", I); // foreach (var EachPar in DPDict) { // Console.WriteLine("DPDict[{0}]={1}", EachPar.Key, EachPar.Value); //} } Console.WriteLine(DPDict[UB]); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("10 30 40 20"); // 30 } else if (InputPattern == "Input2") { WillReturn.Add("2"); WillReturn.Add("10 10"); // 0 } else if (InputPattern == "Input3") { WillReturn.Add("6"); WillReturn.Add("30 10 60 10 60 50"); // 40 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List<string> InputList = GetInputList(); int[] HArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); int UB = HArr.GetUpperBound(0); // 最小コスト[現在位置] なDP表 var DPDict = new Dictionary<int, int>(); DPDict[0] = 0; for (int I = 0; I <= UB; I++) { if (DPDict.ContainsKey(I) == false) continue; Action<int> wkAct = (pNewInd) => { if (pNewInd > UB) return; int NewVal = DPDict[I] + Math.Abs(HArr[I] - HArr[pNewInd]); if (DPDict.ContainsKey(pNewInd)) { if (DPDict[pNewInd] <= NewVal) { return; } } DPDict[pNewInd] = NewVal; }; wkAct(I + 1); // 1つ先にジャンプする場合 wkAct(I + 2); // 2つ先にジャンプする場合 // Console.WriteLine("{0}回目のDPの結果", I); // foreach (var EachPar in DPDict) { // Console.WriteLine("DPDict[{0}]={1}", EachPar.Key, EachPar.Value); //} } Console.WriteLine(DPDict[UB]); } }
[["-", 0, 212, 0, 218, 54, 55, 0, 220, 141, 22], ["+", 0, 212, 0, 218, 54, 55, 0, 220, 141, 22], ["-", 0, 218, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 31, 22], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 22], ["-", 0, 212, 0, 218, 8, 196, 0, 197, 0, 35], ["-", 0, 218, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 218, 8, 196, 0, 57, 15, 16, 31, 22], ["-", 0, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 57, 15, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 57, 15, 213, 3, 4, 0, 28, 0, 22], ["-", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22]]
8
389
19
using System; class Program { const int INF = int.MaxValue; static void Main(string[] args) { int N; if (!int.TryParse(Console.ReadLine(), out N)) return; string[] sh = Console.ReadLine().Split(' '); int[] h = new int[N]; for (int i = 0; i < N; i++) { if (!int.TryParse(sh[i], out h[i])) return; } // DP table int[] dp = new int[100010]; for (int i = 0; i < 100010; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < N; i++) { ChMin(ref dp[i + 1], dp[i] + Math.Abs(h[i] - h[i + 1])); ChMin(ref dp[i + 2], dp[i] + Math.Abs(h[i] - h[i + 2])); } Console.WriteLine(dp[N - 1]); } static bool ChMin(ref int a, int b) { if (a > b) { a = b; return true; } return false; } }
using System; class Program { const int INF = int.MaxValue; static void Main(string[] args) { int N; if (!int.TryParse(Console.ReadLine(), out N)) return; string[] sh = Console.ReadLine().Split(' '); int[] h = new int[N]; for (int i = 0; i < N; i++) { if (!int.TryParse(sh[i], out h[i])) return; } // DP table int[] dp = new int[100010]; for (int i = 0; i < 100010; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < N - 1; i++) { ChMin(ref dp[i + 1], dp[i] + Math.Abs(h[i] - h[i + 1])); if (i + 2 < N) ChMin(ref dp[i + 2], dp[i] + Math.Abs(h[i] - h[i + 2])); } Console.WriteLine(dp[N - 1]); } static bool ChMin(ref int a, int b) { if (a > b) { a = b; return true; } return false; } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 17, 72], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 22], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25]]
8
279
10
using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; using System.Numerics; namespace dpc { class Program { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); if (File.Exists("input.txt")) { Console.SetIn(new StreamReader("input.txt")); } solve_dpcA(); Console.Out.Flush(); } public static void solve_dpcB() {} /* https://atcoder.jp/contests/dp/submissions/3937208 */ public static void solve_dpcA() { int n = Convert.ToInt32(Console.ReadLine()); long[] h = Array.ConvertAll(Console.ReadLine().Split(), long.Parse); long[] dp = new long[n]; // Base case for dp[0] is 0. // Because we cannot calculate absolute diff for the first element. // To calculate the absolute diff for the first element we should have first // - 1 element and first - 2 element. But that would mean that the first // element is not the first! So it is 0. Why zero? Well any other value like // int.MaxInt or int.MinInt would lead to the wrong calculation of the next // elements. dp[0] = 0; // we start from the second element as we have availabe min value // for the base case which is the first element for (int i = 1; i < n; i++) { /* The logic is pretty much the same as for the fibonacci numbers. How can we get to the i-th element? Well, there are only TWO options. Either from i - 1 element or from i - 2 element (this is analysed from the problem statement). Now, we first calculate the absolute diff if we came from the i - 1 element. what does it mean? Suppose we already have an absolute minium value if we reached i - 1 element. We store it in the dp[i - 1]. Right now we are on the dp[i] element. Well, then we can calculate the absolute min diff for the current element. How? It is Math.Abs(h[i] - h[i - 1]). Now let's create a temporary variable to store this value. Let's call it iminus1. Let't set the absolute min diff to this variable. Done. But it is wrong? Why? Because by the prbolem statement we should accomulate the absolute minimum diff. And if we set it like that then we lose the value that we calculated for the i - 1 element. How not to lose it? Well, we already have the calculated value for the i - 1. So we should just add it to the iminus1. No we have the correct formula: iminus1 = dp[i - 1] + Math.Abs(h[i] - h[i - 1]) */ long iminus1 = dp[i - 1] + Math.Abs(h[i] - h[i - 1]); /* What happens here? when we reach the third element (index 2) we can now calculate the accumulated absolute minimum diff fo the i - 2 element. Why? Because i is already equl to 2 and 2 - 2 by the formula i - 2 is 0. Which is the correct index for the previous previous element. */ if (i >= 2) { /* What we do here? Well, we already calculated the correct value for the i - 1 element. Now we need to do the same for the i - 2 element. Let's create a temporary variable that will store this value. You see that the difference in the formula is that in the first example it was i - 1 and in the second it is i - 2. */ long iminus2 = dp[i - 2] + Math.Abs(h[i] - h[i - 2]); /* Finally, what we should do is to choose the minimum between two. Ok we calculated the absolute minimum difference for i - 1 and i - 2 and we accumulated it for the previousely calculated values for i - 1 and i - 2, let's see where is the minium and pick it for our current element. */ dp[i] = Math.Min(iminus1, iminus2); } } /* The answer will be the last element. Why because we start from bottomw (the base case) to top (the last element) and we accomulated the absolute min diff for all possible cases which are if you came from the i - 1 element or from the i - 2 element. Among them on every step we make the most optimal value according to the problem's statement and it accumulates to the last elements in the array. */ Console.WriteLine(dp[n - 1]); } } }
using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; using System.Numerics; namespace dpc { class Program { static void Main(string[] args) { Console.SetOut( new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); if (File.Exists("input.txt")) { Console.SetIn(new StreamReader("input.txt")); } solve_dpcA(); Console.Out.Flush(); } public static void solve_dpcB() {} /* https://atcoder.jp/contests/dp/submissions/3937208 */ public static void solve_dpcA() { int n = Convert.ToInt32(Console.ReadLine()); long[] h = Array.ConvertAll(Console.ReadLine().Split(), long.Parse); long[] dp = new long[n]; // Base case for dp[0] is 0. // Because we cannot calculate absolute diff for the first element. // To calculate the absolute diff for the first element we should have first // - 1 element and first - 2 element. But that would mean that the first // element is not the first! So it is 0. Why zero? Well any other value like // int.MaxInt or int.MinInt would lead to the wrong calculation of the next // elements. dp[0] = 0; // we start from the second element as we have availabe min value // for the base case which is the first element for (int i = 1; i < n; i++) { /* The logic is pretty much the same as for the fibonacci numbers. How can we get to the i-th element? Well, there are only TWO options. Either from i - 1 element or from i - 2 element (this is analysed from the problem statement). Now, we first calculate the absolute diff if we came from the i - 1 element. what does it mean? Suppose we already have an absolute minium value if we reached i - 1 element. We store it in the dp[i - 1]. Right now we are on the dp[i] element. Well, then we can calculate the absolute min diff for the current element. How? It is Math.Abs(h[i] - h[i - 1]). Now let's create a temporary variable to store this value. Let's call it iminus1. Let't set the absolute min diff to this variable. Done. But it is wrong? Why? Because by the prbolem statement we should accomulate the absolute minimum diff. And if we set it like that then we lose the value that we calculated for the i - 1 element. How not to lose it? Well, we already have the calculated value for the i - 1. So we should just add it to the iminus1. No we have the correct formula: iminus1 = dp[i - 1] + Math.Abs(h[i] - h[i - 1]) */ long iminus1 = dp[i - 1] + Math.Abs(h[i] - h[i - 1]); /* What happens here? when we reach the third element (index 2) we can now calculate the accumulated absolute minimum diff fo the i - 2 element. Why? Because i is already equl to 2 and 2 - 2 by the formula i - 2 is 0. Which is the correct index for the previous previous element. */ long iminus2 = int.MaxValue; if (i >= 2) { /* What we do here? Well, we already calculated the correct value for the i - 1 element. Now we need to do the same for the i - 2 element. Let's create a temporary variable that will store this value. You see that the difference in the formula is that in the first example it was i - 1 and in the second it is i - 2. */ iminus2 = dp[i - 2] + Math.Abs(h[i] - h[i - 2]); /* Finally, what we should do is to choose the minimum between two. Ok we calculated the absolute minimum difference for i - 1 and i - 2 and we accumulated it for the previousely calculated values for i - 1 and i - 2, let's see where is the minium and pick it for our current element. */ } dp[i] = Math.Min(iminus1, iminus2); } /* The answer will be the last element. Why because we start from bottomw (the base case) to top (the last element) and we accomulated the absolute min diff for all possible cases which are if you came from the i - 1 element or from the i - 2 element. Among them on every step we make the most optimal value according to the problem's statement and it accumulates to the last elements in the array. */ Console.WriteLine(dp[n - 1]); } } }
[["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 205, 199], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 0, 131], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 141, 22], ["+", 8, 196, 0, 7, 8, 196, 0, 197, 0, 35], ["-", 0, 57, 64, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46], ["-", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
298
10
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { // dp-1 var n = int.Parse(Console.ReadLine()); var inputs = Console.ReadLine().Split(' '); var hs = inputs.Select(i => int.Parse(i)).ToList(); List<int> dp = new List<int>(); dp.Add(Math.Abs(hs[0] - hs[1])); for (int i = 2; i < n; i++) { int a = dp[i - 2] + Math.Abs(dp[i - 2] - hs[i]); int b = Math.Abs(hs[i - 2] - hs[i]); dp.Add(a > b ? b : a); } Console.Write(dp.Last()); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var inputs = Console.ReadLine().Split(' '); var hs = inputs.Select(i => int.Parse(i)).ToList(); List<int> dp = new List<int>(); dp.Add(0); dp.Add(Math.Abs(hs[0] - hs[1])); for (int i = 2; i < n; i++) { int a = dp[i - 1] + Math.Abs(hs[i - 1] - hs[i]); int b = dp[i - 2] + Math.Abs(hs[i - 2] - hs[i]); dp.Add(a > b ? b : a); } Console.Write(dp.Last()); } }
[["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["-", 3, 4, 0, 28, 0, 16, 31, 204, 205, 22], ["+", 3, 4, 0, 28, 0, 16, 31, 204, 205, 22], ["+", 0, 200, 0, 212, 0, 16, 31, 204, 205, 22], ["+", 0, 212, 0, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 212, 0, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72]]
8
203
20
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] h = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); long[] dp = new long[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = (long)Math.Pow(10, 5); changeMin(ref dp[i], dp[i - 1] + Math.Abs(h[i] - h[i - 1])); if (i > 1) changeMin(ref dp[i], dp[i - 2] + Math.Abs(h[i] - h[i - 2])); } Console.WriteLine(dp[n - 1]); } static bool changeMin(ref long a, long b) { if (a > b) { a = b; return true; } return false; } } }
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] h = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); long[] dp = new long[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = long.MaxValue; changeMin(ref dp[i], dp[i - 1] + Math.Abs(h[i] - h[i - 1])); if (i > 1) changeMin(ref dp[i], dp[i - 2] + Math.Abs(h[i] - h[i - 2])); } Console.WriteLine(dp[n - 1]); } static bool changeMin(ref long a, long b) { if (a > b) { a = b; return true; } return false; } } }
[["-", 8, 196, 0, 1, 0, 11, 12, 74, 0, 24], ["-", 8, 196, 0, 1, 0, 11, 12, 74, 0, 25], ["-", 0, 11, 12, 74, 51, 213, 63, 214, 205, 22], ["-", 0, 11, 12, 74, 51, 213, 63, 214, 141, 22], ["-", 0, 11, 12, 74, 51, 213, 3, 4, 0, 24], ["-", 12, 74, 51, 213, 3, 4, 0, 28, 0, 203], ["-", 0, 11, 12, 74, 51, 213, 3, 4, 0, 21], ["-", 0, 11, 12, 74, 51, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 141, 22]]
8
229
10
using System; using System.Linq; namespace ConsoleApp33 { class Program { static private int N; static private int[] h; static public int[] min; public static void Main() { GetData(); CalculateMin(); // Console.WriteLine(min.Last()); } public static void GetData() { N = int.Parse(Console.ReadLine()); h = new int[N]; min = new int[N]; var hs = Console.ReadLine().Split(' '); h = hs.Select(x => int.Parse(x)).ToArray(); } public static void CalculateMin() { min[0] = 0; min[1] = Math.Abs(h[0] - h[1]); min = min.Select((m, i) => min[i] = i >= 2 ? Math.Min(min[i - 1] + Math.Abs(h[i] - h[i - 1]), min[i - 2] + Math.Abs(h[i] - h[i - 2])) : min[i]) .ToArray(); foreach (var m in min) { Console.WriteLine(m); } } } }
using System; using System.Linq; namespace ConsoleApp33 { class Program { static private int N; static private int[] h; static public int[] min; public static void Main() { GetData(); CalculateMin(); Console.WriteLine(min.Last()); } public static void GetData() { N = int.Parse(Console.ReadLine()); h = new int[N]; min = new int[N]; var hs = Console.ReadLine().Split(' '); h = hs.Select(x => int.Parse(x)).ToArray(); } public static void CalculateMin() { min[0] = 0; min[1] = Math.Abs(h[0] - h[1]); min = min.Select((m, i) => min[i] = i >= 2 ? Math.Min(min[i - 1] + Math.Abs(h[i] - h[i - 1]), min[i - 2] + Math.Abs(h[i] - h[i - 2])) : min[i]) .ToArray(); } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 266], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 24], ["-", 0, 195, 8, 196, 0, 210, 39, 216, 0, 217], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 31, 22], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 267], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 12, 22], ["-", 8, 201, 0, 195, 8, 196, 0, 210, 0, 25], ["-", 0, 195, 8, 196, 0, 210, 8, 196, 0, 45], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 210, 8, 196, 0, 1, 0, 35], ["-", 0, 195, 8, 196, 0, 210, 8, 196, 0, 46]]
8
260
27
using System; using System.Linq; using static System.Console; static class Extensions { public static int ToInt(this string str) => int.Parse(str); } class Program { public static void Main(string[] args) { var n = ReadLine().ToInt(); var h = ReadLine().Split(' ').Select(int.Parse).ToArray(); var dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = int.MaxValue; dp[0] = 0; for (int i = 0; i < n - 2; i++) { var delta1 = dp[i] + Math.Abs(h[i + 1] - h[i]); var delta2 = dp[i] + Math.Abs(h[i + 2] - h[i]); if (dp[i + 1] > delta1) dp[i + 1] = delta1; if (dp[i + 2] > delta2) dp[i + 2] = delta2; } var delta = dp[n - 1] + Math.Abs(h[n - 1] - h[n]); if (dp[n] > delta) dp[n] = delta; WriteLine(dp.Last()); } }
using System; using System.Linq; using static System.Console; static class Extensions { public static int ToInt(this string str) => int.Parse(str); } class Program { public static void Main(string[] args) { var n = ReadLine().ToInt(); var h = ReadLine().Split(' ').Select(int.Parse).ToArray(); var dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = int.MaxValue; dp[0] = 0; for (int i = 0; i < n - 2; i++) { var delta1 = dp[i] + Math.Abs(h[i + 1] - h[i]); var delta2 = dp[i] + Math.Abs(h[i + 2] - h[i]); if (dp[i + 1] > delta1) dp[i + 1] = delta1; if (dp[i + 2] > delta2) dp[i + 2] = delta2; } var delta = dp[n - 2] + Math.Abs(h[n - 2] - h[n - 1]); if (dp[n - 1] > delta) dp[n - 1] = delta; WriteLine(dp.Last()); } }
[["-", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33]]
8
287
10
using System; using System.Collections; using System.Collections.Generic; using System.Linq; class MainClass { public static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var h = new int[N]; var input = Console.ReadLine().Split(' '); // テーブル var dp = new int[N]; for (var i = 0; i < N; i++) { h[i] = int.Parse(input[i]); dp[i] = int.MaxValue; } dp[0] = 0; for (var i = 1; i < N; i++) { dp[i] = chmin(dp[i], dp[i - 1] + Math.Abs(h[i] - h[i - 1])); if (i > 1) { dp[i] = chmin(dp[i], dp[i - 2] + Math.Abs(h[i] - h[i - 2])); } } } public static T chmin<T>(T a, T b) where T : IComparable { if (a.CompareTo(b) > 0) { a = b; return a; } return a; } public static T chmax<T>(T a, T b) where T : IComparable { if (a.CompareTo(b) < 0) { a = b; return a; } return a; } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; class MainClass { public static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var h = new int[N]; var input = Console.ReadLine().Split(' '); // テーブル var dp = new int[N]; for (var i = 0; i < N; i++) { h[i] = int.Parse(input[i]); dp[i] = int.MaxValue; } dp[0] = 0; for (var i = 1; i < N; i++) { dp[i] = chmin(dp[i], dp[i - 1] + Math.Abs(h[i] - h[i - 1])); if (i > 1) { dp[i] = chmin(dp[i], dp[i - 2] + Math.Abs(h[i] - h[i - 2])); } } Console.WriteLine(dp[N - 1]); } public static T chmin<T>(T a, T b) where T : IComparable { if (a.CompareTo(b) > 0) { a = b; return a; } return a; } public static T chmax<T>(T a, T b) where T : IComparable { if (a.CompareTo(b) < 0) { a = b; return a; } return a; } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
316
12
using System; using System.Linq; // AtCoder Educational DP -A Frog public class Program { public static void Main() { var len = int.Parse(Console.ReadLine()); var stage = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var dp = new int[len]; dp[0] = 0; dp[1] = stage[1]; int from1, from2; for (int x = 2; x < len; x++) { from1 = dp[x - 1] + (Math.Abs(stage[x] - stage[x - 1])); from2 = dp[x - 2] + (Math.Abs(stage[x] - stage[x - 2])); dp[x] = (from1 > from2) ? from2 : from1; } // foreach(var a in dp) Console.WriteLine(a); Console.WriteLine(dp[len - 1]); } }
using System; using System.Linq; // AtCoder Educational DP -A Frog public class Program { public static void Main() { var len = int.Parse(Console.ReadLine()); var stage = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var dp = new int[len]; dp[0] = 0; dp[1] = Math.Abs(stage[1] - stage[0]); int from1, from2; for (int x = 2; x < len; x++) { from1 = dp[x - 1] + (Math.Abs(stage[x] - stage[x - 1])); from2 = dp[x - 2] + (Math.Abs(stage[x] - stage[x - 2])); dp[x] = (from1 > from2) ? from2 : from1; } // foreach(var a in dp) Console.WriteLine(a); Console.WriteLine(dp[len - 1]); } }
[["+", 0, 1, 0, 11, 12, 213, 63, 214, 205, 22], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 0, 131], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 141, 22], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 0, 28, 0, 16, 31, 204, 206, 207, 0, 73], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["+", 3, 4, 0, 28, 0, 16, 12, 204, 205, 22], ["+", 0, 28, 0, 16, 12, 204, 206, 207, 0, 70], ["+", 0, 16, 12, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 25]]
8
192
10
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; class Program { static void Main(string[] args) { new Program().Calc(); } Scanner cin; public Program() {} bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; } int[] _dp; int[] _height; void Calc() { cin = new Scanner(); int N = cin.nextInt(); _dp = new int[N + 1]; for (int i = 2; i <= N; ++i) { _dp[i] = int.MaxValue; } _height = new int[N + 1]; for (int i = 1; i <= N; ++i) { _height[i] = cin.nextInt(); } int cost = FlogJump(N); WriteLine(cost); } int FlogJump(int index) { if (index <= 1) { return 0; } if (_dp[index] == int.MaxValue) { chmin(ref _dp[index], FlogJump(index - 1) + Math.Abs(_height[index] - _height[index - 1])); chmin(ref _dp[index], FlogJump(index - 2) + Math.Abs(_height[index] - _height[index - 2])); } return _dp[index]; } } public static class StringExtension { public static char GetChar1(this string str, int indexOneBase) { return str[indexOneBase - 1]; } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; class Program { static void Main(string[] args) { new Program().Calc(); } Scanner cin; public Program() {} bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; } int[] _dp; int[] _height; void Calc() { cin = new Scanner(); int N = cin.nextInt(); _dp = new int[N + 1]; for (int i = 0; i <= N; ++i) { _dp[i] = int.MaxValue; } _height = new int[N + 1]; for (int i = 1; i <= N; ++i) { _height[i] = cin.nextInt(); } int cost = FlogJump(N); WriteLine(cost); } int FlogJump(int index) { if (index <= 1) { return 0; } if (_dp[index] == int.MaxValue) { chmin(ref _dp[index], FlogJump(index - 1) + Math.Abs(_height[index] - _height[index - 1])); if (index >= 3) { chmin(ref _dp[index], FlogJump(index - 2) + Math.Abs(_height[index] - _height[index - 2])); } } return _dp[index]; } } public static class StringExtension { public static char GetChar1(this string str, int indexOneBase) { return str[indexOneBase - 1]; } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 8, 196, 0, 57, 64, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 57, 64, 196, 0, 57, 0, 24], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 17, 20], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 12, 203], ["+", 8, 196, 0, 57, 64, 196, 0, 57, 0, 25], ["+", 0, 57, 64, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 57, 64, 196, 0, 57, 64, 196, 0, 46]]
8
755
10
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { while (args.Length > 0 && args[0] == "debug") new Program().Solve(new ConsoleInput(In)); } public T Min<T>(params T[] a) { if (a.Length == 0) return default(T); T min = a[0]; for (int i = 1; i < a.Length; i++) { if ((dynamic)min > (dynamic)a[i]) min = a[i]; } return min; } public T Max<T>(params T[] a) { if (a.Length == 0) return default(T); T max = a[0]; for (int i = 1; i < a.Length; i++) { if ((dynamic)max < (dynamic)a[i]) max = a[i]; } return max; } public T Lcm<T>(T a, T b) { return (dynamic)a * (dynamic)b / Gcd((dynamic)a, (dynamic)b); } public T Gcd<T>(T a, T b) { if ((dynamic)a < (dynamic)b) return Gcd(b, a); while ((dynamic)b != 0) { var remainder = (dynamic)a % (dynamic)b; a = b; b = remainder; } return a; } public void Solve(ConsoleInput input) { var n = input.ReadInt; var h = input.ReadIntArray(n); var dp = Enumerable.Repeat(-1, n).ToArray(); dp[0] = 0; dp[1] = Abs(h[0] - h[1]); for (var i = 0; i < n - 2; i++) { dp[i + 2] = Min(Abs(h[i + 1] - h[i + 2]) + dp[i + 1], Abs(h[i] - h[i + 2]) + dp[i]); } WriteLine($"{dp[n - 1]}"); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Math; namespace atcoder { class Program { static void Main(string[] args) { // while (args.Length > 0 && args[0] == "debug") new Program().Solve(new ConsoleInput(In)); } public T Min<T>(params T[] a) { if (a.Length == 0) return default(T); T min = a[0]; for (int i = 1; i < a.Length; i++) { if ((dynamic)min > (dynamic)a[i]) min = a[i]; } return min; } public T Max<T>(params T[] a) { if (a.Length == 0) return default(T); T max = a[0]; for (int i = 1; i < a.Length; i++) { if ((dynamic)max < (dynamic)a[i]) max = a[i]; } return max; } public T Lcm<T>(T a, T b) { return (dynamic)a * (dynamic)b / Gcd((dynamic)a, (dynamic)b); } public T Gcd<T>(T a, T b) { if ((dynamic)a < (dynamic)b) return Gcd(b, a); while ((dynamic)b != 0) { var remainder = (dynamic)a % (dynamic)b; a = b; b = remainder; } return a; } public void Solve(ConsoleInput input) { var n = input.ReadInt; var h = input.ReadIntArray(n); var dp = Enumerable.Repeat(-1, n).ToArray(); dp[0] = 0; dp[1] = Abs(h[0] - h[1]); for (var i = 0; i < n - 2; i++) { dp[i + 2] = Min(Abs(h[i + 1] - h[i + 2]) + dp[i + 1], Abs(h[i] - h[i + 2]) + dp[i]); } WriteLine($"{dp[n - 1]}"); } } public class ConsoleInput { private readonly System.IO.TextReader _stream; private readonly char _separator; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } }
[["-", 8, 201, 0, 195, 8, 196, 0, 52, 0, 89], ["-", 8, 201, 0, 195, 8, 196, 0, 52, 0, 24], ["-", 0, 52, 15, 16, 31, 16, 31, 214, 205, 22], ["-", 0, 52, 15, 16, 31, 16, 31, 214, 0, 131], ["-", 0, 52, 15, 16, 31, 16, 31, 214, 141, 22], ["-", 8, 196, 0, 52, 15, 16, 31, 16, 17, 47], ["-", 8, 196, 0, 52, 15, 16, 31, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 52, 15, 16, 17, 98], ["-", 0, 52, 15, 16, 12, 16, 31, 204, 205, 22], ["-", 15, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["-", 12, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["-", 15, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 52, 15, 16, 12, 16, 17, 60], ["-", 0, 52, 15, 16, 12, 16, 12, 5, 0, 62], ["-", 0, 52, 15, 16, 12, 16, 12, 5, 0, 222], ["-", 8, 201, 0, 195, 8, 196, 0, 52, 0, 25]]
8
842
17
using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var answer = new int[N]; // 配列を初期化 for (int i = 0; i < N; i++) { answer[i] = 100000; } // スペース区切りの整数の入力 var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); // Startは0 answer[0] = 0; // 二番目のコスト。 answer[1] = input[1] - input[0]; for (int i = 2; i < N; i++) { Math.Abs(-2); var a = answer[i - 1] + Math.Abs(input[i] - input[i - 1]); var b = answer[i - 2] + Math.Abs(input[i] - input[i - 2]); if (a > b) answer[i] = b; else answer[i] = a; } Console.WriteLine(answer[N - 1]); } }
using System; using System.Collections.Generic; using System.Linq; class Program { public static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var answer = new int[N]; // 配列を初期化 for (int i = 0; i < N; i++) { answer[i] = 100000; } // スペース区切りの整数の入力 var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); // Startは0 answer[0] = 0; // 二番目のコスト。 answer[1] = Math.Abs(input[1] - input[0]); for (int i = 2; i < N; i++) { var a = answer[i - 1] + Math.Abs(input[i] - input[i - 1]); var b = answer[i - 2] + Math.Abs(input[i] - input[i - 2]); if (a > b) answer[i] = b; else answer[i] = a; } Console.WriteLine(answer[N - 1]); } }
[["+", 0, 1, 0, 11, 12, 213, 63, 214, 205, 22], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 0, 131], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 141, 22], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 241, 0, 33], ["-", 0, 213, 3, 4, 0, 28, 0, 241, 0, 203], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
241
13
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC88D { class Program { static void Main(string[] args) { EDPC.B_Frog2(); } class EDPC { public static void B_Frog2() { var NK = ReadIntN(); int N = NK[0]; int K = NK[1]; var Hs = ReadIntN(); int inf = 100000; var dp = new long[N]; for (int i = 1; i < dp.Length; i++) { dp[i] = inf; } for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j <= Math.Min(i + K, N - 1); j++) { dp[j] = Math.Min(dp[j], dp[i] + Math.Abs(Hs[j] - Hs[i])); } } Console.WriteLine(dp[N - 1]); Console.ReadLine(); } } #region Reader static string ReadStr() { return Console.ReadLine(); } static string[] ReadStrN() { return Console.ReadLine().Split(' '); } static int ReadInt() { return Convert.ToInt32(Console.ReadLine()); } static int[] ReadIntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } static long ReadLong() { return Convert.ToInt64(Console.ReadLine()); } static long[] ReadLongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } static long[,] ReadLongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = ReadLongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } static Tuple<string, string>[] ReadTupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = ReadStrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<long, long>[] ReadTupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<int, int>[] ReadTupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = ReadIntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<double, double>[] ReadTupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } static Dictionary<int, HashSet<int>> ReadAdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = ReadIntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } #endregion } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC88D { class Program { static void Main(string[] args) { EDPC.B_Frog2(); } class EDPC { public static void B_Frog2() { var NK = ReadIntN(); int N = NK[0]; int K = NK[1]; var Hs = ReadIntN(); int inf = 100000; var dp = new long[N]; for (int i = 1; i < dp.Length; i++) { dp[i] = (long)Math.Pow(10, 10); } for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j <= Math.Min(i + K, N - 1); j++) { dp[j] = Math.Min(dp[j], dp[i] + Math.Abs(Hs[j] - Hs[i])); } } Console.WriteLine(dp[N - 1]); Console.ReadLine(); } } #region Reader static string ReadStr() { return Console.ReadLine(); } static string[] ReadStrN() { return Console.ReadLine().Split(' '); } static int ReadInt() { return Convert.ToInt32(Console.ReadLine()); } static int[] ReadIntN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt32(s)) .ToArray(); } static long ReadLong() { return Convert.ToInt64(Console.ReadLine()); } static long[] ReadLongN() { return Console.ReadLine() .Split(' ') .Select(s => Convert.ToInt64(s)) .ToArray(); } static long[,] ReadLongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = ReadLongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } static Tuple<string, string>[] ReadTupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = ReadStrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<long, long>[] ReadTupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<int, int>[] ReadTupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = ReadIntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<double, double>[] ReadTupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } static Dictionary<int, HashSet<int>> ReadAdjacencyList(int N, int M) { Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); } for (int i = 0; i < M; ++i) { var ab = ReadIntN(); dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } #endregion } }
[["-", 0, 7, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 0, 24], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 39, 199], ["+", 8, 196, 0, 1, 0, 11, 12, 74, 0, 25], ["+", 0, 11, 12, 74, 51, 213, 63, 214, 205, 22], ["+", 0, 11, 12, 74, 51, 213, 63, 214, 0, 131], ["+", 0, 11, 12, 74, 51, 213, 63, 214, 141, 22], ["+", 0, 11, 12, 74, 51, 213, 3, 4, 0, 24], ["+", 12, 74, 51, 213, 3, 4, 0, 28, 0, 203], ["+", 0, 11, 12, 74, 51, 213, 3, 4, 0, 21], ["+", 0, 11, 12, 74, 51, 213, 3, 4, 0, 25]]
8
985
12
using System; namespace EDPC { class B { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); input = Console.ReadLine().Split(' '); // initialize long[] h = new long[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(input[i]); } long H = 100010; long[] dp = new long[N]; for (int i = 0; i < N; i++) { dp[i] = H; } // Main processing dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = 1; j <= K; j++) { if (j <= i) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i] - h[i - j])); } } } Console.WriteLine(dp[N - 1]); } } }
using System; using System.Linq; namespace EDPC { class B { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); input = Console.ReadLine().Split(' '); // initialize int[] h = new int[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(input[i]); } long H = int.MaxValue; long[] dp = new long[N]; for (int i = 0; i < N; i++) { dp[i] = H; } // var dp = Enumerable.Repeat(int.MaxValue, N).ToArray(); // Main processing dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = 1; j <= K; j++) { if (j <= i) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i] - h[i - j])); } } } Console.WriteLine(dp[N - 1]); } } }
[["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 233], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 234, 22], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 0, 131], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 35], ["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 205, 199], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 0, 131], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 141, 22]]
8
262
52
using System; namespace EDPC { class B { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); input = Console.ReadLine().Split(' '); // initialize long[] h = new long[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(input[i]); } long H = 100010; long[] dp = new long[N]; for (int i = 0; i < N; i++) { dp[i] = H; } // Main processing dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = 1; j <= K; j++) { if (j <= i) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i] - h[i - j])); } } } Console.WriteLine(dp[N - 1]); } } }
using System; using System.Linq; namespace EDPC { class B { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int K = int.Parse(input[1]); input = Console.ReadLine().Split(' '); // initialize int[] h = new int[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(input[i]); } /* long H = int.MaxValue; long[] dp = new long[N]; for (int i = 0; i < N; i++) { dp[i] = H; } */ var dp = Enumerable.Repeat(int.MaxValue, N).ToArray(); // Main processing dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = 1; j <= K; j++) { if (j <= i) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i] - h[i - j])); } } } Console.WriteLine(dp[N - 1]); } } }
[["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 233], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 234, 22], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 0, 131], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 35], ["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["-", 0, 197, 0, 198, 39, 224, 225, 226, 0, 70], ["-", 0, 197, 0, 198, 39, 224, 225, 226, 0, 73], ["+", 8, 196, 0, 197, 0, 198, 39, 216, 0, 217], ["-", 0, 198, 0, 200, 0, 212, 0, 227, 0, 228], ["-", 0, 212, 0, 227, 39, 224, 225, 226, 0, 70], ["-", 0, 212, 0, 227, 39, 224, 225, 226, 0, 22], ["-", 0, 212, 0, 227, 39, 224, 225, 226, 0, 73], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 88], ["+", 0, 213, 63, 214, 205, 213, 63, 214, 205, 22], ["+", 0, 213, 63, 214, 205, 213, 63, 214, 0, 131], ["+", 0, 213, 63, 214, 205, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 7, 10, 198, 0, 200, 141, 22], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 32], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 7, 0, 35], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 31, 22], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 0, 131], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 141, 22], ["+", 0, 213, 63, 214, 205, 213, 3, 4, 0, 21], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 22], ["-", 0, 195, 8, 196, 0, 7, 26, 223, 0, 29], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 45], ["-", 8, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 0, 7, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 0, 131], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 3, 4, 0, 24], ["+", 0, 200, 0, 212, 0, 213, 3, 4, 0, 25], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 46]]
8
262
52
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { #region library static int n = 0; static int m = 0; static int r = 0; static int c = 0; static int x = 0; static int y = 0; static string s = ""; static string[] ss; static long sum = 0; static long cnt = 0; static long max = -1; static long min = 2147483647; static bool flg = false; static bool flg2 = false; static string alpha = "abcdefghijklmnopqrstuvwxyz"; static long[] arr; static char[,] map; static long[,] imap; public class Input { static readonly char[] separator = { ' ', '\r', '\n' }; readonly StreamReader sr; readonly Queue<string> queue; public Input(string path = "") { queue = new Queue<string>(); if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); } else { sr = new StreamReader(path); } } public void SetText(IEnumerable<string> items) { foreach (var item in items) { SetText(item); } } public bool SetText(string s) { if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { queue.Enqueue(elem); } return true; } public bool Any() { return queue.Any() || Read(); } bool Read() { if (!SetText(sr.ReadLine())) return false; if (!queue.Any()) return Read(); return queue.Any(); } public string Next() { if (!queue.Any() && !Read()) { return ""; } return queue.Dequeue(); } bool Accumulate(int n) { while (queue.Count() < n) { if (!Read()) return false; } return true; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add) { if (!Accumulate(n)) return null; var a = new T[n]; for (int i = 0; i < n; i++) a[i] = add(parse(queue.Dequeue()), offset); return a; } public string[] Next(int n) { return NextT(n, "", x => x, (x, y) => x); } public int[] NextInt(int n, int offset = 0) { return NextT(n, offset, int.Parse, (x, y) => x + y); } public long[] NextLong(int n, long offset = 0) { return NextT(n, offset, long.Parse, (x, y) => x + y); } public double[] NextDouble(int n, double offset = 0.0) { return NextT(n, offset, double.Parse, (x, y) => x + y); } } static Input input; static int Int() { return input.NextInt(); } static long Long() { return input.NextLong(); } static double Double() { return input.NextDouble(); } static string Text() { return input.Next(); } static void wl(object obj = null) { Console.WriteLine(obj); } static void w(object obj) { Console.Write(obj); } static void Yes() { wl("YES"); } static void No() { wl("NO"); } static void Sum() { wl(sum); } static void Cnt() { wl(cnt); } static void SetArray(long num = -1) { if (n == 0) { n = Int(); } arr = new long[n]; for (int i = 0; i < n; i++) { if (num == -1) arr[i] = Int(); else arr[i] = num; } } static void SetMap(char def = '@') { if (r == 0 && c == 0) { r = Int(); c = Int(); } map = new char[r, c]; for (int i = 0; i < r; i++) { if (def == '@') s = Text(); for (int j = 0; j < c; j++) { map[i, j] = (def == '@') ? s[j] : def; } } } static void SetIMap(long def = -1) { if (r == 0 && c == 0) { r = Int(); c = Int(); } imap = new long[r, c]; for (int i = 0; i < r; i++) { if (def == -1) s = Text(); for (int j = 0; j < c; j++) { imap[i, j] = (def == -1) ? int.Parse(s[j].ToString()) : def; } } } #endregion #region main static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); input = new Input(); Solve(); Console.Out.Flush(); } public static void Solve() { n = Int(); int k = Int(); long[] dp = new long[n + 1]; for (int i = 1; i <= n; i++) { dp[i] = long.MaxValue; } int[] h = new int[n + 1]; for (int i = 1; i <= n; i++) { h[i] = Int(); } dp[1] = 0; for (int i = 2; i <= n; i++) { for (int j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i - j] - h[i])); } else { break; } } } wl(dp[n]); } #endregion }
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { #region library static int n = 0; static int m = 0; static int r = 0; static int c = 0; static int x = 0; static int y = 0; static string s = ""; static string[] ss; static long sum = 0; static long cnt = 0; static long max = -1; static long min = 2147483647; static bool flg = false; static bool flg2 = false; static string alpha = "abcdefghijklmnopqrstuvwxyz"; static long[] arr; static char[,] map; static long[,] imap; public class Input { static readonly char[] separator = { ' ', '\r', '\n' }; readonly StreamReader sr; readonly Queue<string> queue; public Input(string path = "") { queue = new Queue<string>(); if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); } else { sr = new StreamReader(path); } } public void SetText(IEnumerable<string> items) { foreach (var item in items) { SetText(item); } } public bool SetText(string s) { if (s == null) return false; foreach (var elem in s.Trim().Split( separator, StringSplitOptions.RemoveEmptyEntries)) { queue.Enqueue(elem); } return true; } public bool Any() { return queue.Any() || Read(); } bool Read() { if (!SetText(sr.ReadLine())) return false; if (!queue.Any()) return Read(); return queue.Any(); } public string Next() { if (!queue.Any() && !Read()) { return ""; } return queue.Dequeue(); } bool Accumulate(int n) { while (queue.Count() < n) { if (!Read()) return false; } return true; } public int NextInt() { return int.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add) { if (!Accumulate(n)) return null; var a = new T[n]; for (int i = 0; i < n; i++) a[i] = add(parse(queue.Dequeue()), offset); return a; } public string[] Next(int n) { return NextT(n, "", x => x, (x, y) => x); } public int[] NextInt(int n, int offset = 0) { return NextT(n, offset, int.Parse, (x, y) => x + y); } public long[] NextLong(int n, long offset = 0) { return NextT(n, offset, long.Parse, (x, y) => x + y); } public double[] NextDouble(int n, double offset = 0.0) { return NextT(n, offset, double.Parse, (x, y) => x + y); } } static Input input; static int Int() { return input.NextInt(); } static long Long() { return input.NextLong(); } static double Double() { return input.NextDouble(); } static string Text() { return input.Next(); } static void wl(object obj = null) { Console.WriteLine(obj); } static void w(object obj) { Console.Write(obj); } static void Yes() { wl("YES"); } static void No() { wl("NO"); } static void Sum() { wl(sum); } static void Cnt() { wl(cnt); } static void SetArray(long num = -1) { if (n == 0) { n = Int(); } arr = new long[n]; for (int i = 0; i < n; i++) { if (num == -1) arr[i] = Int(); else arr[i] = num; } } static void SetMap(char def = '@') { if (r == 0 && c == 0) { r = Int(); c = Int(); } map = new char[r, c]; for (int i = 0; i < r; i++) { if (def == '@') s = Text(); for (int j = 0; j < c; j++) { map[i, j] = (def == '@') ? s[j] : def; } } } static void SetIMap(long def = -1) { if (r == 0 && c == 0) { r = Int(); c = Int(); } imap = new long[r, c]; for (int i = 0; i < r; i++) { if (def == -1) s = Text(); for (int j = 0; j < c; j++) { imap[i, j] = (def == -1) ? int.Parse(s[j].ToString()) : def; } } } #endregion #region main static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); input = new Input(); Solve(); Console.Out.Flush(); } public static void Solve() { n = Int(); int k = Int(); long[] dp = new long[n]; for (int i = 0; i < n; i++) { dp[i] = long.MaxValue; } int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = Int(); } dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(h[i - j] - h[i])); } else { break; } } } wl(dp[n - 1]); } #endregion }
[["-", 0, 227, 39, 224, 225, 226, 0, 16, 17, 72], ["-", 0, 227, 39, 224, 225, 226, 0, 16, 12, 203], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
1,439
20
using System; using System.Linq; namespace atcoder { class Program { static void Main(string[] args) { int[] input = Console.ReadLine().Split(' ').Select(e => int.Parse(e)).ToArray(); int n = input[0]; int k = input[1]; int[] hArray = Console.ReadLine().Split(' ').Select(e => int.Parse(e)).ToArray(); int[] dpArray = (new int[n]).Select(e => 10000).ToArray(); dpArray[0] = 0; for (int idx = 0; idx < n; idx++) { for (int j = Math.Max(0, idx - k); j < idx; j++) { int dist = Math.Abs(hArray[idx] - hArray[j]) + dpArray[j]; dpArray[idx] = Math.Min(dpArray[idx], dist); } } Console.WriteLine(dpArray.Last()); } } }
using System; using System.Linq; namespace atcoder { class Program { static void Main(string[] args) { int[] input = Console.ReadLine().Split(' ').Select(e => int.Parse(e)).ToArray(); int n = input[0]; int k = input[1]; double[] hArray = Console.ReadLine().Split(' ').Select(e => double.Parse(e)).ToArray(); double[] dpArray = (new double[n]).Select(e => double.MaxValue).ToArray(); dpArray[0] = 0; for (int idx = 0; idx < n; idx++) { for (int j = Math.Max(0, idx - k); j < idx; j++) { double dist = Math.Abs(hArray[idx] - hArray[j]) + dpArray[j]; dpArray[idx] = Math.Min(dpArray[idx], dist); } } Console.WriteLine(dpArray.Last()); } } }
[["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 205, 199], ["-", 63, 214, 205, 23, 0, 227, 39, 224, 39, 199], ["+", 63, 214, 205, 23, 0, 227, 39, 224, 39, 199], ["-", 205, 213, 3, 4, 0, 28, 0, 218, 8, 203], ["+", 3, 4, 0, 28, 0, 218, 8, 214, 205, 199], ["+", 3, 4, 0, 28, 0, 218, 8, 214, 0, 131], ["+", 3, 4, 0, 28, 0, 218, 8, 214, 141, 22], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199]]
8
234
14
using System; using System.Linq; public class Hello { public static void Main() { var input = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var n = input[0]; var k = input[1]; var a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var dp = new long[n]; for (var i = 1; i < n; i++) { dp[i] = 10000; for (var j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = Math.Min(dp[i], Math.Abs(a[i] - a[i - j]) + dp[i - j]); } } } Console.WriteLine(dp[n - 1]); } }
using System; using System.Linq; using System.Collections.Generic; public class Hello { public static void Main() { var input = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var n = input[0]; var k = input[1]; var a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); var dp = new long[n]; for (var i = 1; i < n; i++) { dp[i] = long.MaxValue; for (var j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = Math.Min(dp[i], dp[i - j] + Math.Abs(a[i] - a[i - j])); } } } Console.WriteLine(dp[n - 1]); } }
[["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 233], ["+", 0, 208, 0, 231, 141, 232, 234, 232, 234, 22], ["+", 0, 208, 0, 231, 141, 232, 234, 232, 0, 131], ["+", 0, 208, 0, 231, 141, 232, 234, 232, 141, 22], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 0, 131], ["+", 36, 36, 0, 208, 0, 231, 141, 232, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 231, 0, 35], ["-", 0, 7, 8, 196, 0, 1, 0, 11, 12, 203], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 205, 199], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 16, 31, 204, 205, 22], ["+", 0, 28, 0, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 22], ["+", 0, 28, 0, 16, 31, 204, 206, 207, 0, 73], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 3, 4, 0, 28, 0, 16, 12, 204, 205, 22], ["-", 0, 28, 0, 16, 12, 204, 206, 207, 0, 70], ["-", 12, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["-", 12, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["-", 12, 204, 206, 207, 0, 28, 0, 16, 12, 22], ["-", 0, 28, 0, 16, 12, 204, 206, 207, 0, 73]]
8
200
25
using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { string[] NK = Console.ReadLine().Split(' '); int N = int.Parse(NK[0]); int K = int.Parse(NK[1]); string[] Input = Console.ReadLine().Split(' '); int[] h = new int[N]; // DPテーブル int[] dp = new int[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(Input[i]); } //入力完了 // dp[0] = 0; // dp[1] = Math.Abs(h[1] - h[0]); for (int i = 0; i < K; i++) { dp[i] = Math.Abs(h[i] - h[0]); } int tmp = 0; for (int i = K; i < N; i++) { // 100回やる // tmp = 2147483647; for (int j = 1; j <= K; j++) { if (j == 1) { tmp = dp[i - j] + Math.Abs(h[i] - h[i - j]); } if (tmp > dp[i - j] + Math.Abs(h[i] - h[i - j])) tmp = dp[i - j] + Math.Abs(h[i] - h[i - j]); } dp[i] = tmp; } Console.WriteLine(dp[N - 1]); } }
using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { string[] NK = Console.ReadLine().Split(' '); int N = int.Parse(NK[0]); int K = int.Parse(NK[1]); if (K > N) K = N; string[] Input = Console.ReadLine().Split(' '); int[] h = new int[N]; // DPテーブル int[] dp = new int[N]; for (int i = 0; i < N; i++) { h[i] = int.Parse(Input[i]); } //入力完了 // dp[0] = 0; // dp[1] = Math.Abs(h[1] - h[0]); for (int i = 0; i < K; i++) { dp[i] = Math.Abs(h[i] - h[0]); } int tmp = 0; for (int i = K; i < N; i++) { // 100回やる // tmp = 2147483647; for (int j = 1; j <= K; j++) { if (j == 1) { tmp = dp[i - j] + Math.Abs(h[i] - h[i - j]); } if (tmp > dp[i - j] + Math.Abs(h[i] - h[i - j])) tmp = dp[i - j] + Math.Abs(h[i] - h[i - j]); } dp[i] = tmp; } Console.WriteLine(dp[N - 1]); } }
[["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 121], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 24], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 31, 22], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 12, 22], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 25], ["+", 8, 196, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 196, 0, 57, 64, 1, 0, 11, 12, 22], ["+", 0, 195, 8, 196, 0, 57, 64, 1, 0, 35]]
8
327
10
using System; using System.Collections.Generic; using System.Linq; class Procon { public Procon() {} static void Main(string[] agrs) { new Procon().Do(); } Scanner cin; public void Do() { cin = new Scanner(); int N = cin.nextInt(); int[,] abc = new int[N, 3]; int[,] DP = new int[N + 1, 3]; for (int i = 0; i < N; i++) { abc[i, 0] = cin.nextInt(); abc[i, 1] = cin.nextInt(); abc[i, 2] = cin.nextInt(); } for (int i = 1; i < N + 1; i++) { for (int j = 0; j < 3; j++) { DP[i, (j + 1) % 3] = Math.Max(DP[i, (j + 1) % 3], DP[i - 1, j] + abc[i - 1, (j + 1) % 3]); DP[i, (j + 2) % 3] = Math.Max(DP[i, (j + 2) % 3], DP[i - 1, j] + abc[i - 1, (j + 2) % 3]); } Console.WriteLine("{0} {1} {2}", DP[i, 0], DP[i, 1], DP[i, 2]); } Console.WriteLine(Math.Max(Math.Max(DP[N, 0], DP[N, 1]), DP[N, 2])); } } class Scanner { string[] s; int i; int max_i; public Scanner() { s = new string[0]; max_i = 0; i = 0; } public string nextString() { if (i >= s.Length) { s = Console.ReadLine().Split(' '); max_i = s.Length; i = 0; if (max_i == 0) { return ""; } return s[i++]; } else { return s[i++]; } } public int nextInt() { return int.Parse(nextString()); } public long nextLong() { return long.Parse(nextString()); } public double nextDouble() { return double.Parse(nextString()); } public string[] arrayString() { return Console.ReadLine().Split(' '); } public int[] arrayInt() { string[] str = Console.ReadLine().Split(' '); int[] array = new int[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = int.Parse(str[i]); } return array; } public long[] arrayLong() { string[] str = Console.ReadLine().Split(' '); long[] array = new long[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = long.Parse(str[i]); } return array; } public double[] arrayDouble() { string[] str = Console.ReadLine().Split(' '); double[] array = new double[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = double.Parse(str[i]); } return array; } }
using System; using System.Collections.Generic; using System.Linq; class Procon { public Procon() {} static void Main(string[] agrs) { new Procon().Do(); } Scanner cin; public void Do() { cin = new Scanner(); int N = cin.nextInt(); int[,] abc = new int[N, 3]; int[,] DP = new int[N + 1, 3]; for (int i = 0; i < N; i++) { abc[i, 0] = cin.nextInt(); abc[i, 1] = cin.nextInt(); abc[i, 2] = cin.nextInt(); } for (int i = 1; i < N + 1; i++) { for (int j = 0; j < 3; j++) { DP[i, (j + 1) % 3] = Math.Max(DP[i, (j + 1) % 3], DP[i - 1, j] + abc[i - 1, (j + 1) % 3]); DP[i, (j + 2) % 3] = Math.Max(DP[i, (j + 2) % 3], DP[i - 1, j] + abc[i - 1, (j + 2) % 3]); } } Console.WriteLine(Math.Max(Math.Max(DP[N, 0], DP[N, 1]), DP[N, 2])); } } class Scanner { string[] s; int i; int max_i; public Scanner() { s = new string[0]; max_i = 0; i = 0; } public string nextString() { if (i >= s.Length) { s = Console.ReadLine().Split(' '); max_i = s.Length; i = 0; if (max_i == 0) { return ""; } return s[i++]; } else { return s[i++]; } } public int nextInt() { return int.Parse(nextString()); } public long nextLong() { return long.Parse(nextString()); } public double nextDouble() { return double.Parse(nextString()); } public string[] arrayString() { return Console.ReadLine().Split(' '); } public int[] arrayInt() { string[] str = Console.ReadLine().Split(' '); int[] array = new int[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = int.Parse(str[i]); } return array; } public long[] arrayLong() { string[] str = Console.ReadLine().Split(' '); long[] array = new long[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = long.Parse(str[i]); } return array; } public double[] arrayDouble() { string[] str = Console.ReadLine().Split(' '); double[] array = new double[str.Length]; for (int i = 0; i < str.Length; i++) { array[i] = double.Parse(str[i]); } return array; } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 62], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 21], ["-", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 21], ["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
776
30
using System; using System.Collections.Generic; using System.Numerics; using System.Linq; using static System.Math; class MainClass { public static void Main(string[] args) { var _ = LnInts(); var N = _[0]; var W = _[1]; var Vs = new int [N][]; for (var i = 0; i < N; i++) { Vs[i] = LnInts(); } var dp = new int [N + 1][]; for (int i = 0; i < dp.Length; i++) { dp[i] = new int[W + 1]; } dp[0][1] = 0; for (int i = 0; i < N; i++) { for (int w = 1; w <= W; w++) { if (w >= Vs[i][0]) { dp[i + 1][w] = Max(dp[i][w], dp[i][w - Vs[i][0]] + Vs[i][1]); } else { dp[i + 1][w] = dp[i][w]; } } } Console.WriteLine(dp[N][W]); } public static long Gcd(long a, long b) { if (a > b) { var r = a % b; if (r == 0) return b; else return Gcd(b, r); } else { var r = b % a; if (r == 0) return a; else return Gcd(a, r); } } public static int Gcd(int a, int b) { if (a > b) { var r = a % b; if (r == 0) return b; else return Gcd(b, r); } else { var r = b % a; if (r == 0) return a; else return Gcd(a, r); } } public static int[] LnInts() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ins = new int[vs.Length]; for (int i = 0; i < vs.Length; i++) ins[i] = int.Parse(vs[i]); return ins; } public static int LnInt() { var vs = Console.ReadLine().Trim(); return int.Parse(vs); } public static long[] LnLngs() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ins = new long[vs.Length]; for (int i = 0; i < vs.Length; i++) ins[i] = long.Parse(vs[i]); return ins; } public static long LnLng() { var vs = Console.ReadLine().Trim(); return long.Parse(vs); } public static string[] LnStrs() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return vs; } public static string LnStr() { var vs = Console.ReadLine().Trim(); return vs; } }
using System; using System.Collections.Generic; using System.Numerics; using System.Linq; using static System.Math; class MainClass { public static void Main(string[] args) { var _ = LnInts(); var N = _[0]; var W = _[1]; var Vs = new long [N][]; for (var i = 0; i < N; i++) { Vs[i] = LnLngs(); } var dp = new long [N + 1][]; for (int i = 0; i < dp.Length; i++) { dp[i] = new long[W + 1]; } dp[0][1] = 0; for (int i = 0; i < N; i++) { for (int w = 0; w <= W; w++) { if (w >= Vs[i][0]) { dp[i + 1][w] = Max(dp[i][w], dp[i][w - Vs[i][0]] + Vs[i][1]); } dp[i + 1][w] = Max(dp[i + 1][w], dp[i][w]); } } Console.WriteLine(dp[N][W]); } public static long Gcd(long a, long b) { if (a > b) { var r = a % b; if (r == 0) return b; else return Gcd(b, r); } else { var r = b % a; if (r == 0) return a; else return Gcd(a, r); } } public static int Gcd(int a, int b) { if (a > b) { var r = a % b; if (r == 0) return b; else return Gcd(b, r); } else { var r = b % a; if (r == 0) return a; else return Gcd(a, r); } } public static int[] LnInts() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ins = new int[vs.Length]; for (int i = 0; i < vs.Length; i++) ins[i] = int.Parse(vs[i]); return ins; } public static int LnInt() { var vs = Console.ReadLine().Trim(); return int.Parse(vs); } public static long[] LnLngs() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ins = new long[vs.Length]; for (int i = 0; i < vs.Length; i++) ins[i] = long.Parse(vs[i]); return ins; } public static long LnLng() { var vs = Console.ReadLine().Trim(); return long.Parse(vs); } public static string[] LnStrs() { var vs = Console.ReadLine().Trim().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return vs; } public static string LnStr() { var vs = Console.ReadLine().Trim(); return vs; } }
[["-", 0, 212, 0, 227, 39, 224, 39, 224, 39, 199], ["+", 0, 212, 0, 227, 39, 224, 39, 224, 39, 199], ["-", 8, 196, 0, 1, 0, 11, 12, 213, 63, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 213, 63, 22], ["-", 0, 1, 0, 11, 12, 227, 39, 224, 39, 199], ["+", 0, 1, 0, 11, 12, 227, 39, 224, 39, 199], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["-", 0, 7, 8, 196, 0, 57, 75, 196, 0, 45], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 204, 205, 204, 205, 22], ["+", 0, 28, 0, 204, 205, 204, 206, 207, 0, 70], ["+", 205, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 205, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 205, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 0, 28, 0, 204, 205, 204, 206, 207, 0, 73], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 213, 3, 4, 0, 25], ["-", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46]]
8
720
26
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using static System.Console; using static System.Math; using System.Reflection; namespace CP { class Atria { static void Main(string[] args) { int[] nw = ReadLine().Split(' ').Select(int.Parse).ToArray(); int n = nw[0]; int W = nw[1]; var ls = new List<MyClass>(); for (int i = 0; i < n; i++) { var input = ReadLine().Split(' ').Select(int.Parse).ToArray(); ls.Add(new MyClass(input[0], input[1])); } int[,] dp = new int[n + 1, W + 1]; for (int i = 0; i < n; i++) { for (int w = 0; w <= W; w++) { if (w >= ls[i].weight) { dp[i + 1, w] = Max(dp[i, w - ls[i].weight] + ls[i].value, dp[i, w]); } else dp[i + 1, w] = dp[i, w]; } } WriteLine(dp[n, W]); } class MyClass { public MyClass(int weight, int value) { this.value = value; this.weight = weight; } public int value { get; set; } public int weight { get; set; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using static System.Console; using static System.Math; using System.Reflection; namespace CP { class Atria { static void Main(string[] args) { int[] nw = ReadLine().Split(' ').Select(int.Parse).ToArray(); int n = nw[0]; int W = nw[1]; var ls = new List<MyClass>(); for (int i = 0; i < n; i++) { var input = ReadLine().Split(' ').Select(long.Parse).ToArray(); ls.Add(new MyClass(input[0], input[1])); } long[,] dp = new long[n + 1, W + 1]; for (int i = 0; i < n; i++) { for (int w = 0; w <= W; w++) { if (w >= ls[i].weight) { dp[i + 1, w] = Max(dp[i, w - ls[i].weight] + ls[i].value, dp[i, w]); } else dp[i + 1, w] = dp[i, w]; } } WriteLine(dp[n, W]); } class MyClass { public MyClass(long weight, long value) { this.weight = weight; this.value = value; } public long weight { get; set; } public long value { get; set; } } } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 8, 201, 0, 263, 54, 55, 0, 220, 39, 199], ["+", 8, 201, 0, 263, 54, 55, 0, 220, 39, 199], ["-", 8, 196, 0, 1, 0, 11, 31, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 11, 31, 214, 141, 22], ["-", 0, 263, 8, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 263, 8, 196, 0, 1, 0, 11, 12, 22], ["-", 8, 201, 0, 235, 8, 201, 0, 277, 39, 199], ["-", 8, 201, 0, 235, 8, 201, 0, 277, 141, 22], ["+", 8, 201, 0, 235, 8, 201, 0, 277, 39, 199], ["+", 8, 201, 0, 235, 8, 201, 0, 277, 141, 22]]
8
350
26
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; public class Hello { static public int mod = 1000000007; public static void Main() { int[] num = inta(); int volume = num[1]; long[,] dp = new long[num[0] + 1, volume + 1]; for (int i = 1; i <= num[0]; i++) { int[] read = inta(); for (int j = 0; j <= volume; j++) { if (j >= read[0]) { dp[i, j] = Max(dp[i - 1, j - read[0]] + read[1], dp[i - 1, j]); } } } WriteLine(dp[num[0], volume]); } // ncr mod static int ncr(int n, int r) { r = Min(r, n - r); long nn = 1; for (int i = n - r + 1; i <= n; i++) { nn = nn * i % mod; } long rr = 1; for (int i = 1; i <= r; i++) { rr = rr * i % mod; } rr = square((int)rr, mod - 2); nn = nn * rr % mod; return (int)nn; } // a^b mod static int square(int a, int b) { string binary = Convert.ToString(b, 2); int bileng = binary.Length; long a_power = a; long value = 1; for (int i = bileng - 1; i >= 0; i--) { if (binary[i] == '1') { value = value * a_power % mod; } a_power = a_power * a_power % mod; } return (int)value; } // static int square2(long a,long b,long m){var list = new List<int>();int // temp = 1;long n2 = a;list.Add(1);int count = 1;while(temp < // b){list.Add((int)n2);temp*=2;count++;n2 = n2*n2%m;}long ret = 1;while(b > // 0){if(temp <= b){b -= temp;ret = // ret*list[count]%1000000007;}count--;temp/=2;}return(int)ret;} 各種読取 static string rstr() { return ReadLine(); } static int rint() { return int.Parse(ReadLine()); } static long rlong() { return long.Parse(ReadLine()); } static string[] stra() { return ReadLine().Split(' '); } static int[] inta() { string[] read_str_array = ReadLine().Split(' '); int[] return_int_array = new int[read_str_array.Length]; for (int countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_int_array[countup_i] = int.Parse(read_str_array[countup_i]); } return return_int_array; } static int[,] inta2(int num_array, int in_array) { int[,] int_array2 = new int[num_array, in_array]; for (int i = 0; i < num_array; i++) { int[] temp_array = inta(); for (int j = 0; j < in_array; j++) { int_array2[i, j] = temp_array[j]; } } return int_array2; } static long[] longa() { string[] read_str_array = ReadLine().Split(' '); long[] return_long_array = new long[read_str_array.Length]; for (long countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_long_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_long_array; } static double[] doublea() { string[] read_str_array = ReadLine().Split(' '); double[] return_double_array = new double[read_str_array.Length]; for (long countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_double_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_double_array; } // ----------------------------- static long divideup(long divided, long divid) { long temp_divide = divided / divid; if (divided % divid > 0) { temp_divide++; } return temp_divide; } static long GCD(long a, long b) { if (a < b) { long temp = a; a = b; b = temp; } if (a % b == 0) { return b; } else { long temp = b; b = a % b; a = temp; return GCD(a, b); } } static long LCM(long a, long b) { return a * b / GCD(a, b); } static long Max3(long a, long b, long c) { long r = Max(a, b); r = Max(r, c); return r; } } //知見 // (string) hogestr -> (char[]) hoge // char hoge = hogestr.ToCharArray(); // (char[]) hoge -> (string) hogestr // string hoge = new string(hoge); // Hashtable × Dictionary ○ // Dictionary hoge<string,int> // hoge.ContainsKey(string) -> return boolean // hoge[string] -> return int // hoge[int] -> CE // Dictionary hoge<int1,int2> // hoge<int1,int2>: hoge[int1] -> return int2 // KeyValuePair<int,int> k <-> k.Key == int1 / k.Value == int2 // SortedDictionary() > SortedList() ??? // キュー -> 幅優先 ??? // var queue = new Queue<int>(); queue.Enqueue() queue.Dequeue() // スタック -> 深さ優先 ??? // var stack = new Stack<int>(); stack.Push(hoge) stack.Pop() stackPeek() // stack.Count // if (list?.Count > 0) リストが空でもnullでもない // ソート // list.Sort(StringComparer.Ordinal); listの辞書順ソート // StringComparer.Ordinal とは // 出力 // foreach(var a int list){ WriteLine(a); } より // WriteLine(string.Join("\n", list)); が速い
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math; public class Hello { static public int mod = 1000000007; public static void Main() { int[] num = inta(); int volume = num[1]; long[,] dp = new long[num[0] + 1, volume + 1]; for (int i = 1; i <= num[0]; i++) { int[] read = inta(); for (int j = 0; j <= volume; j++) { if (j >= read[0]) { dp[i, j] = Max(dp[i - 1, j - read[0]] + read[1], dp[i - 1, j]); } else { dp[i, j] = dp[i - 1, j]; } } } WriteLine(dp[num[0], volume]); } // ncr mod static int ncr(int n, int r) { r = Min(r, n - r); long nn = 1; for (int i = n - r + 1; i <= n; i++) { nn = nn * i % mod; } long rr = 1; for (int i = 1; i <= r; i++) { rr = rr * i % mod; } rr = square((int)rr, mod - 2); nn = nn * rr % mod; return (int)nn; } // a^b mod static int square(int a, int b) { string binary = Convert.ToString(b, 2); int bileng = binary.Length; long a_power = a; long value = 1; for (int i = bileng - 1; i >= 0; i--) { if (binary[i] == '1') { value = value * a_power % mod; } a_power = a_power * a_power % mod; } return (int)value; } // static int square2(long a,long b,long m){var list = new List<int>();int // temp = 1;long n2 = a;list.Add(1);int count = 1;while(temp < // b){list.Add((int)n2);temp*=2;count++;n2 = n2*n2%m;}long ret = 1;while(b > // 0){if(temp <= b){b -= temp;ret = // ret*list[count]%1000000007;}count--;temp/=2;}return(int)ret;} 各種読取 static string rstr() { return ReadLine(); } static int rint() { return int.Parse(ReadLine()); } static long rlong() { return long.Parse(ReadLine()); } static string[] stra() { return ReadLine().Split(' '); } static int[] inta() { string[] read_str_array = ReadLine().Split(' '); int[] return_int_array = new int[read_str_array.Length]; for (int countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_int_array[countup_i] = int.Parse(read_str_array[countup_i]); } return return_int_array; } static int[,] inta2(int num_array, int in_array) { int[,] int_array2 = new int[num_array, in_array]; for (int i = 0; i < num_array; i++) { int[] temp_array = inta(); for (int j = 0; j < in_array; j++) { int_array2[i, j] = temp_array[j]; } } return int_array2; } static long[] longa() { string[] read_str_array = ReadLine().Split(' '); long[] return_long_array = new long[read_str_array.Length]; for (long countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_long_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_long_array; } static double[] doublea() { string[] read_str_array = ReadLine().Split(' '); double[] return_double_array = new double[read_str_array.Length]; for (long countup_i = 0; countup_i < read_str_array.Length; countup_i++) { return_double_array[countup_i] = long.Parse(read_str_array[countup_i]); } return return_double_array; } // ----------------------------- static long divideup(long divided, long divid) { long temp_divide = divided / divid; if (divided % divid > 0) { temp_divide++; } return temp_divide; } static long GCD(long a, long b) { if (a < b) { long temp = a; a = b; b = temp; } if (a % b == 0) { return b; } else { long temp = b; b = a % b; a = temp; return GCD(a, b); } } static long LCM(long a, long b) { return a * b / GCD(a, b); } static long Max3(long a, long b, long c) { long r = Max(a, b); r = Max(r, c); return r; } } //知見 // (string) hogestr -> (char[]) hoge // char hoge = hogestr.ToCharArray(); // (char[]) hoge -> (string) hogestr // string hoge = new string(hoge); // Hashtable × Dictionary ○ // Dictionary hoge<string,int> // hoge.ContainsKey(string) -> return boolean // hoge[string] -> return int // hoge[int] -> CE // Dictionary hoge<int1,int2> // hoge<int1,int2>: hoge[int1] -> return int2 // KeyValuePair<int,int> k <-> k.Key == int1 / k.Value == int2 // SortedDictionary() > SortedList() ??? // キュー -> 幅優先 ??? // var queue = new Queue<int>(); queue.Enqueue() queue.Dequeue() // スタック -> 深さ優先 ??? // var stack = new Stack<int>(); stack.Push(hoge) stack.Pop() stackPeek() // stack.Count // if (list?.Count > 0) リストが空でもnullでもない // ソート // list.Sort(StringComparer.Ordinal); listの辞書順ソート // StringComparer.Ordinal とは // 出力 // foreach(var a int list){ WriteLine(a); } より // WriteLine(string.Join("\n", list)); が速い
[["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["+", 0, 7, 8, 196, 0, 57, 75, 196, 0, 45], ["+", 75, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 21], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 75, 196, 0, 1, 0, 11, 12, 204, 205, 22], ["+", 0, 1, 0, 11, 12, 204, 206, 207, 0, 70], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 0, 1, 0, 11, 12, 204, 206, 207, 0, 21], ["+", 0, 11, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 12, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 75, 196, 0, 1, 0, 35], ["+", 0, 7, 8, 196, 0, 7, 8, 196, 0, 46]]
8
979
19
using System; using System.Linq; class DP { static void Main(string[] args) { var st = Console.ReadLine().Split().Select(long.Parse).ToArray(); long N = st[0]; long W = st[1]; var w = new long[N]; var v = new long[N]; for (int i = 0; i < N; i++) { var x = Console.ReadLine().Split().Select(long.Parse).ToArray(); w[i] = x[0]; v[i] = x[1]; } //考えうる価値の最大値 long max_value = N * 1000 + 100; //少し大きめに配列を確保 var dp = new long[N + 2, max_value + 10]; long max_weight = 1000000000L * N + 100L; //値は全て大きな値で初期化する for (int i = 0; i < N; i++) { for (int j = 0; j <= max_value; j++) { dp[i, j] = max_weight; } } for (int i = 0; i < N; i++) { dp[i, 0] = 0; } for (int j = 1; j < max_value; j++) { if (j <= v[0]) { dp[0, j] = w[0]; } else { dp[0, j] = 0; } } for (int i = 0; i < N - 1; i++) { for (int j = 0; j <= max_value; j++) { long kari = dp[i, j]; if (j - v[i + 1] >= 0) { kari = Math.Min(kari, dp[i, j - v[i + 1]] + w[i + 1]); } dp[i + 1, j] = kari; } } long ans = 0; for (int j = 0; j <= max_value; j++) { if (dp[N - 1, j] <= W) { ans = j; } } Console.WriteLine(ans); } }
using System; using System.Linq; class DP { static void Main(string[] args) { var st = Console.ReadLine().Split().Select(long.Parse).ToArray(); long N = st[0]; long W = st[1]; var w = new long[N]; var v = new long[N]; for (int i = 0; i < N; i++) { var x = Console.ReadLine().Split().Select(long.Parse).ToArray(); w[i] = x[0]; v[i] = x[1]; } //考えうる価値の最大値 long max_value = N * 1000 + 100; //少し大きめに配列を確保 var dp = new long[N + 2, max_value + 10]; long max_weight = 1000000000L * N + 100L; //値は全て大きな値で初期化する for (int i = 0; i < N; i++) { for (int j = 0; j <= max_value; j++) { dp[i, j] = max_weight; } } for (int i = 0; i < N; i++) { dp[i, 0] = 0; } for (int j = 1; j < max_value; j++) { if (j <= v[0]) { dp[0, j] = w[0]; } } for (int i = 0; i < N - 1; i++) { for (int j = 0; j <= max_value; j++) { long kari = dp[i, j]; if (j - v[i + 1] >= 0) { kari = Math.Min(kari, dp[i, j - v[i + 1]] + w[i + 1]); } dp[i + 1, j] = kari; } } long ans = 0; for (int j = 0; j <= max_value; j++) { if (dp[N - 1, j] <= W) { ans = j; } } Console.WriteLine(ans); } }
[["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["-", 0, 7, 8, 196, 0, 57, 75, 196, 0, 45], ["-", 75, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 203], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 21], ["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["-", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 0, 57, 75, 196, 0, 1, 0, 11, 12, 203], ["-", 8, 196, 0, 57, 75, 196, 0, 1, 0, 35], ["-", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46]]
8
441
12
using System; namespace kozaiyApp { public class EKnapsack2 { static void Main() { int ans = 0; string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int Knapsack = int.Parse(input[1]); long[] W = new long[N]; long[] V = new long[N]; int totalV = 0; for (int i = 0; i < N; i++) { string[] inputData = Console.ReadLine().Split(' '); W[i] = long.Parse(inputData[0]); V[i] = long.Parse(inputData[1]); totalV += int.Parse(inputData[1]); } long[,] DP = new long[N + 1, totalV + 1]; for (int i = 0; i <= N; i++) { for (int j = 0; j <= totalV; j++) { if (i == 0) { DP[i, j] = 0; } else if (V[i - 1] > j) { DP[i, j] = DP[i - 1, j]; } else { DP[i, j] = Math.Min(DP[i - 1, j], W[i - 1] + DP[i - 1, j - V[i - 1]]); } } } for (int j = totalV; j >= 0; j--) { if (DP[N, j] <= Knapsack) { ans += j; break; } } Console.WriteLine(ans); } } }
using System; namespace kozaiyApp { public class EKnapsack2 { static void Main() { int ans = 0; string[] input = Console.ReadLine().Split(' '); int N = int.Parse(input[0]); int Knapsack = int.Parse(input[1]); long[] W = new long[N]; long[] V = new long[N]; int totalV = 0; for (int i = 0; i < N; i++) { string[] inputData = Console.ReadLine().Split(' '); W[i] = long.Parse(inputData[0]); V[i] = long.Parse(inputData[1]); totalV += int.Parse(inputData[1]); } long[,] DP = new long[N + 1, totalV + 1]; for (int i = 0; i <= N; i++) { for (int j = 0; j <= totalV; j++) { if (i == 0 && j == 0) { DP[i, j] = 0; } else if (i == 0) { DP[i, j] = 100000000000; } else if (V[i - 1] > j) { DP[i, j] = DP[i - 1, j]; } else { DP[i, j] = Math.Min(DP[i - 1, j], W[i - 1] + DP[i - 1, j - V[i - 1]]); } } } for (int j = totalV; j >= 0; j--) { if (DP[N, j] <= Knapsack) { ans += j; break; } } Console.WriteLine(ans); } } }
[["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 60], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 203], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["+", 0, 7, 8, 196, 0, 57, 75, 57, 0, 121], ["+", 0, 7, 8, 196, 0, 57, 75, 57, 0, 24], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 31, 22], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 75, 57, 0, 25], ["+", 8, 196, 0, 57, 75, 57, 64, 196, 0, 45], ["+", 64, 196, 0, 1, 0, 11, 31, 204, 205, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 70], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 21], ["+", 0, 1, 0, 11, 31, 204, 206, 207, 0, 73], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 12, 203]]
8
357
22
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { var nw = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int n = nw[0]; int w = nw[1]; var wv = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { var temp = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); wv[i] = new Tuple<long, long>(temp[0], temp[1]); } long max = wv.Sum(x => x.Item2); var dp = new long[n + 1, max + 1]; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < max + 1; j++) { dp[i, j] = -1; } } dp[0, 0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < max + 1; j++) { if (dp[i - 1, j] == -1) { continue; } long nextV = j + wv[i - 1].Item2; long nextW = dp[i - 1, j] + wv[i - 1].Item1; if (nextW <= w && nextV <= max) { dp[i, nextV] = Math.Max(dp[i, nextV], nextW); } dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j]); } } long ans = 0; for (int i = 0; i < max + 1; i++) { if (dp[n, i] != -1) { ans = Math.Max(ans, i); } } Console.WriteLine(ans); } } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { var nw = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int n = nw[0]; int w = nw[1]; var wv = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { var temp = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); wv[i] = new Tuple<long, long>(temp[0], temp[1]); } long max = wv.Sum(x => x.Item2); var dp = new long[n + 1, max + 1]; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < max + 1; j++) { dp[i, j] = long.MaxValue; } } dp[0, 0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < max + 1; j++) { if (dp[i - 1, j] == long.MaxValue) { continue; } long nextV = j + wv[i - 1].Item2; long nextW = dp[i - 1, j] + wv[i - 1].Item1; if (nextW <= w && nextV <= max) { dp[i, nextV] = Math.Min(dp[i, nextV], nextW); } dp[i, j] = Math.Min(dp[i, j], dp[i - 1, j]); } } long ans = 0; for (int i = 0; i < max + 1; i++) { if (dp[n, i] != long.MaxValue) { ans = Math.Max(ans, i); } } Console.WriteLine(ans); } } }
[["-", 8, 196, 0, 1, 0, 11, 12, 241, 0, 33], ["-", 8, 196, 0, 1, 0, 11, 12, 241, 0, 203], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 205, 199], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 11, 12, 214, 141, 22], ["-", 8, 196, 0, 57, 15, 16, 12, 241, 0, 33], ["-", 8, 196, 0, 57, 15, 16, 12, 241, 0, 203], ["+", 8, 196, 0, 57, 15, 16, 12, 214, 205, 199], ["+", 8, 196, 0, 57, 15, 16, 12, 214, 0, 131], ["+", 8, 196, 0, 57, 15, 16, 12, 214, 141, 22], ["-", 0, 1, 0, 11, 12, 213, 63, 214, 141, 22], ["+", 0, 1, 0, 11, 12, 213, 63, 214, 141, 22]]
8
473
19
using System; using System.Linq; using System.Collections; using System.Collections.Generic; class Program { struct cell { public long val; public long weight; public cell(long w, long v) { val = v; weight = w; } public static cell operator +(cell lhs, cell rhs) { return new cell(lhs.weight + rhs.weight, lhs.val + rhs.val); } } public static void Main() { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]); int W = int.Parse(s[1]); cell[] List = new cell[N]; long[][] MinW = new long [N + 1][]; long VSum = 0; for (int i = 0; i < N; i++) { s = Console.ReadLine().Split(' '); List[i] = new cell(int.Parse(s[0]), int.Parse(s[1])); VSum += List[i].val; } for (int i = 0; i <= N; i++) MinW[i] = new long[VSum + 1]; for (int i = 0; i <= N; i++) { for (int j = 1; j <= VSum; j++) { MinW[i][j] = long.MaxValue / 2; } } for (int i = 0; i < N; i++) { for (long j = i; j <= N; j++) { MinW[j][List[i].val] = Math.Min(List[i].weight, MinW[j][List[i].val]); } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= VSum; j++) { if (j >= List[i - 1].val) { if (MinW[i - 1][j] > MinW[i - 1][j - List[i - 1].val] + List[i - 1].weight) MinW[i][j] = MinW[i - 1][j - List[i - 1].val] + List[i - 1].weight; else MinW[i][j] = MinW[i - 1][j]; } else MinW[i][j] = MinW[i - 1][j]; } } Console.WriteLine(Array.FindLastIndex(MinW[N], x => W >= x)); } }
using System; using System.Linq; using System.Collections; using System.Collections.Generic; class Program { struct cell { public long val; public long weight; public cell(long w, long v) { val = v; weight = w; } public static cell operator +(cell lhs, cell rhs) { return new cell(lhs.weight + rhs.weight, lhs.val + rhs.val); } } public static void Main() { string[] s = Console.ReadLine().Split(' '); int N = int.Parse(s[0]); int W = int.Parse(s[1]); cell[] List = new cell[N + 1]; long[][] MinW = new long [N + 1][]; long VSum = 0; for (int i = 1; i <= N; i++) { s = Console.ReadLine().Split(' '); List[i] = new cell(int.Parse(s[0]), int.Parse(s[1])); VSum += List[i].val; } for (int i = 0; i <= N; i++) MinW[i] = new long[VSum + 1]; for (int i = 0; i <= N; i++) { for (int j = 1; j <= VSum; j++) MinW[i][j] = long.MaxValue; } for (int i = 1; i <= N; i++) { for (long j = i; j <= N; j++) { MinW[j][List[i].val] = Math.Min(List[i].weight, MinW[j][List[i].val]); } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= VSum; j++) { if (j >= List[i].val) { if (MinW[i - 1][j] - List[i].weight > MinW[i - 1][j - List[i].val]) MinW[i][j] = MinW[i - 1][j - List[i].val] + List[i].weight; else MinW[i][j] = MinW[i - 1][j]; } else MinW[i][j] = MinW[i - 1][j]; } } Console.WriteLine(Array.FindLastIndex(MinW[N], x => W >= x)); } }
[["+", 0, 227, 39, 224, 225, 226, 0, 16, 17, 72], ["+", 0, 227, 39, 224, 225, 226, 0, 16, 12, 203], ["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 196, 0, 7, 8, 196, 0, 45], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 85], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 46], ["-", 205, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["-", 205, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 15, 16, 31, 16, 31, 204, 206, 207, 0, 73], ["+", 64, 196, 0, 57, 15, 16, 31, 16, 17, 33], ["+", 15, 16, 31, 16, 12, 214, 205, 204, 205, 22], ["+", 31, 16, 12, 214, 205, 204, 206, 207, 0, 70], ["+", 12, 214, 205, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 31, 16, 12, 214, 0, 131], ["+", 0, 57, 15, 16, 31, 16, 12, 214, 141, 22], ["-", 15, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["-", 64, 196, 0, 57, 15, 16, 12, 16, 17, 72], ["-", 15, 16, 12, 16, 12, 214, 205, 204, 205, 22], ["-", 12, 16, 12, 214, 205, 204, 206, 207, 0, 70], ["-", 205, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["-", 0, 57, 15, 16, 12, 16, 12, 214, 0, 131], ["-", 0, 57, 15, 16, 12, 16, 12, 214, 141, 22]]
8
569
38
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { string s = Console.ReadLine(); string t = Console.ReadLine(); int n = s.Length; int m = t.Length; var dp = new int[n + 1, m + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == t[j]) { dp[i + 1, j + 1] = Math.Max(dp[i + 1, j + 1], dp[i, j] + 1); } dp[i, j + 1] = Math.Max(dp[i, j + 1], dp[i, j]); dp[i + 1, j] = Math.Max(dp[i + 1, j], dp[i, j]); } } int ii = n; int jj = m; string ans = ""; while (ii > 0 && jj > 0) { if (dp[ii, jj] == dp[ii, jj - 1]) { jj--; } else if (dp[ii, jj] == dp[ii - 1, jj]) { ii--; } else { ii--; jj--; ans = s[ii] + ans; } } Console.WriteLine(ans); } } }
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { static void Main(string[] args) { string s = Console.ReadLine(); string t = Console.ReadLine(); int n = s.Length; int m = t.Length; var dp = new int[n + 1, m + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == t[j]) { dp[i + 1, j + 1] = Math.Max(dp[i + 1, j + 1], dp[i, j] + 1); } dp[i + 1, j + 1] = Math.Max(dp[i + 1, j + 1], dp[i + 1, j]); dp[i + 1, j + 1] = Math.Max(dp[i + 1, j + 1], dp[i, j + 1]); } } int ii = n; int jj = m; string ans = ""; while (ii > 0 && jj > 0) { if (dp[ii, jj] == dp[ii, jj - 1]) { jj--; } else if (dp[ii, jj] == dp[ii - 1, jj]) { ii--; } else { ii--; jj--; ans = s[ii] + ans; } } Console.WriteLine(ans); } } }
[["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 72], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
356
12
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Threading.Tasks; using System.Globalization; namespace Atcoder { class Program { static void Main(string[] args) { StreamScanner scan = new StreamScanner(System.Console.OpenStandardInput()); char[] s = scan.Scan().ToCharArray(); char[] t = scan.Scan().ToCharArray(); int[,] dp = new int[s.Length + 1, t.Length + 1]; for (int i = 1; i <= s.Length; i++) { for (int k = 1; k <= t.Length; k++) { int max = Math.Max(dp[i - 1, k], dp[i, k - 1]); if (s[i - 1] == t[k - 1]) { max = Math.Max(dp[i - 1, k - 1] + 1, max); } dp[i, k] = max; } } int x = s.Length; int y = t.Length; string res = ""; while (true) { if (x == 0 || y == 0) { break; } if (dp[x - 1, y - 1] + 1 == dp[x, y] && dp[x - 1, y] != dp[x, y] && dp[x, y - 1] != dp[x, y]) { res = s[x - 1] + res; x--; y--; } else { x--; } } Console.WriteLine(res); } static int nibutan(int med, int[] a) // med以上が何個あるか { int mae = 0; int ato = a.Length; while (true) { if (ato - mae <= 1) { return mae; } int mid = (mae + ato) / 2; if (a[mid] > med) { ato = mid; } if (a[mid] <= med) { mae = mid; } } } public static int h; public static int w; public static int BFS(int height, int width, char[,] map) { int[] dx = new int[] { -1, 0, 0, 1 }; int[] dy = new int[] { 0, 1, -1, 0 }; bool[,] visited = new bool[h, w]; int[,] len = new int[h, w]; var queue = new Queue<Tuple<int, int>>(); queue.Enqueue(new Tuple<int, int>(height, width)); int max = 0; visited[height, width] = true; while (queue.Count > 0) { var now = queue.Dequeue(); for (int i = 0; i < 4; i++) { int ny = now.Item1 + dy[i]; int nx = now.Item2 + dx[i]; if (ny >= h || ny < 0 || nx >= w || nx < 0) continue; if (visited[ny, nx] || map[ny, nx] == '#') continue; visited[ny, nx] = true; len[ny, nx] = len[now.Item1, now.Item2] + 1; max = Math.Max(max, len[ny, nx]); queue.Enqueue(new Tuple<int, int>(ny, nx)); } } return max; } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } private readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) throw new EndOfStreamException(); if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public long Long() { long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (int)Long(); } public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); } } public class UnionFind { public int[] data; public UnionFind(int size) { data = new int[size]; for (int i = 0; i < size; i++) data[i] = -1; } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x != y) { if (data[y] < data[x]) { var tmp = y; y = x; x = tmp; } data[x] += data[y]; data[y] = x; } return x != y; } public bool IsSameGroup(int x, int y) { return Root(x) == Root(y); } public int Root(int x) { return data[x] < 0 ? x : data[x] = Root(data[x]); } } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Threading.Tasks; using System.Globalization; namespace Atcoder { class Program { static void Main(string[] args) { StreamScanner scan = new StreamScanner(System.Console.OpenStandardInput()); char[] s = scan.Scan().ToCharArray(); char[] t = scan.Scan().ToCharArray(); int[,] dp = new int[s.Length + 1, t.Length + 1]; for (int i = 1; i <= s.Length; i++) { for (int k = 1; k <= t.Length; k++) { int max = Math.Max(dp[i - 1, k], dp[i, k - 1]); if (s[i - 1] == t[k - 1]) { max = Math.Max(dp[i - 1, k - 1] + 1, max); } dp[i, k] = max; } } int x = s.Length; int y = t.Length; string res = ""; while (true) { if (x == 0 || y == 0) { break; } if (dp[x - 1, y - 1] + 1 == dp[x, y] && dp[x - 1, y] != dp[x, y] && dp[x, y - 1] != dp[x, y]) { res = s[x - 1] + res; x--; y--; } else { if (dp[x - 1, y] == dp[x, y]) { x--; } else { y--; } } } Console.WriteLine(res); } static int nibutan(int med, int[] a) // med以上が何個あるか { int mae = 0; int ato = a.Length; while (true) { if (ato - mae <= 1) { return mae; } int mid = (mae + ato) / 2; if (a[mid] > med) { ato = mid; } if (a[mid] <= med) { mae = mid; } } } public static int h; public static int w; public static int BFS(int height, int width, char[,] map) { int[] dx = new int[] { -1, 0, 0, 1 }; int[] dy = new int[] { 0, 1, -1, 0 }; bool[,] visited = new bool[h, w]; int[,] len = new int[h, w]; var queue = new Queue<Tuple<int, int>>(); queue.Enqueue(new Tuple<int, int>(height, width)); int max = 0; visited[height, width] = true; while (queue.Count > 0) { var now = queue.Dequeue(); for (int i = 0; i < 4; i++) { int ny = now.Item1 + dy[i]; int nx = now.Item2 + dx[i]; if (ny >= h || ny < 0 || nx >= w || nx < 0) continue; if (visited[ny, nx] || map[ny, nx] == '#') continue; visited[ny, nx] = true; len[ny, nx] = len[now.Item1, now.Item2] + 1; max = Math.Max(max, len[ny, nx]); queue.Enqueue(new Tuple<int, int>(ny, nx)); } } return max; } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } private readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) throw new EndOfStreamException(); if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public long Long() { long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != '-' && (b < '0' || '9' < b)); if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (int)Long(); } public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); } } public class UnionFind { public int[] data; public UnionFind(int size) { data = new int[size]; for (int i = 0; i < size; i++) data[i] = -1; } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x != y) { if (data[y] < data[x]) { var tmp = y; y = x; x = tmp; } data[x] += data[y]; data[y] = x; } return x != y; } public bool IsSameGroup(int x, int y) { return Root(x) == Root(y); } public int Root(int x) { return data[x] < 0 ? x : data[x] = Root(data[x]); } } }
[["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 121], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 24], ["+", 75, 196, 0, 57, 15, 16, 31, 204, 205, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 21], ["+", 15, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 57, 75, 196, 0, 57, 15, 16, 17, 60], ["+", 75, 196, 0, 57, 15, 16, 12, 204, 205, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 70], ["+", 15, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 21], ["+", 0, 57, 15, 16, 12, 204, 206, 207, 0, 73], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 25], ["+", 0, 57, 75, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 57, 64, 196, 0, 1, 0, 223, 0, 68], ["+", 75, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 57, 75, 196, 0, 57, 64, 196, 0, 46], ["+", 8, 196, 0, 57, 75, 196, 0, 57, 0, 95], ["+", 0, 57, 75, 196, 0, 57, 75, 196, 0, 45], ["+", 0, 57, 75, 196, 0, 1, 0, 223, 0, 22], ["+", 0, 57, 75, 196, 0, 57, 75, 196, 0, 46]]
8
1,315
26
// ReSharper disable ArrangeTypeMemberModifiers // ReSharper disable ConvertIfStatementToSwitchStatement // ReSharper disable FunctionRecursiveOnAllPaths // ReSharper disable InconsistentNaming // ReSharper disable InlineOutVariableDeclaration // ReSharper disable InvertIf // ReSharper disable JoinDeclarationAndInitializer // ReSharper disable MemberCanBeMadeStatic.Global // ReSharper disable MemberCanBeMadeStatic.Local // ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable PossibleNullReferenceException // ReSharper disable RedundantUsingDirective // ReSharper disable SuggestVarOrType_BuiltInTypes // ReSharper disable SuggestVarOrType_Elsewhere // ReSharper disable TailRecursiveCall // ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Local // ReSharper disable UseObjectOrCollectionInitializert #if true && UTAKA_LOCAL //#if false #define UTAKA_DEBUG #endif using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text; namespace UtakaApp { public partial class Program { //========================================================================================= // Answer //========================================================================================= public const string SiteName = "AtCoder"; public const string ContestName = "dp"; public const string ProblemName = "F"; private readonly ConsoleInput cin; private readonly IConsole cou; public Program(ConsoleInput cin, IConsole cou) { this.cin = cin; this.cou = cou; } public static void Main(string[] args) { #if UTAKA_LOCAL //#if false new TestCaseCheckerForAtCoder().TestProblems(); #else var cin = new ConsoleInput(System.Console.In); var console = new MyConsole(); new Program(cin, console).Solve(); console.Flush(); #endif } public void Solve() { string S = cin.Read; string T = cin.Read; // dp[i][j] : Sのi文字目までとTのj文字目までのLCSの長さ int sLen = S.Length; int tLen = T.Length; long[][] dp = new long [sLen + 1][]; for (int i = 0; i < dp.Length; i++) { dp[i] = new long[tLen + 1]; } for (int i = 1; i <= sLen; i++) { for (int j = 1; j <= tLen; j++) { if (S[i - 1] == T[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.Max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int a = sLen; int b = tLen; while (dp[a][b] != 0) { if (dp[a - 1][b - 1] == dp[a][b] - 1 && S[a - 1] == T[b - 1]) { ans = S[a - 1] + ans; a--; b--; } else if (dp[a - 1][b] == dp[a][b]) { a--; } else { b--; } } cou.WriteLine(ans); // while (dp[a - 1][b - 1] == dp[a][b] - 1&& S[a - 1] == T[b - // 1]) // { // kl // } cou.WriteLine(dp[sLen][tLen]); } } //========================================================================================= // Library //========================================================================================= public partial class Program { /// <summary> /// 要素数 (a, b) の、defaultValue で満たされた配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <param name="defaultValue">デフォルト値</param> /// <returns>指定した条件で初期化された配列</returns> public static T[,] Array2D<T>(int a, int b, T defaultValue) where T : struct { var ret = new T[a, b]; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { ret[i, j] = defaultValue; } } return ret; } /// <summary> /// 要素数 (a, b) のdefalut値で満たされた配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <returns>初期化された配列</returns> public static T[,] Array2D<T>(int a, int b) where T : struct { var ret = new T[a, b]; return ret; } /// <summary> /// 要素数 (a, b) の、defaultValue で満たされたJag配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <param name="defaultValue">デフォルト値</param> /// <returns>指定した条件で初期化された配列</returns> public static T[][] JagArray2D<T>(int a, int b, T defaultValue) where T : struct { var ret = new T[a][]; for (int i = 0; i < a; i++) { ret[i] = Enumerable.Repeat(defaultValue, b).ToArray(); } return ret; } /// <summary> /// 要素数 (a, b) のdefalut値で満たされたJag配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <returns>初期化された配列</returns> public static T[][] JagArray2D<T>(int a, int b) where T : struct { var ret = new T[a][]; for (int i = 0; i < a; i++) { ret[i] = new T[b]; } return ret; } } //========================================================================================= // Debug IO //========================================================================================= public class ConsoleInput { private readonly char _separator = ' '; private readonly TextReader _stream; private readonly Queue<string> inputStream; public ConsoleInput(TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) { return inputStream.Dequeue(); } var tmp = _stream.ReadLine().Split(_separator); for (var i = 0; i < tmp.Length; ++i) { inputStream.Enqueue(tmp[i]); } return inputStream.Dequeue(); } } public string ReadLine => _stream.ReadLine(); public int ReadInt => int.Parse(Read); public long ReadLong => long.Parse(Read); public double ReadDouble => double.Parse(Read); public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) { ret[i] = Read; } return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) { ret[i] = ReadInt; } return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) { ret[i] = ReadLong; } return ret; } } public interface IConsole { void Flush(); void Write(object obj); void Write(string str); void WriteLine(object obj); void WriteLine(string str); } public class MyConsole : IConsole { public MyConsole() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } public void Flush() { Console.Out.Flush(); } public void Write(object obj) { Write(obj.ToString()); } public void Write(string str) { Console.Write(str); } public void WriteLine(object obj) { WriteLine(obj.ToString()); } public void WriteLine(string str) { Console.WriteLine(str); } } #if UTAKA_LOCAL public class DebugConsole : IConsole { private readonly StringBuilder mSb; public DebugConsole() { mSb = new StringBuilder(); } public void Flush() { // 何もしない } public void Write(object obj) { Write(obj.ToString()); } public void Write(string str) { mSb.Append(str); } public void WriteLine(object obj) { WriteLine(obj.ToString()); } public void WriteLine(string str) { mSb.AppendLine(str); } public string GetAllOutput() { return mSb.ToString(); } } #endif #if UTAKA_LOCAL public class TestCaseCheckerForAtCoder { private string GetDirectoryPath() { var problemPart = string.IsNullOrEmpty(Program.ProblemName) ? "" : $"/{Program.ProblemName}"; return $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/AlgorithmPrac/{Program.SiteName}/{Program.ContestName}{problemPart}"; } private string GetInputFilePath(int testCaseNumber) { return $"{GetDirectoryPath()}/sample-{testCaseNumber}.in"; } private string GetOutputFilePath(int testCaseNumber) { return $"{GetDirectoryPath()}/sample-{testCaseNumber}.out"; } public bool TestProblem(int testCaseNumber) { var inputFilePath = GetInputFilePath(testCaseNumber); var outputFilePath = GetOutputFilePath(testCaseNumber); TextReader inputStream = new StreamReader(inputFilePath); var cin = new ConsoleInput(inputStream); var debugConsoleWriter = new DebugConsole(); new Program(cin, debugConsoleWriter).Solve(); var output = debugConsoleWriter.GetAllOutput(); TextReader outputStream = new StreamReader(outputFilePath); var outputAnswer = outputStream.ReadToEnd(); Dbg.WriteLine(output); Dbg.WriteLine(outputAnswer); var isCorrect = output == outputAnswer; return isCorrect; } public void TestProblems(int targetTestCaseNumber = -1) { bool isSuccessAll = true; int testCaseNumber = 0; while (true) { testCaseNumber++; if (targetTestCaseNumber >= 0 && targetTestCaseNumber != testCaseNumber) { continue; } var inputFileName = GetInputFilePath(testCaseNumber); if (!File.Exists(inputFileName)) { break; } Console.WriteLine( $"TestCase {testCaseNumber} ====================================================="); var result = TestProblem(testCaseNumber); if (result) { Console.WriteLine("Success"); } else { isSuccessAll = false; Console.WriteLine("Failure *****"); } } if (isSuccessAll) { Console.WriteLine("!!!!!!!!! All Success !!!!!!!!!"); } } } #endif #if UTAKA_LOCAL public static class Dbg { public static void WriteLine(string str) { Console.WriteLine(str); } public static void Write(string str) { Console.Write(str); } } #else public static class Dbg { public static void WriteLine(string str) {} public static void Write(string str) {} } #endif }
// ReSharper disable ArrangeTypeMemberModifiers // ReSharper disable ConvertIfStatementToSwitchStatement // ReSharper disable FunctionRecursiveOnAllPaths // ReSharper disable InconsistentNaming // ReSharper disable InlineOutVariableDeclaration // ReSharper disable InvertIf // ReSharper disable JoinDeclarationAndInitializer // ReSharper disable MemberCanBeMadeStatic.Global // ReSharper disable MemberCanBeMadeStatic.Local // ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable PossibleNullReferenceException // ReSharper disable RedundantUsingDirective // ReSharper disable SuggestVarOrType_BuiltInTypes // ReSharper disable SuggestVarOrType_Elsewhere // ReSharper disable TailRecursiveCall // ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Local // ReSharper disable UseObjectOrCollectionInitializert #if true && UTAKA_LOCAL //#if false #define UTAKA_DEBUG #endif using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text; namespace UtakaApp { public partial class Program { //========================================================================================= // Answer //========================================================================================= public const string SiteName = "AtCoder"; public const string ContestName = "dp"; public const string ProblemName = "F"; private readonly ConsoleInput cin; private readonly IConsole cou; public Program(ConsoleInput cin, IConsole cou) { this.cin = cin; this.cou = cou; } public static void Main(string[] args) { #if UTAKA_LOCAL //#if false new TestCaseCheckerForAtCoder().TestProblems(); #else var cin = new ConsoleInput(System.Console.In); var console = new MyConsole(); new Program(cin, console).Solve(); console.Flush(); #endif } public void Solve() { string S = cin.Read; string T = cin.Read; // dp[i][j] : Sのi文字目までとTのj文字目までのLCSの長さ int sLen = S.Length; int tLen = T.Length; long[][] dp = new long [sLen + 1][]; for (int i = 0; i < dp.Length; i++) { dp[i] = new long[tLen + 1]; } for (int i = 1; i <= sLen; i++) { for (int j = 1; j <= tLen; j++) { if (S[i - 1] == T[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.Max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int a = sLen; int b = tLen; while (dp[a][b] != 0) { if (dp[a - 1][b - 1] == dp[a][b] - 1 && S[a - 1] == T[b - 1]) { ans = S[a - 1] + ans; a--; b--; } else if (dp[a - 1][b] == dp[a][b]) { a--; } else { b--; } } cou.WriteLine(ans); // while (dp[a - 1][b - 1] == dp[a][b] - 1&& S[a - 1] == T[b - // 1]) // { // kl // } } } //========================================================================================= // Library //========================================================================================= public partial class Program { /// <summary> /// 要素数 (a, b) の、defaultValue で満たされた配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <param name="defaultValue">デフォルト値</param> /// <returns>指定した条件で初期化された配列</returns> public static T[,] Array2D<T>(int a, int b, T defaultValue) where T : struct { var ret = new T[a, b]; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { ret[i, j] = defaultValue; } } return ret; } /// <summary> /// 要素数 (a, b) のdefalut値で満たされた配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <returns>初期化された配列</returns> public static T[,] Array2D<T>(int a, int b) where T : struct { var ret = new T[a, b]; return ret; } /// <summary> /// 要素数 (a, b) の、defaultValue で満たされたJag配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <param name="defaultValue">デフォルト値</param> /// <returns>指定した条件で初期化された配列</returns> public static T[][] JagArray2D<T>(int a, int b, T defaultValue) where T : struct { var ret = new T[a][]; for (int i = 0; i < a; i++) { ret[i] = Enumerable.Repeat(defaultValue, b).ToArray(); } return ret; } /// <summary> /// 要素数 (a, b) のdefalut値で満たされたJag配列を作成します。 /// </summary> /// <typeparam name="T">配列の型</typeparam> /// <param name="a">1次元の要素数</param> /// <param name="b">2次元の要素数</param> /// <returns>初期化された配列</returns> public static T[][] JagArray2D<T>(int a, int b) where T : struct { var ret = new T[a][]; for (int i = 0; i < a; i++) { ret[i] = new T[b]; } return ret; } } //========================================================================================= // Debug IO //========================================================================================= public class ConsoleInput { private readonly char _separator = ' '; private readonly TextReader _stream; private readonly Queue<string> inputStream; public ConsoleInput(TextReader stream, char separator = ' ') { _separator = separator; _stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) { return inputStream.Dequeue(); } var tmp = _stream.ReadLine().Split(_separator); for (var i = 0; i < tmp.Length; ++i) { inputStream.Enqueue(tmp[i]); } return inputStream.Dequeue(); } } public string ReadLine => _stream.ReadLine(); public int ReadInt => int.Parse(Read); public long ReadLong => long.Parse(Read); public double ReadDouble => double.Parse(Read); public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) { ret[i] = Read; } return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) { ret[i] = ReadInt; } return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) { ret[i] = ReadLong; } return ret; } } public interface IConsole { void Flush(); void Write(object obj); void Write(string str); void WriteLine(object obj); void WriteLine(string str); } public class MyConsole : IConsole { public MyConsole() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } public void Flush() { Console.Out.Flush(); } public void Write(object obj) { Write(obj.ToString()); } public void Write(string str) { Console.Write(str); } public void WriteLine(object obj) { WriteLine(obj.ToString()); } public void WriteLine(string str) { Console.WriteLine(str); } } #if UTAKA_LOCAL public class DebugConsole : IConsole { private readonly StringBuilder mSb; public DebugConsole() { mSb = new StringBuilder(); } public void Flush() { // 何もしない } public void Write(object obj) { Write(obj.ToString()); } public void Write(string str) { mSb.Append(str); } public void WriteLine(object obj) { WriteLine(obj.ToString()); } public void WriteLine(string str) { mSb.AppendLine(str); } public string GetAllOutput() { return mSb.ToString(); } } #endif #if UTAKA_LOCAL public class TestCaseCheckerForAtCoder { private string GetDirectoryPath() { var problemPart = string.IsNullOrEmpty(Program.ProblemName) ? "" : $"/{Program.ProblemName}"; return $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/AlgorithmPrac/{Program.SiteName}/{Program.ContestName}{problemPart}"; } private string GetInputFilePath(int testCaseNumber) { return $"{GetDirectoryPath()}/sample-{testCaseNumber}.in"; } private string GetOutputFilePath(int testCaseNumber) { return $"{GetDirectoryPath()}/sample-{testCaseNumber}.out"; } public bool TestProblem(int testCaseNumber) { var inputFilePath = GetInputFilePath(testCaseNumber); var outputFilePath = GetOutputFilePath(testCaseNumber); TextReader inputStream = new StreamReader(inputFilePath); var cin = new ConsoleInput(inputStream); var debugConsoleWriter = new DebugConsole(); new Program(cin, debugConsoleWriter).Solve(); var output = debugConsoleWriter.GetAllOutput(); TextReader outputStream = new StreamReader(outputFilePath); var outputAnswer = outputStream.ReadToEnd(); Dbg.WriteLine(output); Dbg.WriteLine(outputAnswer); var isCorrect = output == outputAnswer; return isCorrect; } public void TestProblems(int targetTestCaseNumber = -1) { bool isSuccessAll = true; int testCaseNumber = 0; while (true) { testCaseNumber++; if (targetTestCaseNumber >= 0 && targetTestCaseNumber != testCaseNumber) { continue; } var inputFileName = GetInputFilePath(testCaseNumber); if (!File.Exists(inputFileName)) { break; } Console.WriteLine( $"TestCase {testCaseNumber} ====================================================="); var result = TestProblem(testCaseNumber); if (result) { Console.WriteLine("Success"); } else { isSuccessAll = false; Console.WriteLine("Failure *****"); } } if (isSuccessAll) { Console.WriteLine("!!!!!!!!! All Success !!!!!!!!!"); } } } #endif #if UTAKA_LOCAL public static class Dbg { public static void WriteLine(string str) { Console.WriteLine(str); } public static void Write(string str) { Console.Write(str); } } #else public static class Dbg { public static void WriteLine(string str) {} public static void Write(string str) {} } #endif }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 3, 4, 0, 28, 0, 204, 205, 204, 205, 22], ["-", 0, 28, 0, 204, 205, 204, 206, 207, 0, 70], ["-", 0, 204, 205, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 28, 0, 204, 205, 204, 206, 207, 0, 73], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
1,881
13
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Competitive { internal class Solution { public int N; public long MOD = 1000000007; public string S, T; public void Run() { S = Console.ReadLine(); T = Console.ReadLine(); int[,] DP = new int[S.Length + 1, T.Length + 1]; for (int s = 1; s <= S.Length; s++) { for (int t = 1; t <= T.Length; t++) { DP[s, t] = Math.Max(DP[s, t], DP[s - 1, t - 1] + (S[s - 1] == T[t - 1] ? 1 : 0)); DP[s, t] = Math.Max(DP[s, t], DP[s - 1, t] + 0); DP[s, t] = Math.Max(DP[s, t], DP[s, t - 1] + 0); } } // 復元 { int s = S.Length; int t = T.Length; int v = DP[s, t]; List<char> chs = new List<char>(); while (s > 0 && t > 0 && v > 0) { if (v == DP[s - 1, t]) { s -= 1; } if (v == DP[s, t - 1]) { t -= 1; } if (v == DP[s - 1, t - 1] + 1) { chs.Add(S[s - 1]); s -= 1; t -= 1; v -= 1; } } chs.Reverse(); Console.WriteLine(string.Join("", chs)); } } } // libs ---------- // common ---------- internal static class Input { public static string ReadString() { string line = Console.ReadLine(); return line; } public static int ReadInt() { string line = Console.ReadLine(); return int.Parse(line); } public static int[] ReadIntArray() { string line = Console.ReadLine(); return line.Split(' ').Select(int.Parse).ToArray(); } public static long ReadLong() { string line = Console.ReadLine(); return long.Parse(line); } public static long[] ReadLongArray() { string line = Console.ReadLine(); return line.Split(' ').Select(long.Parse).ToArray(); } public static double[] ReadDoubleArray() { string line = Console.ReadLine(); return line.Split(' ').Select(double.Parse).ToArray(); } } internal class Program { public static void Main(string[] args) { var s = new Solution(); s.Run(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Competitive { internal class Solution { public int N; public long MOD = 1000000007; public string S, T; public void Run() { S = Console.ReadLine(); T = Console.ReadLine(); int SL = S.Length; int TL = T.Length; int[,] DP = new int[SL + 1, TL + 1]; for (int s = 1; s <= SL; s++) { for (int t = 1; t <= TL; t++) { DP[s, t] = Math.Max(DP[s, t], DP[s - 1, t - 1] + (S[s - 1] == T[t - 1] ? 1 : 0)); DP[s, t] = Math.Max(DP[s, t], DP[s - 1, t] + 0); DP[s, t] = Math.Max(DP[s, t], DP[s, t - 1] + 0); } } // 復元 { int s = SL; int t = TL; int v = DP[s, t]; List<char> chs = new List<char>(); while (s > 0 && t > 0 && v > 0) { if (v == DP[s - 1, t]) { s -= 1; continue; } if (v == DP[s, t - 1]) { t -= 1; continue; } if (v == DP[s - 1, t - 1] + 1) { chs.Add(S[s - 1]); s -= 1; t -= 1; v -= 1; continue; } } chs.Reverse(); Console.WriteLine(string.Join("", chs)); } } } // libs ---------- // common ---------- internal static class Input { public static string ReadString() { string line = Console.ReadLine(); return line; } public static int ReadInt() { string line = Console.ReadLine(); return int.Parse(line); } public static int[] ReadIntArray() { string line = Console.ReadLine(); return line.Split(' ').Select(int.Parse).ToArray(); } public static long ReadLong() { string line = Console.ReadLine(); return long.Parse(line); } public static long[] ReadLongArray() { string line = Console.ReadLine(); return line.Split(' ').Select(long.Parse).ToArray(); } public static double[] ReadDoubleArray() { string line = Console.ReadLine(); return line.Split(' ').Select(double.Parse).ToArray(); } } internal class Program { public static void Main(string[] args) { var s = new Solution(); s.Run(); } } }
[["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 32], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 205, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 0, 131], ["+", 0, 198, 0, 200, 0, 212, 0, 214, 141, 22], ["+", 8, 201, 0, 195, 8, 196, 0, 197, 0, 35], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 39, 224, 225, 226, 0, 16, 31, 214, 205, 22], ["-", 39, 224, 225, 226, 0, 16, 31, 214, 0, 131], ["-", 39, 224, 225, 226, 0, 16, 31, 214, 141, 22], ["+", 0, 227, 39, 224, 225, 226, 0, 16, 31, 22], ["-", 8, 196, 0, 7, 15, 16, 12, 214, 205, 22], ["-", 8, 196, 0, 7, 15, 16, 12, 214, 0, 131], ["-", 8, 196, 0, 7, 15, 16, 12, 214, 141, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["-", 0, 198, 0, 200, 0, 212, 0, 214, 205, 22], ["-", 0, 198, 0, 200, 0, 212, 0, 214, 0, 131], ["-", 0, 198, 0, 200, 0, 212, 0, 214, 141, 22], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 22], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 116, 0, 117]]
8
624
44
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Globalization; using System.Diagnostics; class Myon { public Myon() {} public static int Main() { new Myon().calc(); return 0; } Scanner cin; void calc() { cin = new Scanner(); string s = cin.next(); string t = cin.next(); int N = s.Length; int M = t.Length; int[,] dp = new int[N + 1, M + 1]; for (int i = 0; i <= N; i++) { for (int j = 0; j <= M; j++) { if (i != 0) dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j]); if (j != 0) dp[i, j] = Math.Max(dp[i, j], dp[i, j - 1]); if (i != 0 && j != 0 && s[i - 1] == t[j - 1]) dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j - 1] + 1); } } string S = ""; int a = N; int b = M; while (dp[a, b] != 0) { if (dp[a - 1, b - 1] == dp[a, b] - 1) { S = s[a - 1] + S; a--; b--; } else if (dp[a - 1, b] == dp[a, b]) a--; else b--; } Console.WriteLine(S); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class XRand { uint x, y, z, w; public XRand() { init(); } public XRand(uint s) { init(); init_xor128(s); } void init() { x = 314159265; y = 358979323; z = 846264338; w = 327950288; } public void init_xor128(uint s) { z ^= s; z ^= z >> 21; z ^= z << 35; z ^= z >> 4; z *= 736338717; } uint next() { uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8; } public long nextLong(long m) { return (long)((((ulong)next() << 32) + next()) % (ulong)m); } public int nextInt(int m) { return (int)(next() % m); } public int nextIntP(int a) { return (int)Math.Pow(a, nextDouble()); } public int nextInt(int min, int max) { return min + nextInt(max - min + 1); } public double nextDouble() { return (double)next() / uint.MaxValue; } public double nextDoubleP(double a) { return Math.Pow(a, nextDouble()); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Globalization; using System.Diagnostics; class Myon { public Myon() {} public static int Main() { new Myon().calc(); return 0; } Scanner cin; void calc() { cin = new Scanner(); string s = cin.next(); string t = cin.next(); int N = s.Length; int M = t.Length; int[,] dp = new int[N + 1, M + 1]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j]); dp[i, j] = Math.Max(dp[i, j], dp[i, j - 1]); if (s[i - 1] == t[j - 1]) dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j - 1] + 1); } } string S = ""; int a = N; int b = M; while (dp[a, b] != 0) { if (dp[a - 1, b - 1] == dp[a, b] - 1 && s[a - 1] == t[b - 1]) { S = s[a - 1] + S; a--; b--; } else if (dp[a - 1, b] == dp[a, b]) a--; else b--; } Console.WriteLine(S); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } class XRand { uint x, y, z, w; public XRand() { init(); } public XRand(uint s) { init(); init_xor128(s); } void init() { x = 314159265; y = 358979323; z = 846264338; w = 327950288; } public void init_xor128(uint s) { z ^= s; z ^= z >> 21; z ^= z << 35; z ^= z >> 4; z *= 736338717; } uint next() { uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8; } public long nextLong(long m) { return (long)((((ulong)next() << 32) + next()) % (ulong)m); } public int nextInt(int m) { return (int)(next() % m); } public int nextIntP(int a) { return (int)Math.Pow(a, nextDouble()); } public int nextInt(int min, int max) { return min + nextInt(max - min + 1); } public double nextDouble() { return (double)next() / uint.MaxValue; } public double nextDoubleP(double a) { return Math.Pow(a, nextDouble()); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 121], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 24], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 31, 22], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 79], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["-", 8, 196, 0, 7, 8, 196, 0, 57, 0, 25], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 31, 22], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 17, 79], ["-", 0, 57, 15, 16, 31, 16, 31, 16, 12, 203], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 98], ["-", 0, 57, 15, 16, 31, 16, 12, 16, 31, 22], ["-", 0, 57, 15, 16, 31, 16, 12, 16, 17, 79], ["-", 0, 57, 15, 16, 31, 16, 12, 16, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["+", 31, 16, 12, 16, 31, 204, 206, 207, 0, 73], ["+", 0, 57, 15, 16, 31, 16, 12, 16, 17, 33], ["+", 0, 57, 15, 16, 31, 16, 12, 16, 12, 203], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 98], ["+", 0, 57, 15, 16, 12, 16, 31, 204, 205, 22], ["+", 15, 16, 12, 16, 31, 204, 206, 207, 0, 70], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 16, 12, 16, 12, 204, 205, 22], ["+", 15, 16, 12, 16, 12, 204, 206, 207, 0, 70], ["+", 12, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 15, 16, 12, 16, 12, 204, 206, 207, 0, 73]]
8
1,030
38
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Convert; using static System.Math; // using Debug; // using static System.Globalization.CultureInfo; using System.Text; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); // WriteLine(Solve()); } static void Solve() { var nm = Input.ar; var adj = Input.edge<int>(nm[0]); while (nm[1]-- > 0) { var xy = Input.ar; xy[0]--; xy[1]--; adj[xy[0]].Add(xy[1]); } var list = TopologicalSort(adj); var dp = new int[nm[0] + 1]; for (var i = 0; i < nm[0]; i++) foreach (var ad in adj[list[i]]) dp[ad] = dp[list[i]] + 1; WriteLine(dp.Max()); } public static List<int> TopologicalSort(IList<IEnumerable<int>> adj) { var res = new List<int>(adj.Count); var th = new bool[adj.Count]; for (var i = 0; i < adj.Count; i++) if (!th[i]) Topodfs(adj, i, th, res); res.Reverse(); return res; } private static void Topodfs(IList<IEnumerable<int>> adj, int index, bool[] th, List<int> res) { th[index] = true; foreach (var ad in adj[index]) if (!th[ad]) Topodfs(adj, ad, th, res); res.Add(index); } } public class Input { public static string read => ReadLine().Trim(); public static int[] ar => read.Split(' ').Select(int.Parse).ToArray(); public static int num => ToInt32(read); public static long[] arL => read.Split(' ').Select(long.Parse).ToArray(); public static long numL => ToInt64(read); public static char[][] gred(int h) => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray(); public static int[] ar1D(int n) => Enumerable.Repeat(0, n).Select(_ => num).ToArray(); public static long[] arL1D(int n) => Enumerable.Repeat(0, n).Select(_ => numL).ToArray(); public static string[] strs(int n) => Enumerable.Repeat(0, n).Select(_ => read).ToArray(); public static int[][] ar2D(int n) => Enumerable.Repeat(0, n).Select(_ => ar).ToArray(); public static long[][] arL2D(int n) => Enumerable.Repeat(0, n).Select(_ => arL).ToArray(); public static List<T>[] edge<T>(int n) => Enumerable.Repeat(0, n).Select(_ => new List<T>()).ToArray(); public static T getValue<T>(string g) { var t = typeof(T); if (t == typeof(int)) return (T)(object) int.Parse(g); if (t == typeof(long)) return (T)(object) long.Parse(g); if (t == typeof(string)) return (T)(object)g; if (t == typeof(char)) return (T)(object) char.Parse(g); if (t == typeof(double)) return (T)(object) double.Parse(g); if (t == typeof(bool)) return (T)(object) bool.Parse(g); return default(T); } public static void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public static void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public static void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public static void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } private static Queue<string> sc = new Queue<string>(); public static T Next<T>() { if (sc.Count == 0) foreach (var item in read.Split(' ')) sc.Enqueue(item); return getValue<T>(sc.Dequeue()); } public const long Inf = (long)1e18; public const double eps = 1e-6; public const string Alfa = "abcdefghijklmnopqrstuvwxyz"; public const int MOD = 1000000007; }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Convert; using static System.Math; // using Debug; // using static System.Globalization.CultureInfo; using System.Text; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); // WriteLine(Solve()); } static void Solve() { var nm = Input.ar; var adj = Input.edge<int>(nm[0]); while (nm[1]-- > 0) { var xy = Input.ar; xy[0]--; xy[1]--; adj[xy[0]].Add(xy[1]); } var list = TopologicalSort(adj); var dp = new int[nm[0] + 1]; for (var i = 0; i < nm[0]; i++) foreach (var ad in adj[list[i]]) dp[ad] = Max(dp[ad], dp[list[i]] + 1); WriteLine(dp.Max()); } public static List<int> TopologicalSort(IList<IEnumerable<int>> adj) { var res = new List<int>(adj.Count); var th = new bool[adj.Count]; for (var i = 0; i < adj.Count; i++) if (!th[i]) Topodfs(adj, i, th, res); res.Reverse(); return res; } private static void Topodfs(IList<IEnumerable<int>> adj, int index, bool[] th, List<int> res) { th[index] = true; foreach (var ad in adj[index]) if (!th[ad]) Topodfs(adj, ad, th, res); res.Add(index); } } public class Input { public static string read => ReadLine().Trim(); public static int[] ar => read.Split(' ').Select(int.Parse).ToArray(); public static int num => ToInt32(read); public static long[] arL => read.Split(' ').Select(long.Parse).ToArray(); public static long numL => ToInt64(read); public static char[][] gred(int h) => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray(); public static int[] ar1D(int n) => Enumerable.Repeat(0, n).Select(_ => num).ToArray(); public static long[] arL1D(int n) => Enumerable.Repeat(0, n).Select(_ => numL).ToArray(); public static string[] strs(int n) => Enumerable.Repeat(0, n).Select(_ => read).ToArray(); public static int[][] ar2D(int n) => Enumerable.Repeat(0, n).Select(_ => ar).ToArray(); public static long[][] arL2D(int n) => Enumerable.Repeat(0, n).Select(_ => arL).ToArray(); public static List<T>[] edge<T>(int n) => Enumerable.Repeat(0, n).Select(_ => new List<T>()).ToArray(); public static T getValue<T>(string g) { var t = typeof(T); if (t == typeof(int)) return (T)(object) int.Parse(g); if (t == typeof(long)) return (T)(object) long.Parse(g); if (t == typeof(string)) return (T)(object)g; if (t == typeof(char)) return (T)(object) char.Parse(g); if (t == typeof(double)) return (T)(object) double.Parse(g); if (t == typeof(bool)) return (T)(object) bool.Parse(g); return default(T); } public static void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); } public static void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); } public static void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); } public static void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); } private static Queue<string> sc = new Queue<string>(); public static T Next<T>() { if (sc.Count == 0) foreach (var item in read.Split(' ')) sc.Enqueue(item); return getValue<T>(sc.Dequeue()); } public const long Inf = (long)1e18; public const double eps = 1e-6; public const string Alfa = "abcdefghijklmnopqrstuvwxyz"; public const int MOD = 1000000007; }
[["+", 8, 210, 8, 1, 0, 11, 12, 213, 63, 22], ["+", 8, 1, 0, 11, 12, 213, 3, 4, 0, 24], ["+", 12, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 8, 1, 0, 11, 12, 213, 3, 4, 0, 21], ["+", 8, 1, 0, 11, 12, 213, 3, 4, 0, 25]]
8
1,202
8
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammingContest { class Writer : IDisposable { private System.IO.TextWriter Out { get; } private StringBuilder Sb { get; } private bool IsReactive { get; } public Writer(string path) : this(new System.IO.StreamWriter(path)) {} public Writer(bool isReactive) : this(null, isReactive) {} public Writer(System.IO.TextWriter writer = null, bool isReactive = false) { this.Out = (writer ?? Console.Out); this.IsReactive = isReactive; if (!this.IsReactive) { this.Sb = new StringBuilder(); } } public void Dispose() { if (!this.IsReactive) { this.Out.Write(Sb.ToString()); } if (!this.Out.Equals(Console.Out)) { this.Out.Dispose(); } } public void Write(object val) { if (this.IsReactive) { this.Out.Write(val.ToString()); this.Out.Flush(); } else { this.Sb.Append(val.ToString()); } } public void WriteFormat(string format, params object[] vals) { if (this.IsReactive) { this.Out.Write(format, vals); this.Out.Flush(); } else { this.Sb.AppendFormat(format, vals); } } public void WriteLine(object val = null) => this.WriteFormat((val?.ToString() ?? string.Empty) + Environment.NewLine); public void WriteLine(int val) => this.WriteLine(val.ToString()); public void WriteLine(long val) => this.WriteLine(val.ToString()); public void WriteLine(string val) => this.WriteLine((object)val); public void WriteLine(string format, params object[] vals) => this.WriteFormat(format + Environment.NewLine, vals); } class Scanner : IDisposable { private Queue<string> Buffer { get; } private char[] Sep { get; } private System.IO.TextReader Reader { get; } public Scanner(string path, char[] sep = null) : this(new System.IO.StreamReader(path), sep) {} public Scanner(System.IO.TextReader reader = null, char[] sep = null) { this.Buffer = new Queue<string>(); this.Sep = (sep ?? new char[] { ' ' }); this.Reader = (reader ?? Console.In); } private void CheckBuffer() { if (this.Buffer.Count == 0 && this.Reader.Peek() != -1) { string str = string.Empty; for (; string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str); str = this.Reader.ReadLine()) ; var strs = str.Split(this.Sep).Where( el => !(string.IsNullOrEmpty(el) || string.IsNullOrWhiteSpace(el))); foreach (var el in strs) { this.Buffer.Enqueue(el); } } } public void Dispose() { if (!this.Reader.Equals(Console.In)) { this.Reader.Dispose(); } } public string Next() { this.CheckBuffer(); return this.Buffer.Dequeue(); } public string[] GetStringArray(int N) => Enumerable.Range(0, N).Select(e => this.Next()).ToArray(); public int NextInt() => int.Parse(this.Next()); public int[] GetIntArray(int N) => Enumerable.Range(0, N).Select(e => this.NextInt()).ToArray(); public double NextDouble() => double.Parse(this.Next()); public double[] GetdoubleArray(int N) => Enumerable.Range(0, N).Select(e => this.NextDouble()).ToArray(); public long NextLong() => long.Parse(this.Next()); public long[] GetLongArray(int N) => Enumerable.Range(0, N).Select(e => this.NextLong()).ToArray(); public bool IsEnd { get { this.CheckBuffer(); return this.Buffer.Count == 0; } } } class MainClass : IDisposable { private Scanner Sc { get; } private Writer Wr { get; } private string InFilePath => "in.txt"; private string OutFilePath => "out.txt"; public MainClass() { this.Wr = new Writer(this.IsReactive); // this.Wr = new Writer(this.OutFilePath); #if DEBUG if (!this.IsReactive) { this.Sc = new Scanner(this.InFilePath); } else { this.Sc = new Scanner(); } #else this.Sc = new Scanner(); #endif this.Solve(); } static void Main(string[] args) { using (new MainClass()) {} } public void Dispose() { this.Sc?.Dispose(); this.Wr?.Dispose(); #if DEBUG Console.WriteLine("press any key to continue..."); Console.ReadKey(); #endif } void Solve() { int N = Sc.NextInt(); int M = Sc.NextInt(); Graph G = new Graph(N); HashSet<int> se = new HashSet<int>(); for (int i = 0; i < M; i++) { int x = Sc.NextInt() - 1; int y = Sc.NextInt() - 1; G.AddEdge(x, y, 1); se.Add(y); } { var t = G.TopologicalSort(); w = new int[N]; for (int i = 0; i < t.Length; i++) { w[t[i]] = i; } } SkewHeap<State> que = new SkewHeap<State>((l, r) => { if (l.Len == r.Len) { return (w[l.Idx] - w[r.Idx]); } else { return -(l.Len - r.Len); } }); for (int i = 0; i < N; i++) { if (!se.Contains(i)) { que.Push(new State(i, 0)); } } int[] dp = new int[N]; for (int i = 0; i < N; i++) { dp[i] = -1; } while (que.Count > 0) { var e = que.Poll(); int idx = e.Idx; int len = e.Len; if (len <= dp[idx]) { continue; } dp[idx] = len; Wr.WriteLine(idx); foreach (var el in G.ListGraph[idx]) { if (dp[el.To] >= len + 1) { continue; } que.Push(new State(el.To, len + 1)); } } Wr.WriteLine(Math.Max(0, dp.Max())); } int[] w; class State : IComparable<State> { public int Idx { get; set; } public int Len { get; set; } public State(int i, int l) { Idx = i; Len = l; } public int CompareTo(State other) { throw new NotImplementedException(); } } private bool IsReactive => false; // TODO: reactive check !! } class Graph { public const long InfCost = (long)1e18; public struct Edge : IComparable<Edge> { public int To { get; } public long Cost { get; } public Edge(int to, long cost) { this.To = to; this.Cost = cost; } public int CompareTo(Edge other) => Math.Sign(this.Cost - other.Cost); } public List<Edge>[] ListGraph { get; } public Graph(int N) { this.ListGraph = new List<Edge> [N].Select(e => new List<Edge>()).ToArray(); } public void AddEdge(int from, int to, long cost) => this.ListGraph[from].Add(new Edge(to, cost)); public int[] TopologicalSort() { int N = this.ListGraph.Length; var tsort = new List<int>(); var inCnt = new int[N]; for (int from = 0; from < N; from++) { foreach (var e in this.ListGraph[from]) { inCnt[e.To]++; } } var q = new Queue<int>(); for (int i = 0; i < N; i++) { if (inCnt[i] == 0) { q.Enqueue(i); } } while (q.Count > 0) { int now = q.Dequeue(); tsort.Add(now); foreach (var e in this.ListGraph[now]) { inCnt[e.To]--; if (inCnt[e.To] == 0) { q.Enqueue(e.To); } } } return tsort.Count == N ? tsort.ToArray() : null; } } class SkewHeap<T> where T : IComparable<T> { private class HeapNode { public HeapNode l, r; public T val; public static Func<T, T, int> CompareTo; public HeapNode(T val) { this.val = val; this.l = this.r = null; } public static HeapNode Meld(HeapNode leftNode, HeapNode rightNode) { if (leftNode == null) { return rightNode; } if (rightNode == null) { return leftNode; } if (HeapNode.CompareTo(leftNode.val, rightNode.val) > 0) { var t = rightNode; rightNode = leftNode; leftNode = t; } leftNode.r = HeapNode.Meld(leftNode.r, rightNode); var t2 = leftNode.l; leftNode.l = leftNode.r; leftNode.r = t2; return leftNode; } } private HeapNode topNode; public int Count { get; private set; } public SkewHeap() : this(null, null) {} public SkewHeap(Func<T, T, int> comp) : this(null, comp) {} public SkewHeap(IEnumerable<T> source) : this(source, null) {} public SkewHeap(IEnumerable<T> source, Func<T, T, int> compare) { HeapNode.CompareTo = (compare ?? ((T l, T r) => l.CompareTo(r))); this.topNode = null; this.Count = 0; if (source != null) { foreach (var e in source) this.Push(e); } } /// <summary> /// 捨てる /// </summary> public void Pop() { this.topNode = HeapNode.Meld(this.topNode.l, this.topNode.r); this.Count--; } /// <summary> /// 見る /// </summary> public T Top() => this.topNode.val; /// <summary> /// 入れる /// </summary> public void Push(T val) { this.topNode = HeapNode.Meld(this.topNode, new HeapNode(val)); this.Count++; } /// <summary> /// 出す /// </summary> public T Poll() { T retVal = this.Top(); this.Pop(); return retVal; } public bool IsEmpty => this.topNode == null; public void Merge(SkewHeap<T> otherHeap) { this.Count += otherHeap.Count; this.topNode = HeapNode.Meld(this.topNode, otherHeap.topNode); } } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProgrammingContest { class Writer : IDisposable { private System.IO.TextWriter Out { get; } private StringBuilder Sb { get; } private bool IsReactive { get; } public Writer(string path) : this(new System.IO.StreamWriter(path)) {} public Writer(bool isReactive) : this(null, isReactive) {} public Writer(System.IO.TextWriter writer = null, bool isReactive = false) { this.Out = (writer ?? Console.Out); this.IsReactive = isReactive; if (!this.IsReactive) { this.Sb = new StringBuilder(); } } public void Dispose() { if (!this.IsReactive) { this.Out.Write(Sb.ToString()); } if (!this.Out.Equals(Console.Out)) { this.Out.Dispose(); } } public void Write(object val) { if (this.IsReactive) { this.Out.Write(val.ToString()); this.Out.Flush(); } else { this.Sb.Append(val.ToString()); } } public void WriteFormat(string format, params object[] vals) { if (this.IsReactive) { this.Out.Write(format, vals); this.Out.Flush(); } else { this.Sb.AppendFormat(format, vals); } } public void WriteLine(object val = null) => this.WriteFormat((val?.ToString() ?? string.Empty) + Environment.NewLine); public void WriteLine(int val) => this.WriteLine(val.ToString()); public void WriteLine(long val) => this.WriteLine(val.ToString()); public void WriteLine(string val) => this.WriteLine((object)val); public void WriteLine(string format, params object[] vals) => this.WriteFormat(format + Environment.NewLine, vals); } class Scanner : IDisposable { private Queue<string> Buffer { get; } private char[] Sep { get; } private System.IO.TextReader Reader { get; } public Scanner(string path, char[] sep = null) : this(new System.IO.StreamReader(path), sep) {} public Scanner(System.IO.TextReader reader = null, char[] sep = null) { this.Buffer = new Queue<string>(); this.Sep = (sep ?? new char[] { ' ' }); this.Reader = (reader ?? Console.In); } private void CheckBuffer() { if (this.Buffer.Count == 0 && this.Reader.Peek() != -1) { string str = string.Empty; for (; string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str); str = this.Reader.ReadLine()) ; var strs = str.Split(this.Sep).Where( el => !(string.IsNullOrEmpty(el) || string.IsNullOrWhiteSpace(el))); foreach (var el in strs) { this.Buffer.Enqueue(el); } } } public void Dispose() { if (!this.Reader.Equals(Console.In)) { this.Reader.Dispose(); } } public string Next() { this.CheckBuffer(); return this.Buffer.Dequeue(); } public string[] GetStringArray(int N) => Enumerable.Range(0, N).Select(e => this.Next()).ToArray(); public int NextInt() => int.Parse(this.Next()); public int[] GetIntArray(int N) => Enumerable.Range(0, N).Select(e => this.NextInt()).ToArray(); public double NextDouble() => double.Parse(this.Next()); public double[] GetdoubleArray(int N) => Enumerable.Range(0, N).Select(e => this.NextDouble()).ToArray(); public long NextLong() => long.Parse(this.Next()); public long[] GetLongArray(int N) => Enumerable.Range(0, N).Select(e => this.NextLong()).ToArray(); public bool IsEnd { get { this.CheckBuffer(); return this.Buffer.Count == 0; } } } class MainClass : IDisposable { private Scanner Sc { get; } private Writer Wr { get; } private string InFilePath => "in.txt"; private string OutFilePath => "out.txt"; public MainClass() { this.Wr = new Writer(this.IsReactive); // this.Wr = new Writer(this.OutFilePath); #if DEBUG if (!this.IsReactive) { this.Sc = new Scanner(this.InFilePath); } else { this.Sc = new Scanner(); } #else this.Sc = new Scanner(); #endif this.Solve(); } static void Main(string[] args) { using (new MainClass()) {} } public void Dispose() { this.Sc?.Dispose(); this.Wr?.Dispose(); #if DEBUG Console.WriteLine("press any key to continue..."); Console.ReadKey(); #endif } void Solve() { int N = Sc.NextInt(); int M = Sc.NextInt(); Graph G = new Graph(N); HashSet<int> se = new HashSet<int>(); for (int i = 0; i < M; i++) { int x = Sc.NextInt() - 1; int y = Sc.NextInt() - 1; G.AddEdge(x, y, 1); se.Add(y); } { var t = G.TopologicalSort(); w = new int[N]; for (int i = 0; i < t.Length; i++) { w[t[i]] = i; } } SkewHeap<State> que = new SkewHeap<State>((l, r) => { if (l.Len == r.Len) { return (w[l.Idx] - w[r.Idx]); } else { return -(l.Len - r.Len); } }); for (int i = 0; i < N; i++) { if (!se.Contains(i)) { que.Push(new State(i, 0)); } } int[] dp = new int[N]; for (int i = 0; i < N; i++) { dp[i] = -1; } while (que.Count > 0) { var e = que.Poll(); int idx = e.Idx; int len = e.Len; if (len <= dp[idx]) { continue; } dp[idx] = len; foreach (var el in G.ListGraph[idx]) { if (dp[el.To] >= len + 1) { continue; } que.Push(new State(el.To, len + 1)); } } Wr.WriteLine(Math.Max(0, dp.Max())); } int[] w; class State : IComparable<State> { public int Idx { get; set; } public int Len { get; set; } public State(int i, int l) { Idx = i; Len = l; } public int CompareTo(State other) { throw new NotImplementedException(); } } private bool IsReactive => false; // TODO: reactive check !! } class Graph { public const long InfCost = (long)1e18; public struct Edge : IComparable<Edge> { public int To { get; } public long Cost { get; } public Edge(int to, long cost) { this.To = to; this.Cost = cost; } public int CompareTo(Edge other) => Math.Sign(this.Cost - other.Cost); } public List<Edge>[] ListGraph { get; } public Graph(int N) { this.ListGraph = new List<Edge> [N].Select(e => new List<Edge>()).ToArray(); } public void AddEdge(int from, int to, long cost) => this.ListGraph[from].Add(new Edge(to, cost)); public int[] TopologicalSort() { int N = this.ListGraph.Length; var tsort = new List<int>(); var inCnt = new int[N]; for (int from = 0; from < N; from++) { foreach (var e in this.ListGraph[from]) { inCnt[e.To]++; } } var q = new Queue<int>(); for (int i = 0; i < N; i++) { if (inCnt[i] == 0) { q.Enqueue(i); } } while (q.Count > 0) { int now = q.Dequeue(); tsort.Add(now); foreach (var e in this.ListGraph[now]) { inCnt[e.To]--; if (inCnt[e.To] == 0) { q.Enqueue(e.To); } } } return tsort.Count == N ? tsort.ToArray() : null; } } class SkewHeap<T> where T : IComparable<T> { private class HeapNode { public HeapNode l, r; public T val; public static Func<T, T, int> CompareTo; public HeapNode(T val) { this.val = val; this.l = this.r = null; } public static HeapNode Meld(HeapNode leftNode, HeapNode rightNode) { if (leftNode == null) { return rightNode; } if (rightNode == null) { return leftNode; } if (HeapNode.CompareTo(leftNode.val, rightNode.val) > 0) { var t = rightNode; rightNode = leftNode; leftNode = t; } leftNode.r = HeapNode.Meld(leftNode.r, rightNode); var t2 = leftNode.l; leftNode.l = leftNode.r; leftNode.r = t2; return leftNode; } } private HeapNode topNode; public int Count { get; private set; } public SkewHeap() : this(null, null) {} public SkewHeap(Func<T, T, int> comp) : this(null, comp) {} public SkewHeap(IEnumerable<T> source) : this(source, null) {} public SkewHeap(IEnumerable<T> source, Func<T, T, int> compare) { HeapNode.CompareTo = (compare ?? ((T l, T r) => l.CompareTo(r))); this.topNode = null; this.Count = 0; if (source != null) { foreach (var e in source) this.Push(e); } } /// <summary> /// 捨てる /// </summary> public void Pop() { this.topNode = HeapNode.Meld(this.topNode.l, this.topNode.r); this.Count--; } /// <summary> /// 見る /// </summary> public T Top() => this.topNode.val; /// <summary> /// 入れる /// </summary> public void Push(T val) { this.topNode = HeapNode.Meld(this.topNode, new HeapNode(val)); this.Count++; } /// <summary> /// 出す /// </summary> public T Poll() { T retVal = this.Top(); this.Pop(); return retVal; } public bool IsEmpty => this.topNode == null; public void Merge(SkewHeap<T> otherHeap) { this.Count += otherHeap.Count; this.topNode = HeapNode.Meld(this.topNode, otherHeap.topNode); } } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["-", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
8
2,454
7