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.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Console; namespace AtCoder_1 { class Program { static void Main(string[] args) { var N = CinTAr(); var a1 = N[0] - 1; var a2 = N[0]; var now = 0; if (now >= N[1]) { Cout(0); return; } now = a2; int c = 1; while (true) { if (now >= N[1]) { break; } c++; now += a1; } Cout(c); Cin(); } static int GCD(int a, int b) { if (a < b) { swap(ref a, ref b); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } } static void swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; } static readonly int MOD10E9 = (1000000000 + 7); static bool isInt(double d) { return (int)d == d; } static string Cin() { return ReadLine(); } static string[] CinAr() { return ReadLine().Split(' '); } static int CinT() { return S2i(Cin()); } static int[] CinTAr() { return CinAr().Select((x, i) => { return S2i(x); }).ToArray(); } static long CinL() { return S2L(Cin()); } static long[] CinLAr() { return CinAr().Select((x, i) => { return S2L(x); }).ToArray(); } static double CinD() { return S2D(Cin()); } static double[] CinDAr() { return CinAr().Select((x, i) => { return S2D(x); }).ToArray(); } static int S2i(string s) { return int.Parse(s); } static long S2L(string s) { return long.Parse(s); } static double S2D(string s) { return double.Parse(s); } static void Cout(object s) { WriteLine(s); } static int[] yakusuu(int n) { if (n == 1) { return new int[] { 1 }; } HashSet<int> res = new HashSet<int>(); for (int i = 1; i < Math.Ceiling(n / 2.0); i++) { if (n % i == 0) { res.Add(i); res.Add(n / i); } } return res.ToArray(); } static void CoutAr<Type>(IEnumerable<Type> types) { string text = string.Join(" ", types); WriteLine(text); } public static long nCr(int n, int r) { // naive: return Factorial(n) / (Factorial(r) * Factorial(n - r)); return nPr(n, r) / Factorial(r); } public static long nPr(int n, int r) { // naive: return Factorial(n) / Factorial(n - r); return FactorialDivision(n, n - r); } private static long FactorialDivision(int topFactorial, int divisorFactorial) { long result = 1; for (int i = topFactorial; i > divisorFactorial; i--) result *= i; return result; } private static long Factorial(int i) { if (i <= 1) return 1; return i * Factorial(i - 1); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using static System.Console; namespace AtCoder_1 { class Program { static void Main(string[] args) { var N = CinTAr(); var a1 = N[0] - 1; var a2 = N[0]; var now = 1; if (now >= N[1]) { Cout(0); return; } now = a2; int c = 1; while (true) { if (now >= N[1]) { break; } c++; now += a1; } Cout(c); Cin(); } static int GCD(int a, int b) { if (a < b) { swap(ref a, ref b); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } } static void swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; } static readonly int MOD10E9 = (1000000000 + 7); static bool isInt(double d) { return (int)d == d; } static string Cin() { return ReadLine(); } static string[] CinAr() { return ReadLine().Split(' '); } static int CinT() { return S2i(Cin()); } static int[] CinTAr() { return CinAr().Select((x, i) => { return S2i(x); }).ToArray(); } static long CinL() { return S2L(Cin()); } static long[] CinLAr() { return CinAr().Select((x, i) => { return S2L(x); }).ToArray(); } static double CinD() { return S2D(Cin()); } static double[] CinDAr() { return CinAr().Select((x, i) => { return S2D(x); }).ToArray(); } static int S2i(string s) { return int.Parse(s); } static long S2L(string s) { return long.Parse(s); } static double S2D(string s) { return double.Parse(s); } static void Cout(object s) { WriteLine(s); } static int[] yakusuu(int n) { if (n == 1) { return new int[] { 1 }; } HashSet<int> res = new HashSet<int>(); for (int i = 1; i < Math.Ceiling(n / 2.0); i++) { if (n % i == 0) { res.Add(i); res.Add(n / i); } } return res.ToArray(); } static void CoutAr<Type>(IEnumerable<Type> types) { string text = string.Join(" ", types); WriteLine(text); } public static long nCr(int n, int r) { // naive: return Factorial(n) / (Factorial(r) * Factorial(n - r)); return nPr(n, r) / Factorial(r); } public static long nPr(int n, int r) { // naive: return Factorial(n) / Factorial(n - r); return FactorialDivision(n, n - r); } private static long FactorialDivision(int topFactorial, int divisorFactorial) { long result = 1; for (int i = topFactorial; i > divisorFactorial; i--) result *= i; return result; } private static long Factorial(int i) { if (i <= 1) return 1; return i * Factorial(i - 1); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
766
2
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int Nmasu = int.Parse(Console.ReadLine()); int[] Htakasa = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray(); int[] ans = new int[Nmasu]; int cnt = 0; for (int i = 1; i < Nmasu; i++) { if (Htakasa[i] >= Htakasa[i - 1]) { ans[i] = ans[i - 1] + 1; } else { ans[i] = 0; } } Console.WriteLine(ans.Max()); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int Nmasu = int.Parse(Console.ReadLine()); int[] Htakasa = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray(); int[] ans = new int[Nmasu]; int cnt = 0; for (int i = 1; i < Nmasu; i++) { if (Htakasa[i] <= Htakasa[i - 1]) { ans[i] = ans[i - 1] + 1; } else { ans[i] = 0; } } Console.WriteLine(ans.Max()); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 19]]
8
165
2
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder.Contest.C { class Program { public static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Scanner cin = new Scanner(); int n = cin.nextInt(); var a = cin.ArrayInt(n); var b = new int[n - 1]; int ans = 0; int now = 0; for (int i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; if (a[i - 1] >= a[i]) { now++; if (now > ans) ans = now; } else { now = 0; } } Console.WriteLine(now); Console.Out.Flush(); } } 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.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder.Contest.C { class Program { public static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Scanner cin = new Scanner(); int n = cin.nextInt(); var a = cin.ArrayLong(n); var b = new long[n - 1]; int ans = 0; int now = 0; for (int i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; if (a[i - 1] >= a[i]) { now++; if (now > ans) ans = now; } else { now = 0; } } Console.WriteLine(ans); Console.Out.Flush(); } } 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, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22]]
8
565
6
using System; namespace C { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] WH = Console.ReadLine().Split(' '); int[] H = new int[N]; for (int i = 0; i < N; i++) { H[i] = int.Parse(WH[i]); } bool flg = false; int cnt = 0; int maxcnt = 0; for (int i = 0; i < N - 1; i++) { if (H[i] >= H[i + 1]) { if (flg == false) { flg = true; cnt = 0; } cnt++; } else { flg = false; if (maxcnt < cnt) { maxcnt = cnt; } } } Console.WriteLine(cnt); } } }
using System; namespace C { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] WH = Console.ReadLine().Split(' '); int[] H = new int[N]; for (int i = 0; i < N; i++) { H[i] = int.Parse(WH[i]); } bool flg = false; int cnt = 0; int maxcnt = 0; for (int i = 0; i < N - 1; i++) { if (H[i] >= H[i + 1]) { if (flg == false) { flg = true; cnt = 0; } cnt++; } else { flg = false; } if (maxcnt < cnt) { maxcnt = cnt; } } Console.WriteLine(maxcnt); } } }
[["+", 0, 7, 8, 196, 0, 57, 75, 196, 0, 46], ["-", 0, 57, 75, 196, 0, 57, 64, 196, 0, 46], ["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22]]
8
190
4
using System; using System.Collections.Generic; using static System.Math; using System.Linq; using System.Text; class DandA { static void Main(string[] args) { int n = ReadLineInt(); int[] hList = ReadLineIntArray(); int rtn = 0; int wk = 0; for (int i = 0; i < n - 1; i++) { if (hList[i] > hList[i + 1]) { wk++; } else { wk = 0; } if (rtn < wk) { rtn = wk; } } Console.WriteLine(rtn); } static string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"; static string[] strAry = new string[] { "L", "R", "U", "D" }; static private string ReadLineStr() { return Console.ReadLine(); } static private int ReadLineInt() { return int.Parse(Console.ReadLine()); } static private long ReadLineLong() { return long.Parse(Console.ReadLine()); } static private double ReadLineDouble() { return double.Parse(Console.ReadLine()); } static private string[] ReadLineStrArray() { return Console.ReadLine().Trim().Split(); } static private int[] ReadLineIntArray() { return Console.ReadLine() .Trim() .Split() .Select(a => int.Parse(a)) .ToArray(); } static private long[] ReadLineLongArray() { return Console.ReadLine() .Trim() .Split() .Select(a => long.Parse(a)) .ToArray(); } static private double[] ReadLineDoubleArray() { return Console.ReadLine() .Trim() .Split() .Select(a => double.Parse(a)) .ToArray(); } static private decimal[] ReadLineDecimalArray() { return Console.ReadLine() .Trim() .Split() .Select(a => decimal.Parse(a)) .ToArray(); } static private string[][] ReadMLineStrArray(int arg) { return new int [arg].Select(_ => Console.ReadLine().Split()).ToArray(); } static private int[][] ReadMLineIntArray(int arg) { return new int [arg] .Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()) .ToArray(); } static private bool IsPrime(int num) { if (num < 2) return false; if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = Math.Sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } static private string IntToNString(int x, int n) { var nstring = ""; var numbers = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; int r = 1; int q = x; while (q > 0) { r = q % n; nstring += numbers[r]; q = q / n; } return string.Join("", nstring.Reverse()); } }
using System; using System.Collections.Generic; using static System.Math; using System.Linq; using System.Text; class DandA { static void Main(string[] args) { int n = ReadLineInt(); int[] hList = ReadLineIntArray(); int rtn = 0; int wk = 0; for (int i = 0; i < n - 1; i++) { if (hList[i] >= hList[i + 1]) { wk++; } else { wk = 0; } if (rtn < wk) { rtn = wk; } } Console.WriteLine(rtn); } static string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"; static string[] strAry = new string[] { "L", "R", "U", "D" }; static private string ReadLineStr() { return Console.ReadLine(); } static private int ReadLineInt() { return int.Parse(Console.ReadLine()); } static private long ReadLineLong() { return long.Parse(Console.ReadLine()); } static private double ReadLineDouble() { return double.Parse(Console.ReadLine()); } static private string[] ReadLineStrArray() { return Console.ReadLine().Trim().Split(); } static private int[] ReadLineIntArray() { return Console.ReadLine() .Trim() .Split() .Select(a => int.Parse(a)) .ToArray(); } static private long[] ReadLineLongArray() { return Console.ReadLine() .Trim() .Split() .Select(a => long.Parse(a)) .ToArray(); } static private double[] ReadLineDoubleArray() { return Console.ReadLine() .Trim() .Split() .Select(a => double.Parse(a)) .ToArray(); } static private decimal[] ReadLineDecimalArray() { return Console.ReadLine() .Trim() .Split() .Select(a => decimal.Parse(a)) .ToArray(); } static private string[][] ReadMLineStrArray(int arg) { return new int [arg].Select(_ => Console.ReadLine().Split()).ToArray(); } static private int[][] ReadMLineIntArray(int arg) { return new int [arg] .Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()) .ToArray(); } static private bool IsPrime(int num) { if (num < 2) return false; if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = Math.Sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } static private string IntToNString(int x, int n) { var nstring = ""; var numbers = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; int r = 1; int q = x; while (q > 0) { r = q % n; nstring += numbers[r]; q = q / n; } return string.Join("", nstring.Reverse()); } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20]]
8
746
2
using System; class P { static void Main() { int N = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int count = 0; int i; int ans = 0; for (i = 0; i < N - 1; i++) { if (a[i] >= a[i + 1]) { count++; } if (a[i] < a[i + 1]) { if (ans <= count) { ans = count; count = 0; } else { count = 0; } } } Console.WriteLine(ans); } }
using System; class P { static void Main() { int N = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int count = 0; int i; int ans = 0; for (i = 0; i < N - 1; i++) { if (a[i] >= a[i + 1]) { count++; } if (a[i] < a[i + 1]) { if (ans <= count) { ans = count; count = 0; } else { count = 0; } } } Console.WriteLine(ans <= count ? count : ans); } }
[["+", 3, 4, 0, 28, 0, 41, 15, 16, 17, 19], ["+", 3, 4, 0, 28, 0, 41, 15, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 101], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 64, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 0, 102], ["+", 0, 213, 3, 4, 0, 28, 0, 41, 75, 22]]
8
147
6
using System; class Program { static void Main(string[] args) { // 整数の入力 int N = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input = Console.ReadLine().Split(' '); int[] H = new int[N]; for (int i = 0; i < N; i++) { H[i] = int.Parse(input[i]); } // int A = int.Parse(input[0]); // int B = int.Parse(input[1]); // 文字列の入力 // string s = Console.ReadLine(); //出力 int anser = 0; int count = 0; int nowH = H[0]; for (int i = 0; i < N; i++) { if (nowH >= H[i]) { count++; } else { count = 0; } if (count > anser) { anser = count; } nowH = H[i]; } Console.WriteLine(anser); } }
using System; class Program { static void Main(string[] args) { // 整数の入力 int N = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input = Console.ReadLine().Split(' '); int[] H = new int[N]; for (int i = 0; i < N; i++) { H[i] = int.Parse(input[i]); } // int A = int.Parse(input[0]); // int B = int.Parse(input[1]); // 文字列の入力 // string s = Console.ReadLine(); //出力 int anser = 0; int count = 0; int nowH = H[0]; for (int i = 1; i < N; i++) { if (nowH >= H[i]) { count++; } else { count = 0; } if (count > anser) { anser = count; } nowH = H[i]; } Console.WriteLine(anser); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203]]
8
180
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder_20190707_1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] s = Console.ReadLine().Split(' '); List<int> upStairs = new List<int>(); upStairs.Add(0); for (int i = 0; i < s.Length - 1; i++) { if (int.Parse(s[i]) < int.Parse(s[i + 1])) { upStairs.Add(i); } } upStairs.Add(n - 1); int answer = 0; for (int i = 0; i < upStairs.Count - 1; i++) { if (upStairs[i + 1] - upStairs[i] > answer) { answer = upStairs[i + 1] - upStairs[i]; } } Console.WriteLine(answer - 1); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder_20190707_1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] s = Console.ReadLine().Split(' '); List<int> upStairs = new List<int>(); upStairs.Add(-1); for (int i = 0; i < s.Length - 1; i++) { if (int.Parse(s[i]) < int.Parse(s[i + 1])) { upStairs.Add(i); } } upStairs.Add(n - 1); int answer = 0; for (int i = 0; i < upStairs.Count - 1; i++) { if (upStairs[i + 1] - upStairs[i] > answer) { answer = upStairs[i + 1] - upStairs[i]; } } Console.WriteLine(answer - 1); } } }
[["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 241, 0, 33], ["+", 0, 213, 3, 4, 0, 28, 0, 241, 0, 203]]
8
227
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] h = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int max = 0; int cnt = 0; for (int i = 0; i < n - 1; i++) { if (h[i] > h[i + 1]) { cnt++; } else { if (max < cnt) { max = cnt; } cnt = 0; if (max >= (n - i - 2)) break; } } if (max < cnt) { max = cnt; } Console.WriteLine(max); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] h = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int max = 0; int cnt = 0; for (int i = 0; i < n - 1; i++) { if (h[i] >= h[i + 1]) { cnt++; } else { if (max < cnt) { max = cnt; } cnt = 0; if (max >= (n - i - 2)) break; } } if (max < cnt) { max = cnt; } Console.WriteLine(max); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20]]
8
188
2
using System; using System.Linq; class Program { static void Main(string[] argv) { var n = int.Parse(Console.ReadLine()); var v = Console.ReadLine().Split(' '); var r = 0; var t = 0; for (int i = 0; i < n - 1; i++) { if (int.Parse(v[i + 0]) > int.Parse(v[i + 1])) { t++; r = t > r ? t : r; } else t = 0; } Console.WriteLine(r); return; } }
using System; using System.Linq; class Program { static void Main(string[] argv) { var n = int.Parse(Console.ReadLine()); var v = Console.ReadLine().Split(' '); var r = 0; var t = 0; for (int i = 0; i < n - 1; i++) { if (int.Parse(v[i + 0]) >= int.Parse(v[i + 1])) { t++; r = t > r ? t : r; } else t = 0; } Console.WriteLine(r); return; } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20]]
8
136
2
using System; namespace ABC139_C { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var line = Console.ReadLine().Split(); int now = 0, count = 0, max = 0; int pre = int.Parse(line[0]); for (int i = 1; i < n - 1; i++) { now = int.Parse(line[i]); if (pre >= now) count++; else count = 0; if (count > max) max = count; pre = now; } Console.WriteLine(max); } } }
using System; namespace ABC139_C { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var line = Console.ReadLine().Split(); int now = 0, count = 0, max = 0; int pre = int.Parse(line[0]); for (int i = 1; i < n; i++) { now = int.Parse(line[i]); if (pre >= now) count++; else count = 0; if (count > max) max = count; pre = now; } Console.WriteLine(max); } } }
[["-", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203]]
8
140
2
using System; using System.Linq; namespace ABC139_C { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split().Select(int.Parse).ToArray(); int count = 0, max = 0; for (int i = 0; i < n - 1; i++) { if (h[i] <= h[i + 1]) count++; else count = 0; if (count > max) max = count; } Console.WriteLine(max); } } }
using System; using System.Linq; namespace ABC139_C { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split().Select(int.Parse).ToArray(); int count = 0, max = 0; for (int i = 0; i < n - 1; i++) { if (h[i] >= h[i + 1]) count++; else count = 0; if (count > max) max = count; } Console.WriteLine(max); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20]]
8
131
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var H = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); int[] count = new int[N]; Array.Reverse(count); count[0] = 1; for (int i = 1; i < N; i++) { if (H[i] > H[i - 1]) { count[i] = count[i - 1] + 1; } else { count[i] = 0; } } Console.WriteLine(count.Max()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var H = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); int[] count = new int[N]; Array.Reverse(H); count[0] = 0; for (int i = 1; i < N; i++) { if (H[i] >= H[i - 1]) { count[i] = count[i - 1] + 1; // Console.WriteLine(H[i] + " " + count[i]); } else { count[i] = 0; // Console.WriteLine(H[i] + " "+count[i]); } } Console.WriteLine(count.Max()); } } }
[["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["+", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 20]]
8
177
6
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(' '); int max = 0, now = 0, pre = int.Parse(str[0]), count = 0; for (int i = 1; i < str.Length; ++i) { now = int.Parse(str[i]); if (now < pre) { count++; } else { if (max < count) max = count; count = 0; } pre = now; } if (max < count) max = count; Console.WriteLine("{0}", max); } } }
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(' '); int max = 0, now = 0, pre = int.Parse(str[0]), count = 0; for (int i = 1; i < str.Length; ++i) { now = int.Parse(str[i]); if (now <= pre) { count++; } else { if (max < count) max = count; count = 0; } pre = now; } if (max < count) max = count; Console.WriteLine("{0}", max); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 19]]
8
162
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC_Training { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); if (n == 1) { Console.WriteLine("0"); } List<bool> vs = new List<bool>(); for (int i = 0; i < n - 1; i++) { if (h[i] >= h[i + 1]) { vs.Add(true); } else { vs.Add(false); } } vs.Add(false); int mx = 0; int count = 0; foreach (var tmp in vs) { if (tmp) { count++; } else { mx = Math.Max(mx, count); count = 0; } } Console.WriteLine(mx); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC_Training { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var h = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); if (n == 1) { Console.WriteLine("0"); return; } List<bool> vs = new List<bool>(); for (int i = 0; i < n - 1; i++) { if (h[i] >= h[i + 1]) { vs.Add(true); } else { vs.Add(false); } } vs.Add(false); int mx = 0; int count = 0; foreach (var tmp in vs) { if (tmp) { count++; } else { mx = Math.Max(mx, count); count = 0; } } Console.WriteLine(mx); } } }
[["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 35]]
8
229
2
using System; using System.Collections.Generic; namespace ABC139_C { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(); List<long> list = new List<long>(); for (long index = 0; index < n; index++) { list.Add(long.Parse(input[index])); } long maxAns = 0; for (int index = 0; index < n - 1; index++) { long ans = 0; for (int j = index; j < n - 1; j++) { if (j > 0 && list[j - 1] >= list[j]) { break; } if (list[j] < list[j + 1]) { break; } ans++; } if (maxAns < ans) { maxAns = ans; } } Console.WriteLine(maxAns); } } }
using System; using System.Collections.Generic; namespace ABC139_C { class Program { static void Main(string[] args) { long n = long.Parse(Console.ReadLine()); string[] input = Console.ReadLine().Split(); List<long> list = new List<long>(); for (long index = 0; index < n; index++) { list.Add(long.Parse(input[index])); } long maxAns = 0; for (int index = 0; index < n - 1; index++) { long ans = 0; for (int j = index; j < n - 1; j++) { if (index > 0 && list[index - 1] >= list[index]) { break; } if (list[j] < list[j + 1]) { break; } ans++; } if (maxAns < ans) { maxAns = ans; } } Console.WriteLine(maxAns); } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 16, 31, 16, 31, 22], ["-", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["-", 12, 16, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 12, 16, 12, 204, 206, 207, 0, 28, 0, 22]]
8
211
6
using System; using System.Linq; using CompLib.Util; public class Program { private int N, K; private string S; public void Solve() { var sc = new Scanner(); N = sc.NextInt(); K = sc.NextInt(); S = sc.Next(); // 最初1回 // 最小の文字が最後になるように切る // 次から // 最小の文字が2倍になる // N = 5000でも14回あれば全部最小 char min = S.Min(); char[] ans = new char[N]; for (int i = 0; i < N; i++) { ans[i] = 'z'; } char[] u = new char[2 * N]; for (int i = 0; i < N; i++) { u[i] = S[i]; u[2 * N - 1 - i] = S[i]; } if (K == 1) { for (int i = 0; i <= N; i++) { var tmp = new char[N]; for (int j = 0; j < N; j++) { tmp[j] = u[i + j]; } ChMin(ref ans, tmp); } Console.WriteLine(new string(ans)); return; } for (int i = 0; i <= N; i++) { // 最初 if (u[i + N - 1] != min) { continue; } char[] tmp = new char[N]; for (int j = 0; j < N; j++) { tmp[j] = u[i + j]; } int last = 0; for (int j = N - 1; j >= 0; j--) { if (tmp[j] == min) last++; else break; } // K-2回 for (int j = 0; j < K - 2; j++) { var tmpU = new char[2 * N]; for (int k = 0; k < N; k++) { tmpU[j] = tmp[j]; tmpU[2 * N - 1 - j] = tmp[j]; } if (last * 2 >= N) { Console.WriteLine(new string(min, N)); return; } var next = new char[N]; for (int k = 0; k < N; k++) { next[k] = tmpU[k + last]; } tmp = next; last *= 2; } if (last * 2 >= N) { Console.WriteLine(new string(min, N)); return; } var result = new char[N]; for (int j = 0; j < last; j++) result[j] = min; for (int j = 0; j < N - last; j++) { result[j + last] = tmp[N - j - 1]; } ChMin(ref ans, result); } Console.WriteLine(new string(ans)); } void ChMin(ref char[] s, char[] ar) { for (int i = 0; i < N; i++) { if (ar[i] < s[i]) { s = ar; return; } if (ar[i] > s[i]) { return; } } } public static void Main(string[] args) => new Program().Solve(); } namespace CompLib.Util { using System; using System.Linq; class Scanner { private string[] _line; private int _index; private const char Separator = ' '; public Scanner() { _line = new string[0]; _index = 0; } public string Next() { while (_index >= _line.Length) { _line = Console.ReadLine().Split(Separator); _index = 0; } return _line[_index++]; } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public decimal NextDecimal() => decimal.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { _line = Console.ReadLine().Split(Separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray(); } }
using System; using System.Linq; using CompLib.Util; public class Program { private int N, K; private string S; public void Solve() { var sc = new Scanner(); N = sc.NextInt(); K = sc.NextInt(); S = sc.Next(); // 最初1回 // 最小の文字が最後になるように切る // 次から // 最小の文字が2倍になる // N = 5000でも14回あれば全部最小 char min = S.Min(); char[] ans = new char[N]; for (int i = 0; i < N; i++) { ans[i] = 'z'; } char[] u = new char[2 * N]; for (int i = 0; i < N; i++) { u[i] = S[i]; u[2 * N - 1 - i] = S[i]; } if (K == 1) { for (int i = 0; i <= N; i++) { var tmp = new char[N]; for (int j = 0; j < N; j++) { tmp[j] = u[i + j]; } ChMin(ref ans, tmp); } Console.WriteLine(new string(ans)); return; } for (int i = 0; i <= N; i++) { // 最初 if (u[i + N - 1] != min) { continue; } char[] tmp = new char[N]; for (int j = 0; j < N; j++) { tmp[j] = u[i + j]; } int last = 0; for (int j = N - 1; j >= 0; j--) { if (tmp[j] == min) last++; else break; } // K-2回 for (int j = 0; j < K - 2; j++) { var tmpU = new char[2 * N]; for (int k = 0; k < N; k++) { tmpU[k] = tmp[k]; tmpU[2 * N - 1 - k] = tmp[k]; } if (last * 2 >= N) { Console.WriteLine(new string(min, N)); return; } var next = new char[N]; for (int k = 0; k < N; k++) { next[k] = tmpU[k + last]; } // Console.WriteLine(next); tmp = next; last *= 2; } if (last * 2 >= N) { Console.WriteLine(new string(min, N)); return; } var result = new char[N]; for (int j = 0; j < last; j++) result[j] = min; for (int j = 0; j < N - last; j++) { result[j + last] = tmp[N - j - 1]; } ChMin(ref ans, result); } Console.WriteLine(new string(ans)); } void ChMin(ref char[] s, char[] ar) { for (int i = 0; i < N; i++) { if (ar[i] < s[i]) { s = ar; return; } if (ar[i] > s[i]) { return; } } } public static void Main(string[] args) => new Program().Solve(); } namespace CompLib.Util { using System; using System.Linq; class Scanner { private string[] _line; private int _index; private const char Separator = ' '; public Scanner() { _line = new string[0]; _index = 0; } public string Next() { while (_index >= _line.Length) { _line = Console.ReadLine().Split(Separator); _index = 0; } return _line[_index++]; } public int NextInt() => int.Parse(Next()); public long NextLong() => long.Parse(Next()); public double NextDouble() => double.Parse(Next()); public decimal NextDecimal() => decimal.Parse(Next()); public char NextChar() => Next()[0]; public char[] NextCharArray() => Next().ToCharArray(); public string[] Array() { _line = Console.ReadLine().Split(Separator); _index = _line.Length; return _line; } public int[] IntArray() => Array().Select(int.Parse).ToArray(); public long[] LongArray() => Array().Select(long.Parse).ToArray(); public double[] DoubleArray() => Array().Select(double.Parse).ToArray(); public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray(); } }
[["-", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 11, 31, 204, 206, 207, 0, 28, 0, 22], ["-", 0, 11, 12, 204, 206, 207, 0, 28, 0, 22], ["+", 0, 11, 12, 204, 206, 207, 0, 28, 0, 22], ["-", 31, 204, 206, 207, 0, 28, 0, 16, 12, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 22]]
8
1,011
8
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.IO; using System.Text; using System.Diagnostics; using static util; using P = pair<int, int>; using Binary = System.Func<System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.BinaryExpression>; using Unary = System.Func<System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.UnaryExpression>; class Program { static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; static Scan sc = new Scan(); const int M = 1000000007; const int M2 = 998244353; const long LM = 1L << 60; const double eps = 1e-11; static void Main(string[] args) { int n, k; sc.Multi(out n, out k); var s = sc.Str; char fir = 'z'; foreach (var item in s) { if (fir > item) { fir = item; } } if (k > 15) { DBG(new string(fir, n)); return; } var ans = s; var rev = new string(s.ToCharArray().Reverse().ToArray()); if (ans.CompareTo(rev) > 0) ans = rev; int fc = 0; for (int i = 0; i < n; i++) { if (s[i] == fir) { ++fc; } else { if (fc == 0) continue; long ffc = fc; for (int j = 0; j < k - 1; j++) { ffc *= 2; } if (ffc >= n) { DBG(new string(fir, n)); return; } var sb = new StringBuilder(); sb.Append(fir, (int)ffc); for (int j = 0; j < n - ffc; j++) { if (i + j < n) sb.Append(s[i + j]); else sb.Append(s[n * 2 - 1 - (i + j)]); } var st = sb.ToString(); if (ans.CompareTo(st) > 0) { ans = st; } fc = 0; } } if (fc > 0) { long ffc = fc; for (int j = 0; j < k; j++) { ffc *= 2; } if (ffc >= n) { DBG(new string(fir, n)); return; } var sb = new StringBuilder(); sb.Append(fir, (int)ffc); for (int j = 0; j < n - ffc; j++) { sb.Append(s[n - 2 - j]); } var st = sb.ToString(); if (ans.CompareTo(st) > 0) { ans = st; } } Prt(ans); sw.Flush(); } static void DBG(string a) => Console.WriteLine(a); static void DBG<T>(IEnumerable<T> a) => DBG(string.Join(" ", a)); static void DBG(params object[] a) => DBG(string.Join(" ", a)); static void Prt(string a) => sw.WriteLine(a); static void Prt<T>(IEnumerable<T> a) => Prt(string.Join(" ", a)); static void Prt(params object[] a) => Prt(string.Join(" ", a)); } class pair<T, U> : IComparable<pair<T, U>> { public T v1; public U v2; public pair() : this(default(T), default(U)) {} public pair(T v1, U v2) { this.v1 = v1; this.v2 = v2; } public int CompareTo(pair<T, U> a) { int c = Comparer<T>.Default.Compare(v1, a.v1); return c != 0 ? c : Comparer<U>.Default.Compare(v2, a.v2); } public override string ToString() => v1 + " " + v2; public void Deconstruct(out T a, out U b) { a = v1; b = v2; } public static bool operator>(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) > 0; public static bool operator<(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) < 0; public static bool operator >=(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) >= 0; public static bool operator <=(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) <= 0; } static class util { public static pair<T, U> make_pair<T, U>(T v1, U v2) => new pair<T, U>(v1, v2); public static T sq<T>(T a) => Operator<T>.Multiply(a, a); public static T Max<T>(params T[] a) => a.Max(); public static T Min<T>(params T[] a) => a.Min(); public static bool inside(int i, int j, int h, int w) => i >= 0 && i < h && j >= 0 && j < w; static readonly int[] dd = { 0, 1, 0, -1 }; static readonly string dstring = "RDLU"; public static P[] adjacents(this P p) => adjacents(p.v1, p.v2); public static P[] adjacents(this P p, int h, int w) => adjacents(p.v1, p.v2, h, w); public static pair<P, char>[] adjacents_with_str(int i, int j) => Enumerable.Range(0, dd.Length) .Select(k => new pair<P, char>(new P(i + dd[k], j + dd[k ^ 1]), dstring[k])) .ToArray(); public static pair<P, char>[] adjacents_with_str(int i, int j, int h, int w) => Enumerable.Range(0, dd.Length) .Select(k => new pair<P, char>(new P(i + dd[k], j + dd[k ^ 1]), dstring[k])) .Where(p => inside(p.v1.v1, p.v1.v2, h, w)) .ToArray(); public static P[] adjacents(int i, int j) => Enumerable.Range(0, dd.Length) .Select(k => new P(i + dd[k], j + dd[k ^ 1])) .ToArray(); public static P[] adjacents(int i, int j, int h, int w) => Enumerable.Range(0, dd.Length) .Select(k => new P(i + dd[k], j + dd[k ^ 1])) .Where(p => inside(p.v1, p.v2, h, w)) .ToArray(); public static void Assert(bool cond) { if (!cond) throw new Exception(); } public static Dictionary<T, int> compress<T>(this IEnumerable<T> a) => a.Distinct() .OrderBy(v => v) .Select((v, i) => new { v, i }) .ToDictionary(p => p.v, p => p.i); public static Dictionary<T, int> compress<T>(params IEnumerable<T>[] a) => compress(a.Aggregate(Enumerable.Union)); public static void swap<T>(ref T a, ref T b) where T : struct { var t = a; a = b; b = t; } public static void swap<T>(this IList<T> a, int i, int j) where T : struct { var t = a[i]; a[i] = a[j]; a[j] = t; } public static T[] copy<T>(this IList<T> a) { var ret = new T[a.Count]; for (int i = 0; i < a.Count; i++) ret[i] = a[i]; return ret; } } static class Operator<T> { static readonly ParameterExpression x = Expression.Parameter(typeof(T), "x"); static readonly ParameterExpression y = Expression.Parameter(typeof(T), "y"); public static readonly Func<T, T, T> Add = Lambda(Expression.Add); public static readonly Func<T, T, T> Subtract = Lambda(Expression.Subtract); public static readonly Func<T, T, T> Multiply = Lambda(Expression.Multiply); public static readonly Func<T, T, T> Divide = Lambda(Expression.Divide); public static readonly Func<T, T> Plus = Lambda(Expression.UnaryPlus); public static readonly Func<T, T> Negate = Lambda(Expression.Negate); public static Func<T, T, T> Lambda(Binary op) => Expression.Lambda<Func<T, T, T>>(op(x, y), x, y).Compile(); public static Func<T, T> Lambda(Unary op) => Expression.Lambda<Func<T, T>>(op(x), x).Compile(); } class Scan { StreamReader sr; public Scan() { sr = new StreamReader(Console.OpenStandardInput()); } public Scan(string path) { sr = new StreamReader(path); } public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public string Str => sr.ReadLine().Trim(); public pair<T, U> Pair<T, U>() { T a; U b; Multi(out a, out b); return new pair<T, U>(a, b); } public P P => Pair<int, int>(); public int[] IntArr => StrArr.Select(int.Parse).ToArray(); public long[] LongArr => StrArr.Select(long.Parse).ToArray(); public double[] DoubleArr => StrArr.Select(double.Parse).ToArray(); public string[] StrArr => Str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 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); public void Multi<T>(out T a) => a = cv<T>(Str); public void Multi<T, U>(out T a, out U b) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); } public void Multi<T, U, V>(out T a, out U b, out V c) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } public void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = StrArr; 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]); } }
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.IO; using System.Text; using System.Diagnostics; using static util; using P = pair<int, int>; using Binary = System.Func<System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.BinaryExpression>; using Unary = System.Func<System.Linq.Expressions.ParameterExpression, System.Linq.Expressions.UnaryExpression>; class Program { static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; static Scan sc = new Scan(); const int M = 1000000007; const int M2 = 998244353; const long LM = 1L << 60; const double eps = 1e-11; static void Main(string[] args) { int n, k; sc.Multi(out n, out k); var s = sc.Str; char fir = 'z'; foreach (var item in s) { if (fir > item) { fir = item; } } if (k > 15) { DBG(new string(fir, n)); return; } var ans = s; var rev = new string(s.ToCharArray().Reverse().ToArray()); if (ans.CompareTo(rev) > 0) ans = rev; int fc = 0; for (int i = 0; i < n; i++) { if (s[i] == fir) { ++fc; } else { if (fc == 0) continue; long ffc = fc; for (int j = 0; j < k - 1; j++) { ffc *= 2; } if (ffc >= n) { DBG(new string(fir, n)); return; } var sb = new StringBuilder(); sb.Append(fir, (int)ffc); for (int j = 0; j < n - ffc; j++) { if (i + j < n) sb.Append(s[i + j]); else sb.Append(s[n * 2 - 1 - (i + j)]); } var st = sb.ToString(); if (ans.CompareTo(st) > 0) { ans = st; } fc = 0; } } if (fc > 0) { long ffc = fc; for (int j = 0; j < k; j++) { ffc *= 2; } if (ffc >= n) { DBG(new string(fir, n)); return; } var sb = new StringBuilder(); sb.Append(fir, (int)ffc); for (int j = 0; j < n - ffc; j++) { sb.Append(s[n - fc - 1 - j]); } var st = sb.ToString(); if (ans.CompareTo(st) > 0) { ans = st; } } Prt(ans); sw.Flush(); } static void DBG(string a) => Console.WriteLine(a); static void DBG<T>(IEnumerable<T> a) => DBG(string.Join(" ", a)); static void DBG(params object[] a) => DBG(string.Join(" ", a)); static void Prt(string a) => sw.WriteLine(a); static void Prt<T>(IEnumerable<T> a) => Prt(string.Join(" ", a)); static void Prt(params object[] a) => Prt(string.Join(" ", a)); } class pair<T, U> : IComparable<pair<T, U>> { public T v1; public U v2; public pair() : this(default(T), default(U)) {} public pair(T v1, U v2) { this.v1 = v1; this.v2 = v2; } public int CompareTo(pair<T, U> a) { int c = Comparer<T>.Default.Compare(v1, a.v1); return c != 0 ? c : Comparer<U>.Default.Compare(v2, a.v2); } public override string ToString() => v1 + " " + v2; public void Deconstruct(out T a, out U b) { a = v1; b = v2; } public static bool operator>(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) > 0; public static bool operator<(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) < 0; public static bool operator >=(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) >= 0; public static bool operator <=(pair<T, U> a, pair<T, U> b) => a.CompareTo(b) <= 0; } static class util { public static pair<T, U> make_pair<T, U>(T v1, U v2) => new pair<T, U>(v1, v2); public static T sq<T>(T a) => Operator<T>.Multiply(a, a); public static T Max<T>(params T[] a) => a.Max(); public static T Min<T>(params T[] a) => a.Min(); public static bool inside(int i, int j, int h, int w) => i >= 0 && i < h && j >= 0 && j < w; static readonly int[] dd = { 0, 1, 0, -1 }; static readonly string dstring = "RDLU"; public static P[] adjacents(this P p) => adjacents(p.v1, p.v2); public static P[] adjacents(this P p, int h, int w) => adjacents(p.v1, p.v2, h, w); public static pair<P, char>[] adjacents_with_str(int i, int j) => Enumerable.Range(0, dd.Length) .Select(k => new pair<P, char>(new P(i + dd[k], j + dd[k ^ 1]), dstring[k])) .ToArray(); public static pair<P, char>[] adjacents_with_str(int i, int j, int h, int w) => Enumerable.Range(0, dd.Length) .Select(k => new pair<P, char>(new P(i + dd[k], j + dd[k ^ 1]), dstring[k])) .Where(p => inside(p.v1.v1, p.v1.v2, h, w)) .ToArray(); public static P[] adjacents(int i, int j) => Enumerable.Range(0, dd.Length) .Select(k => new P(i + dd[k], j + dd[k ^ 1])) .ToArray(); public static P[] adjacents(int i, int j, int h, int w) => Enumerable.Range(0, dd.Length) .Select(k => new P(i + dd[k], j + dd[k ^ 1])) .Where(p => inside(p.v1, p.v2, h, w)) .ToArray(); public static void Assert(bool cond) { if (!cond) throw new Exception(); } public static Dictionary<T, int> compress<T>(this IEnumerable<T> a) => a.Distinct() .OrderBy(v => v) .Select((v, i) => new { v, i }) .ToDictionary(p => p.v, p => p.i); public static Dictionary<T, int> compress<T>(params IEnumerable<T>[] a) => compress(a.Aggregate(Enumerable.Union)); public static void swap<T>(ref T a, ref T b) where T : struct { var t = a; a = b; b = t; } public static void swap<T>(this IList<T> a, int i, int j) where T : struct { var t = a[i]; a[i] = a[j]; a[j] = t; } public static T[] copy<T>(this IList<T> a) { var ret = new T[a.Count]; for (int i = 0; i < a.Count; i++) ret[i] = a[i]; return ret; } } static class Operator<T> { static readonly ParameterExpression x = Expression.Parameter(typeof(T), "x"); static readonly ParameterExpression y = Expression.Parameter(typeof(T), "y"); public static readonly Func<T, T, T> Add = Lambda(Expression.Add); public static readonly Func<T, T, T> Subtract = Lambda(Expression.Subtract); public static readonly Func<T, T, T> Multiply = Lambda(Expression.Multiply); public static readonly Func<T, T, T> Divide = Lambda(Expression.Divide); public static readonly Func<T, T> Plus = Lambda(Expression.UnaryPlus); public static readonly Func<T, T> Negate = Lambda(Expression.Negate); public static Func<T, T, T> Lambda(Binary op) => Expression.Lambda<Func<T, T, T>>(op(x, y), x, y).Compile(); public static Func<T, T> Lambda(Unary op) => Expression.Lambda<Func<T, T>>(op(x), x).Compile(); } class Scan { StreamReader sr; public Scan() { sr = new StreamReader(Console.OpenStandardInput()); } public Scan(string path) { sr = new StreamReader(path); } public int Int => int.Parse(Str); public long Long => long.Parse(Str); public double Double => double.Parse(Str); public string Str => sr.ReadLine().Trim(); public pair<T, U> Pair<T, U>() { T a; U b; Multi(out a, out b); return new pair<T, U>(a, b); } public P P => Pair<int, int>(); public int[] IntArr => StrArr.Select(int.Parse).ToArray(); public long[] LongArr => StrArr.Select(long.Parse).ToArray(); public double[] DoubleArr => StrArr.Select(double.Parse).ToArray(); public string[] StrArr => Str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 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); public void Multi<T>(out T a) => a = cv<T>(Str); public void Multi<T, U>(out T a, out U b) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); } public void Multi<T, U, V>(out T a, out U b, out V c) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } public void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = StrArr; 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]); } }
[["-", 206, 207, 0, 28, 0, 16, 31, 16, 12, 203], ["+", 0, 28, 0, 16, 31, 16, 31, 16, 12, 22], ["+", 206, 207, 0, 28, 0, 16, 31, 16, 17, 33], ["+", 206, 207, 0, 28, 0, 16, 31, 16, 12, 203]]
8
2,835
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137A { class Program { static void Main(string[] args) { var word = Console.ReadLine().Split(); double a = double.Parse(word[0]); double b = double.Parse(word[1]); double max = a + b; if (max <= (a * b)) { max = a * b; if (max <= (a - b)) max = a - b; } Console.WriteLine(max); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137A { class Program { static void Main(string[] args) { var word = Console.ReadLine().Split(); double a = double.Parse(word[0]); double b = double.Parse(word[1]); double max = a + b; if (max <= (a * b)) max = a * b; else if (max <= (a - b)) max = a - b; Console.WriteLine(max); Console.ReadKey(); } } }
[["-", 0, 195, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 201, 0, 195, 8, 196, 0, 57, 0, 95], ["-", 0, 195, 8, 196, 0, 57, 64, 196, 0, 46]]
8
139
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137A { class Program { static void Main(string[] args) { int A, B; var word = Console.ReadLine().Split(' '); A = int.Parse(word[0]); B = int.Parse(word[1]); if (A + B >= A - B || A + B >= A * B) Console.Write(A + B); else if (A - B >= A + B || A - B >= A * B) Console.WriteLine(A - B); else if (A * B >= A + B || A * B >= A - B) Console.WriteLine(A * B); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137A { class Program { static void Main(string[] args) { int A, B; var word = Console.ReadLine().Split(' '); A = int.Parse(word[0]); B = int.Parse(word[1]); if (A + B >= A - B && A + B >= A * B) Console.Write(A + B); else if (A - B >= A + B && A - B >= A * B) Console.WriteLine(A - B); else if (A * B >= A + B && A * B >= A - B) Console.WriteLine(A * B); Console.ReadKey(); } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 106], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 8, 196, 0, 57, 75, 57, 15, 16, 17, 106], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 17, 98], ["-", 0, 57, 75, 57, 75, 57, 15, 16, 17, 106], ["+", 0, 57, 75, 57, 75, 57, 15, 16, 17, 98]]
8
180
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace kyopro { class Program { static void Main(string[] args) { string[] ab = Console.ReadLine().Split(' '); int a = int.Parse(ab[0]); int b = int.Parse(ab[1]); var kotae = new List<int>(); kotae.Add(a + b); kotae.Add(a - b); kotae.Add(a * b); kotae.Sort((c, d) => c - d); Console.WriteLine(kotae[0]); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace kyopro { class Program { static void Main(string[] args) { string[] ab = Console.ReadLine().Split(' '); int a = int.Parse(ab[0]); int b = int.Parse(ab[1]); var kotae = new List<int>(); kotae.Add(a + b); kotae.Add(a - b); kotae.Add(a * b); kotae.Sort((c, d) => d - c); Console.WriteLine(kotae[0]); } } }
[["-", 3, 4, 0, 28, 0, 218, 8, 16, 31, 22], ["-", 3, 4, 0, 28, 0, 218, 8, 16, 17, 33], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 17, 33], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 12, 22]]
8
153
4
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Numerics; using System.Threading.Tasks; using static System.Console; using static System.Math; using static Func; 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(); uint[] GU => Strs.Select(uint.Parse).ToArray(); decimal[] GD => Strs.Select(decimal.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); static ulong One(int k) => 1ul << k; static ulong All(int k) => One(k) - 1; static bool Has(ulong s, int k) => (s & One(k)) != 0; const int MOD = 1000000007; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); Solve(); Out.Flush(); } void Solve() { var I = G; WriteLine(Max(I[0] + I[1], Max(I[0] - I[1], I[0] * I[2]))); } } struct Data : IComparable<Data> { public readonly int Delay, Bonus; public Data(int a, int b) { Delay = a; Bonus = b; } public int CompareTo(Data other) => other.Bonus.CompareTo(Bonus); } static class Func { public const int Inf = 1073741789; public const long InfL = 4011686018427387913L; public static T[] ConstantArray<T>(int n, T val) { var a = new T[n]; for (var i = 0; i < n; i++) a[i] = val; return a; } public static T[] MakeArray<T>(int n, Func<int, T> f) { var a = new T[n]; for (var i = 0; i < n; i++) a[i] = f(i); return a; } public static T[] Recurse<T>(int n, T a0, Func<int, T, T> f) { var a = new T[n]; a[0] = a0; for (var i = 1; i < n; i++) a[i] = f(i, a[i - 1]); return a; } public static T[] Accumulate<T>(IList<T> a, T zero, Func<T, T, T> f) { var s = new T[a.Count + 1]; s[0] = zero; for (var i = 0; i < a.Count; i++) s[i + 1] = f(s[i], a[i]); return s; } public static int FirstBinary(int min, int max, Predicate<int> pred) { while (min < max) { var mid = (min + max) / 2; if (pred(mid)) max = mid; else min = mid + 1; } return min; } public static long GCD(long a, long b) { var n = (ulong)Abs(a); var m = (ulong)Abs(b); if (n == 0) return (long)m; if (m == 0) return (long)n; int zm = 0, zn = 0; while ((n & 1) == 0) { n >>= 1; zn++; } while ((m & 1) == 0) { m >>= 1; zm++; } while (m != n) { if (m > n) { m -= n; while ((m & 1) == 0) m >>= 1; } else { n -= m; while ((n & 1) == 0) n >>= 1; } } return (long)n << Min(zm, zn); } public static long Mod(long a, long mod) => (a % mod + mod) % mod; public static long Inverse(long a, long mod) => Mod(SolveLinear(Mod(a, mod), mod)[0], mod); 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; } public static long[] SolveLinear(long n, long m) { var A = new long[,] { { n < 0 ? -1 : 1, 0 }, { 0, m < 0 ? -1 : 1 } }; n = Abs(n); m = Abs(m); while (m > 0) { long q = n / m, r = n % m; n = m; m = r; for (var i = 0; i < 2; i++) { var x = A[i, 1]; A[i, 1] = A[i, 0] - q * x; A[i, 0] = x; } } return new long[] { A[0, 0], A[1, 0] }; } public static int CombineHashCodes(int h1, int h2) => (((h1 << 5) + h1) ^ h2); public static int[] GetFact(int lim, int mod) { var f = new int[lim]; f[0] = 1; for (var i = 1; i < lim; i++) f[i] = (int)((long)f[i - 1] * i % mod); return f; } public static int[] GetInvFact(int lim, int mod) { var fc = 1; for (var i = 2; i < lim; i++) fc = (int)((long)fc * i % mod); return GetInvFact(lim, fc, mod); } // GetInvFact(n, (n-1)! mod p, p) gives {1, 1/2, ..., 1/(n-1)!} mod p public static int[] GetInvFact(int lim, int fc_of_lim_m_1, int mod) { var ifc = new int[lim]; ifc[lim - 1] = (int)Inverse(fc_of_lim_m_1, mod); for (var i = lim - 1; i >= 1; i--) ifc[i - 1] = (int)((long)ifc[i] * i % mod); return ifc; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Numerics; using System.Threading.Tasks; using static System.Console; using static System.Math; using static Func; 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(); uint[] GU => Strs.Select(uint.Parse).ToArray(); decimal[] GD => Strs.Select(decimal.Parse).ToArray(); long[] GL => Strs.Select(long.Parse).ToArray(); string Str => ReadLine(); string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); static ulong One(int k) => 1ul << k; static ulong All(int k) => One(k) - 1; static bool Has(ulong s, int k) => (s & One(k)) != 0; const int MOD = 1000000007; public K() { SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); Solve(); Out.Flush(); } void Solve() { var I = G; WriteLine(Max(I[0] + I[1], Max(I[0] - I[1], I[0] * I[1]))); } } struct Data : IComparable<Data> { public readonly int Delay, Bonus; public Data(int a, int b) { Delay = a; Bonus = b; } public int CompareTo(Data other) => other.Bonus.CompareTo(Bonus); } static class Func { public const int Inf = 1073741789; public const long InfL = 4011686018427387913L; public static T[] ConstantArray<T>(int n, T val) { var a = new T[n]; for (var i = 0; i < n; i++) a[i] = val; return a; } public static T[] MakeArray<T>(int n, Func<int, T> f) { var a = new T[n]; for (var i = 0; i < n; i++) a[i] = f(i); return a; } public static T[] Recurse<T>(int n, T a0, Func<int, T, T> f) { var a = new T[n]; a[0] = a0; for (var i = 1; i < n; i++) a[i] = f(i, a[i - 1]); return a; } public static T[] Accumulate<T>(IList<T> a, T zero, Func<T, T, T> f) { var s = new T[a.Count + 1]; s[0] = zero; for (var i = 0; i < a.Count; i++) s[i + 1] = f(s[i], a[i]); return s; } public static int FirstBinary(int min, int max, Predicate<int> pred) { while (min < max) { var mid = (min + max) / 2; if (pred(mid)) max = mid; else min = mid + 1; } return min; } public static long GCD(long a, long b) { var n = (ulong)Abs(a); var m = (ulong)Abs(b); if (n == 0) return (long)m; if (m == 0) return (long)n; int zm = 0, zn = 0; while ((n & 1) == 0) { n >>= 1; zn++; } while ((m & 1) == 0) { m >>= 1; zm++; } while (m != n) { if (m > n) { m -= n; while ((m & 1) == 0) m >>= 1; } else { n -= m; while ((n & 1) == 0) n >>= 1; } } return (long)n << Min(zm, zn); } public static long Mod(long a, long mod) => (a % mod + mod) % mod; public static long Inverse(long a, long mod) => Mod(SolveLinear(Mod(a, mod), mod)[0], mod); 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; } public static long[] SolveLinear(long n, long m) { var A = new long[,] { { n < 0 ? -1 : 1, 0 }, { 0, m < 0 ? -1 : 1 } }; n = Abs(n); m = Abs(m); while (m > 0) { long q = n / m, r = n % m; n = m; m = r; for (var i = 0; i < 2; i++) { var x = A[i, 1]; A[i, 1] = A[i, 0] - q * x; A[i, 0] = x; } } return new long[] { A[0, 0], A[1, 0] }; } public static int CombineHashCodes(int h1, int h2) => (((h1 << 5) + h1) ^ h2); public static int[] GetFact(int lim, int mod) { var f = new int[lim]; f[0] = 1; for (var i = 1; i < lim; i++) f[i] = (int)((long)f[i - 1] * i % mod); return f; } public static int[] GetInvFact(int lim, int mod) { var fc = 1; for (var i = 2; i < lim; i++) fc = (int)((long)fc * i % mod); return GetInvFact(lim, fc, mod); } // GetInvFact(n, (n-1)! mod p, p) gives {1, 1/2, ..., 1/(n-1)!} mod p public static int[] GetInvFact(int lim, int fc_of_lim_m_1, int mod) { var ifc = new int[lim]; ifc[lim - 1] = (int)Inverse(fc_of_lim_m_1, mod); for (var i = lim - 1; i >= 1; i--) ifc[i - 1] = (int)((long)ifc[i] * i % mod); return ifc; } }
[["-", 0, 16, 12, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 16, 12, 204, 206, 207, 0, 28, 0, 203]]
8
1,462
2
using System; class Program { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int A = int.Parse(input[0]); int B = int.Parse(input[1]); if (-100 <= A && B <= 100) { int max = 0; int[] number = { A + B, A - B, A * B }; for (int i = 0; i < 3; i++) { if (max <= number[i]) { max = number[i]; } } Console.WriteLine(max); Console.ReadLine(); } } }
using System; class Program { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int A = int.Parse(input[0]); int B = int.Parse(input[1]); if (-100 <= A && B <= 100) { int max = -99999999; int[] number = { A + B, A - B, A * B }; for (int i = 0; i < 3; i++) { if (max <= number[i]) { max = number[i]; } } Console.WriteLine(max); Console.ReadLine(); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
146
3
using System; using System.IO; namespace Sandbox { internal class Program { private static void Main(string[] args) { var scanner = new Scanner(); var a = scanner.GetNextAsInt(); var b = scanner.GetNextAsInt(); var output = Math.Max(a + b, a - b); output = Math.Max(a + b, a * b); Console.WriteLine(output); } #region String Scanner public class Scanner { private const char Separator = ' '; private readonly StreamReader _Stream = new StreamReader(Console.OpenStandardInput()); private int _Pos; private string[] _Line = new string[0]; #region get a element public string GetNext() { // ReSharper disable once InvertIf if (this._Pos >= this._Line.Length) { // ReSharper disable once PossibleNullReferenceException this._Line = this._Stream.ReadLine().Split(Separator); this._Pos = 0; } return this._Line[this._Pos++]; } public int GetNextAsInt() { return int.Parse(this.GetNext()); } public long GetNextAsLong() { return long.Parse(this.GetNext()); } public double GetNextAsDouble() { return double.Parse(this.GetNext()); } #endregion #region get array public string[] ToArrayFromNextLine() { if (this._Pos >= this._Line.Length) { // ReSharper disable once PossibleNullReferenceException this._Line = this._Stream.ReadLine().Split(Separator); } this._Pos = this._Line.Length; return this._Line; } public int[] ToIntArrayFromNextLine() { return ToIntArray(this.ToArrayFromNextLine()); } public long[] ToLongArrayFromNextLine() { return ToLongArray(this.ToArrayFromNextLine()); } public double[] ToDoubleArrayFromNextLine() { return ToDoubleArray(this.ToArrayFromNextLine()); } #endregion #region convert array private static int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private static long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private static double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion } #endregion } }
using System; using System.IO; namespace Sandbox { internal class Program { private static void Main(string[] args) { var scanner = new Scanner(); var a = scanner.GetNextAsInt(); var b = scanner.GetNextAsInt(); var output = Math.Max(a + b, a - b); output = Math.Max(output, a * b); Console.WriteLine(output); } #region String Scanner public class Scanner { private const char Separator = ' '; private readonly StreamReader _Stream = new StreamReader(Console.OpenStandardInput()); private int _Pos; private string[] _Line = new string[0]; #region get a element public string GetNext() { // ReSharper disable once InvertIf if (this._Pos >= this._Line.Length) { // ReSharper disable once PossibleNullReferenceException this._Line = this._Stream.ReadLine().Split(Separator); this._Pos = 0; } return this._Line[this._Pos++]; } public int GetNextAsInt() { return int.Parse(this.GetNext()); } public long GetNextAsLong() { return long.Parse(this.GetNext()); } public double GetNextAsDouble() { return double.Parse(this.GetNext()); } #endregion #region get array public string[] ToArrayFromNextLine() { if (this._Pos >= this._Line.Length) { // ReSharper disable once PossibleNullReferenceException this._Line = this._Stream.ReadLine().Split(Separator); } this._Pos = this._Line.Length; return this._Line; } public int[] ToIntArrayFromNextLine() { return ToIntArray(this.ToArrayFromNextLine()); } public long[] ToLongArrayFromNextLine() { return ToLongArray(this.ToArrayFromNextLine()); } public double[] ToDoubleArrayFromNextLine() { return ToDoubleArray(this.ToArrayFromNextLine()); } #endregion #region convert array private static int[] ToIntArray(string[] array) { var result = new int[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = int.Parse(array[i]); } return result; } private static long[] ToLongArray(string[] array) { var result = new long[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = long.Parse(array[i]); } return result; } private static double[] ToDoubleArray(string[] array) { var result = new double[array.Length]; for (var i = 0; i < array.Length; i++) { result[i] = double.Parse(array[i]); } return result; } #endregion } #endregion } }
[["-", 12, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["-", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 12, 213, 3, 4, 0, 28, 0, 16, 12, 22], ["+", 0, 11, 12, 213, 3, 4, 0, 28, 0, 22]]
8
568
4
using System; namespace ACpra5 { class Program { static void Main(string[] args) { String[] input = Console.ReadLine().Split(' '); int K = int.Parse(input[0]); int X = int.Parse(input[1]); for (int i = X - K + 1; i < X + K - 1; i++) { Console.Write("{0}", i); } Console.WriteLine(X + K - 1); } } }
using System; namespace ACpra5 { class Program { static void Main(string[] args) { String[] input = Console.ReadLine().Split(' '); int K = int.Parse(input[0]); int X = int.Parse(input[1]); for (int i = X - K + 1; i < X + K - 1; i++) { Console.Write("{0} ", i); } Console.WriteLine(X + K - 1); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
112
2
using System; class oneclue { public static void Main() { String[] str = Console.ReadLine().Split(' '); int k = int.Parse(str[0]); int x = int.Parse(str[1]); for (int i = x - k; i < x + k; i++) { Console.Write("{0} ", i); } Console.WriteLine(x + k); } }
using System; class oneclue { public static void Main() { String[] str = Console.ReadLine().Split(' '); int k = int.Parse(str[0]); int x = int.Parse(str[1]); for (int i = x - k + 1; i < x + k - 1; i++) { Console.Write("{0} ", i); } Console.WriteLine(x + k - 1); } }
[["+", 10, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
99
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp10 { class Program { static void Main(string[] args) { string str = Console.ReadLine(); string[] strArray = str.Split(' '); int k = int.Parse(strArray[0]); int x = int.Parse(strArray[1]); int min = x - k + 1; int max = x + k - 1; for (int ans = min; ans <= max; ans++) Console.Write(ans + ' '); Console.WriteLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp10 { class Program { static void Main(string[] args) { string str = Console.ReadLine(); string[] strArray = str.Split(' '); int k = int.Parse(strArray[0]); int x = int.Parse(strArray[1]); int min = x - k + 1; int max = x + k - 1; for (int ans = min; ans <= max; ans++) Console.Write(ans + " "); Console.WriteLine(); } } }
[["-", 3, 4, 0, 28, 0, 16, 12, 252, 0, 104], ["+", 3, 4, 0, 28, 0, 16, 12, 5, 0, 62]]
8
144
4
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Numerics; namespace AtCoderWorkspace { class Program { static void Main(string[] args) { var cin = new Scanner(); var K = cin.nextInt(); var X = cin.nextInt(); var l = X - K; var r = X + K; if (K == 1) { Console.WriteLine(X); return; } var count = 1; var start = 0; var hankei = K / 2 + 1; if (l < -1000000) { count += Math.Abs(-1000000 - X); start = -1000000; } else { count += hankei; start = X - hankei; } if (r > 1000000) { count += Math.Abs(1000000 - X); } else { count += hankei; } for (int i = start; i < (start + count); i++) { if (i == (start + count) - 1) { Console.Write(i); } else { Console.Write(i + " "); } } } } 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; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Numerics; namespace AtCoderWorkspace { class Program { static void Main(string[] args) { var cin = new Scanner(); var K = cin.nextInt(); var X = cin.nextInt(); var l = X - K; var r = X + K; if (K == 1) { Console.WriteLine(X); return; } var count = 1; var start = 0; var hankei = K - 1; if (l < -1000000) { count += Math.Abs(-1000000 - X); start = -1000000; } else { count += hankei; start = X - hankei; } if (r > 1000000) { count += Math.Abs(1000000 - X); } else { count += hankei; } for (int i = start; i < (start + count); i++) { if (i == (start + count) - 1) { Console.Write(i); } else { Console.Write(i + " "); } } } } 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, 200, 0, 212, 0, 16, 31, 16, 17, 85], ["-", 0, 200, 0, 212, 0, 16, 31, 16, 12, 203], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 33]]
8
597
4
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using Library; using static Library.Comfort; using static Library.Math; using static Library.Algorithm; using static System.Math; using static System.Console; using System.Threading.Tasks; namespace AtCoderer { public class App { public static void Solve() { int a, b; Parse(out a, out b); cout = string.Join("", Enumerable.Range(b - a + 1, a * 2 - 1)); } public static void Main(string[] args) { #if DEBUG while (true) { Solve(); } #else Solve(); #endif } } } namespace Library { public static class Comfort { public const int mod = 1000000007; public class Point { public int a; public int b; public Point(int a, int b) { this.a = a; this.b = b; } } public static void DWrite<T>(T o) { #if DEBUG Console.WriteLine(o); #endif } public static int[] OrderBy(this int[] IArr) { int[] arr = new int[IArr.Length]; Array.Copy(IArr, arr, IArr.Length); Array.Sort(arr); return arr; } public static char[] OrderBy(this string IArr) { char[] arr = new char[IArr.Length]; Array.Copy(IArr.ToCharArray(), arr, IArr.Length); Array.Sort(arr); return arr; } public static int Max(params int[] p) => p.Max(); public static long Max(params long[] p) => p.Max(); public static int Min(params int[] p) => p.Min(); public static long Min(params long[] p) => p.Min(); public static int In() { return int.Parse(Console.ReadLine()); } public static int Inl() { return int.Parse(Console.ReadLine()); } public static int[] Parse() { return Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); } public static long[] Parsel() { return Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); } public static void Parse(out int a, out int b) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; } public static void Parse(out int a, out int b, out int c) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; } public static void Parse(out int a, out int b, out int c, out int d) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; d = r[3]; } public static void Parse(out long a, out long b) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; } public static void Parse(out long a, out long b, out long c) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; } public static void Parse(out long a, out long b, out long c, out long d) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; d = r[3]; } public static string ToString(this IEnumerable<char> v) { return string.Join("", v); } public static string Join<T>(this IEnumerable<T> v) { return string.Join(" ", v); } public static string Join<T>(this IEnumerable<T> v, char c) { return string.Join(c.ToString(), v); } public static string Join<T>(this IEnumerable<T> v, string s) { return string.Join(s, v); } public static object cout { set { Console.WriteLine(value); } } public static object dout { set { DWrite(value); } } } public static class Math { public static bool IsNatural(this float v) => v - (int)v == 0; public static bool IsNatural(this double v) => v - (int)v == 0; public static long nCm(long n, long m) { if (m == 0) return 1; if (n == 0) return 0; return n * nCm(n - 1, m - 1) / m % 1000000007; } public static IEnumerable<int> Factor(int n) { int i = 2; int tmp = n; while (i * i <= n) { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; } public static int Gcd(int a, int b) => a > b? GcdRecursive(a, b) : GcdRecursive(b, a); private static int GcdRecursive(int x, int y) => y == 0 ? x : GcdRecursive(y, x % y); public static long Gcd(long a, long b) => a > b? GcdRecursive(a, b) : GcdRecursive(b, a); private static long GcdRecursive(long x, long y) => y == 0 ? x : GcdRecursive(y, x % y); public static long Lcm(int a, int b) => ((long)a) * b / Gcd(a, b); } public static class Algorithm { public class EnumAll { bool Over = false; int UpperValue; int Dimension; public int[] Current { private set; get; } public EnumAll(int MaxValue, int Dimension) { this.Dimension = Dimension; UpperValue = MaxValue; Current = new int[Dimension]; Current[0] = -1; } public bool Next() { if (Over) return false; bool flag = false; for (int i = 0; i < Dimension; i++) { if (++Current[i] >= UpperValue) Current[i] = 0; else { flag = true; break; } } if (!flag) { Over = true; return false; } return true; } } public class Graph { public int Count = -1; private HashSet<int>[] Joined; public static Graph GenerateComplete(int size) { throw new NotImplementedException(); } public Graph(int size) { Joined = new HashSet<int>[size]; for (int i = 0; i < Joined.Length; i++) Joined[i] = new HashSet<int>(); Count = size; } public int[] GetConnected(int index) { return Joined[index].ToArray(); } public void AddEdge(int a, int b) { if (a == b) DWrite("ループを追加することはできません A:" + a + " B:" + b); else { if (!Joined[a].Add(b)) DWrite("多重辺を追加することはできません A:" + a + " B:" + b); else Joined[b].Add(a); } } public int[] ToTree(int Root) { var ar = new int[Count]; for (int i = 0; i < ar.Length; i++) ar[i] = -10000000; ar[Root] = -1; Queue<int> bfs = new Queue<int>(); bfs.Enqueue(Root); while (bfs.Count != 0) { var t = bfs.Dequeue(); foreach (var v in Joined[t]) { bfs.Enqueue(v); if (ar[v] == -10000000) ar[v] = t; } } return ar; } } public class UnionFind { private int[] Parent; private int GetRoot(int v) { if (Parent[v] < 0) return v; else return Parent[v] = GetRoot(Parent[v]); } public UnionFind(int size) { Parent = new int[size]; for (int i = 0; i < size; i++) Parent[i] = -1; } public bool AreGrouped(int a, int b) { return GetRoot(a) == GetRoot(b); } public void Connect(int a, int b) { a = GetRoot(a); b = GetRoot(b); if (a == b) return; if (Parent[b] < Parent[a]) { Parent[b] += Parent[a]; Parent[a] = b; } else { Parent[a] += Parent[b]; Parent[b] = a; } } public int GetGroupSize(int a) { return -Parent[GetRoot(a)]; } } } }
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using Library; using static Library.Comfort; using static Library.Math; using static Library.Algorithm; using static System.Math; using static System.Console; using System.Threading.Tasks; namespace AtCoderer { public class App { public static void Solve() { int a, b; Parse(out a, out b); cout = string.Join(" ", Enumerable.Range(b - a + 1, a * 2 - 1)); } public static void Main(string[] args) { #if DEBUG while (true) { Solve(); } #else Solve(); #endif } } } namespace Library { public static class Comfort { public const int mod = 1000000007; public class Point { public int a; public int b; public Point(int a, int b) { this.a = a; this.b = b; } } public static void DWrite<T>(T o) { #if DEBUG Console.WriteLine(o); #endif } public static int[] OrderBy(this int[] IArr) { int[] arr = new int[IArr.Length]; Array.Copy(IArr, arr, IArr.Length); Array.Sort(arr); return arr; } public static char[] OrderBy(this string IArr) { char[] arr = new char[IArr.Length]; Array.Copy(IArr.ToCharArray(), arr, IArr.Length); Array.Sort(arr); return arr; } public static int Max(params int[] p) => p.Max(); public static long Max(params long[] p) => p.Max(); public static int Min(params int[] p) => p.Min(); public static long Min(params long[] p) => p.Min(); public static int In() { return int.Parse(Console.ReadLine()); } public static int Inl() { return int.Parse(Console.ReadLine()); } public static int[] Parse() { return Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); } public static long[] Parsel() { return Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); } public static void Parse(out int a, out int b) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; } public static void Parse(out int a, out int b, out int c) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; } public static void Parse(out int a, out int b, out int c, out int d) { var r = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; d = r[3]; } public static void Parse(out long a, out long b) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; } public static void Parse(out long a, out long b, out long c) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; } public static void Parse(out long a, out long b, out long c, out long d) { var r = Console.ReadLine().Split().Select(v => long.Parse(v)).ToArray(); a = r[0]; b = r[1]; c = r[2]; d = r[3]; } public static string ToString(this IEnumerable<char> v) { return string.Join("", v); } public static string Join<T>(this IEnumerable<T> v) { return string.Join(" ", v); } public static string Join<T>(this IEnumerable<T> v, char c) { return string.Join(c.ToString(), v); } public static string Join<T>(this IEnumerable<T> v, string s) { return string.Join(s, v); } public static object cout { set { Console.WriteLine(value); } } public static object dout { set { DWrite(value); } } } public static class Math { public static bool IsNatural(this float v) => v - (int)v == 0; public static bool IsNatural(this double v) => v - (int)v == 0; public static long nCm(long n, long m) { if (m == 0) return 1; if (n == 0) return 0; return n * nCm(n - 1, m - 1) / m % 1000000007; } public static IEnumerable<int> Factor(int n) { int i = 2; int tmp = n; while (i * i <= n) { if (tmp % i == 0) { tmp /= i; yield return i; } else { i++; } } if (tmp != 1) yield return tmp; } public static int Gcd(int a, int b) => a > b? GcdRecursive(a, b) : GcdRecursive(b, a); private static int GcdRecursive(int x, int y) => y == 0 ? x : GcdRecursive(y, x % y); public static long Gcd(long a, long b) => a > b? GcdRecursive(a, b) : GcdRecursive(b, a); private static long GcdRecursive(long x, long y) => y == 0 ? x : GcdRecursive(y, x % y); public static long Lcm(int a, int b) => ((long)a) * b / Gcd(a, b); } public static class Algorithm { public class EnumAll { bool Over = false; int UpperValue; int Dimension; public int[] Current { private set; get; } public EnumAll(int MaxValue, int Dimension) { this.Dimension = Dimension; UpperValue = MaxValue; Current = new int[Dimension]; Current[0] = -1; } public bool Next() { if (Over) return false; bool flag = false; for (int i = 0; i < Dimension; i++) { if (++Current[i] >= UpperValue) Current[i] = 0; else { flag = true; break; } } if (!flag) { Over = true; return false; } return true; } } public class Graph { public int Count = -1; private HashSet<int>[] Joined; public static Graph GenerateComplete(int size) { throw new NotImplementedException(); } public Graph(int size) { Joined = new HashSet<int>[size]; for (int i = 0; i < Joined.Length; i++) Joined[i] = new HashSet<int>(); Count = size; } public int[] GetConnected(int index) { return Joined[index].ToArray(); } public void AddEdge(int a, int b) { if (a == b) DWrite("ループを追加することはできません A:" + a + " B:" + b); else { if (!Joined[a].Add(b)) DWrite("多重辺を追加することはできません A:" + a + " B:" + b); else Joined[b].Add(a); } } public int[] ToTree(int Root) { var ar = new int[Count]; for (int i = 0; i < ar.Length; i++) ar[i] = -10000000; ar[Root] = -1; Queue<int> bfs = new Queue<int>(); bfs.Enqueue(Root); while (bfs.Count != 0) { var t = bfs.Dequeue(); foreach (var v in Joined[t]) { bfs.Enqueue(v); if (ar[v] == -10000000) ar[v] = t; } } return ar; } } public class UnionFind { private int[] Parent; private int GetRoot(int v) { if (Parent[v] < 0) return v; else return Parent[v] = GetRoot(Parent[v]); } public UnionFind(int size) { Parent = new int[size]; for (int i = 0; i < size; i++) Parent[i] = -1; } public bool AreGrouped(int a, int b) { return GetRoot(a) == GetRoot(b); } public void Connect(int a, int b) { a = GetRoot(a); b = GetRoot(b); if (a == b) return; if (Parent[b] < Parent[a]) { Parent[b] += Parent[a]; Parent[a] = b; } else { Parent[a] += Parent[b]; Parent[b] = a; } } public int GetGroupSize(int a) { return -Parent[GetRoot(a)]; } } } }
[["+", 12, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
2,060
1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137C { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] s = new string[N + 1]; s[N] = "zzzzzzzzzzz"; for (int i = 0; i < N; i++) { s[i] = Console.ReadLine(); string[] sort = new string[10]; for (int j = 0; j < 10; j++) { sort[j] = s[i][j].ToString(); } Array.Sort(sort); s[i] = string.Join("", sort); } Array.Sort(s); // Console.WriteLine(string.Join(" ", s)); long ans = 0; List<int> a = new List<int>(); int same = 1; for (int i = 0; i < N; i++) { if (s[i] == s[i + 1]) { same += 1; } else { a.Add(same); same = 1; } } int l = a.Count; for (int i = 0; i < l; i++) { ans += a[i] * (a[i] - 1) / 2; } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC137C { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] s = new string[N + 1]; s[N] = "zzzzzzzzzzz"; for (int i = 0; i < N; i++) { s[i] = Console.ReadLine(); string[] sort = new string[10]; for (int j = 0; j < 10; j++) { sort[j] = s[i][j].ToString(); } Array.Sort(sort); s[i] = string.Join("", sort); } Array.Sort(s); // Console.WriteLine(string.Join(" ", s)); long ans = 0; List<int> a = new List<int>(); int same = 1; for (int i = 0; i < N; i++) { if (s[i] == s[i + 1]) { same += 1; } else { a.Add(same); same = 1; } } int l = a.Count; for (int i = 0; i < l; i++) { ans += (long)a[i] * (a[i] - 1) / 2; } Console.WriteLine(ans); } } }
[["+", 0, 11, 12, 16, 31, 16, 31, 74, 0, 24], ["+", 0, 11, 12, 16, 31, 16, 31, 74, 39, 199], ["+", 0, 11, 12, 16, 31, 16, 31, 74, 0, 25]]
8
305
3
using System; using System.Collections.Generic; using System.Windows; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Atcoder { public class Program { private static long mod = 1000000007; private static long max = 999999999999; static void Main(string[] args) { var n = Read().Int(); var sl = new Dictionary<string, int>(); for (int i = 0; i < n; i++) { var s = Read(); var a = new string[10]; for (int j = 0; j < 10; j++) { a[j] = s[j].ToString(); } Array.Sort(a, StringComparer.OrdinalIgnoreCase); var val = string.Join(string.Empty, a); if (sl.ContainsKey(val)) { sl[val]++; } else { sl.Add(val, 1); } } long ans = 0; foreach (var item in sl) { var c = item.Value; ans += (c * (c - 1)) / 2; } WriteRead(ans); } class ModCombination { public long _mod = 1000000007; public int _max = 1000000; long[] fac; long[] finv; long[] inv; public ModCombination() : this(1000000007, 1000000) {} public ModCombination(long mod, int max) { _mod = mod; _max = max; fac = new long[max]; finv = new long[max]; inv = new long[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 % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } public long Combination(int n, int m) { if (n < m) return 0; if (n < 0 || m < 0) return 0; return fac[n] * (finv[m] * finv[n - m] % _mod) % _mod; } } public static string Read() { return Console.ReadLine(); } public static string[] ReadSplit() { return Console.ReadLine().Split(' '); } public static void WriteRead(object str) { Console.WriteLine(str); Read(); } public static void Write(object str) { Console.WriteLine(str); } private static bool ChangeMin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b) > 0) { a = b; return true; } return false; } private static bool ChangekMax<T>(ref T a, T b) where T : IComparable { if (b.CompareTo(a) > 0) { a = b; return true; } return false; } /*優先度付きキュー*/ class PriorityQueue<T> where T : IComparable<T> { public readonly T[] Array; public readonly bool Greater; public int Count { get; private set; } = 0; public PriorityQueue(int capacity, bool greater = true) { this.Array = new T[capacity]; this.Greater = greater; } public void Enqueue(T item) { this.Array[this.Count] = item; this.Count += 1; this.UpHeap(); } public T Dequeue() { // 先頭要素を末尾にして再構成 this.Swap(0, this.Count - 1); this.Count -= 1; this.DownHeap(); return this.Array[this.Count]; } private void UpHeap() { var n = this.Count - 1; while (n != 0) { // 親要素の座標 var parent = (n - 1) / 2; if (this.Compare(this.Array[n], this.Array[parent]) > 0) { this.Swap(n, parent); n = parent; } else { break; } } } private void DownHeap() { var parent = 0; while (true) { var child = 2 * parent + 1; if (child > this.Count - 1) break; if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0) { // 左より右の子のほうが大きい値の場合、右を入れ替え対象にする child += 1; } if (this.Compare(this.Array[parent], this.Array[child]) < 0) { this.Swap(parent, child); parent = child; } else { break; } } } // aとbを比較する private int Compare(T a, T b) { var c = a.CompareTo(b); if (!this.Greater) { c = -c; } return c; } // aとbを入れ替える private void Swap(int a, int b) { var tmp = this.Array[a]; this.Array[a] = this.Array[b]; this.Array[b] = tmp; } } private static bool NextPermutation(int[] array) { var size = array.Length; var ok = false; // array[i]<array[i+1]を満たす最大のiを求める int i = size - 2; for (; 0 <= i; i--) { if (array[i] < array[i + 1]) { ok = true; break; } } //全ての要素が降順の場合、NextPermutationは存在しない if (ok == false) return false; // array[i]<array[j]を満たす最大のjを求める int j = size - 1; for (;; j--) { if (array[i] < array[j]) break; } // iとjの要素をswapする var tmp = array[i]; array[i] = array[j]; array[j] = tmp; // i以降の要素を反転させる Array.Reverse(array, i + 1, size - (i + 1)); return true; } //要素数Nを引数として渡し、0~N-1でできた順列をすべて返す static List<int[]> GetAllPermutation(int N) { var res = new List<int[]>(); var array = Enumerable.Range(0, N).ToArray(); do { var copy = new int[N]; array.CopyTo(copy, 0); res.Add(copy); } while (NextPermutation(array)); return res; } //配列arrayを引数として渡し,辞書順がarray以上の順列をすべて返す static List<int[]> GetAllPermutation(int[] array) { var size = array.Length; var res = new List<int[]>(); do { var copy = new int[size]; array.CopyTo(copy, 0); res.Add(copy); } while (NextPermutation(array)); return res; } } } public static class Extentions { public static int Int(this object obj) { return int.Parse(obj.ToString()); } public static long Long(this object obj) { return long.Parse(obj.ToString()); } public static decimal Decimal(this object obj) { return decimal.Parse(obj.ToString()); } public static double Double(this object obj) { return double.Parse(obj.ToString()); } public static T2 GetValueZero<T1, T2>(this Dictionary<T1, T2> d, T1 key) where T2 : struct { T2 value; if (d.TryGetValue(key, out value)) { return value; } return new T2(); } }
using System; using System.Collections.Generic; using System.Windows; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Atcoder { public class Program { private static long mod = 1000000007; private static long max = 999999999999; static void Main(string[] args) { var n = Read().Int(); var sl = new Dictionary<string, long>(); for (int i = 0; i < n; i++) { var s = Read(); var a = new string[10]; for (int j = 0; j < 10; j++) { a[j] = s[j].ToString(); } Array.Sort(a, StringComparer.OrdinalIgnoreCase); var val = string.Join(string.Empty, a); if (sl.ContainsKey(val)) { sl[val]++; } else { sl.Add(val, 1); } } long ans = 0; foreach (var item in sl) { var c = item.Value; ans += (c * (c - 1)) / 2; } WriteRead(ans); } class ModCombination { public long _mod = 1000000007; public int _max = 1000000; long[] fac; long[] finv; long[] inv; public ModCombination() : this(1000000007, 1000000) {} public ModCombination(long mod, int max) { _mod = mod; _max = max; fac = new long[max]; finv = new long[max]; inv = new long[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 % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } public long Combination(int n, int m) { if (n < m) return 0; if (n < 0 || m < 0) return 0; return fac[n] * (finv[m] * finv[n - m] % _mod) % _mod; } } public static string Read() { return Console.ReadLine(); } public static string[] ReadSplit() { return Console.ReadLine().Split(' '); } public static void WriteRead(object str) { Console.WriteLine(str); Read(); } public static void Write(object str) { Console.WriteLine(str); } private static bool ChangeMin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b) > 0) { a = b; return true; } return false; } private static bool ChangekMax<T>(ref T a, T b) where T : IComparable { if (b.CompareTo(a) > 0) { a = b; return true; } return false; } /*優先度付きキュー*/ class PriorityQueue<T> where T : IComparable<T> { public readonly T[] Array; public readonly bool Greater; public int Count { get; private set; } = 0; public PriorityQueue(int capacity, bool greater = true) { this.Array = new T[capacity]; this.Greater = greater; } public void Enqueue(T item) { this.Array[this.Count] = item; this.Count += 1; this.UpHeap(); } public T Dequeue() { // 先頭要素を末尾にして再構成 this.Swap(0, this.Count - 1); this.Count -= 1; this.DownHeap(); return this.Array[this.Count]; } private void UpHeap() { var n = this.Count - 1; while (n != 0) { // 親要素の座標 var parent = (n - 1) / 2; if (this.Compare(this.Array[n], this.Array[parent]) > 0) { this.Swap(n, parent); n = parent; } else { break; } } } private void DownHeap() { var parent = 0; while (true) { var child = 2 * parent + 1; if (child > this.Count - 1) break; if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0) { // 左より右の子のほうが大きい値の場合、右を入れ替え対象にする child += 1; } if (this.Compare(this.Array[parent], this.Array[child]) < 0) { this.Swap(parent, child); parent = child; } else { break; } } } // aとbを比較する private int Compare(T a, T b) { var c = a.CompareTo(b); if (!this.Greater) { c = -c; } return c; } // aとbを入れ替える private void Swap(int a, int b) { var tmp = this.Array[a]; this.Array[a] = this.Array[b]; this.Array[b] = tmp; } } private static bool NextPermutation(int[] array) { var size = array.Length; var ok = false; // array[i]<array[i+1]を満たす最大のiを求める int i = size - 2; for (; 0 <= i; i--) { if (array[i] < array[i + 1]) { ok = true; break; } } //全ての要素が降順の場合、NextPermutationは存在しない if (ok == false) return false; // array[i]<array[j]を満たす最大のjを求める int j = size - 1; for (;; j--) { if (array[i] < array[j]) break; } // iとjの要素をswapする var tmp = array[i]; array[i] = array[j]; array[j] = tmp; // i以降の要素を反転させる Array.Reverse(array, i + 1, size - (i + 1)); return true; } //要素数Nを引数として渡し、0~N-1でできた順列をすべて返す static List<int[]> GetAllPermutation(int N) { var res = new List<int[]>(); var array = Enumerable.Range(0, N).ToArray(); do { var copy = new int[N]; array.CopyTo(copy, 0); res.Add(copy); } while (NextPermutation(array)); return res; } //配列arrayを引数として渡し,辞書順がarray以上の順列をすべて返す static List<int[]> GetAllPermutation(int[] array) { var size = array.Length; var res = new List<int[]>(); do { var copy = new int[size]; array.CopyTo(copy, 0); res.Add(copy); } while (NextPermutation(array)); return res; } } } public static class Extentions { public static int Int(this object obj) { return int.Parse(obj.ToString()); } public static long Long(this object obj) { return long.Parse(obj.ToString()); } public static decimal Decimal(this object obj) { return decimal.Parse(obj.ToString()); } public static double Double(this object obj) { return double.Parse(obj.ToString()); } public static T2 GetValueZero<T1, T2>(this Dictionary<T1, T2> d, T1 key) where T2 : struct { T2 value; if (d.TryGetValue(key, out value)) { return value; } return new T2(); } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199]]
8
1,558
2
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace atcoder { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var strs = new Dictionary<string, int>(); for (var i = 0; i < n; i++) { var str = new string(Console.ReadLine().OrderBy(c => c).ToArray()); if (strs.TryGetValue(str, out _)) strs[str]++; else strs.Add(str, 1); } var result = 0; foreach (var cnt in strs.Values) { result += (cnt * (cnt - 1)) / 2; } Console.WriteLine(result); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; namespace atcoder { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var strs = new Dictionary<string, long>(); for (var i = 0; i < n; i++) { var str = new string(Console.ReadLine().OrderBy(c => c).ToArray()); if (strs.TryGetValue(str, out _)) strs[str]++; else strs.Add(str, 1); } var result = 0L; foreach (var cnt in strs.Values) { result += (cnt * (cnt - 1)) / 2; } Console.WriteLine(result); } } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
171
4
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var D = new Dictionary<string, int>(); for (int i = 0; i < n; i++) { string s = Console.ReadLine(); char[] c = new char[10]; for (int j = 0; j < 10; j++) { c[j] = s[j]; } var cc = c.OrderBy(x => x).ToArray(); string ss = ""; for (int j = 0; j < 10; j++) { ss += cc[j].ToString(); } if (D.ContainsKey(ss)) { D[ss]++; } else { D[ss] = 1; } } long ans = 0; foreach (var d in D) { int x = d.Value; ans += x * (x - 1) / 2; } Console.WriteLine(ans); } } } // TLE // namespace ConsoleApp1 //{ // class Program // { // static void Main(string[] args) // { // int n = int.Parse(Console.ReadLine()); // var S = new Dictionary<char,int>[n]; // var P = new int[n]; // for(int i = 0; i < n; i++) // { // S[i] = new Dictionary<char, int>(); // string s = Console.ReadLine(); // for(int j = 0; j < 10; j++) // { // if (S[i].ContainsKey(s[j])) S[i][s[j]]++; // else S[i][s[j]] = 1; // P[i] += (int)s[j]; // } // } // long cnt = 0; // for(int i=0;i < n-1; i++) // { // for(int j = i + 1; j < n; j++) // { // if (P[i] == P[j]) cnt++; // } // } // Console.WriteLine(cnt); // } // } //} // TLE // namespace ConsoleApp1 //{ // class Program // { // static void Main(string[] args) // { // int n = int.Parse(Console.ReadLine()); // var S = new Dictionary<char, int>[n]; // for (int i = 0; i < n; i++) // { // S[i] = new Dictionary<char, int>(); // for (char c = 'a'; c <= 'z'; c++) // { // S[i][c] = 0; // } // string s = Console.ReadLine(); // for (int j = 0; j < 10; j++) // { // S[i][s[j]]++; // } // } // long cnt = 0; // for (int i = 0; i < n - 1; i++) // { // for (int j = i + 1; j < n; j++) // { // bool flag = true; // for (char c = 'a'; c <= 'z'; c++) // { // if (S[i][c] != S[j][c]) // { // flag = false; // break; // } // } // if (flag) cnt++; // } // } // Console.WriteLine(cnt); // } // } //}
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var D = new Dictionary<string, int>(); for (int i = 0; i < n; i++) { string s = Console.ReadLine(); char[] c = new char[10]; for (int j = 0; j < 10; j++) { c[j] = s[j]; } var cc = c.OrderBy(x => x).ToArray(); string ss = ""; for (int j = 0; j < 10; j++) { ss += cc[j].ToString(); } if (D.ContainsKey(ss)) { D[ss]++; } else { D[ss] = 1; } } long ans = 0; foreach (var d in D) { int x = d.Value; ans += (long)x * (x - 1) / 2; } Console.WriteLine(ans); } } } // TLE // namespace ConsoleApp1 //{ // class Program // { // static void Main(string[] args) // { // int n = int.Parse(Console.ReadLine()); // var S = new Dictionary<char,int>[n]; // var P = new int[n]; // for(int i = 0; i < n; i++) // { // S[i] = new Dictionary<char, int>(); // string s = Console.ReadLine(); // for(int j = 0; j < 10; j++) // { // if (S[i].ContainsKey(s[j])) S[i][s[j]]++; // else S[i][s[j]] = 1; // P[i] += (int)s[j]; // } // } // long cnt = 0; // for(int i=0;i < n-1; i++) // { // for(int j = i + 1; j < n; j++) // { // if (P[i] == P[j]) cnt++; // } // } // Console.WriteLine(cnt); // } // } //} // TLE // namespace ConsoleApp1 //{ // class Program // { // static void Main(string[] args) // { // int n = int.Parse(Console.ReadLine()); // var S = new Dictionary<char, int>[n]; // for (int i = 0; i < n; i++) // { // S[i] = new Dictionary<char, int>(); // for (char c = 'a'; c <= 'z'; c++) // { // S[i][c] = 0; // } // string s = Console.ReadLine(); // for (int j = 0; j < 10; j++) // { // S[i][s[j]]++; // } // } // long cnt = 0; // for (int i = 0; i < n - 1; i++) // { // for (int j = i + 1; j < n; j++) // { // bool flag = true; // for (char c = 'a'; c <= 'z'; c++) // { // if (S[i][c] != S[j][c]) // { // flag = false; // break; // } // } // if (flag) cnt++; // } // } // Console.WriteLine(cnt); // } // } //}
[["+", 0, 11, 12, 16, 31, 16, 31, 74, 0, 24], ["+", 0, 11, 12, 16, 31, 16, 31, 74, 39, 199], ["+", 0, 11, 12, 16, 31, 16, 31, 74, 0, 25]]
8
315
3
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var N = sc.ReadInt(); var S = sc.ReadStrArray(N); var map = new HashMap<string, int>(); var ans = 0; foreach (var s in S) { var t = new string(s.OrderBy(c => c).ToArray()); ans += map[t]; map[t]++; } Console.WriteLine(ans); } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var N = sc.ReadInt(); var S = sc.ReadStrArray(N); var map = new HashMap<string, long>(); var ans = 0L; foreach (var s in S) { var t = new string(s.OrderBy(c => c).ToArray()); ans += map[t]; map[t]++; } Console.WriteLine(ans); } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
1,241
4
using Yorukatsu019.Questions; using Yorukatsu019.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu019.Questions { /// <summary> /// ABC137 C /// </summary> public class QuestionC : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var n = inputStream.ReadInt(); var count = new Dictionary<string, int>(); for (int i = 0; i < n; i++) { var temp = inputStream.ReadLine().ToCharArray(); Array.Sort(temp); var s = string.Concat(temp); if (count.ContainsKey(s)) { count[s]++; } else { count.Add(s, 1); } } // nP2 yield return count.Sum(p => p.Value * (p.Value - 1) / 2); } } } namespace Yorukatsu019 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionC(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu019.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 Yorukatsu019.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 Yorukatsu019.Questions; using Yorukatsu019.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu019.Questions { /// <summary> /// ABC137 C /// </summary> public class QuestionC : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var n = inputStream.ReadInt(); var count = new Dictionary<string, long>(); for (int i = 0; i < n; i++) { var temp = inputStream.ReadLine().ToCharArray(); Array.Sort(temp); var s = string.Concat(temp); if (count.ContainsKey(s)) { count[s]++; } else { count.Add(s, 1); } } // nP2 yield return count.Sum(p => p.Value * (p.Value - 1) / 2); } } } namespace Yorukatsu019 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionC(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu019.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 Yorukatsu019.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, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199]]
8
578
2
using System; using System.Collections.Generic; using System.Linq; class Ho { static void Main() { int n = int.Parse(Console.ReadLine()); var dic = new Dictionary<string, int>(); for (var i = 0; i < n; i++) { var aho = Console.ReadLine(); var c = new char[10]; for (var j = 0; j < 10; j++) { c[j] = aho[j]; }   Array.Sort(c); string s = new String(c); if (dic.ContainsKey(s)) { dic[s] += 1; }  else { dic.Add(s, 1); } } long kotae = 0; foreach (var va in dic.Values) { if (va <= 1) { continue; } else { kotae += va * (va - 1) / 2; } } Console.WriteLine(kotae); } }
using System; using System.Collections.Generic; using System.Linq; class Ho { static void Main() { int n = int.Parse(Console.ReadLine()); var dic = new Dictionary<string, long>(); for (var i = 0; i < n; i++) { var aho = Console.ReadLine(); var c = new char[10]; for (var j = 0; j < 10; j++) { c[j] = aho[j]; }   Array.Sort(c); string s = new String(c); if (dic.ContainsKey(s)) { dic[s] += 1; }  else { dic.Add(s, 1); } } long kotae = 0; foreach (var va in dic.Values) { if (va <= 1) { continue; } else { kotae += va * (va - 1) / 2; } } Console.WriteLine(kotae); } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199]]
8
209
2
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static long[] a; static long N; static long K; static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); string[] A = new string[N]; var map = new Dictionary<string, int>(); for (int i = 0; i < N; i++) { A[i] = Console.ReadLine(); var B = A[i].ToCharArray(); Array.Sort(B); A[i] = new String(B); if (map.ContainsKey(A[i])) { map[A[i]] += 1; } else { map.Add(A[i], 0); } } long sum = 0; foreach (int num in map.Values) { sum += num * (num + 1) / 2; } Console.WriteLine(sum); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static long[] a; static long N; static long K; static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); string[] A = new string[N]; var map = new Dictionary<string, int>(); for (int i = 0; i < N; i++) { A[i] = Console.ReadLine(); var B = A[i].ToCharArray(); Array.Sort(B); A[i] = new String(B); if (map.ContainsKey(A[i])) { map[A[i]] += 1; } else { map.Add(A[i], 0); } } long sum = 0; foreach (long num in map.Values) { sum += num * (num + 1) / 2; } Console.WriteLine(sum); } }
[["-", 8, 201, 0, 195, 8, 196, 0, 210, 39, 199], ["+", 8, 201, 0, 195, 8, 196, 0, 210, 39, 199]]
8
228
2
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using Library; using static Library.Input; namespace AtCoderTemplate { class ProgramC { static void Main() { int n = NextInt; var s = Enumerable.Range(0, n) .Select(_ => string.Join("", Line.OrderBy(x => x))) .ToArray(); var dictionary = new Dictionary<string, int>(); foreach (var item in s) { if (dictionary.ContainsKey(item)) { dictionary[item] = dictionary[item] + 1; } else { dictionary[item] = 1; } } System.Console.WriteLine( dictionary.Values.Select(x => x * (x - 1) / 2).Sum()); } } } namespace Library { 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 IEnumerable<long[]> TakeLine(int N) { return Enumerable.Repeat(0, N).Select( _ => Console.ReadLine().Split(' ').Select(long.Parse).ToArray()); } 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 Library; using static Library.Input; namespace AtCoderTemplate { class ProgramC { static void Main() { int n = NextInt; var s = Enumerable.Range(0, n) .Select(_ => string.Join("", Line.OrderBy(x => x))) .ToArray(); var dictionary = new Dictionary<string, long>(); foreach (var item in s) { if (dictionary.ContainsKey(item)) { dictionary[item] = dictionary[item] + 1; } else { dictionary[item] = 1; } } System.Console.WriteLine( dictionary.Values.Select(x => x * (x - 1) / 2).Sum()); } } } namespace Library { 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 IEnumerable<long[]> TakeLine(int N) { return Enumerable.Repeat(0, N).Select( _ => Console.ReadLine().Split(' ').Select(long.Parse).ToArray()); } public static string NextWord() { while (!enumerator.MoveNext()) { enumerator = StrArr.AsEnumerable().GetEnumerator(); } return enumerator.Current; } } }
[["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199]]
8
391
2
using System; using System.Linq; using System.Collections.Generic; using System.Text; public class AtCoderC { static void Main() { int n = int.Parse(Console.ReadLine()); string[] strs = new string[n]; for (int i = 0; i < n; i++) { string s = Console.ReadLine(); strs[i] = string.Join("", s.OrderBy(c => c)); } Array.Sort(strs, new StringComparator()); long answer = 0; string before = strs[0]; int count = 1; for (int i = 1; i < n; i++) { if (before.Equals(strs[i])) { count++; } else { answer += count * (count - 1) / 2; count = 1; before = strs[i]; } } answer += count * (count - 1) / 2; Console.WriteLine(answer); } // 文字列比較のComparator class StringComparator : IComparer<string> { int IComparer<string>.Compare(string a, string b) { for (int i = 0; i < Math.Min(a.Length, b.Length); i++) { if (a[i] < b[i]) { return -1; } else if (a[i] > b[i]) { return 1; } } if (a.Length < b.Length) { return -1; } else if (a.Length > b.Length) { return 1; } else { return 0; } } } }
using System; using System.Linq; using System.Collections.Generic; using System.Text; public class AtCoderC { static void Main() { int n = int.Parse(Console.ReadLine()); string[] strs = new string[n]; for (int i = 0; i < n; i++) { string s = Console.ReadLine(); strs[i] = string.Join("", s.OrderBy(c => c)); } Array.Sort(strs, new StringComparator()); long answer = 0; string before = strs[0]; long count = 1; for (int i = 1; i < n; i++) { if (before.Equals(strs[i])) { count++; } else { answer += count * (count - 1) / 2; count = 1; before = strs[i]; } } answer += count * (count - 1) / 2; Console.WriteLine(answer); } // 文字列比較のComparator class StringComparator : IComparer<string> { int IComparer<string>.Compare(string a, string b) { for (int i = 0; i < Math.Min(a.Length, b.Length); i++) { if (a[i] < b[i]) { return -1; } else if (a[i] > b[i]) { return 1; } } if (a.Length < b.Length) { return -1; } else if (a.Length > b.Length) { return 1; } else { return 0; } } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
339
2
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using static Ha2ne2Util.Cin; using static Ha2ne2Util.Util; using static System.Console; using static System.Math; using System.Collections; using Pair = System.Tuple<long, long>; using Ha2ne2Util; /// <summary> /// https://atcoder.jp/contests/abc137 /// 2020/02/12 /// </summary> namespace ABC137 { public class Program { public static void Main(string[] args) { // A(); 3m // B(); 10m C(); // D(); // E(); // F(); } public static void F() {} public static void E() {} public static void D() {} public static void C() { long N = rl; string[] s = new string[N]; for (int i = 0; i < N; i++) { s[i] = rs; } HashMap<string, int> hm = new HashMap<string, int>(); foreach (var str in s) { hm[new string(str.OrderBy(c => c).ToArray())]++; } long ans = 0; foreach (var val in hm.Values) { ans += val * (val - 1) / 2; } Console.WriteLine(ans); } public static void B() { long K = rl; long X = rl; StringBuilder sb = new StringBuilder(); for (int i = (int)(X - K + 1); i <= X + K - 1; i++) { sb.Append(i + " "); } Console.WriteLine(sb.ToString()); } public static void A() { long A = rl; long B = rl; long plus = A + B; long minus = A - B; long mul = A * B; Console.WriteLine(Max(Max(plus, minus), mul)); } } } namespace Ha2ne2Util { public static class Cin { #region public public static long rl => ReadLong(); public static long[] rla => ReadLongArray(); public static double rd => ReadDouble(); public static double[] rda => ReadDoubleArray(); public static string rs => ReadString(); public static int ReadInt() { return int.Parse(Next()); } public static long ReadLong() { return long.Parse(Next()); } public static double ReadDouble() { return double.Parse(Next()); } public static string ReadString() { return Next(); } public static int[] ReadIntArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } public static long[] ReadLongArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); } public static double[] ReadDoubleArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); } public static void ReadCols(out long[] a, out long[] b, long N) { a = new long[N]; b = new long[N]; for (int i = 0; i < N; i++) { a[i] = ReadLong(); b[i] = ReadLong(); } } public static void ReadCols(out long[] a, out long[] b, out long[] c, long N) { a = new long[N]; b = new long[N]; c = new long[N]; for (int i = 0; i < N; i++) { a[i] = ReadLong(); b[i] = ReadLong(); c[i] = ReadLong(); } } public static int[][] ReadIntTable(int n) { Tokens = null; int[][] ret = new int [n][]; for (int i = 0; i < n; i++) { ret[i] = ReadIntArray(); } return ret; } public static long[][] ReadLongTable(long n) { Tokens = null; long[][] ret = new long [n][]; for (long i = 0; i < n; i++) { ret[i] = ReadLongArray(); } return ret; } #endregion #region private private static string[] Tokens { get; set; } private static int Pointer { get; set; } public static string Next() { if (Tokens == null || Tokens.Length <= Pointer) { Tokens = Console.ReadLine().Split(' '); Pointer = 0; } return Tokens[Pointer++]; } #endregion } /// <summary> /// ユーティリティー /// </summary> public static class Util { // 10^17 public static readonly long INF = (long)1e17; // 10^9 + 7 public readonly static long MOD = (long)1e9 + 7; /// <summary> /// Console.WriteLineの自動フラッシュをしないようにする /// </summary> public static void DontAutoFlush() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } public static void Flush() { Console.Out.Flush(); } public static IEnumerable<int> Range(int end) { for (int i = 0; i < end; i++) { yield return i; } } public static IEnumerable<int> Range(int from, int end) { for (int i = from; i < end; i++) { yield return i; } } public static IEnumerable<int> Range(int from, int end, int step) { for (int i = from; i < end; i += step) { yield return i; } } /// <summary> /// ソートをして結果を返します。 /// 破壊的関数です。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] Sort<T>(T[] array) { Array.Sort<T>(array); return array; } /// <summary> /// 降順ソートをして結果を返します。 /// 破壊的関数です。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] SortDecend<T>(T[] array) { Array.Sort<T>(array); Array.Reverse(array); return array; } /// <summary> /// 引数aと引数bの値を入れ替えます /// </summary> /// <typeparam name="T"></typeparam> /// <param name="a"></param> /// <param name="b"></param> public static void Swap<T>(ref T a, ref T b) { T _a = a; a = b; b = _a; } /// <summary> /// 先頭と末尾に要素を増やした新しい配列を返します。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] AddHeadAndTail<T>(T[] array) { int len = array.Length; T[] res = new T[len + 2]; Array.Copy(array, 0, res, 1, len); return res; } /// <summary> /// 昇順ソート済みの配列を2分探索します。 /// 要素が見つからなかった場合はnより大きい数値の中で最小の数値のインデックスを返す。 /// </summary> /// <param name="array"></param> /// <param name="n"></param> /// <returns></returns> public static int BinarySearch(long[] array, long n) { int left = 0; int right = array.Length - 1; while (left <= right) { int mid = (right - left) / 2 + left; if (array[mid] == n) { return mid; } else if (n < array[mid]) { right = mid - 1; } else { left = mid + 1; } } return left; } public static int GCD(int a, int b) { return (b == 0) ? a : GCD(b, a % b); } /// <summary> /// nの約数の配列を返します。 /// 配列は昇順ソートされています。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static int[] GetDivisor(int n) { double sqrt = Math.Sqrt(n); List<int> divisor = new List<int>(); for (int i = 1; i <= sqrt; i++) { if (n % i == 0) { divisor.Add(i); int d = n / i; if (d != i) { divisor.Add(d); } } } return Sort(divisor.ToArray()); } public static void Deconstruct<T>(this T[] items, out T t0) { t0 = items.Length > 0 ? items[0] : default(T); } public static void Deconstruct<T>(this T[] items, out T t0, out T t1) { if (items.Length < 2) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; } public static void Deconstruct<T>(this T[] items, out T t0, out T t1, out T t2) { if (items.Length < 3) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; t2 = items[2]; } public static void Deconstruct<T>(this T[] items, out T t0, out T t1, out T t2, out T t3) { if (items.Length < 4) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; t2 = items[2]; t3 = items[3]; } public static void ChMax(ref long a, long b) { if (a < b) a = b; } public static void ChMin(ref long a, long b) { if (a > b) a = b; } public static long ModAdd(long a, long b) { long res = a + b; if (res >= MOD) return res % MOD; return res; } public static long ModMul(long a, long b) { long res = a * b; if (res >= MOD) return res % MOD; return res; } public static void ChModAdd(ref long a, long b) { a += b; if (a >= MOD) a %= MOD; } public static void ChModMul(ref long a, long b) { a *= b; if (a >= MOD) a %= MOD; } public static T MinBy<T>(this IEnumerable<T> source, Func<T, long> conv) { T min = source.First(); long minValue = long.MaxValue; foreach (T x in source) { long n = conv(x); if (n < minValue) { min = x; } } return min; } public static T MaxBy<T>(this IEnumerable<T> source, Func<T, long> conv) { T min = source.First(); long maxValue = long.MinValue; foreach (T x in source) { long n = conv(x); if (maxValue < n) { min = x; } } return min; } public static void FillArray<T>(T[] array, T value) { int max = array.Length; for (int i = 0; i < max; i++) { array[i] = value; } } public static void FillArray<T>(T[,] array, T value) { int max0 = array.GetLength(0); int max1 = array.GetLength(1); for (int i = 0; i < max0; i++) { for (int j = 0; j < max1; j++) { array[i, j] = value; } } } public static void FillArray<T>(T[,,] array, T value) { int max0 = array.GetLength(0); int max1 = array.GetLength(1); int max2 = array.GetLength(2); for (int i = 0; i < max0; i++) { for (int j = 0; j < max1; j++) { for (int k = 0; k < max2; k++) { array[i, j, k] = value; } } } } /// <summary> /// 累積和を求めて返します。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static long[] Accumulate(long[] array) { long[] acc = new long[array.Length + 1]; for (int i = 0; i < array.Length; i++) { acc[i + 1] = acc[i] + array[i]; } return acc; } public static double[] Accumulate(double[] array) { double[] acc = new double[array.Length + 1]; for (int i = 0; i < array.Length; i++) { acc[i + 1] = acc[i] + array[i]; } return acc; } /// <summary> /// めぐる式二分探索 /// okにはpredを満たすindexを、ngにはpredを満たさないindexを指定します。 /// 引数がng<okの場合、最小のokとなるindexをokに代入します。(lower_bound) /// 引数がok<ngの場合、最大のokとなるindexをokに代入します。(upper_bound) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="okIndex"></param> /// <param name="ngIndex"></param> /// <param name="pred"></param> public static void MeguruBinarySearch<T>(T[] array, ref long okIndex, ref long ngIndex, Predicate<T> pred) { while (Math.Abs(okIndex - ngIndex) > 1) { long mid = (okIndex + ngIndex) / 2; if (pred(array[mid])) { okIndex = mid; } else { ngIndex = mid; } } } } public class HashMap<K, V> : Dictionary<K, V> { private V DefaltValue; public HashMap() {} public HashMap(V defaultValue) { DefaltValue = defaultValue; } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = DefaltValue; } set { base[i] = value; } } } /// <summary> /// UnionFindTree /// </summary> public class UnionFindTree { public int[] Node; public UnionFindTree(int N) { Node = new int[N]; for (int i = 0; i < N; i++) { Node[i] = -1; } } public bool IsSameGroup(int x, int y) { return GetRoot(x) == GetRoot(y); } public bool Merge(int x, int y) { int xr = GetRoot(x); int yr = GetRoot(y); if (xr == yr) return false; // xが、大きなグループを示すようにSwapする(値が小さい方が大きなグループ) if (Node[xr] > Node[yr]) Swap(ref xr, ref yr); // グループの数を合算する Node[xr] += Node[yr]; // 根を張り替える Node[yr] = xr; return true; } public int Size(int x) { return -Node[GetRoot(x)]; } public int GetRoot(int n) { if (Node[n] < 0) { return n; } else { // 根を張りなおす。 Node[n] = GetRoot(Node[n]); return Node[n]; } } } /// <summary> /// PriorityQueue /// </summary> /// <typeparam name="T"></typeparam> public class PriorityQueue<T> { private Comparison<T> _comparison = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority queue /// </summary> /// <param name="maxSize">max size</param> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int maxSize, int type = 0) { _heap = new T[maxSize]; _type = type; } public PriorityQueue(int maxSize, Comparison<T> comparison) { _heap = new T[maxSize]; _comparison = comparison; } private int Compare(T x, T y) { return _comparison(x, y); // if (_comparison != null) return _comparison(x, y); // return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using static Ha2ne2Util.Cin; using static Ha2ne2Util.Util; using static System.Console; using static System.Math; using System.Collections; using Pair = System.Tuple<long, long>; using Ha2ne2Util; /// <summary> /// https://atcoder.jp/contests/abc137 /// 2020/02/12 /// </summary> namespace ABC137 { public class Program { public static void Main(string[] args) { // A(); 3m // B(); 10m C(); // D(); // E(); // F(); } public static void F() {} public static void E() {} public static void D() {} public static void C() { long N = rl; string[] s = new string[N]; for (int i = 0; i < N; i++) { s[i] = rs; } HashMap<string, long> hm = new HashMap<string, long>(); foreach (var str in s) { hm[new string(str.OrderBy(c => c).ToArray())]++; } long ans = 0; foreach (var val in hm.Values) { ans += val * (val - 1) / 2; } Console.WriteLine(ans); } public static void B() { long K = rl; long X = rl; StringBuilder sb = new StringBuilder(); for (int i = (int)(X - K + 1); i <= X + K - 1; i++) { sb.Append(i + " "); } Console.WriteLine(sb.ToString()); } public static void A() { long A = rl; long B = rl; long plus = A + B; long minus = A - B; long mul = A * B; Console.WriteLine(Max(Max(plus, minus), mul)); } } } namespace Ha2ne2Util { public static class Cin { #region public public static long rl => ReadLong(); public static long[] rla => ReadLongArray(); public static double rd => ReadDouble(); public static double[] rda => ReadDoubleArray(); public static string rs => ReadString(); public static int ReadInt() { return int.Parse(Next()); } public static long ReadLong() { return long.Parse(Next()); } public static double ReadDouble() { return double.Parse(Next()); } public static string ReadString() { return Next(); } public static int[] ReadIntArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } public static long[] ReadLongArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse); } public static double[] ReadDoubleArray() { Tokens = null; return Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); } public static void ReadCols(out long[] a, out long[] b, long N) { a = new long[N]; b = new long[N]; for (int i = 0; i < N; i++) { a[i] = ReadLong(); b[i] = ReadLong(); } } public static void ReadCols(out long[] a, out long[] b, out long[] c, long N) { a = new long[N]; b = new long[N]; c = new long[N]; for (int i = 0; i < N; i++) { a[i] = ReadLong(); b[i] = ReadLong(); c[i] = ReadLong(); } } public static int[][] ReadIntTable(int n) { Tokens = null; int[][] ret = new int [n][]; for (int i = 0; i < n; i++) { ret[i] = ReadIntArray(); } return ret; } public static long[][] ReadLongTable(long n) { Tokens = null; long[][] ret = new long [n][]; for (long i = 0; i < n; i++) { ret[i] = ReadLongArray(); } return ret; } #endregion #region private private static string[] Tokens { get; set; } private static int Pointer { get; set; } public static string Next() { if (Tokens == null || Tokens.Length <= Pointer) { Tokens = Console.ReadLine().Split(' '); Pointer = 0; } return Tokens[Pointer++]; } #endregion } /// <summary> /// ユーティリティー /// </summary> public static class Util { // 10^17 public static readonly long INF = (long)1e17; // 10^9 + 7 public readonly static long MOD = (long)1e9 + 7; /// <summary> /// Console.WriteLineの自動フラッシュをしないようにする /// </summary> public static void DontAutoFlush() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } public static void Flush() { Console.Out.Flush(); } public static IEnumerable<int> Range(int end) { for (int i = 0; i < end; i++) { yield return i; } } public static IEnumerable<int> Range(int from, int end) { for (int i = from; i < end; i++) { yield return i; } } public static IEnumerable<int> Range(int from, int end, int step) { for (int i = from; i < end; i += step) { yield return i; } } /// <summary> /// ソートをして結果を返します。 /// 破壊的関数です。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] Sort<T>(T[] array) { Array.Sort<T>(array); return array; } /// <summary> /// 降順ソートをして結果を返します。 /// 破壊的関数です。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] SortDecend<T>(T[] array) { Array.Sort<T>(array); Array.Reverse(array); return array; } /// <summary> /// 引数aと引数bの値を入れ替えます /// </summary> /// <typeparam name="T"></typeparam> /// <param name="a"></param> /// <param name="b"></param> public static void Swap<T>(ref T a, ref T b) { T _a = a; a = b; b = _a; } /// <summary> /// 先頭と末尾に要素を増やした新しい配列を返します。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static T[] AddHeadAndTail<T>(T[] array) { int len = array.Length; T[] res = new T[len + 2]; Array.Copy(array, 0, res, 1, len); return res; } /// <summary> /// 昇順ソート済みの配列を2分探索します。 /// 要素が見つからなかった場合はnより大きい数値の中で最小の数値のインデックスを返す。 /// </summary> /// <param name="array"></param> /// <param name="n"></param> /// <returns></returns> public static int BinarySearch(long[] array, long n) { int left = 0; int right = array.Length - 1; while (left <= right) { int mid = (right - left) / 2 + left; if (array[mid] == n) { return mid; } else if (n < array[mid]) { right = mid - 1; } else { left = mid + 1; } } return left; } public static int GCD(int a, int b) { return (b == 0) ? a : GCD(b, a % b); } /// <summary> /// nの約数の配列を返します。 /// 配列は昇順ソートされています。 /// </summary> /// <param name="n"></param> /// <returns></returns> public static int[] GetDivisor(int n) { double sqrt = Math.Sqrt(n); List<int> divisor = new List<int>(); for (int i = 1; i <= sqrt; i++) { if (n % i == 0) { divisor.Add(i); int d = n / i; if (d != i) { divisor.Add(d); } } } return Sort(divisor.ToArray()); } public static void Deconstruct<T>(this T[] items, out T t0) { t0 = items.Length > 0 ? items[0] : default(T); } public static void Deconstruct<T>(this T[] items, out T t0, out T t1) { if (items.Length < 2) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; } public static void Deconstruct<T>(this T[] items, out T t0, out T t1, out T t2) { if (items.Length < 3) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; t2 = items[2]; } public static void Deconstruct<T>(this T[] items, out T t0, out T t1, out T t2, out T t3) { if (items.Length < 4) throw new ArgumentException(); t0 = items[0]; t1 = items[1]; t2 = items[2]; t3 = items[3]; } public static void ChMax(ref long a, long b) { if (a < b) a = b; } public static void ChMin(ref long a, long b) { if (a > b) a = b; } public static long ModAdd(long a, long b) { long res = a + b; if (res >= MOD) return res % MOD; return res; } public static long ModMul(long a, long b) { long res = a * b; if (res >= MOD) return res % MOD; return res; } public static void ChModAdd(ref long a, long b) { a += b; if (a >= MOD) a %= MOD; } public static void ChModMul(ref long a, long b) { a *= b; if (a >= MOD) a %= MOD; } public static T MinBy<T>(this IEnumerable<T> source, Func<T, long> conv) { T min = source.First(); long minValue = long.MaxValue; foreach (T x in source) { long n = conv(x); if (n < minValue) { min = x; } } return min; } public static T MaxBy<T>(this IEnumerable<T> source, Func<T, long> conv) { T min = source.First(); long maxValue = long.MinValue; foreach (T x in source) { long n = conv(x); if (maxValue < n) { min = x; } } return min; } public static void FillArray<T>(T[] array, T value) { int max = array.Length; for (int i = 0; i < max; i++) { array[i] = value; } } public static void FillArray<T>(T[,] array, T value) { int max0 = array.GetLength(0); int max1 = array.GetLength(1); for (int i = 0; i < max0; i++) { for (int j = 0; j < max1; j++) { array[i, j] = value; } } } public static void FillArray<T>(T[,,] array, T value) { int max0 = array.GetLength(0); int max1 = array.GetLength(1); int max2 = array.GetLength(2); for (int i = 0; i < max0; i++) { for (int j = 0; j < max1; j++) { for (int k = 0; k < max2; k++) { array[i, j, k] = value; } } } } /// <summary> /// 累積和を求めて返します。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <returns></returns> public static long[] Accumulate(long[] array) { long[] acc = new long[array.Length + 1]; for (int i = 0; i < array.Length; i++) { acc[i + 1] = acc[i] + array[i]; } return acc; } public static double[] Accumulate(double[] array) { double[] acc = new double[array.Length + 1]; for (int i = 0; i < array.Length; i++) { acc[i + 1] = acc[i] + array[i]; } return acc; } /// <summary> /// めぐる式二分探索 /// okにはpredを満たすindexを、ngにはpredを満たさないindexを指定します。 /// 引数がng<okの場合、最小のokとなるindexをokに代入します。(lower_bound) /// 引数がok<ngの場合、最大のokとなるindexをokに代入します。(upper_bound) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="okIndex"></param> /// <param name="ngIndex"></param> /// <param name="pred"></param> public static void MeguruBinarySearch<T>(T[] array, ref long okIndex, ref long ngIndex, Predicate<T> pred) { while (Math.Abs(okIndex - ngIndex) > 1) { long mid = (okIndex + ngIndex) / 2; if (pred(array[mid])) { okIndex = mid; } else { ngIndex = mid; } } } } public class HashMap<K, V> : Dictionary<K, V> { private V DefaltValue; public HashMap() {} public HashMap(V defaultValue) { DefaltValue = defaultValue; } new public V this[K i] { get { V v; return TryGetValue(i, out v) ? v : base[i] = DefaltValue; } set { base[i] = value; } } } /// <summary> /// UnionFindTree /// </summary> public class UnionFindTree { public int[] Node; public UnionFindTree(int N) { Node = new int[N]; for (int i = 0; i < N; i++) { Node[i] = -1; } } public bool IsSameGroup(int x, int y) { return GetRoot(x) == GetRoot(y); } public bool Merge(int x, int y) { int xr = GetRoot(x); int yr = GetRoot(y); if (xr == yr) return false; // xが、大きなグループを示すようにSwapする(値が小さい方が大きなグループ) if (Node[xr] > Node[yr]) Swap(ref xr, ref yr); // グループの数を合算する Node[xr] += Node[yr]; // 根を張り替える Node[yr] = xr; return true; } public int Size(int x) { return -Node[GetRoot(x)]; } public int GetRoot(int n) { if (Node[n] < 0) { return n; } else { // 根を張りなおす。 Node[n] = GetRoot(Node[n]); return Node[n]; } } } /// <summary> /// PriorityQueue /// </summary> /// <typeparam name="T"></typeparam> public class PriorityQueue<T> { private Comparison<T> _comparison = null; private int _type = 0; private T[] _heap; private int _sz = 0; private int _count = 0; /// <summary> /// Priority queue /// </summary> /// <param name="maxSize">max size</param> /// <param name="type">0: asc, 1:desc</param> public PriorityQueue(int maxSize, int type = 0) { _heap = new T[maxSize]; _type = type; } public PriorityQueue(int maxSize, Comparison<T> comparison) { _heap = new T[maxSize]; _comparison = comparison; } private int Compare(T x, T y) { return _comparison(x, y); // if (_comparison != null) return _comparison(x, y); // return _type == 0 ? x.CompareTo(y) : y.CompareTo(x); } public void Push(T x) { _count++; // node number var i = _sz++; while (i > 0) { // parent node number var p = (i - 1) / 2; if (Compare(_heap[p], x) <= 0) break; _heap[i] = _heap[p]; i = p; } _heap[i] = x; } public T Pop() { _count--; T ret = _heap[0]; T x = _heap[--_sz]; int i = 0; while (i * 2 + 1 < _sz) { // children int a = i * 2 + 1; int b = i * 2 + 2; if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b; if (Compare(_heap[a], x) >= 0) break; _heap[i] = _heap[a]; i = a; } _heap[i] = x; return ret; } public int Count() { return _count; } public T Peek() { return _heap[0]; } public bool Contains(T x) { for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true; return false; } public void Clear() { while (this.Count() > 0) this.Pop(); } public IEnumerator<T> GetEnumerator() { var ret = new List<T>(); while (this.Count() > 0) { ret.Add(this.Pop()); } foreach (var r in ret) { this.Push(r); yield return r; } } public T[] ToArray() { T[] array = new T[_sz]; int i = 0; foreach (var r in this) { array[i++] = r; } return array; } } }
[["-", 0, 197, 0, 198, 39, 236, 237, 238, 0, 199], ["+", 0, 197, 0, 198, 39, 236, 237, 238, 0, 199], ["-", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199], ["+", 0, 212, 0, 230, 39, 236, 237, 238, 0, 199]]
8
3,457
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp9 { class Program { static void MainA(string[] args) { Console.WriteLine( int.Parse(Console.ReadLine()) < 3200 ? "red" : Console.ReadLine()); } static void MainB(string[] args) { Console.ReadLine(); Console.WriteLine(1.0d / Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .Select(x => 1.0d / x) .Sum()); } static void Main(string[] args) { int[] arr = Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .OrderBy(x => x) .ToArray(); double sum = arr[0]; var arr2 = arr.Skip(1); foreach (var n in arr2) { sum = (sum + n) / 2; } Console.WriteLine(sum); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp9 { class Program { static void MainA(string[] args) { Console.WriteLine( int.Parse(Console.ReadLine()) < 3200 ? "red" : Console.ReadLine()); } static void MainB(string[] args) { Console.ReadLine(); Console.WriteLine(1.0d / Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .Select(x => 1.0d / x) .Sum()); } static void Main(string[] args) { Console.ReadLine(); int[] arr = Console.ReadLine() .Split(' ') .Select(x => int.Parse(x)) .OrderBy(x => x) .ToArray(); double sum = arr[0]; var arr2 = arr.Skip(1); foreach (var n in arr2) { sum = (sum + n) / 2; } Console.WriteLine(sum); } } }
[["+", 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], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
232
6
using System; using System.Collections.Generic; using System.Linq; namespace atcoder_csharp { class Program { static void Main() { var n = Util.ReadInt(); var values = Util.ReadInts().OrderBy(v => v).ToList(); if (n == 1) { Console.WriteLine(values[0]); return; } var avg = (double)values[0]; for (var i = 1; i < n; i++) { avg = (double)(values[i] + avg) / 2; } Console.WriteLine(avg); } } class Util { private const bool debug = true; public static string ReadStr() { return debug ? ReadDebugLine() : Console.ReadLine(); } public static List<string> ReadStrs() { return ReadStr().Split(' ').ToList(); } public static int ReadInt() { return int.Parse(ReadStr()); } public static List<int> ReadInts() { return ReadStrs().Select(s => int.Parse(s)).ToList(); } private static List<string> _lines; private static int _lineIndex = 0; private static string ReadDebugLine() { if (_lines == null) { _lines = System.IO.File.ReadAllLines("debug.txt").ToList(); } return _lines[_lineIndex++]; } } }
using System; using System.Collections.Generic; using System.Linq; namespace atcoder_csharp { class Program { static void Main() { var n = Util.ReadInt(); var values = Util.ReadInts().OrderBy(v => v).ToList(); if (n == 1) { Console.WriteLine(values[0]); return; } var avg = (double)values[0]; for (var i = 1; i < n; i++) { avg = (double)(values[i] + avg) / 2; } Console.WriteLine(avg); } } class Util { private const bool debug = false; public static string ReadStr() { return debug ? ReadDebugLine() : Console.ReadLine(); } public static List<string> ReadStrs() { return ReadStr().Split(' ').ToList(); } public static int ReadInt() { return int.Parse(ReadStr()); } public static List<int> ReadInts() { return ReadStrs().Select(s => int.Parse(s)).ToList(); } private static List<string> _lines; private static int _lineIndex = 0; private static string ReadDebugLine() { if (_lines == null) { _lines = System.IO.File.ReadAllLines("debug.txt").ToList(); } return _lines[_lineIndex++]; } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 211, 0, 146], ["+", 0, 198, 0, 200, 0, 212, 0, 211, 0, 147]]
8
295
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoderPre { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var v = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Array.Sort(v); for (int i = 1; i < N; i++) { v[i] = (v[i] + v[i - 1]) / 2; } Console.WriteLine(v[N - 1]); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoderPre { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); var v = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); Array.Sort(v); for (int i = 1; i < N; i++) { v[i] = (v[i] + v[i - 1]) / 2; } Console.WriteLine(v[N - 1]); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 205, 199]]
8
141
2
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { double N = int.Parse(Console.ReadLine()); string[] vals = Console.ReadLine().Split(); List<int> nums = new List<int>(); for (int i = 0; i < N; i++) { nums.Add(int.Parse(vals[i])); } nums.Sort(); double ans = (nums[0] + nums[1]) / 2; nums.RemoveRange(0, 2); var len = nums.Count(); // nums.RemoveRange(); for (int i = 0; i < len; i++) { ans = (ans + nums[i]) / 2; } // double ans = (nums[0] + nums[1]) / Math.Pow(2.0 , N-1); // for (int i = 2; i < N; i++) //{ // ans += nums[i] / Math.Pow(2.0, N - i); //} Console.WriteLine(ans); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { double N = int.Parse(Console.ReadLine()); string[] vals = Console.ReadLine().Split(); List<int> nums = new List<int>(); for (int i = 0; i < N; i++) { nums.Add(int.Parse(vals[i])); } nums.Sort(); double ans = (nums[0] + nums[1]) / 2.0; nums.RemoveRange(0, 2); var len = nums.Count(); // nums.RemoveRange(); for (int i = 0; i < len; i++) { ans = (ans + nums[i]) / 2; } // double ans = (nums[0] + nums[1]) / Math.Pow(2.0 , N-1); // for (int i = 2; i < N; i++) //{ // ans += nums[i] / Math.Pow(2.0, N - i); //} Console.WriteLine(ans); } }
[["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 250]]
8
187
2
using System; class bable { public static void Main() { int kosuu = int.Parse(Console.ReadLine()); double[] value = new double[kosuu]; string[] s = Console.ReadLine().Split(' '); for (int i = 0; i < kosuu; i++) { value[i] = double.Parse(s[i]); } double stb = 0; for (int i = 0; i < kosuu - 1; i++) { for (int j = i; j < kosuu - 1; j++) { if (value[j] > value[j + 1]) { stb = value[j]; value[j] = value[j + 1]; value[j + 1] = stb; } } } double sum = (value[0] + value[1]) / 2; for (int p = 2; p < kosuu; p++) { sum = (sum + value[p]) / 2; } Console.WriteLine("{0}", sum); } }
using System; class bable { public static void Main() { int kosuu = int.Parse(Console.ReadLine()); double[] value = new double[kosuu]; string[] s = Console.ReadLine().Split(' '); for (int i = 0; i < kosuu; i++) { value[i] = double.Parse(s[i]); } double stb = 0; for (int i = 0; i < kosuu - 1; i++) { for (int j = 0; j < kosuu - 1 - i; j++) { if (value[j] > value[j + 1]) { stb = value[j]; value[j] = value[j + 1]; value[j + 1] = stb; } } } double sum = (value[0] + value[1]) / 2; for (int p = 2; p < kosuu; p++) { sum = (sum + value[p]) / 2; } Console.WriteLine("{0}", sum); } }
[["-", 0, 7, 10, 198, 0, 200, 0, 212, 0, 22], ["+", 0, 7, 10, 198, 0, 200, 0, 212, 0, 203], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 22]]
8
231
4
using System; using System.Linq; using System.Collections.Generic; namespace C { class Program { static void Main(string[] args) { int c = int.Parse(Console.ReadLine()); string[] array = Console.ReadLine().Split(' '); var input = new List<string>(); input.AddRange(array); var values = input.Select(i => double.Parse(i)).ToList(); while (values.Count != 1) { values.Sort(); var nval = values.Take(2).Sum() / 2.0; var tmp = values.Skip(2).ToList(); tmp.Add(nval); values = tmp; } Console.WriteLine(values); } } }
using System; using System.Linq; using System.Collections.Generic; namespace C { class Program { static void Main(string[] args) { int c = int.Parse(Console.ReadLine()); string[] array = Console.ReadLine().Split(' '); var input = new List<string>(); input.AddRange(array); var values = input.Select(i => double.Parse(i)).ToList(); while (values.Count != 1) { values.Sort(); var nval = values.Take(2).Sum() / 2.0; var tmp = values.Skip(2).ToList(); tmp.Add(nval); values = tmp; } Console.WriteLine(values[0]); } } }
[["+", 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]]
8
169
3
using System; using System.Linq; using System.Text; using System.Collections.Generic; public class Alchemist { public static void Main() { int N = int.Parse(Console.ReadLine()); var foods = Console.ReadLine().Split(' ').Select(int.Parse).ToList(); var min = new int[2]; for (int i = 1; i < N; i++) { foods = foods.OrderBy(x => x).ToList(); min[0] = foods[0]; foods.RemoveAt(0); min[1] = foods[0]; foods.RemoveAt(0); foods.Add((int)min.Average()); } Console.WriteLine(foods[0]); } }
using System; using System.Linq; using System.Text; using System.Collections.Generic; public class Alchemist { public static void Main() { int N = int.Parse(Console.ReadLine()); var foods = Console.ReadLine().Split(' ').Select(double.Parse).ToList(); var min = new double[2]; for (int i = 1; i < N; i++) { foods = foods.OrderBy(x => x).ToList(); min[0] = foods[0]; foods.RemoveAt(0); min[1] = foods[0]; foods.RemoveAt(0); foods.Add(min.Average()); } Console.WriteLine(foods[0]); } }
[["-", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["+", 205, 213, 3, 4, 0, 28, 0, 214, 205, 199], ["-", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["+", 0, 200, 0, 212, 0, 227, 39, 224, 39, 199], ["-", 0, 213, 3, 4, 0, 28, 0, 74, 0, 24], ["-", 0, 213, 3, 4, 0, 28, 0, 74, 39, 199], ["-", 0, 213, 3, 4, 0, 28, 0, 74, 0, 25]]
8
172
7
using System; using System.Linq; public class Test { public static void Main() { // your code goes here int n = int.Parse(Console.ReadLine()); var v = Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); Array.Sort(v); double ans = (v[0] + v[1]) / 2; for (int i = 2; i > n; i++) { ans = (v[i] + ans) / 2; } Console.WriteLine(ans); } }
using System; using System.Linq; public class Test { public static void Main() { // your code goes here int n = int.Parse(Console.ReadLine()); var v = Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); Array.Sort(v); double ans = (v[0] + v[1]) / 2; for (int i = 2; i != n; i++) { ans = (v[i] + ans) / 2; } Console.WriteLine(ans); } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 47], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 79]]
8
125
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { //入力 int N = int.Parse(Console.ReadLine()); var V = Console.ReadLine() .Split(' ') .Select(x => double.Parse(x)) .OrderBy(x => x) .ToList(); //検証 for (int i = 0; i <= N - 2; i++) { //新たに、平均化した値をリストに加える V.Add(((V[0] + V[1]) / 2d)); //頭の二つを削除する V.RemoveAt(0); V.RemoveAt(0); //昇順に並べる V.OrderBy(x => x); } //出力 Console.WriteLine(V[0]); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { //入力 int N = int.Parse(Console.ReadLine()); var V = Console.ReadLine() .Split(' ') .Select(x => double.Parse(x)) .OrderBy(x => x) .ToList(); //検証 for (int i = 0; i <= N - 2; i++) { //新たに、平均化した値をリストに加える V.Add(((V[0] + V[1]) / 2d)); //頭の二つを削除する V.RemoveAt(0); V.RemoveAt(0); //昇順に並べる V.Sort(); } //出力 Console.WriteLine(V[0]); } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 0, 28, 0, 218, 54, 219, 0, 220, 141, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 218, 0, 221], ["-", 0, 213, 3, 4, 0, 28, 0, 218, 8, 22]]
8
173
5
using System; class ProgramC { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); string[] input_b = Console.ReadLine().Split(' '); long[] b = new long[n]; for (int i = 0; i < n; i++) b[i] = long.Parse(input_b[i]); Array.Sort(b); for (int i = 1; i < n; i++) { long temp = (b[i - 1] + b[i]) / 2; b[i] = temp; } long ans = b[n - 1]; Console.WriteLine(ans); } }
using System; class ProgramC { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); string[] input_b = Console.ReadLine().Split(' '); double[] b = new double[n]; for (int i = 0; i < n; i++) b[i] = double.Parse(input_b[i]); Array.Sort(b); for (int i = 1; i < n; i++) { double temp = (b[i - 1] + b[i]) / 2.0; b[i] = temp; } double ans = b[n - 1]; Console.WriteLine(ans); } }
[["-", 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, 1, 0, 11, 12, 213, 63, 214, 205, 199], ["+", 8, 1, 0, 11, 12, 213, 63, 214, 205, 199], ["-", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 7, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 250], ["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
161
12
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Numerics; namespace PAST0 { using static Ex; public class Solve { public static void Main(string[] args) { new PAST_K(); } } public class PAST_K { char[] s, t; public PAST_K() { s = Read.ToCharArray(); t = Read.ToCharArray(); int len = 'z' - 'a' + 1; var list = new List<long>[len]; while (len > 0) { len--; list[len] = new List<long>(); } for (long i = 0; i < s.Length; i++) { var c = s[i]; list[c - 'a'].Add(i); } long loop = 0; long j = 0; for (long i = 0; i < t.Length; i++) { var c = list[t[i] - 'a']; if (!c.Any()) { (-1).Cw(); return; } var index = UpperBound(c, j); if (index < 0) index = ~index; if (index < c.Count) j = c[index]; else { loop++; j = c[0]; } } long min = s.Length * loop + j + 1; Math.Max(loop + 1, min).Cw(); } } public class PAST_L { public PAST_L() {} } public static class Ex { public static void Cw(this object s) { Console.WriteLine(s); } public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; } public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; } static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); } public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); } public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); } public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); } public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); } public static string Read { get { return Console.ReadLine().Trim(); } } public static int ReadInt { get { return int.Parse(Read); } } public static long ReadLong { get { return long.Parse(Read); } } public static double ReadDouble { get { return double.Parse(Read); } } public static string[] ReadArray() { return Read.Split(' '); } public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); } public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); } public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } 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; } public static int UpperBound<T>(List<T> a, T v) { return UpperBound(a, v, Comparer<T>.Default); } public static int UpperBound<T>(List<T> a, T v, Comparer<T> cmp) { var l = 0; var r = a.Count - 1; while (l <= r) { var mid = l + (r - l) / 2; var res = cmp.Compare(a[mid], v); if (res <= 0) l = mid + 1; else r = mid - 1; } return l; } public class PriorityQueue<T> where T : IComparable<T> { List<T> list = new List<T>(); void PushHeap(T v) { int n = list.Count; list.Add(v); while (n != 0) { int i = (n - 1) / 2; if (list[n].CompareTo(list[i]) > 0) Swap(list, i, n); n = i; } } void PopHeap() { int n = list.Count - 1; list[0] = list[n]; list.RemoveAt(n); for (int i = 0, j; (j = 2 * i + 1) < n;) { if ((j != n - 1) && (list[j].CompareTo(list[j + 1]) < 0)) j++; if (list[i].CompareTo(list[j]) < 0) Swap(list, i, j); i = j; } } public void Push(T elem) { PushHeap(elem); } public T Pop() { var v = Peek; PopHeap(); return v; } public T Peek { get { return list[0]; } } public int Count { get { return list.Count; } } } } public 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)]; } } }
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Numerics; namespace PAST0 { using static Ex; public class Solve { public static void Main(string[] args) { new PAST_K(); } } public class PAST_K { char[] s, t; public PAST_K() { s = Read.ToCharArray(); t = Read.ToCharArray(); int len = 'z' - 'a' + 1; var list = new List<long>[len]; while (len > 0) { len--; list[len] = new List<long>(); } for (long i = 0; i < s.Length; i++) { var c = s[i]; list[c - 'a'].Add(i); } long loop = -1; long j = s.Length; for (long i = 0; i < t.Length; i++) { var c = list[t[i] - 'a']; if (!c.Any()) { (-1).Cw(); return; } var index = UpperBound(c, j); if (index < 0) index = ~index; if (index < c.Count) j = c[index]; else { loop++; j = c[0]; } } long min = s.Length * loop + j + 1; Math.Max(loop + 1, min).Cw(); } } public class PAST_L { public PAST_L() {} } public static class Ex { public static void Cw(this object s) { Console.WriteLine(s); } public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; } public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; } public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; } static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); } public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); } public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); } public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); } public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); } public static string Read { get { return Console.ReadLine().Trim(); } } public static int ReadInt { get { return int.Parse(Read); } } public static long ReadLong { get { return long.Parse(Read); } } public static double ReadDouble { get { return double.Parse(Read); } } public static string[] ReadArray() { return Read.Split(' '); } public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); } public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); } public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } 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; } public static int UpperBound<T>(List<T> a, T v) { return UpperBound(a, v, Comparer<T>.Default); } public static int UpperBound<T>(List<T> a, T v, Comparer<T> cmp) { var l = 0; var r = a.Count - 1; while (l <= r) { var mid = l + (r - l) / 2; var res = cmp.Compare(a[mid], v); if (res <= 0) l = mid + 1; else r = mid - 1; } return l; } public class PriorityQueue<T> where T : IComparable<T> { List<T> list = new List<T>(); void PushHeap(T v) { int n = list.Count; list.Add(v); while (n != 0) { int i = (n - 1) / 2; if (list[n].CompareTo(list[i]) > 0) Swap(list, i, n); n = i; } } void PopHeap() { int n = list.Count - 1; list[0] = list[n]; list.RemoveAt(n); for (int i = 0, j; (j = 2 * i + 1) < n;) { if ((j != n - 1) && (list[j].CompareTo(list[j + 1]) < 0)) j++; if (list[i].CompareTo(list[j]) < 0) Swap(list, i, j); i = j; } } public void Push(T elem) { PushHeap(elem); } public T Pop() { var v = Peek; PopHeap(); return v; } public T Peek { get { return list[0]; } } public int Count { get { return list.Count; } } } } public 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)]; } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203], ["+", 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
2,232
7
using Yorukatsu049.Questions; using Yorukatsu049.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu049.Questions { public class QuestionE : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var s = inputStream.ReadLine(); var t = inputStream.ReadLine(); if (!new HashSet<char>(s).IsSupersetOf(t)) { yield return -1; yield break; } var doubleS = s + s; var counts = Enumerable.Range(0, 26) .Select(_ => new BinaryIndexedTree(doubleS.Length)) .ToArray(); for (int i = 0; i < doubleS.Length; i++) { counts[doubleS[i] - 'a'].AddAt(i, 1); } long cursor = 0; foreach (var c in t) { var targetBit = counts[c - 'a']; var nextIndex = targetBit.GetLowerBound( targetBit.Sum((int)(cursor % s.Length)) + 1); cursor += nextIndex - cursor % s.Length; } yield return cursor + 1; } public class BinaryIndexedTree { long[] _data; public int Length { get; } public BinaryIndexedTree(int length) { _data = new long[length + 1]; // 内部的には1-indexedにする Length = length; } public BinaryIndexedTree(IEnumerable<long> data, int length) : this(length) { var count = 0; foreach (var n in data) { AddAt(count++, n); } } public BinaryIndexedTree(ICollection<long> collection) : this(collection, collection.Count) {} /// <summary> /// BITの<c>index</c>番目の要素に<c>n</c>を加算します。 /// </summary> /// <param name="index">加算するインデックス(0-indexed)</param> /// <param name="value">加算する数</param> public void AddAt(int i, long value) { unchecked { if ((uint)i >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(i)); } } i++; // 1-indexedにする while (i <= Length) { _data[i] += value; i += i & -i; // LSBの加算 } } /// <summary> /// [0, <c>end</c>)の部分和を返します。 /// </summary> /// <param name="end">部分和を求める半開区間の終了インデックス</param> /// <returns>区間の部分和</returns> public long Sum(int i) { unchecked { if ((uint)i >= (uint)_data.Length) { throw new ArgumentOutOfRangeException(nameof(i)); } } long sum = 0; while (i > 0) { sum += _data[i]; i -= i & -i; // LSBの減算 } return sum; } /// <summary> /// [<c>start</c>, <c>end</c>)の部分和を返します。 /// </summary> /// <param name="start">部分和を求める半開区間の開始インデックス</param> /// <param name="end">部分和を求める半開区間の終了インデックス</param> /// <returns>区間の部分和</returns> public long Sum(int start, int end) => Sum(end) - Sum(start); /// <summary> /// [0, /// <c>index</c>)の部分和が<c>sum</c>未満になる最大の<c>index</c>を返します。 /// BIT上の各要素は0以上の数である必要があります。 /// </summary> /// <param name="sum"></param> /// <returns></returns> public int GetLowerBound(long sum) { int index = 0; for (int offset = GetMostSignificantBitOf(Length); offset > 0; offset >>= 1) { if (index + offset < _data.Length && _data[index + offset] < sum) { index += offset; sum -= _data[index]; } } return index; } int GetMostSignificantBitOf(int n) { int k = 1; while ((k << 1) <= n) { k <<= 1; }; return k; } } } } namespace Yorukatsu049 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionE(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu049.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 Yorukatsu049.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 Yorukatsu049.Questions; using Yorukatsu049.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Yorukatsu049.Questions { public class QuestionE : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var s = inputStream.ReadLine(); var t = inputStream.ReadLine(); if (!new HashSet<char>(s).IsSupersetOf(t)) { yield return -1; yield break; } var doubleS = s + s; var counts = Enumerable.Range(0, 26) .Select(_ => new BinaryIndexedTree(doubleS.Length)) .ToArray(); for (int i = 0; i < doubleS.Length; i++) { counts[doubleS[i] - 'a'].AddAt(i, 1); } long cursor = 0; foreach (var c in t) { var targetBit = counts[c - 'a']; var nextIndex = targetBit.GetLowerBound( targetBit.Sum((int)(cursor % s.Length)) + 1) + 1; cursor += nextIndex - cursor % s.Length; } yield return cursor; } public class BinaryIndexedTree { long[] _data; public int Length { get; } public BinaryIndexedTree(int length) { _data = new long[length + 1]; // 内部的には1-indexedにする Length = length; } public BinaryIndexedTree(IEnumerable<long> data, int length) : this(length) { var count = 0; foreach (var n in data) { AddAt(count++, n); } } public BinaryIndexedTree(ICollection<long> collection) : this(collection, collection.Count) {} /// <summary> /// BITの<c>index</c>番目の要素に<c>n</c>を加算します。 /// </summary> /// <param name="index">加算するインデックス(0-indexed)</param> /// <param name="value">加算する数</param> public void AddAt(int i, long value) { unchecked { if ((uint)i >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(i)); } } i++; // 1-indexedにする while (i <= Length) { _data[i] += value; i += i & -i; // LSBの加算 } } /// <summary> /// [0, <c>end</c>)の部分和を返します。 /// </summary> /// <param name="end">部分和を求める半開区間の終了インデックス</param> /// <returns>区間の部分和</returns> public long Sum(int i) { unchecked { if ((uint)i >= (uint)_data.Length) { throw new ArgumentOutOfRangeException(nameof(i)); } } long sum = 0; while (i > 0) { sum += _data[i]; i -= i & -i; // LSBの減算 } return sum; } /// <summary> /// [<c>start</c>, <c>end</c>)の部分和を返します。 /// </summary> /// <param name="start">部分和を求める半開区間の開始インデックス</param> /// <param name="end">部分和を求める半開区間の終了インデックス</param> /// <returns>区間の部分和</returns> public long Sum(int start, int end) => Sum(end) - Sum(start); /// <summary> /// [0, /// <c>index</c>)の部分和が<c>sum</c>未満になる最大の<c>index</c>を返します。 /// BIT上の各要素は0以上の数である必要があります。 /// </summary> /// <param name="sum"></param> /// <returns></returns> public int GetLowerBound(long sum) { int index = 0; for (int offset = GetMostSignificantBitOf(Length); offset > 0; offset >>= 1) { if (index + offset < _data.Length && _data[index + offset] < sum) { index += offset; sum -= _data[index]; } } return index; } int GetMostSignificantBitOf(int n) { int k = 1; while ((k << 1) <= n) { k <<= 1; }; return k; } } } } namespace Yorukatsu049 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionE(); // 問題に合わせて書き換え var answers = question.Solve(Console.In); foreach (var answer in answers) { Console.WriteLine(answer); } } } } #region Base Classes namespace Yorukatsu049.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 Yorukatsu049.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, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["-", 0, 195, 8, 196, 0, 285, 0, 16, 17, 72], ["-", 0, 195, 8, 196, 0, 285, 0, 16, 12, 203]]
8
1,022
4
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var S = sc.ReadStr(); var T = sc.ReadStr(); var pos = new List<int>[26]; for (int i = 0; i < 26; i++) { pos[i] = new List<int>(); } for (int i = 0; i < S.Length; i++) { var ci = S[i] - 'a'; pos[ci].Add(i); } for (int i = 0; i < S.Length; i++) { var ci = S[i] - 'a'; pos[ci].Add(i + S.Length); } var now = 0; var ans = 0L; foreach (var c in T) { var ci = c - 'a'; if (pos[ci].Count == 0) { Console.WriteLine(-1); return; } var ui = pos[ci].UpperBound(now); var index = pos[ci][ui]; now = index; if (now >= S.Length) { now -= S.Length; ans += S.Length; } } ans += now + 1; Console.WriteLine(ans); } static Scanner sc = new Scanner(); } static partial class Algorithm { static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb) { var l = 0; var r = a.Count - 1; while (l <= r) { var m = (l + r) / 2; var res = cmp.Compare(a[m], v); if (res < 0 || (res == 0 && !islb)) l = m + 1; else r = m - 1; } return l; } public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); } public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); } public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); } public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); } } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var S = sc.ReadStr(); var T = sc.ReadStr(); var pos = new List<int>[26]; for (int i = 0; i < 26; i++) { pos[i] = new List<int>(); } for (int i = 0; i < S.Length; i++) { var ci = S[i] - 'a'; pos[ci].Add(i); } for (int i = 0; i < S.Length; i++) { var ci = S[i] - 'a'; pos[ci].Add(i + S.Length); } var now = 0; var ans = 0L; foreach (var c in T) { var ci = c - 'a'; if (pos[ci].Count == 0) { Console.WriteLine(-1); return; } var ui = pos[ci].LowerBound(now); var index = pos[ci][ui]; now = index + 1; if (now >= S.Length) { now -= S.Length; ans += S.Length; } } ans += now; Console.WriteLine(ans); } static Scanner sc = new Scanner(); } static partial class Algorithm { static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb) { var l = 0; var r = a.Count - 1; while (l <= r) { var m = (l + r) / 2; var res = cmp.Compare(a[m], v); if (res < 0 || (res == 0 && !islb)) l = m + 1; else r = m - 1; } return l; } public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); } public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); } public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); } public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); } } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
[["-", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 0, 200, 0, 212, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
1,590
6
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var S = sc.ReadStr(); var T = sc.ReadStr(); var countSum = new int[S.Length + 1, 26]; for (int i = 0; i < S.Length; i++) { for (int j = 0; j < 26; j++) { countSum[i + 1, j] = countSum[i, j]; } countSum[i + 1, S[i] - 'a']++; } for (int i = 0; i < T.Length; i++) { if (countSum[S.Length, T[i] - 'a'] == 0) { Console.WriteLine(-1); return; } } Func<char, int, int> f = (c, prev) => { var l = prev; var r = S.Length + 1; var prevCount = l < 0 ? 0 : countSum[prev, c - 'a']; while (r - l > 1) { var mid = l + (r - l) / 2; var mcount = countSum[mid, c - 'a']; if (prevCount < mcount) r = mid; else l = mid; } return r; }; var x = 0; var prev1 = 0; for (int i = 0; i < T.Length; i++) { { // 直前の文字が見つかったインデックスより後ろで探す var r = f(T[i], prev1 - 1); if (r <= S.Length) { prev1 = r; continue; } } { // 見つからなかった場合は先頭から探す x++; var r = f(T[i], -1); prev1 = r; } } var ans = (long)x * S.Length; ans += prev1; Console.WriteLine(ans); } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var S = sc.ReadStr(); var T = sc.ReadStr(); var countSum = new int[S.Length + 1, 26]; for (int i = 0; i < S.Length; i++) { for (int j = 0; j < 26; j++) { countSum[i + 1, j] = countSum[i, j]; } countSum[i + 1, S[i] - 'a']++; } for (int i = 0; i < T.Length; i++) { if (countSum[S.Length, T[i] - 'a'] == 0) { Console.WriteLine(-1); return; } } Func<char, int, int> f = (c, prev) => { var l = prev; var r = S.Length + 1; var prevCount = l < 0 ? 0 : countSum[prev, c - 'a']; while (r - l > 1) { var mid = l + (r - l) / 2; var mcount = countSum[mid, c - 'a']; if (prevCount < mcount) r = mid; else l = mid; } return r; }; var x = 0; var prev1 = 0; for (int i = 0; i < T.Length; i++) { { // 直前の文字が見つかったインデックスより後ろで探す var r = f(T[i], prev1); if (r <= S.Length) { prev1 = r; continue; } } { // 見つからなかった場合は先頭から探す x++; var r = f(T[i], -1); prev1 = r; } } var ans = (long)x * S.Length; ans += prev1; Console.WriteLine(ans); } 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 Tuple<int, int> ReadTup(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add); } public Tuple<long, long> ReadTupLong(int add = 0) { return Tuple.Create(this.ReadLong() + add, this.ReadLong() + add); } public Tuple<int, int, int> ReadTup3(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } public Tuple<int, int, int, int> ReadTup4(int add = 0) { return Tuple.Create(this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add, this.ReadInt() + add); } 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; } public Tuple<long, long>[] ReadTupLongArray(int n, int add = 0) { var array = new Tuple<long, long>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTupLong(add); } return array; } public Tuple<int, int>[] ReadTupArray(int n, int add = 0) { var array = new Tuple<int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup(add); } return array; } public Tuple<int, int, int>[] ReadTup3Array(int n, int add = 0) { var array = new Tuple<int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup3(add); } return array; } public Tuple<int, int, int, int>[] ReadTup4Array(int n, int add = 0) { var array = new Tuple<int, int, int, int>[n]; for (int i = 0; i < n; i++) { array[i] = this.ReadTup4(add); } return array; } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
1,417
2
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Numerics; using static System.Math; using static System.Array; using static AtCoder.IO_ShortCut; using static AtCoder.Tool; using static AtCoder.ModInt; namespace AtCoder { class AC { const int MOD = 1000000007; // const int MOD = 998244353; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 2; const double EPS = 1e-8; static readonly int[] dI = { 0, 1, 0, -1, 1, -1, -1, 1 }; static readonly int[] dJ = { 1, 0, -1, 0, 1, 1, -1, -1 }; // static List<List<int>> G = new List<List<int>>(); static List<List<Edge>> G = new List<List<Edge>>(); // static List<Edge> E = new List<Edge>(); static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var s = Str; var t = Str; var ind = new List<List<int>>(); for (var i = 0; i < 26; i++) ind.Add(new List<int>()); for (var i = 0; i < s.Length; i++) { ind[s[i] - 'a'].Add(i); } long ans = 1; int k = 0; int L = s.Length; for (var i = 0; i < t.Length; i++) { int cur = t[i] - 'a'; int l = -1; int r = ind[cur].Count(); if (r == 0) { OutL(-1); return; } while (r - l > 1) { var mid = (l + r) / 2; if (ind[cur][mid] <= k) l = mid; else r = mid; } if (r == ind[cur].Count()) { ans++; k = ind[cur][0]; } else { k = ind[cur][r]; } } // Out_Sep(ans, k); OutL(L * (ans - 1) + k + 1); } public struct Edge { public int from; public int to; public long dist; public Edge(int t, long c) { from = -1; to = t; dist = c; } public Edge(int f, int t, long c) { from = f; to = t; dist = c; } } } struct ModInt { public long value; private const int MOD = 1000000007; // private const int MOD = 998244353; public ModInt(long value) { this.value = value; } public static implicit operator ModInt(long a) { var ret = a % MOD; return new ModInt(ret < 0 ? (ret + MOD) : ret); } public static ModInt operator +(ModInt a, ModInt b) => (a.value + b.value); public static ModInt operator -(ModInt a, ModInt b) => (a.value - b.value); public static ModInt operator *(ModInt a, ModInt b) => (a.value * b.value); public static ModInt operator /(ModInt a, ModInt b) => a * Modpow(b, MOD - 2); public static ModInt operator <<(ModInt a, int n) => (a.value << n); public static ModInt operator>>(ModInt a, int n) => (a.value >> n); public static ModInt operator ++(ModInt a) => a.value + 1; public static ModInt operator --(ModInt a) => a.value - 1; public static ModInt Modpow(ModInt a, long n) { var k = a; ModInt ret = 1; while (n > 0) { if ((n & 1) != 0) ret *= k; k *= k; n >>= 1; } return ret; } private static readonly List<long> Factorials = new List<long>() { 1 }; public static ModInt Fac(long n) { for (var i = Factorials.Count(); i <= n; i++) { Factorials.Add((Factorials[i - 1] * i) % MOD); } return Factorials[(int)n]; } public static ModInt nCr(long n, long r) { return n < r ? 0 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(ModInt a) => (int)a.value; } static class IO_ShortCut { public static string[] _ReadSplit => Console.ReadLine().Split(); public static int[] _ReadSplitInt => ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] _ReadSplitLong => ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] _ReadSplit_Double => ConvertAll(Console.ReadLine().Split(), double.Parse); public static string Str => Console.ReadLine(); public static int Int => int.Parse(Console.ReadLine()); public static long Long => long.Parse(Console.ReadLine()); public static double Double => double.Parse(Console.ReadLine()); public static T Conv<T>(string input) { if (typeof(T).Equals(typeof(ModInt))) { return (T)(dynamic)(long.Parse(input)); } return (T)Convert.ChangeType(input, typeof(T)); } public static void Input<T>(out T a) => a = Conv<T>(Console.ReadLine()); public static void Input<T, U>(out T a, out U b) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); } public static void Input<T, U, V>(out T a, out U b, out V c) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); } public static void Input<T, U, V, W>(out T a, out U b, out V c, out W d) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); } public static void Input<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); e = Conv<X>(q[4]); } public static void OutL(object s) => Console.WriteLine(s); public static void Out_Sep<T>(IEnumerable<T> s) => Console.WriteLine(string.Join(" ", s)); public static void Out_Sep<T>(IEnumerable<T> s, string sep) => Console.WriteLine(string.Join($"{sep}", s)); public static void Out_Sep(params object[] s) => Console.WriteLine(string.Join(" ", s)); public static void Out_One(object s) => Console.Write($"{s} "); public static void Out_One(object s, string sep) => Console.Write($"{s}{sep}"); public static void Endl() => Console.WriteLine(); } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { array = ConvertAll(array, x => initialvalue); } static public void Swap<T>(ref T a, ref T b) { T keep = a; a = b; b = keep; } static public void Display<T>(T[,] array2d, int n, int m) { for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { Console.Write($"{array2d[i, j]} "); } Console.WriteLine(); } } static public long LPow(int a, int b) => (long)Pow(a, b); static public bool Bit(long x, int dig) => ((1L << dig) & x) != 0; static public int Sig(long a) => a == 0 ? 0 : (int)(a / Abs(a)); } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Numerics; using static System.Math; using static System.Array; using static AtCoder.IO_ShortCut; using static AtCoder.Tool; using static AtCoder.ModInt; namespace AtCoder { class AC { const int MOD = 1000000007; // const int MOD = 998244353; const int INF = int.MaxValue / 2; const long SINF = long.MaxValue / 2; const double EPS = 1e-8; static readonly int[] dI = { 0, 1, 0, -1, 1, -1, -1, 1 }; static readonly int[] dJ = { 1, 0, -1, 0, 1, 1, -1, -1 }; // static List<List<int>> G = new List<List<int>>(); static List<List<Edge>> G = new List<List<Edge>>(); // static List<Edge> E = new List<Edge>(); static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } static void Solve() { var s = Str; var t = Str; var ind = new List<List<int>>(); for (var i = 0; i < 26; i++) ind.Add(new List<int>()); for (var i = 0; i < s.Length; i++) { ind[s[i] - 'a'].Add(i); } long ans = 1; int k = -1; int L = s.Length; for (var i = 0; i < t.Length; i++) { int cur = t[i] - 'a'; int l = -1; int r = ind[cur].Count(); if (r == 0) { OutL(-1); return; } while (r - l > 1) { var mid = (l + r) / 2; if (ind[cur][mid] <= k) l = mid; else r = mid; } if (r == ind[cur].Count()) { ans++; k = ind[cur][0]; } else { k = ind[cur][r]; } } // Out_Sep(ans, k); OutL(L * (ans - 1) + k + 1); } public struct Edge { public int from; public int to; public long dist; public Edge(int t, long c) { from = -1; to = t; dist = c; } public Edge(int f, int t, long c) { from = f; to = t; dist = c; } } } struct ModInt { public long value; private const int MOD = 1000000007; // private const int MOD = 998244353; public ModInt(long value) { this.value = value; } public static implicit operator ModInt(long a) { var ret = a % MOD; return new ModInt(ret < 0 ? (ret + MOD) : ret); } public static ModInt operator +(ModInt a, ModInt b) => (a.value + b.value); public static ModInt operator -(ModInt a, ModInt b) => (a.value - b.value); public static ModInt operator *(ModInt a, ModInt b) => (a.value * b.value); public static ModInt operator /(ModInt a, ModInt b) => a * Modpow(b, MOD - 2); public static ModInt operator <<(ModInt a, int n) => (a.value << n); public static ModInt operator>>(ModInt a, int n) => (a.value >> n); public static ModInt operator ++(ModInt a) => a.value + 1; public static ModInt operator --(ModInt a) => a.value - 1; public static ModInt Modpow(ModInt a, long n) { var k = a; ModInt ret = 1; while (n > 0) { if ((n & 1) != 0) ret *= k; k *= k; n >>= 1; } return ret; } private static readonly List<long> Factorials = new List<long>() { 1 }; public static ModInt Fac(long n) { for (var i = Factorials.Count(); i <= n; i++) { Factorials.Add((Factorials[i - 1] * i) % MOD); } return Factorials[(int)n]; } public static ModInt nCr(long n, long r) { return n < r ? 0 : Fac(n) / (Fac(r) * Fac(n - r)); } public static explicit operator int(ModInt a) => (int)a.value; } static class IO_ShortCut { public static string[] _ReadSplit => Console.ReadLine().Split(); public static int[] _ReadSplitInt => ConvertAll(Console.ReadLine().Split(), int.Parse); public static long[] _ReadSplitLong => ConvertAll(Console.ReadLine().Split(), long.Parse); public static double[] _ReadSplit_Double => ConvertAll(Console.ReadLine().Split(), double.Parse); public static string Str => Console.ReadLine(); public static int Int => int.Parse(Console.ReadLine()); public static long Long => long.Parse(Console.ReadLine()); public static double Double => double.Parse(Console.ReadLine()); public static T Conv<T>(string input) { if (typeof(T).Equals(typeof(ModInt))) { return (T)(dynamic)(long.Parse(input)); } return (T)Convert.ChangeType(input, typeof(T)); } public static void Input<T>(out T a) => a = Conv<T>(Console.ReadLine()); public static void Input<T, U>(out T a, out U b) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); } public static void Input<T, U, V>(out T a, out U b, out V c) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); } public static void Input<T, U, V, W>(out T a, out U b, out V c, out W d) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); } public static void Input<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var q = _ReadSplit; a = Conv<T>(q[0]); b = Conv<U>(q[1]); c = Conv<V>(q[2]); d = Conv<W>(q[3]); e = Conv<X>(q[4]); } public static void OutL(object s) => Console.WriteLine(s); public static void Out_Sep<T>(IEnumerable<T> s) => Console.WriteLine(string.Join(" ", s)); public static void Out_Sep<T>(IEnumerable<T> s, string sep) => Console.WriteLine(string.Join($"{sep}", s)); public static void Out_Sep(params object[] s) => Console.WriteLine(string.Join(" ", s)); public static void Out_One(object s) => Console.Write($"{s} "); public static void Out_One(object s, string sep) => Console.Write($"{s}{sep}"); public static void Endl() => Console.WriteLine(); } public static class Tool { static public void Initialize<T>(ref T[] array, T initialvalue) { array = ConvertAll(array, x => initialvalue); } static public void Swap<T>(ref T a, ref T b) { T keep = a; a = b; b = keep; } static public void Display<T>(T[,] array2d, int n, int m) { for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { Console.Write($"{array2d[i, j]} "); } Console.WriteLine(); } } static public long LPow(int a, int b) => (long)Pow(a, b); static public bool Bit(long x, int dig) => ((1L << dig) & x) != 0; static public int Sig(long a) => a == 0 ? 0 : (int)(a / Abs(a)); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
1,937
3
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { ABC138.E(); } class ABC138 { public static void A() { var A = Read.Int(); var S = Read.Str(); Console.WriteLine(A >= 3200 ? S : "red"); } public static void B() { var N = Read.Int(); var A = Read.IntN(); Console.WriteLine(1.0 / A.Select(x => 1.0 / x).Sum()); } public static void C() { var N = Read.Int(); var V = Read.IntN().OrderBy(x => x).ToArray(); double ans = V[0]; for (int i = 1; i < N; ++i) { ans = (ans + V[i]) / 2.0; } Console.WriteLine(ans); } public static void D() { var N = Read.Int(); var Q = Read.Int(); var Adj = Read.AdjacencyList(N, N - 1); var PX = Read.TupleInt(Q); var X = new long[N + 1]; foreach (var px in PX) { X[px.Item1] += px.Item2; } var res = Enumerable.Range(0, N + 1).Select(i => -1L).ToArray(); var queue = new Queue<Tuple<int, long>>(); queue.Enqueue(new Tuple<int, long>(1, X[1])); while (queue.Count > 0) { var t = queue.Dequeue(); var v = t.Item1; var x = t.Item2; res[v] = x; foreach (var w in Adj[v]) { if (res[w] >= 0) { continue; } queue.Enqueue(new Tuple<int, long>(w, x + X[w])); } } Console.WriteLine(String.Join(" ", res.Skip(1))); } public static void E() { var S = Read.Str(); var T = Read.Str(); var N = S.Length; var next = Enumerable.Range(0, 26) .Select(i => Enumerable.Range(0, N).Select(j => -1).ToArray()) .ToArray(); var idx = new int[26]; var SS = S + S; for (int i = 1; i < N * 2; ++i) { var c = SS[i] - 'a'; for (int j = idx[c]; j < Math.Min(N, i); ++j) { next[c][j] = i; } idx[c] = i; } long current = 0; long count = 0; foreach (var t in T) { current = next[t - 'a'][current]; if (current < 0) { Console.WriteLine(-1); return; } if (current >= N) { count++; current -= N; } } Console.WriteLine(count * N + current + 1); } public static void F() {} static MOD F_Calc(long K) { var P3 = new MOD[60]; var ans = (MOD)0; P3[0] = 0; P3[1] = 1; P3[2] = 4; for (int i = 0; i < 60; ++i) { if (i > 2) { P3[i] = P3[i - 1] * 3 + 1; } if ((K & (1L << i)) > 0) { ans = P3[i] + ans * 2 + 1; } } return ans.V; } } class Util {} struct MOD { public static long P = 1000000007; public readonly long V; public MOD(long a) { this.V = a; } public static implicit operator MOD(long a) { return new MOD(a); } public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); } public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); } public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); } public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); } // public MOD Inverse() { return Util.Inverse(V, P); } public MOD Inverse() { long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0; while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; } c %= p; return c + (c < 0 ? p : 0); } // public MOD Power(long n) { return Util.Power(V, n, P); } public MOD Power(long n) { long a = V, p = P; long res = 1; long mult = a; for (int i = 0; i < 40; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; } return res; } public override string ToString() { return V.ToString(); } } 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, bool isDirectional = false) { 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]); if (!isDirectional) { dict[ab[1]].Add(ab[0]); } } return dict; } public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M) { Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); } return dict; } } } }
using System; using System.Linq; using System.Collections.Generic; namespace Contest { class Program { static void Main(string[] args) { ABC138.E(); } class ABC138 { public static void A() { var A = Read.Int(); var S = Read.Str(); Console.WriteLine(A >= 3200 ? S : "red"); } public static void B() { var N = Read.Int(); var A = Read.IntN(); Console.WriteLine(1.0 / A.Select(x => 1.0 / x).Sum()); } public static void C() { var N = Read.Int(); var V = Read.IntN().OrderBy(x => x).ToArray(); double ans = V[0]; for (int i = 1; i < N; ++i) { ans = (ans + V[i]) / 2.0; } Console.WriteLine(ans); } public static void D() { var N = Read.Int(); var Q = Read.Int(); var Adj = Read.AdjacencyList(N, N - 1); var PX = Read.TupleInt(Q); var X = new long[N + 1]; foreach (var px in PX) { X[px.Item1] += px.Item2; } var res = Enumerable.Range(0, N + 1).Select(i => -1L).ToArray(); var queue = new Queue<Tuple<int, long>>(); queue.Enqueue(new Tuple<int, long>(1, X[1])); while (queue.Count > 0) { var t = queue.Dequeue(); var v = t.Item1; var x = t.Item2; res[v] = x; foreach (var w in Adj[v]) { if (res[w] >= 0) { continue; } queue.Enqueue(new Tuple<int, long>(w, x + X[w])); } } Console.WriteLine(String.Join(" ", res.Skip(1))); } public static void E() { var S = Read.Str(); var T = Read.Str(); var N = S.Length; var next = Enumerable.Range(0, 26) .Select(i => Enumerable.Range(0, N).Select(j => -1).ToArray()) .ToArray(); var idx = new int[26]; var SS = S + S; for (int i = 1; i < N * 2; ++i) { var c = SS[i] - 'a'; for (int j = idx[c]; j < Math.Min(N, i); ++j) { next[c][j] = i; } idx[c] = i; } long current = N - 1; long count = -1; foreach (var t in T) { current = next[t - 'a'][current]; if (current < 0) { Console.WriteLine(-1); return; } if (current >= N) { count++; current -= N; } } Console.WriteLine(count * N + current + 1); } public static void F() {} static MOD F_Calc(long K) { var P3 = new MOD[60]; var ans = (MOD)0; P3[0] = 0; P3[1] = 1; P3[2] = 4; for (int i = 0; i < 60; ++i) { if (i > 2) { P3[i] = P3[i - 1] * 3 + 1; } if ((K & (1L << i)) > 0) { ans = P3[i] + ans * 2 + 1; } } return ans.V; } } class Util {} struct MOD { public static long P = 1000000007; public readonly long V; public MOD(long a) { this.V = a; } public static implicit operator MOD(long a) { return new MOD(a); } public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); } public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); } public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); } public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); } // public MOD Inverse() { return Util.Inverse(V, P); } public MOD Inverse() { long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0; while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; } c %= p; return c + (c < 0 ? p : 0); } // public MOD Power(long n) { return Util.Power(V, n, P); } public MOD Power(long n) { long a = V, p = P; long res = 1; long mult = a; for (int i = 0; i < 40; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; } return res; } public override string ToString() { return V.ToString(); } } 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, bool isDirectional = false) { 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]); if (!isDirectional) { dict[ab[1]].Add(ab[0]); } } return dict; } public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M) { Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); } for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); dict[ab[1]].Add(ab[0], ab[2]); } return dict; } } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 31, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
2,257
7
using System.Collections; using System.Text; using System.Threading.Tasks; using static System.Math; using static System.Console; using System.Collections.Generic; using System.Linq; using System; using System.Numerics; class Program { static void Main() { #region 入力 string s = ReadStr; string t = ReadStr; #endregion Dictionary<char, List<int>> sMap = new Dictionary<char, List<int>>(); for (int i = 0; i < s.Length; i++) { if (sMap.ContainsKey(s[i])) sMap[s[i]].Add(i); else sMap.Add(s[i], new List<int>() { i }); } long current = -1; long ans = 0; for (int i = 0; i < t.Length; i++) { if (sMap.ContainsKey(t[i])) { Vector2Int v = BinarySearch(sMap[t[i]], n => n < current); if (v.Y == sMap[t[i]].Count) { ans += s.Length; current = sMap[t[i]][0]; } else current = sMap[t[i]][v.Y]; } else { WriteLine(-1); ReadLine(); return; } } WriteLine(ans + current + 1); // WriteLine(BinarySearch(new List<int>() { 1, 3, 5, 6, 9 }, n => n < // 10).X); ReadLine(); } static Vector2Int BinarySearch(List<int> numbers, Func<int, bool> judge) { int max = numbers.Count; int min = -1; int mid = (min + max) / 2; while (max - min > 1) { if (judge(numbers[mid])) { min = mid; mid = (mid + max) / 2; } else { max = mid; mid = (mid + min) / 2; } } if (max == numbers.Count) return new Vector2Int(mid, mid + 1); else if (min == -1) return new Vector2Int(-1, 0); else { return judge(numbers[mid]) != judge(numbers[mid + 1]) ? new Vector2Int(mid, mid + 1) : new Vector2Int(mid - 1, mid); } } struct Vector2Int { public Vector2Int(int x, int y) { X = x; Y = y; } public int X; public int Y; public double Magnitude => Math.Sqrt(X * X + Y * Y); public static Vector2Int zero => new Vector2Int(0, 0); public static Vector2Int u => new Vector2Int(0, 1); public static Vector2Int r => new Vector2Int(1, 0); public static Vector2Int d => new Vector2Int(0, -1); public static Vector2Int l => new Vector2Int(-1, 0); public static Vector2Int[] AllDirections => new Vector2Int[4] { u, r, d, l }; public static Vector2Int operator +(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X + b.X, a.Y + b.Y); } public static Vector2Int operator -(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X - b.X, a.Y - b.Y); } public static bool operator ==(Vector2Int a, Vector2Int b) { return a.X == b.X && a.Y == b.Y; } public static bool operator !=(Vector2Int a, Vector2Int b) { return a.X != b.X || a.Y != b.Y; } public static Vector2Int operator *(Vector2Int v, int n) { return new Vector2Int(v.X * n, v.Y * n); } public static Vector2Int operator *(int n, Vector2Int v) { return new Vector2Int(v.X * n, v.Y * n); } } struct Vector3Int { public Vector3Int(int x, int y, int z) { X = x; Y = y; Z = z; } public int X; public int Y; public int Z; public double Magnitude => Math.Sqrt(X * X + Y * Y + Z * Z); public static Vector3Int zero => new Vector3Int(0, 0, 0); public static Vector3Int u => new Vector3Int(0, 1, 0); public static Vector3Int r => new Vector3Int(1, 0, 0); public static Vector3Int d => new Vector3Int(0, -1, 0); public static Vector3Int l => new Vector3Int(-1, 0, 0); public static Vector3Int operator +(Vector3Int a, Vector3Int b) { return new Vector3Int(a.X + b.X, a.Y + b.Y, a.Z + b.Z); } public static Vector3Int operator -(Vector3Int a, Vector3Int b) { return new Vector3Int(a.X - b.X, a.Y - b.Y, a.Z - b.Z); } public static bool operator ==(Vector3Int a, Vector3Int b) { return a.X == b.X && a.Y == b.Y && a.Z == b.Z; } public static bool operator !=(Vector3Int a, Vector3Int b) { return a.X != b.X || a.Y != b.Y || a.Z != b.Z; } public static Vector3Int operator *(Vector3Int v, int n) { return new Vector3Int(v.X * n, v.Y * n, v.Z * n); } public static Vector3Int operator *(int n, Vector3Int v) { return new Vector3Int(v.X * n, v.Y * n, v.Z * n); } } #region Reader static Vector2Int ReVec2Int { get { string[] s = Console.ReadLine().Split(' '); return new Vector2Int(int.Parse(s[0]), int.Parse(s[1])); } } static Vector2Int[] ReadVec2Ints(int n) { List<Vector2Int> list = new List<Vector2Int>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; list.Add(new Vector2Int(ab[0], ab[1])); } return list.ToArray(); } static HashSet<Vector2Int> ReadVec2IntHash(int n) { HashSet<Vector2Int> hash = new HashSet<Vector2Int>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; hash.Add(new Vector2Int(ab[0], ab[1])); } return hash; } static string ReadStr => Console.ReadLine(); static string[] ReadStrs => Console.ReadLine().Split(' '); static int ReadInt => Convert.ToInt32(Console.ReadLine()); static int[] ReadInts => Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); static long ReLong => Convert.ToInt64(Console.ReadLine()); static long[] ReadLongs => Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); static long[,] ReLongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = ReadLongs; for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } static Tuple<string, string>[] ReTupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = ReadStrs; list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<long, long>[] ReTupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongs; list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<int, int>[] ReTupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<double, double>[] ReTupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongs; list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } static Dictionary<int, HashSet<int>> ReAdjacencyList(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 = ReadInts; dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } #endregion }
using System.Collections; using System.Text; using System.Threading.Tasks; using static System.Math; using static System.Console; using System.Collections.Generic; using System.Linq; using System; using System.Numerics; class Program { static void Main() { #region 入力 string s = ReadStr; string t = ReadStr; #endregion Dictionary<char, List<int>> sMap = new Dictionary<char, List<int>>(); for (int i = 0; i < s.Length; i++) { if (sMap.ContainsKey(s[i])) sMap[s[i]].Add(i); else sMap.Add(s[i], new List<int>() { i }); } long current = -1; long ans = 0; for (int i = 0; i < t.Length; i++) { if (sMap.ContainsKey(t[i])) { Vector2Int v = BinarySearch(sMap[t[i]], n => n <= current); if (v.Y == sMap[t[i]].Count) { ans += s.Length; current = sMap[t[i]][0]; } else current = sMap[t[i]][v.Y]; } else { WriteLine(-1); ReadLine(); return; } } WriteLine(ans + current + 1); // WriteLine(BinarySearch(new List<int>() { 1, 3, 5, 6, 9 }, n => n < // 10).X); ReadLine(); } static Vector2Int BinarySearch(List<int> numbers, Func<int, bool> judge) { int max = numbers.Count; int min = -1; int mid = (min + max) / 2; while (max - min > 1) { if (judge(numbers[mid])) { min = mid; mid = (mid + max) / 2; } else { max = mid; mid = (mid + min) / 2; } } if (max == numbers.Count) return new Vector2Int(mid, mid + 1); else if (min == -1) return new Vector2Int(-1, 0); else { return judge(numbers[mid]) != judge(numbers[mid + 1]) ? new Vector2Int(mid, mid + 1) : new Vector2Int(mid - 1, mid); } } struct Vector2Int { public Vector2Int(int x, int y) { X = x; Y = y; } public int X; public int Y; public double Magnitude => Math.Sqrt(X * X + Y * Y); public static Vector2Int zero => new Vector2Int(0, 0); public static Vector2Int u => new Vector2Int(0, 1); public static Vector2Int r => new Vector2Int(1, 0); public static Vector2Int d => new Vector2Int(0, -1); public static Vector2Int l => new Vector2Int(-1, 0); public static Vector2Int[] AllDirections => new Vector2Int[4] { u, r, d, l }; public static Vector2Int operator +(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X + b.X, a.Y + b.Y); } public static Vector2Int operator -(Vector2Int a, Vector2Int b) { return new Vector2Int(a.X - b.X, a.Y - b.Y); } public static bool operator ==(Vector2Int a, Vector2Int b) { return a.X == b.X && a.Y == b.Y; } public static bool operator !=(Vector2Int a, Vector2Int b) { return a.X != b.X || a.Y != b.Y; } public static Vector2Int operator *(Vector2Int v, int n) { return new Vector2Int(v.X * n, v.Y * n); } public static Vector2Int operator *(int n, Vector2Int v) { return new Vector2Int(v.X * n, v.Y * n); } } struct Vector3Int { public Vector3Int(int x, int y, int z) { X = x; Y = y; Z = z; } public int X; public int Y; public int Z; public double Magnitude => Math.Sqrt(X * X + Y * Y + Z * Z); public static Vector3Int zero => new Vector3Int(0, 0, 0); public static Vector3Int u => new Vector3Int(0, 1, 0); public static Vector3Int r => new Vector3Int(1, 0, 0); public static Vector3Int d => new Vector3Int(0, -1, 0); public static Vector3Int l => new Vector3Int(-1, 0, 0); public static Vector3Int operator +(Vector3Int a, Vector3Int b) { return new Vector3Int(a.X + b.X, a.Y + b.Y, a.Z + b.Z); } public static Vector3Int operator -(Vector3Int a, Vector3Int b) { return new Vector3Int(a.X - b.X, a.Y - b.Y, a.Z - b.Z); } public static bool operator ==(Vector3Int a, Vector3Int b) { return a.X == b.X && a.Y == b.Y && a.Z == b.Z; } public static bool operator !=(Vector3Int a, Vector3Int b) { return a.X != b.X || a.Y != b.Y || a.Z != b.Z; } public static Vector3Int operator *(Vector3Int v, int n) { return new Vector3Int(v.X * n, v.Y * n, v.Z * n); } public static Vector3Int operator *(int n, Vector3Int v) { return new Vector3Int(v.X * n, v.Y * n, v.Z * n); } } #region Reader static Vector2Int ReVec2Int { get { string[] s = Console.ReadLine().Split(' '); return new Vector2Int(int.Parse(s[0]), int.Parse(s[1])); } } static Vector2Int[] ReadVec2Ints(int n) { List<Vector2Int> list = new List<Vector2Int>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; list.Add(new Vector2Int(ab[0], ab[1])); } return list.ToArray(); } static HashSet<Vector2Int> ReadVec2IntHash(int n) { HashSet<Vector2Int> hash = new HashSet<Vector2Int>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; hash.Add(new Vector2Int(ab[0], ab[1])); } return hash; } static string ReadStr => Console.ReadLine(); static string[] ReadStrs => Console.ReadLine().Split(' '); static int ReadInt => Convert.ToInt32(Console.ReadLine()); static int[] ReadInts => Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); static long ReLong => Convert.ToInt64(Console.ReadLine()); static long[] ReadLongs => Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); static long[,] ReLongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = ReadLongs; for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; } static Tuple<string, string>[] ReTupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = ReadStrs; list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<long, long>[] ReTupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongs; list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<int, int>[] ReTupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = ReadInts; list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); } static Tuple<double, double>[] ReTupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = ReadLongs; list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); } static Dictionary<int, HashSet<int>> ReAdjacencyList(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 = ReadInts; dict[ab[0]].Add(ab[1]); dict[ab[1]].Add(ab[0]); } return dict; } #endregion }
[["-", 3, 4, 0, 28, 0, 218, 8, 16, 17, 18], ["+", 3, 4, 0, 28, 0, 218, 8, 16, 17, 19]]
8
2,098
2
using System; using System.Collections.Generic; using System.Linq; namespace ABC138E { class Program { static void Main(string[] args) { // Console.WriteLine(search(new List<long> {1, 3, 5, 7, 9}, 15)); string s = CIN.String(); string t = CIN.String(); HashSet<char> ssub = new HashSet<char>(); // 文字charに対して、Indexの位置を保存しておく 二分探索で取得する Dictionary<char, List<int>> map = new Dictionary<char, List<int>>(); for (char c = 'a'; c <= 'z'; c++) { map.Add(c, new List<int>()); } for (int i = 0; i < s.Length; i++) { ssub.Add(s[i]); map[s[i]].Add(i); } for (int i = 0; i < t.Length; i++) { if (!ssub.Contains(t[i])) { Console.WriteLine(-1); return; } } int curIndex = -1; long ans = 0; for (int i = 0; i < t.Length; i++) { char curChar = t[i]; var list = map[curChar]; var min = search(list, curIndex); if (min == list.Count) { ans += s.Length - curIndex - 1; curIndex = -1; i--; // Console.WriteLine(curChar + " " + min + " :" + ans); continue; } ans += list[min] - curIndex; curIndex = list[min]; // Console.WriteLine(curChar + " " + min + " " + list[min] + " :" + ans); } Console.WriteLine(ans); } static int search(List<int> list, long min) { int lb = -1; int ub = list.Count; while (ub - lb > 1) { int mid = (lb + ub) / 2; if (list[mid] >= min) { ub = mid; } else { lb = mid; } } return ub; } } internal static class CIN { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ABC138E { class Program { static void Main(string[] args) { // Console.WriteLine(search(new List<long> {1, 3, 5, 7, 9}, 15)); string s = CIN.String(); string t = CIN.String(); HashSet<char> ssub = new HashSet<char>(); // 文字charに対して、Indexの位置を保存しておく 二分探索で取得する Dictionary<char, List<int>> map = new Dictionary<char, List<int>>(); for (char c = 'a'; c <= 'z'; c++) { map.Add(c, new List<int>()); } for (int i = 0; i < s.Length; i++) { ssub.Add(s[i]); map[s[i]].Add(i); } for (int i = 0; i < t.Length; i++) { if (!ssub.Contains(t[i])) { Console.WriteLine(-1); return; } } int curIndex = -1; long ans = 0; for (int i = 0; i < t.Length; i++) { char curChar = t[i]; var list = map[curChar]; var min = search(list, curIndex + 1); if (min == list.Count) { ans += s.Length - curIndex - 1; curIndex = -1; i--; // Console.WriteLine(curChar + " " + min + " :" + ans); continue; } ans += list[min] - curIndex; curIndex = list[min]; // Console.WriteLine(curChar + " " + min + " " + list[min] + " :" + ans); } Console.WriteLine(ans); } static int search(List<int> list, long min) { int lb = -1; int ub = list.Count; while (ub - lb > 1) { int mid = (lb + ub) / 2; if (list[mid] >= min) { ub = mid; } else { lb = mid; } } return ub; } } internal static class CIN { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
561
2
using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using E = System.Linq.Enumerable; internal partial class Solver { public void Run() { var s = ns(); var t = ns(); var next = new int[s.Length, 26]; for (int i = 0; i < 26; i++) { char c = (char)('a' + i); var nextPoint = s.IndexOf(c); for (int j = s.Length - 1; j >= 0; j--) { next[j, i] = nextPoint; if (s[j] == c) { nextPoint = j; } } } // next.Dump(); long ans = 1; int current = 0; foreach (var c in t) { var newPos = next[current, (int)(c - 'a')]; if (newPos == -1) { ans = -1; break; } var diff = newPos - current; if (diff <= 0) diff += s.Length; ans += diff; current = newPos; } cout.WriteLine(ans); } } // PREWRITEN CODE BEGINS FROM HERE internal partial class Solver : Scanner { public static void Main(string[] args) { #if LOCAL 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 { public static T Dump<T>(this T obj) { #if LOCAL return LINQPad.Extensions.Dump(obj); #else return 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.Generic; using System.Globalization; using System.IO; using System.Linq; using E = System.Linq.Enumerable; internal partial class Solver { public void Run() { var s = ns(); var t = ns(); var next = new int[s.Length, 26]; for (int i = 0; i < 26; i++) { char c = (char)('a' + i); var nextPoint = s.IndexOf(c); for (int j = s.Length - 1; j >= 0; j--) { next[j, i] = nextPoint; if (s[j] == c) { nextPoint = j; } } } // next.Dump(); long ans = 0; int current = s.Length - 1; foreach (var c in t) { var newPos = next[current, (int)(c - 'a')]; if (newPos == -1) { ans = -1; break; } var diff = newPos - current; if (diff <= 0) diff += s.Length; ans += diff; current = newPos; } cout.WriteLine(ans); } } // PREWRITEN CODE BEGINS FROM HERE internal partial class Solver : Scanner { public static void Main(string[] args) { #if LOCAL 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 { public static T Dump<T>(this T obj) { #if LOCAL return LINQPad.Extensions.Dump(obj); #else return 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; } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 205, 22], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 0, 131], ["+", 0, 200, 0, 212, 0, 16, 31, 214, 141, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
1,059
8
using System; using System.Collections.Generic; using System.Linq; namespace ABC138 { class E { static void Main(string[] args) { string s = Console.ReadLine(); string t = Console.ReadLine(); Dictionary<char, List<long>> dict = new Dictionary<char, List<long>>(); for (int i = 0; i < s.Length; i++) { if (!dict.ContainsKey(s[i])) { dict.Add(s[i], new List<long>()); dict[s[i]].Add(i); } else if (dict.ContainsKey(s[i])) { dict[s[i]].Add(i); } } // NGの時 for (int i = 0; i < t.Length; i++) { if (!dict.ContainsKey(t[i])) { Console.WriteLine(-1); return; } } //<--debug // for (long i = 0; i < t.Length; i++) //{ // Console.WriteLine(string.Join(" ", dict[t[i]])); //} // debug--> // long count = 0; long current = 0; int index = 0; long turn = 0; foreach (var c in t) { var list = dict[c]; index = list.BinarySearch(current); if (index < 0) { index = ~index; } if (list.Count <= index) { index = 0; turn++; } current = list[index]; /* if (list[index] < current) { count += (long)s.Length - (current + 1) + list[index] + 1; current = list[index]; } else { count += list[index] - current; current = list[index]; } */ // Console.WriteLine("count : " + count); } Console.WriteLine((long)s.Length * turn + current + 1); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ABC138 { class E { static void Main(string[] args) { string s = Console.ReadLine(); string t = Console.ReadLine(); Dictionary<char, List<long>> dict = new Dictionary<char, List<long>>(); for (int i = 0; i < s.Length; i++) { if (!dict.ContainsKey(s[i])) { dict.Add(s[i], new List<long>()); dict[s[i]].Add(i); } else if (dict.ContainsKey(s[i])) { dict[s[i]].Add(i); } } // NGの時 for (int i = 0; i < t.Length; i++) { if (!dict.ContainsKey(t[i])) { Console.WriteLine(-1); return; } } //<--debug // for (long i = 0; i < t.Length; i++) //{ // Console.WriteLine(string.Join(" ", dict[t[i]])); //} // debug--> // long count = 0; long current = -1; int index = 0; long turn = 0; foreach (var c in t) { var list = dict[c]; index = list.BinarySearch(current + 1); if (index < 0) { index = ~index; } if (list.Count <= index) { index = 0; turn++; } current = list[index]; /* if (list[index] < current) { count += (long)s.Length - (current + 1) + list[index] + 1; current = list[index]; } else { count += list[index] - current; current = list[index]; } */ // Console.WriteLine("count : " + count); } Console.WriteLine((long)s.Length * turn + current + 1); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
317
5
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; static class CodeJam { static long Solve(string S, string T) { var lists = new List<int>[26]; for (int i = 0; i < 26; ++i) { lists[i] = new List<int>(); } for (int i = 0; i < S.Length; ++i) { lists[S[i] - 'a'].Add(i); } int a = 0, b = 0; foreach (char t in T) { int i = t - 'a'; int N = lists[i].Count; if (N == 0) { return -1; } Func<int> search = () => { int L = 0, R = N - 1; if (b <= lists[i][0]) { return 0; } if (lists[i][R] < b) { return N; } while (R - L >= 2) { int m = (L + R) / 2; if (b <= lists[i][m]) { R = m; } else { L = m; } } return R; }; int j = search(); if (j < N) { b = lists[i][j]; } else { b = lists[i][0]; ++a; } } return Math.BigMul(S.Length, a) + b + 1; } static void Main(string[] args) { string S = Console.ReadLine(); string T = Console.ReadLine(); Console.WriteLine(Solve(S, T)); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; static class CodeJam { static long Solve(string S, string T) { var lists = new List<int>[26]; for (int i = 0; i < 26; ++i) { lists[i] = new List<int>(); } for (int i = 0; i < S.Length; ++i) { lists[S[i] - 'a'].Add(i); } int a = 0, b = -1; foreach (char t in T) { int i = t - 'a'; int N = lists[i].Count; if (N == 0) { return -1; } Func<int> search = () => { int L = 0, R = N - 1; if (b < lists[i][0]) { return 0; } if (lists[i][R] <= b) { return N; } while (R - L >= 2) { int m = (L + R) / 2; if (b < lists[i][m]) { R = m; } else { L = m; } } return R; }; int j = search(); if (j < N) { b = lists[i][j]; } else { b = lists[i][0]; ++a; } } return Math.BigMul(S.Length, a) + b + 1; } static void Main(string[] args) { string S = Console.ReadLine(); string T = Console.ReadLine(); Console.WriteLine(Solve(S, T)); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203], ["-", 0, 218, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 218, 8, 196, 0, 57, 15, 16, 17, 18], ["-", 0, 218, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 218, 8, 196, 0, 57, 15, 16, 17, 19], ["-", 0, 52, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 18]]
8
378
9
using System; using System.Linq; using System.Collections.Generic; namespace E { class Program { static void Main(string[] args) { Console.WriteLine(Run(args)); } private static long Run(string[] args) { var s = ConsoleReader.String(); var t = ConsoleReader.String(); var td = t.Distinct(); var sd = s.Distinct(); if (!td.All(c => sd.Contains(c))) { return -1; } var map = sd.ToDictionary(c => c, c => new List<int>()); for (int i = 0; i < s.Length; i++) { map[s[i]].Add(i); } var pos = 0; var ans = 0L; foreach (var c in t) { var indexList = map[c]; var listIndex = indexList.BinarysSearch(i => i > pos); if (listIndex > -1) { pos = indexList[listIndex]; } else { ans += s.Length; pos = indexList.First(); } } ans += pos + 1; return ans; } } public static class ListExtensions { /// <summary> /// 二分探索を行うListの拡張メソッド /// <param name="list">拡張対象</param> /// <param name="compFunction">比較関数</param> /// <return>探索結果のインデックス。マッチしない場合は-1を返す</return> /// </summary> public static int BinarysSearch<T>(this List<T> list, Func<T, bool> compFunction) { int left = -1; int right = list.Count; while (right - left > 1) { int mid = left + (right - left) / 2; if (compFunction(list[mid])) { right = mid; } else { left = mid; } } if (right >= list.Count) { return -1; } else { return right; } } } internal static class ConsoleReader { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
using System; using System.Linq; using System.Collections.Generic; namespace E { class Program { static void Main(string[] args) { Console.WriteLine(Run(args)); } private static long Run(string[] args) { var s = ConsoleReader.String(); var t = ConsoleReader.String(); var td = t.Distinct(); var sd = s.Distinct(); if (!td.All(c => sd.Contains(c))) { return -1; } var map = sd.ToDictionary(c => c, c => new List<int>()); for (int i = 0; i < s.Length; i++) { map[s[i]].Add(i); } var pos = -1; var ans = 0L; foreach (var c in t) { var indexList = map[c]; var listIndex = indexList.BinarysSearch(i => i > pos); if (listIndex > -1) { // Console.WriteLine(c + " list:" + String.Join(",", indexList)); // Console.WriteLine("BPos:" + pos); pos = indexList[listIndex]; // Console.WriteLine("APos:" + pos); } else { ans += s.Length; pos = indexList.First(); } } ans += pos + 1; return ans; } } public static class ListExtensions { /// <summary> /// 二分探索を行うListの拡張メソッド /// <param name="list">拡張対象</param> /// <param name="compFunction">比較関数</param> /// <return>探索結果のインデックス。マッチしない場合は-1を返す</return> /// </summary> public static int BinarysSearch<T>(this List<T> list, Func<T, bool> compFunction) { int left = -1; int right = list.Count; while (right - left > 1) { int mid = left + (right - left) / 2; if (compFunction(list[mid])) { right = mid; } else { left = mid; } } if (right >= list.Count) { return -1; } else { return right; } } } internal static class ConsoleReader { public static string String() { return Console.ReadLine(); } public static int Int() { return int.Parse(Console.ReadLine()); } public static int[] IntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } public static long Long() { return long.Parse(Console.ReadLine()); } public static long[] LongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); } public static double[] DoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
533
3
using System; using System.Collections.Generic; namespace E___Strings_of_Impurity { class Program { static void Main(string[] args) { Console.WriteLine(solver()); } static long solver() { var s = Console.ReadLine(); var t = Console.ReadLine(); var alphabets = new List<int>[26]; for (int i = 0; i < 26; i++) alphabets[i] = new List<int>(); for (int i = 0; i < s.Length; i++) alphabets[s[i] - 'a'].Add(i + 1); long result = 0; var currentIndex = 0; for (int i = 0; i < t.Length; i++) { var currentChar = alphabets[t[i] - 'a']; if (currentChar.Count == 0) return -1; currentIndex = binarySearch(currentChar, currentIndex); if (currentIndex == -1) { result += s.Length; currentIndex = currentChar[0]; } } return result + currentIndex; } static int binarySearch(List<int> list, int key) { int left = -1; int right = list.Count; while (right - left > 1) { int mid = left + (right - left) / 2; if (list[mid] >= key) { right = mid; } else { left = mid; } } if (right >= list.Count) { return -1; } else { return list[right]; } } } }
using System; using System.Collections.Generic; namespace E___Strings_of_Impurity { class Program { static void Main(string[] args) { Console.WriteLine(solver()); } static long solver() { var s = Console.ReadLine(); var t = Console.ReadLine(); var alphabets = new List<int>[26]; for (int i = 0; i < 26; i++) alphabets[i] = new List<int>(); for (int i = 0; i < s.Length; i++) alphabets[s[i] - 'a'].Add(i + 1); long result = 0; var currentIndex = 0; for (int i = 0; i < t.Length; i++) { var currentChar = alphabets[t[i] - 'a']; if (currentChar.Count == 0) return -1; currentIndex = binarySearch(currentChar, currentIndex); if (currentIndex == -1) { result += s.Length; currentIndex = currentChar[0]; } } return result + currentIndex; } static int binarySearch(List<int> list, int key) { int left = -1; int right = list.Count; while (right - left > 1) { int mid = left + (right - left) / 2; if (list[mid] > key) { right = mid; } else { left = mid; } } if (right >= list.Count) { return -1; } else { return list[right]; } } } }
[["-", 0, 52, 8, 196, 0, 57, 15, 16, 17, 20], ["+", 0, 52, 8, 196, 0, 57, 15, 16, 17, 47]]
8
324
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static Input; using static Util; class Program { static string S; static string T; static void INPUT() { S = NextString(); T = NextString(); } static void OUTPUT() { var SS = (S + S).ToCharArray(); var d = new List<List<int>>(); var sl = S.Length; for (int i = 0; i < 26; i++) d.Add(new List<int>()); for (int i = 0; i < SS.Length; i++) d[SS[i] - 'a'].Add(i + 1); var dic = new Dictionary<int, int[]>(); for (int i = 0; i < 26; i++) { dic[i] = d[i].ToArray(); } var ans = 0L; var p = 0; foreach (var c in T) { var cc = c - 'a'; if (dic[cc].Count() == 0) { Console.WriteLine(-1); return; } } foreach (var c in T) { var cc = c - 'a'; // p = d[cc].FirstOrDefault(__ => __ > p); p = dic[cc][LowerBound<int>(dic[cc], p + 1)]; if (p > sl) { ans += sl; p -= sl; } } ans += p; Console.WriteLine(ans); } static void Main(string[] args) { #if DEBUG TESTINPUT(); #else TESTINPUT(); // INPUT(); #endif OUTPUT(); } static void TESTINPUT() { // programming // sentence } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.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(); } } class Util { /// <summary> /// 指定した値以上の先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値以上の先頭のインデクス</returns> public static int LowerBound<T>(T[] arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) < 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int LowerBound<T>(T[] arr, T value) where T : IComparable { return LowerBound<T>(arr, 0, arr.Length, value, Comparer<T>.Default); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static Input; using static Util; class Program { static string S; static string T; static void INPUT() { S = NextString(); T = NextString(); } static void OUTPUT() { var SS = (S + S).ToCharArray(); var d = new List<List<int>>(); var sl = S.Length; for (int i = 0; i < 26; i++) d.Add(new List<int>()); for (int i = 0; i < SS.Length; i++) d[SS[i] - 'a'].Add(i + 1); var dic = new Dictionary<int, int[]>(); for (int i = 0; i < 26; i++) { dic[i] = d[i].ToArray(); } var ans = 0L; var p = 0; foreach (var c in T) { var cc = c - 'a'; if (dic[cc].Count() == 0) { Console.WriteLine(-1); return; } } foreach (var c in T) { var cc = c - 'a'; // p = d[cc].FirstOrDefault(__ => __ > p); p = dic[cc][LowerBound<int>(dic[cc], p + 1)]; if (p > sl) { ans += sl; p -= sl; } } ans += p; Console.WriteLine(ans); } static void Main(string[] args) { #if DEBUG TESTINPUT(); #else // TESTINPUT(); INPUT(); #endif OUTPUT(); } static void TESTINPUT() { // programming // sentence } } public class Input { public const long MOD = 1000000007; public const int INF = 1000000007; private static Queue<string> q = new Queue<string>(); private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); } public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); } public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); } public static string NextString() { Confirm(); return q.Dequeue(); } public static double NextDouble() { Confirm(); return double.Parse(q.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(); } } class Util { /// <summary> /// 指定した値以上の先頭のインデクスを返す /// </summary> /// <typeparam name="T">比較する値の型</typeparam> /// <param name="arr">対象の配列(※ソート済みであること)</param> /// <param name="start">開始インデクス [inclusive]</param> /// <param name="end">終了インデクス [exclusive]</param> /// <param name="value">検索する値</param> /// <param name="comparer">比較関数(インターフェイス)</param> /// <returns>指定した値以上の先頭のインデクス</returns> public static int LowerBound<T>(T[] arr, int start, int end, T value, IComparer<T> comparer) { int low = start; int high = end; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (comparer.Compare(arr[mid], value) < 0) low = mid + 1; else high = mid; } return low; } //引数省略のオーバーロード public static int LowerBound<T>(T[] arr, T value) where T : IComparable { return LowerBound<T>(arr, 0, arr.Length, value, Comparer<T>.Default); } }
[["-", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22], ["+", 0, 195, 8, 196, 0, 1, 0, 213, 63, 22]]
8
832
2
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program { const int mod = (int)1e9 + 7; static void Main() { Sc sc = new Sc(); var s = sc.S; var t = sc.S; int n = s.Length; var h = new int [n][]; h[n - 1] = Enumerable.Repeat(-1, 26).ToArray(); long ans = 1; for (int i = n - 2; i >= 0; i--) { h[i] = new int[26]; for (int j = 0; j < 26; j++) { h[i][j] = h[i + 1][j]; } h[i][s[i + 1] - 97] = i + 1; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { h[i][j] = h[(i + 1) % n][j]; } h[i][s[(i + 1) % n] - 97] = (i + 1) % n; } for (int i = 0, j = 0; i < t.Length; i++) { if (i == 0 && t[0] == s[0]) { i++; continue; } if (h[j][t[i] - 97] == -1) { ans = -1; break; } int p = (h[j][t[i] - 97] + n - j) % n; p = p == 0 ? n : p; ans += p; j = h[j][t[i] - 97]; } Console.WriteLine("{0}", ans); } } public class Sc { public int I { get { return int.Parse(Console.ReadLine()); } } public long L { get { return long.Parse(Console.ReadLine()); } } public double D { get { return double.Parse(Console.ReadLine()); } } public string S { get { return Console.ReadLine(); } } public int[] Ia { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } public long[] La { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } } public double[] Da { get { return Array.ConvertAll(Console.ReadLine().Split(), double.Parse); } } public string[] Sa { get { return Console.ReadLine().Split(); } } public object[] Oa { get { return Console.ReadLine().Split(); } } public int[] Ia2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), int.Parse); } } public int[] Ia3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), int.Parse); } public int[] Ia3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), int.Parse); } public long[] La2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), long.Parse); } } public long[] La3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), long.Parse); } public long[] La3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), long.Parse); } public T[] Arr<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(); } return a; } public T[] Arr<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i); } return a; } public T[] Arr<T>(int n, Func<string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(Console.ReadLine().Split()); } return a; } public T[] Arr<T>(int n, Func<int, string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i, Console.ReadLine().Split()); } return a; } }
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program { const int mod = (int)1e9 + 7; static void Main() { Sc sc = new Sc(); var s = sc.S; var t = sc.S; int n = s.Length; var h = new int [n][]; h[n - 1] = Enumerable.Repeat(-1, 26).ToArray(); long ans = 1; for (int i = n - 2; i >= 0; i--) { h[i] = new int[26]; for (int j = 0; j < 26; j++) { h[i][j] = h[i + 1][j]; } h[i][s[i + 1] - 97] = i + 1; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { h[i][j] = h[(i + 1) % n][j]; } h[i][s[(i + 1) % n] - 97] = (i + 1) % n; } for (int i = 0, j = 0; i < t.Length; i++) { // i:t j:h if (i == 0 && t[0] == s[0]) { continue; } if (h[j][t[i] - 97] == -1) { ans = -1; break; } int p = (h[j][t[i] - 97] + n - j) % n; p = p == 0 ? n : p; ans += p; j = h[j][t[i] - 97]; } Console.WriteLine("{0}", ans); } } public class Sc { public int I { get { return int.Parse(Console.ReadLine()); } } public long L { get { return long.Parse(Console.ReadLine()); } } public double D { get { return double.Parse(Console.ReadLine()); } } public string S { get { return Console.ReadLine(); } } public int[] Ia { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } public long[] La { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } } public double[] Da { get { return Array.ConvertAll(Console.ReadLine().Split(), double.Parse); } } public string[] Sa { get { return Console.ReadLine().Split(); } } public object[] Oa { get { return Console.ReadLine().Split(); } } public int[] Ia2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), int.Parse); } } public int[] Ia3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), int.Parse); } public int[] Ia3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), int.Parse); } public long[] La2 { get { return Array.ConvertAll(("0 " + Console.ReadLine() + " 0").Split(), long.Parse); } } public long[] La3(int a) { return Array.ConvertAll((a.ToString() + " " + Console.ReadLine()).Split(), long.Parse); } public long[] La3(bool a, int b, bool c, int d) { return Array.ConvertAll(((a ? b.ToString() + " " : "") + Console.ReadLine() + (c ? " " + d.ToString() : "")) .Split(), long.Parse); } public T[] Arr<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(); } return a; } public T[] Arr<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i); } return a; } public T[] Arr<T>(int n, Func<string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(Console.ReadLine().Split()); } return a; } public T[] Arr<T>(int n, Func<int, string[], T> f) { var a = new T[n]; for (int i = 0; i < n; i++) { a[i] = f(i, Console.ReadLine().Split()); } return a; } }
[["-", 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
1,234
3
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class ABC138E { static public void Solve() { var s = NS; var t = NS; var chCheckert = new bool[26]; var chCheckers = new bool[26]; foreach (var item in s) { chCheckers[item - 'a'] = true; } foreach (var item in t) { chCheckert[item - 'a'] = true; } var ok = true; for (var i = 0; i < 26; i++) { if (chCheckert[i]) { if (!chCheckers[i]) { ok = false; break; } } } if (!ok) { Console.WriteLine(-1); return; } var s2 = Repeat(0, 26).Select(_ => new BT<long>()).ToArray(); for (var i = 0; i < s.Length; i++) { if (chCheckert[s[i] - 'a']) { s2[s[i] - 'a'].Add(i); } } var ans = 0L; foreach (var item in t) { var pos = ans % s.Length; var idxf = s2[item - 'a'].FindUpper(pos + 1); var idx = s2[item - 'a'].Min(); if (idxf.Item1) { ans += idxf.Item2 - pos; } else { ans += s.Length - pos + idx; } } Console.WriteLine(ans + 1); } 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>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = 0; while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); [MethodImpl(MethodImplOptions.AggressiveInlining)] static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); [MethodImpl(MethodImplOptions.AggressiveInlining)] static T Pow<T>(T x, long y) { T a = (dynamic)1; while (y != 0) { if ((y & 1) == 1) a *= (dynamic)x; x *= (dynamic)x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } class PQ<T> where T : IComparable { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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> where TK : IComparable { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; [MethodImpl(MethodImplOptions.AggressiveInlining)] public UF(long s) { d = Repeat(-1L, s).ToArray(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsSame(long x, long y) => Root(x) == Root(y); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Count(long x) => -d[Root(d[x])]; } struct Mod : IEquatable<object> { static public long _mod = 1000000007; long _val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mod(long x) { if (x < _mod && x >= 0) _val = x; else if ((_val = x % _mod) < 0) _val += _mod; } static public implicit operator Mod(long x) => new Mod(x); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator long(Mod x) => x._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator +(Mod x, Mod y) { var t = x._val + y._val; return t >= _mod ? new Mod { _val = t - _mod } : new Mod { _val = t }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator -(Mod x, Mod y) { var t = x._val - y._val; return t < 0 ? new Mod { _val = t + _mod } : new Mod { _val = t }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator *(Mod x, Mod y) => x._val * y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator /(Mod x, Mod y) => x._val * Inverse(y._val); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator ==(Mod x, Mod y) => x._val == y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator !=(Mod x, Mod y) => x._val != y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] bool IEquatable<object>.Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Mod obj) => obj == null ? false : _val == obj._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _val.GetHashCode(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => _val.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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)]; } } struct Mat<T> { T[,] m; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mat(T[,] v) { m = (T[,])v.Clone(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator Mat<T>(T[,] v) => new Mat<T>(v); public T this[int r, int c] { get { return m[r, c]; } set { m[r, c] = value; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator +(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator +(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)b[r, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator -(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator -(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)b[r, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator *(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] *= (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator *(Mat<T> a, Mat<T> b) { var nr = a.m.GetLength(0); var nc = b.m.GetLength(1); var tm = new T[nr, nc]; for (int i = 0; i < nr; ++i) for (int j = 0; j < nc; ++j) tm[i, j] = (dynamic)0; for (int r = 0; r < nr; ++r) for (int c = 0; c < nc; ++c) for (int i = 0; i < a.m.GetLength(1); ++i) tm[r, c] += a[r, i] * (dynamic)b[i, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> Pow(Mat<T> x, long y) { var n = x.m.GetLength(0); var t = (Mat<T>)new T[n, n]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) t[i, j] = (dynamic)(i == j ? 1 : 0); while (y != 0) { if ((y & 1) == 1) t *= x; x *= x; y >>= 1; } return t; } } 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public BT(Comparison<T> _c) { c = _c; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public BT() : this((x, y) => x.CompareTo(y)) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] bool R(Node n) => n != null && !n.b; [MethodImpl(MethodImplOptions.AggressiveInlining)] bool B(Node n) => n != null && n.b; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(T x) { r = A(r, x); r.b = true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Have(T x) { var t = FindUpper(x); return t.Item1 && t.Item2.CompareTo(x) == 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<bool, T> FindUpper(T x, bool findSame = true) { var v = FU(r, x, findSame); return v == null ? Tuple.Create(false, default(T)) : v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Tuple<bool, T> FU(Node n, T x, bool s) { if (n == null) return null; var r = c(x, n.v); if (r < 0) { var v = FU(n.l, x, s); return v == null ? Tuple.Create(true, n.v) : v; } if (r > 0 || !s && r == 0) return FU(n.r, x, s); return Tuple.Create(true, n.v); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<bool, T> FindLower(T x, bool findSame = true) { var v = FL(r, x, findSame); return v == null ? Tuple.Create(false, default(T)) : v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Tuple<bool, T> FL(Node n, T x, bool s) { if (n == null) return null; var r = c(x, n.v); if (r < 0 || !s && r == 0) return FL(n.l, x, s); if (r > 0) { var v = FL(n.r, x, s); return v == null ? Tuple.Create(true, n.v) : v; } return Tuple.Create(true, n.v); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Any() => r != null; [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Count() => r?.c ?? 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] public IEnumerable<T> List() => L(r); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Dict(Func<K, V> _d) { d = _d; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } } 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Deque(int cap = 16) { b = new T[c = cap]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 ImplicitTreep<T, E> where T : IEquatable<T> where E : IEquatable<E> { class Node { public E value; public T acc; public E lazy; public uint priority; public int cnt; public bool rev; public Node l; public Node r; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Node(E val, E ei, T ti) { priority = xorshift; value = val; acc = ti; lazy = ei; cnt = 1; rev = false; l = null; r = null; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node CreateNode(E val) => new Node(val, ei, ti); T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; Node root = null; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ImplicitTreep(T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { ti = _ti; ei = _ei; f = _f; g = _g; h = _h; } [MethodImpl(MethodImplOptions.AggressiveInlining)] int Count(Node n) => n?.cnt ?? 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] T Acc(Node n) => n == null ? ti : n.acc; [MethodImpl(MethodImplOptions.AggressiveInlining)] void UpdateCount(Node n) { if (n != null) n.cnt = 1 + Count(n.l) + Count(n.r); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void UpdateAcc(Node n) { if (n != null) n.acc = f(Acc(n.l), f(g(ti, n.value), Acc(n.r))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Pushup(Node n) { UpdateCount(n); UpdateAcc(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Pushdown(Node n) { if (n?.rev ?? false) { n.rev = false; var t = n.l; n.l = n.r; n.r = t; if (n.l != null) n.l.rev ^= true; if (n.r != null) n.r.rev ^= true; } if (n != null && !n.lazy.Equals(ei)) { Action<Node> apply = t => { if (t == null) return; t.lazy = h(t.lazy, n.lazy); t.acc = g(t.acc, n.lazy); }; apply(n.l); apply(n.r); n.value = h(n.value, n.lazy); n.lazy = ei; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Split(Node n, long key, out Node l, out Node r) { if (n == null) { l = r = null; return; } Pushdown(n); var implicitKey = Count(n.l) + 1; if (key < implicitKey) { Split(n.l, key, out l, out n.l); r = n; } else { Split(n.r, key - implicitKey, out n.r, out r); l = n; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Insert(ref Node n, long key, Node x) { if (n == null) { n = x; return; } Pushdown(n); if (Count(n.l) == key || x.priority > n.priority) { Split(n, key, out x.l, out x.r); n = x; } else { if (Count(n.l) > key) { Insert(ref n.l, key, x); } else { Insert(ref n.r, key - Count(n.l) - 1, x); } } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Merge(out Node n, Node l, Node r) { Pushdown(l); Pushdown(r); if (l == null || r == null) { n = l != null ? l : r; } else if (l.priority > r.priority) { Merge(out l.r, l.r, r); n = l; } else { Merge(out r.l, l, r.l); n = r; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Erase(ref Node n, long key) { Pushdown(n); if (Count(n.l) == key) { Merge(out n, n.l, n.r); } else { if (Count(n.l) > key) { Erase(ref n.l, key); } else { Erase(ref n.r, key - Count(n.l) - 1); } } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Update(Node n, long l, long r, E x) { Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); n2.lazy = h(n2.lazy, x); Merge(out n2, n2, n3); Merge(out n, n1, n2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] T Query(Node n, long l, long r) { Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); var ret = Acc(n2); Merge(out n2, n2, n3); Merge(out n, n1, n2); return ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] long Find(Node n, T x, long offset, bool left = true) { if (f(Acc(n), x).Equals(x)) { return -1; } else { if (left) { if (n.l != null && !f(n.l.acc, x).Equals(x)) { return Find(n.l, x, offset, left); } else { return (!f(g(ti, n.value), x).Equals(x)) ? offset + Count(n.l) : Find(n.r, x, offset + Count(n.l) + 1, left); } } else { if (n.r != null && !f(n.r.acc, x).Equals(x)) { return Find(n.r, x, offset + Count(n.l) + 1, left); } else { return (!f(g(ti, n.value), x).Equals(x)) ? offset + Count(n.l) : Find(n.l, x, offset, left); } } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Reverse(Node n, long l, long r) { if (l >= r) return; Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); n2.rev ^= true; Merge(out n2, n2, n3); Merge(out n, n1, n2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Rotate(Node n, long l, long m, long r) { Reverse(n, l, r); Reverse(n, l, l + r - m); Reverse(n, l + r - m, r); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Insert(long idx, E x) => Insert(ref root, idx, CreateNode(x)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(E x) => Insert(Count(), x); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(long l, long r, E x) => Update(root, l, r, x); [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query(long l, long r) => Query(root, l, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Find(long l, long r, T x, bool left = true) { Node n1, n2, n3; Split(root, l, out n1, out n2); Split(n2, r - l, out n2, out n3); var ret = Find(n2, x, l, left); Merge(out n2, n2, n3); Merge(out root, n1, n2); return ret < 0 ? r : ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Erase(long idx) => Erase(ref root, idx); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reverse(long l, long r) => Reverse(root, l, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Rotate(long l, long m, long r) => Rotate(root, l, m, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Count() => Count(root); public E this[long idx] { get { Node n1, n2, n3; Split(root, idx + 1, out n1, out n2); Split(n1, idx, out n1, out n3); var ret = n3.value; Merge(out n1, n1, n3); Merge(out root, n1, n2); return ret; } private set {} } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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> where T : IEquatable<T> where E : IEquatable<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; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using static System.Math; using System.Text; using System.Threading; namespace Program { public static class ABC138E { static public void Solve() { var s = NS; var t = NS; var chCheckert = new bool[26]; var chCheckers = new bool[26]; foreach (var item in s) { chCheckers[item - 'a'] = true; } foreach (var item in t) { chCheckert[item - 'a'] = true; } var ok = true; for (var i = 0; i < 26; i++) { if (chCheckert[i]) { if (!chCheckers[i]) { ok = false; break; } } } if (!ok) { Console.WriteLine(-1); return; } var s2 = Repeat(0, 26).Select(_ => new BT<long>()).ToArray(); for (var i = 0; i < s.Length; i++) { if (chCheckert[s[i] - 'a']) { s2[s[i] - 'a'].Add(i); } } var ans = -1L; foreach (var item in t) { var pos = ans % s.Length; var idxf = s2[item - 'a'].FindUpper(pos + 1); var idx = s2[item - 'a'].Min(); if (idxf.Item1) { ans += idxf.Item2 - pos; } else { ans += s.Length - pos + idx; } } Console.WriteLine(ans + 1); } 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>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c); [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l) where T : IComparable { Array.Sort(l, (x, y) => y.CompareTo(x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static void RevSort<T>(T[] l, Comparison<T> comp) where T : IComparable { Array.Sort(l, (x, y) => comp(y, x)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); [MethodImpl(MethodImplOptions.AggressiveInlining)] static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = 0; while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); [MethodImpl(MethodImplOptions.AggressiveInlining)] static Mat<T> Pow<T>(Mat<T> x, long y) => Mat<T>.Pow(x, y); [MethodImpl(MethodImplOptions.AggressiveInlining)] static T Pow<T>(T x, long y) { T a = (dynamic)1; while (y != 0) { if ((y & 1) == 1) a *= (dynamic)x; x *= (dynamic)x; y >>= 1; } return a; } static List<long> _fact = new List<long>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void _B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static Func<TR> Lambda<TR>(Func<Func<TR>, TR> f) { Func<TR> t = () => default(TR); return t = () => f(t); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } class PQ<T> where T : IComparable { List<T> h; Comparison<T> c; public T Peek => h[0]; public int Count => h.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, Comparison<T> c, bool asc = true) { h = new List<T>(cap); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<T> c, bool asc = true) { h = new List<T>(); this.c = asc ? c : (x, y) => c(y, x); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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> where TK : IComparable { PQ<Tuple<TK, TV>> q; public Tuple<TK, TV> Peek => q.Peek; public int Count => q.Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(Comparison<TK> c, bool asc = true) { q = new PQ<Tuple<TK, TV>>((x, y) => c(x.Item1, y.Item1), asc); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(int cap, bool asc = true) : this(cap, (x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public PQ(bool asc = true) : this((x, y) => x.CompareTo(y), asc) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Push(TK k, TV v) => q.Push(Tuple.Create(k, v)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<TK, TV> Pop() => q.Pop(); } public class UF { long[] d; [MethodImpl(MethodImplOptions.AggressiveInlining)] public UF(long s) { d = Repeat(-1L, s).ToArray(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsSame(long x, long y) => Root(x) == Root(y); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Root(long x) => d[x] < 0 ? x : d[x] = Root(d[x]); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Count(long x) => -d[Root(d[x])]; } struct Mod : IEquatable<object> { static public long _mod = 1000000007; long _val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mod(long x) { if (x < _mod && x >= 0) _val = x; else if ((_val = x % _mod) < 0) _val += _mod; } static public implicit operator Mod(long x) => new Mod(x); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator long(Mod x) => x._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator +(Mod x, Mod y) { var t = x._val + y._val; return t >= _mod ? new Mod { _val = t - _mod } : new Mod { _val = t }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator -(Mod x, Mod y) { var t = x._val - y._val; return t < 0 ? new Mod { _val = t + _mod } : new Mod { _val = t }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator *(Mod x, Mod y) => x._val * y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mod operator /(Mod x, Mod y) => x._val * Inverse(y._val); [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator ==(Mod x, Mod y) => x._val == y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] static public bool operator !=(Mod x, Mod y) => x._val != y._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] bool IEquatable<object>.Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj == null ? false : Equals((Mod)obj); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Mod obj) => obj == null ? false : _val == obj._val; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _val.GetHashCode(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => _val.ToString(); static List<Mod> _fact = new List<Mod>() { 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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)]; } } struct Mat<T> { T[,] m; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Mat(T[,] v) { m = (T[,])v.Clone(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public implicit operator Mat<T>(T[,] v) => new Mat<T>(v); public T this[int r, int c] { get { return m[r, c]; } set { m[r, c] = value; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator +(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator +(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] += (dynamic)b[r, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator -(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator -(Mat<T> a, Mat<T> b) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] -= (dynamic)b[r, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator *(Mat<T> a, T x) { var tm = (T[,])a.m.Clone(); for (int r = 0; r < tm.GetLength(0); ++r) for (int c = 0; c < tm.GetLength(1); ++c) tm[r, c] *= (dynamic)x; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> operator *(Mat<T> a, Mat<T> b) { var nr = a.m.GetLength(0); var nc = b.m.GetLength(1); var tm = new T[nr, nc]; for (int i = 0; i < nr; ++i) for (int j = 0; j < nc; ++j) tm[i, j] = (dynamic)0; for (int r = 0; r < nr; ++r) for (int c = 0; c < nc; ++c) for (int i = 0; i < a.m.GetLength(1); ++i) tm[r, c] += a[r, i] * (dynamic)b[i, c]; return tm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static public Mat<T> Pow(Mat<T> x, long y) { var n = x.m.GetLength(0); var t = (Mat<T>)new T[n, n]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) t[i, j] = (dynamic)(i == j ? 1 : 0); while (y != 0) { if ((y & 1) == 1) t *= x; x *= x; y >>= 1; } return t; } } 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tree(List<long>[] p_, long r_) { N = p_.Length; p = p_; r = r_; lca = false; euler = false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<long, long>[] BFSLeaf() => BFSRoot().Reverse().ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public BT(Comparison<T> _c) { c = _c; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public BT() : this((x, y) => x.CompareTo(y)) {} [MethodImpl(MethodImplOptions.AggressiveInlining)] bool R(Node n) => n != null && !n.b; [MethodImpl(MethodImplOptions.AggressiveInlining)] bool B(Node n) => n != null && n.b; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node RtLR(Node n) { n.l = RtL(n.l); return RtR(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node RtRL(Node n) { n.r = RtR(n.r); return RtL(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(T x) { r = A(r, x); r.b = true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Remove(T x) { r = Rm(r, x); if (r != null) r.b = true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Have(T x) { var t = FindUpper(x); return t.Item1 && t.Item2.CompareTo(x) == 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<bool, T> FindUpper(T x, bool findSame = true) { var v = FU(r, x, findSame); return v == null ? Tuple.Create(false, default(T)) : v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Tuple<bool, T> FU(Node n, T x, bool s) { if (n == null) return null; var r = c(x, n.v); if (r < 0) { var v = FU(n.l, x, s); return v == null ? Tuple.Create(true, n.v) : v; } if (r > 0 || !s && r == 0) return FU(n.r, x, s); return Tuple.Create(true, n.v); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Tuple<bool, T> FindLower(T x, bool findSame = true) { var v = FL(r, x, findSame); return v == null ? Tuple.Create(false, default(T)) : v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] Tuple<bool, T> FL(Node n, T x, bool s) { if (n == null) return null; var r = c(x, n.v); if (r < 0 || !s && r == 0) return FL(n.l, x, s); if (r > 0) { var v = FL(n.r, x, s); return v == null ? Tuple.Create(true, n.v) : v; } return Tuple.Create(true, n.v); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Min() { Node n = r, p = null; while (n != null) { p = n; n = n.l; } return p == null ? default(T) : p.v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Max() { Node n = r, p = null; while (n != null) { p = n; n = n.r; } return p == null ? default(T) : p.v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Any() => r != null; [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Count() => r?.c ?? 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] public IEnumerable<T> List() => L(r); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Dict(Func<K, V> _d) { d = _d; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } } } 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; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Deque(int cap = 16) { b = new T[c = cap]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] int gi(int i) { if (i >= c) throw new Exception(); var r = o + i; return r >= c ? r - c : r; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushFront(T x) { if (Count == c) e(); if (--o < 0) o += b.Length; b[o] = x; ++Count; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PushBack(T x) { if (Count == c) e(); var i = o + Count++; b[i >= c ? i - c : i] = x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T PopBack() { if (Count == 0) throw new Exception(); return b[gi(--Count)]; } public T Back => b[gi(Count - 1)]; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 ImplicitTreep<T, E> where T : IEquatable<T> where E : IEquatable<E> { class Node { public E value; public T acc; public E lazy; public uint priority; public int cnt; public bool rev; public Node l; public Node r; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Node(E val, E ei, T ti) { priority = xorshift; value = val; acc = ti; lazy = ei; cnt = 1; rev = false; l = null; r = null; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] Node CreateNode(E val) => new Node(val, ei, ti); T ti; E ei; Func<T, T, T> f; Func<T, E, T> g; Func<E, E, E> h; Node root = null; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ImplicitTreep(T _ti, E _ei, Func<T, T, T> _f, Func<T, E, T> _g, Func<E, E, E> _h) { ti = _ti; ei = _ei; f = _f; g = _g; h = _h; } [MethodImpl(MethodImplOptions.AggressiveInlining)] int Count(Node n) => n?.cnt ?? 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] T Acc(Node n) => n == null ? ti : n.acc; [MethodImpl(MethodImplOptions.AggressiveInlining)] void UpdateCount(Node n) { if (n != null) n.cnt = 1 + Count(n.l) + Count(n.r); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void UpdateAcc(Node n) { if (n != null) n.acc = f(Acc(n.l), f(g(ti, n.value), Acc(n.r))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Pushup(Node n) { UpdateCount(n); UpdateAcc(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Pushdown(Node n) { if (n?.rev ?? false) { n.rev = false; var t = n.l; n.l = n.r; n.r = t; if (n.l != null) n.l.rev ^= true; if (n.r != null) n.r.rev ^= true; } if (n != null && !n.lazy.Equals(ei)) { Action<Node> apply = t => { if (t == null) return; t.lazy = h(t.lazy, n.lazy); t.acc = g(t.acc, n.lazy); }; apply(n.l); apply(n.r); n.value = h(n.value, n.lazy); n.lazy = ei; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Split(Node n, long key, out Node l, out Node r) { if (n == null) { l = r = null; return; } Pushdown(n); var implicitKey = Count(n.l) + 1; if (key < implicitKey) { Split(n.l, key, out l, out n.l); r = n; } else { Split(n.r, key - implicitKey, out n.r, out r); l = n; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Insert(ref Node n, long key, Node x) { if (n == null) { n = x; return; } Pushdown(n); if (Count(n.l) == key || x.priority > n.priority) { Split(n, key, out x.l, out x.r); n = x; } else { if (Count(n.l) > key) { Insert(ref n.l, key, x); } else { Insert(ref n.r, key - Count(n.l) - 1, x); } } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Merge(out Node n, Node l, Node r) { Pushdown(l); Pushdown(r); if (l == null || r == null) { n = l != null ? l : r; } else if (l.priority > r.priority) { Merge(out l.r, l.r, r); n = l; } else { Merge(out r.l, l, r.l); n = r; } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Erase(ref Node n, long key) { Pushdown(n); if (Count(n.l) == key) { Merge(out n, n.l, n.r); } else { if (Count(n.l) > key) { Erase(ref n.l, key); } else { Erase(ref n.r, key - Count(n.l) - 1); } } Pushup(n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Update(Node n, long l, long r, E x) { Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); n2.lazy = h(n2.lazy, x); Merge(out n2, n2, n3); Merge(out n, n1, n2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] T Query(Node n, long l, long r) { Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); var ret = Acc(n2); Merge(out n2, n2, n3); Merge(out n, n1, n2); return ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] long Find(Node n, T x, long offset, bool left = true) { if (f(Acc(n), x).Equals(x)) { return -1; } else { if (left) { if (n.l != null && !f(n.l.acc, x).Equals(x)) { return Find(n.l, x, offset, left); } else { return (!f(g(ti, n.value), x).Equals(x)) ? offset + Count(n.l) : Find(n.r, x, offset + Count(n.l) + 1, left); } } else { if (n.r != null && !f(n.r.acc, x).Equals(x)) { return Find(n.r, x, offset + Count(n.l) + 1, left); } else { return (!f(g(ti, n.value), x).Equals(x)) ? offset + Count(n.l) : Find(n.l, x, offset, left); } } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Reverse(Node n, long l, long r) { if (l >= r) return; Node n1, n2, n3; Split(n, l, out n1, out n2); Split(n2, r - l, out n2, out n3); n2.rev ^= true; Merge(out n2, n2, n3); Merge(out n, n1, n2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Rotate(Node n, long l, long m, long r) { Reverse(n, l, r); Reverse(n, l, l + r - m); Reverse(n, l + r - m, r); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Insert(long idx, E x) => Insert(ref root, idx, CreateNode(x)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(E x) => Insert(Count(), x); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(long l, long r, E x) => Update(root, l, r, x); [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query(long l, long r) => Query(root, l, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Find(long l, long r, T x, bool left = true) { Node n1, n2, n3; Split(root, l, out n1, out n2); Split(n2, r - l, out n2, out n3); var ret = Find(n2, x, l, left); Merge(out n2, n2, n3); Merge(out root, n1, n2); return ret < 0 ? r : ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Erase(long idx) => Erase(ref root, idx); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reverse(long l, long r) => Reverse(root, l, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Rotate(long l, long m, long r) => Rotate(root, l, m, r); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Count() => Count(root); public E this[long idx] { get { Node n1, n2, n3; Split(root, idx + 1, out n1, out n2); Split(n1, idx, out n1, out n3); var ret = n3.value; Merge(out n1, n1, n3); Merge(out root, n1, n2); return ret; } private set {} } } class SegTree<T> { int n; T ti; Func<T, T, T> f; T[] dat; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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> where T : IEquatable<T> where E : IEquatable<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; [MethodImpl(MethodImplOptions.AggressiveInlining)] 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(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] T Reflect(long i) => laz[i].Equals(ei) ? dat[i] : g(dat[i], laz[i]); [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Thrust(long i) { for (var j = height; j > 0; j--) Eval(i >> j); } [MethodImpl(MethodImplOptions.AggressiveInlining)] void Recalc(long i) { while ((i >>= 1) > 0) dat[i] = f(Reflect((i << 1) | 0), Reflect((i << 1) | 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } } } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203]]
8
12,197
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.ABC_138 { class E { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); string t = Read(); List<int>[] poses = new List<int>[26]; for (int i = 0; i < 26; i++) poses[i] = new List<int>(); for (int i = 0; i < s.Length; i++) { poses[s[i] - 'a'].Add(i); } for (int i = 0; i < t.Length; i++) { if (poses[t[i] - 'a'].Count == 0) { WriteLine(-1); return; } } for (int i = 0; i < 26; i++) poses[i].Add(s.Length + 10); long res = 0; int pos = 0; for (int i = 0; i < t.Length; i++) { int bottom = -1; int c = t[i] - 'a'; int top = poses[c].Count - 1; while (bottom + 1 < top) { int mid = (bottom + top + 1) / 2; if (pos < poses[c][mid]) top = mid; else bottom = mid; } if (top == poses[c].Count - 1) { res += s.Length - pos; res += poses[c][0]; pos = poses[c][0]; } else { res += poses[c][top] - pos; pos = poses[c][top]; } } WriteLine(res + 1); } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } 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.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.ABC_138 { class E { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); string t = Read(); List<int>[] poses = new List<int>[26]; for (int i = 0; i < 26; i++) poses[i] = new List<int>(); for (int i = 0; i < s.Length; i++) { poses[s[i] - 'a'].Add(i); } for (int i = 0; i < t.Length; i++) { if (poses[t[i] - 'a'].Count == 0) { WriteLine(-1); return; } } for (int i = 0; i < 26; i++) poses[i].Add(s.Length + 10); long res = 0; int pos = -1; for (int i = 0; i < t.Length; i++) { int bottom = -1; int c = t[i] - 'a'; int top = poses[c].Count - 1; while (bottom + 1 < top) { int mid = (bottom + top + 1) / 2; if (pos < poses[c][mid]) top = mid; else bottom = mid; } if (top == poses[c].Count - 1) { res += s.Length - pos; res += poses[c][0]; pos = poses[c][0]; } else { res += poses[c][top] - pos; pos = poses[c][top]; } } WriteLine(res); } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } 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); } } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 33], ["+", 0, 198, 0, 200, 0, 212, 0, 241, 0, 203], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
626
5
using System; using System.Linq; using System.Collections.Generic; namespace Algorithm { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var count = 0; var cur = ""; for (var i = 0; i < s.Length; i++) { if (cur != s[i].ToString()) { cur = s[i].ToString(); count++; } else { cur = s[i].ToString(); if (i < s.Length - 2) { cur += s[i + 1].ToString(); count++; i++; } } } Console.WriteLine(count); } } }
using System; using System.Linq; using System.Collections.Generic; namespace Algorithm { class Program { static void Main(string[] args) { var s = Console.ReadLine(); var count = 0; var cur = ""; for (var i = 0; i < s.Length; i++) { if (cur != s[i].ToString()) { cur = s[i].ToString(); count++; } else { cur = s[i].ToString(); if (i < s.Length - 1) { cur += s[i + 1].ToString(); count++; i++; } } } Console.WriteLine(count); } } }
[["-", 75, 196, 0, 57, 15, 16, 12, 16, 12, 203], ["+", 75, 196, 0, 57, 15, 16, 12, 16, 12, 203]]
8
153
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.AGC_037 { class A { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); int cnt = 0; bool nowDouble = true; for (int i = 0; i < s.Length; i++) { if (nowDouble) { nowDouble = false; cnt++; } else { if (s[i] == s[i - 1]) { nowDouble = true; i++; if (i < s.Length - 1) cnt++; } else { cnt++; } } } WriteLine(cnt); } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } 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.Linq; using System.Text; using static System.Console; using static System.Math; namespace AtTest.AGC_037 { class A { static void Main(string[] args) { var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false }; SetOut(sw); Method(args); Out.Flush(); } static void Method(string[] args) { string s = Read(); int cnt = 0; bool nowDouble = true; for (int i = 0; i < s.Length; i++) { if (nowDouble) { nowDouble = false; cnt++; } else { if (s[i] == s[i - 1]) { nowDouble = true; if (i < s.Length - 1) cnt++; i++; } else { cnt++; } } } WriteLine(cnt); } private static string Read() { return ReadLine(); } private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); } 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); } } }
[["-", 0, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["-", 0, 57, 64, 196, 0, 1, 0, 223, 0, 29], ["-", 75, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 64, 196, 0, 57, 64, 1, 0, 223, 0, 29], ["+", 0, 57, 64, 196, 0, 57, 64, 1, 0, 35], ["+", 0, 57, 64, 196, 0, 1, 0, 223, 0, 22]]
8
380
6
using System; class Program { static void Main(string[] args) { string s; s = Console.ReadLine(); int index = 0; int k = 0; string sub = string.Empty; while (index < s.Length) { if (sub.Equals(s.Substring(index, 1))) { if (index + 2 < s.Length) { sub = s.Substring(index, 2); index += 2; k++; } else { index += 1; } } else { sub = s.Substring(index, 1); index += 1; k++; } } Console.WriteLine(k); } }
using System; class Program { static void Main(string[] args) { string s; s = Console.ReadLine(); int index = 0; int k = 0; string sub = string.Empty; while (index < s.Length) { if (sub.Equals(s.Substring(index, 1))) { if (index + 2 <= s.Length) { sub = s.Substring(index, 2); index += 2; k++; } else { index += 1; } } else { sub = s.Substring(index, 1); index += 1; k++; } } Console.WriteLine(k); } }
[["-", 0, 57, 64, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 17, 19]]
8
139
2
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 { public void Run() { var n = ni(); var s = ns(); int r = 0, g = 0, b = 0; int rg = 0, gb = 0, rb = 0; long ans = 1; int mod = 998244353; foreach (var c in s) { if (c == 'R') { r++; } else if (c == 'G') { g++; } else { b++; } if (rg > 0 && b > 0) { ans *= rg * b; ans %= mod; rg--; b--; } else if (gb > 0 && r > 0) { ans *= gb * r; ans %= mod; gb--; r--; } else if (rb > 0 && g > 0) { ans *= rb * g; ans %= mod; rb--; g--; } else if (r > 0 && g > 0) { ans *= r * g; ans %= mod; rg++; r--; g--; } else if (b > 0 && g > 0) { ans *= b * g; ans %= mod; rb++; b--; g--; } else if (b > 0 && r > 0) { ans *= b * r; ans %= mod; rb++; b--; r--; } } for (int i = 1; i <= n; i++) { ans *= i; ans %= mod; } 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 CultureInfo ci = CultureInfo.InvariantCulture; private readonly char[] buffer = new char[2 * 1024]; private int cursor = 0, length = 0; private string Token; private readonly StringBuilder sb = new StringBuilder(1024); public Scanner() : this(Console.In) {} public Scanner(TextReader reader) { Reader = reader; } public int NextInt() { return (int)NextLong(); } public long NextLong() { var s = Next(); long r = 0; int i = 0; bool negative = false; if (s[i] == '-') { negative = true; i++; } for (; i < s.Length; i++) { r = r * 10 + (s[i] - '0'); #if DEBUG if (!char.IsDigit(s[i])) throw new FormatException(); #endif } return negative ? -r : r; } 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 (Token == null) { if (!StockToken()) { throw new InvalidOperationException(); } } var token = Token; Token = null; return token; } public bool HasNext() { if (Token != null) { return true; } return StockToken(); } private bool StockToken() { while (true) { sb.Clear(); while (true) { if (cursor >= length) { cursor = 0; if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) { break; } } var c = buffer[cursor++]; if (33 <= c && c <= 126) { sb.Append(c); } else { if (sb.Length > 0) break; } } if (sb.Length > 0) { Token = sb.ToString(); return true; } return false; } } }
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 { public void Run() { var n = ni(); var s = ns(); int r = 0, g = 0, b = 0; int rg = 0, gb = 0, rb = 0; long ans = 1; int mod = 998244353; foreach (var c in s) { if (c == 'R') { r++; } else if (c == 'G') { g++; } else { b++; } if (rg > 0 && b > 0) { ans *= rg * b; ans %= mod; rg--; b--; } else if (gb > 0 && r > 0) { ans *= gb * r; ans %= mod; gb--; r--; } else if (rb > 0 && g > 0) { ans *= rb * g; ans %= mod; rb--; g--; } else if (r > 0 && g > 0) { ans *= r * g; ans %= mod; rg++; r--; g--; } else if (b > 0 && g > 0) { ans *= b * g; ans %= mod; gb++; b--; g--; } else if (b > 0 && r > 0) { ans *= b * r; ans %= mod; rb++; b--; r--; } } for (int i = 1; i <= n; i++) { ans *= i; ans %= mod; } 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 CultureInfo ci = CultureInfo.InvariantCulture; private readonly char[] buffer = new char[2 * 1024]; private int cursor = 0, length = 0; private string Token; private readonly StringBuilder sb = new StringBuilder(1024); public Scanner() : this(Console.In) {} public Scanner(TextReader reader) { Reader = reader; } public int NextInt() { return (int)NextLong(); } public long NextLong() { var s = Next(); long r = 0; int i = 0; bool negative = false; if (s[i] == '-') { negative = true; i++; } for (; i < s.Length; i++) { r = r * 10 + (s[i] - '0'); #if DEBUG if (!char.IsDigit(s[i])) throw new FormatException(); #endif } return negative ? -r : r; } 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 (Token == null) { if (!StockToken()) { throw new InvalidOperationException(); } } var token = Token; Token = null; return token; } public bool HasNext() { if (Token != null) { return true; } return StockToken(); } private bool StockToken() { while (true) { sb.Clear(); while (true) { if (cursor >= length) { cursor = 0; if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) { break; } } var c = buffer[cursor++]; if (33 <= c && c <= 126) { sb.Append(c); } else { if (sb.Length > 0) break; } } if (sb.Length > 0) { Token = sb.ToString(); return true; } return false; } } }
[["-", 75, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["+", 75, 57, 64, 196, 0, 1, 0, 223, 0, 22]]
8
1,374
2
using System; using System.Collections.Generic; using System.Text; using System.Numerics; using System.IO; namespace SolutionCS { class Program { const int M = 998244353; static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string s = Console.ReadLine(); long onlyR = 0; long onlyG = 0; long onlyB = 0; long RG = 0; long RB = 0; long GB = 0; long noball = N - 1; long ans = N; switch (s[0]) { case 'R': onlyR++; break; case 'G': onlyG++; break; case 'B': onlyB++; break; } for (int i = 1; i < N * 3; i++) { Char c = s[i]; switch (c) { case 'R': if (GB == 0) { if (onlyG != 0) { ans *= onlyG; RG++; onlyG--; } else if (onlyB != 0) { ans *= onlyB; RB++; onlyB--; } else { ans *= noball; noball--; onlyR++; } } else { ans *= GB; GB--; } break; case 'G': if (RB == 0) { if (onlyR != 0) { ans *= onlyR; onlyR--; RG++; } else if (onlyB != 0) { ans *= onlyB; RB++; onlyB--; } else { ans *= noball; noball--; onlyG++; } } else { ans *= RB; RB--; } break; case 'B': if (RG == 0) { if (onlyR != 0) { ans *= onlyR; onlyR--; RG++; } else if (onlyG != 0) { ans *= onlyG; GB++; onlyG--; } else { ans *= noball; noball--; onlyB++; } } else { ans *= RG; RG--; } break; } ans %= M; } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Text; using System.Numerics; using System.IO; namespace SolutionCS { class Program { const int M = 998244353; static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string s = Console.ReadLine(); long onlyR = 0; long onlyG = 0; long onlyB = 0; long RG = 0; long RB = 0; long GB = 0; long noball = N - 1; long ans = N; switch (s[0]) { case 'R': onlyR++; break; case 'G': onlyG++; break; case 'B': onlyB++; break; } for (int i = 1; i < N * 3; i++) { Char c = s[i]; switch (c) { case 'R': if (GB == 0) { if (onlyG != 0) { ans *= onlyG; RG++; onlyG--; } else if (onlyB != 0) { ans *= onlyB; RB++; onlyB--; } else { ans *= noball; noball--; onlyR++; } } else { ans *= GB; GB--; } break; case 'G': if (RB == 0) { if (onlyR != 0) { ans *= onlyR; onlyR--; RG++; } else if (onlyB != 0) { ans *= onlyB; GB++; onlyB--; } else { ans *= noball; noball--; onlyG++; } } else { ans *= RB; RB--; } break; case 'B': if (RG == 0) { if (onlyR != 0) { ans *= onlyR; onlyR--; RB++; } else if (onlyG != 0) { ans *= onlyG; GB++; onlyG--; } else { ans *= noball; noball--; onlyB++; } } else { ans *= RG; RG--; } break; } ans %= M; } Console.WriteLine(ans); } } }
[["-", 75, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["+", 75, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["-", 0, 57, 64, 196, 0, 1, 0, 223, 0, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 223, 0, 22]]
8
422
4
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(); string[] str2 = Console.ReadLine().Split(); int[] A = new int[N]; int[] B = new int[N]; var pq = new PriorityQueue<int>((x, y) => B[y] - B[x]); for (var i = 0; i < N; i++) { A[i] = int.Parse(str[i]); B[i] = int.Parse(str2[i]); pq.Enqueue(i); } int ct = 0; int[] F = new int[N]; int[] G = new int[N]; while (pq.Count > 0) { int a1 = pq.Dequeue(); bool flag = false; int a0 = (a1 + N - 1) % N; int a2 = (a1 + 1) % N; while (B[a0] + B[a2] <= B[a1] && B[a1] - B[a0] - B[a2] >= A[a1]) { int cta1 = (B[a1] - B[a0] - B[a2] - 1) / (B[a0] + B[a2]) + 1; int cta2 = (B[a1] - A[a1] - 1) / (B[a0] + B[a2]) + 1; int cta = Math.Min(cta1, cta2); B[a1] -= (B[a0] + B[a2]) * cta; ct += cta; flag = true; } if (flag) { if (F[a0] == 1 && G[a0] == 0) { F[a0] = 0; pq.Enqueue(a0); } if (F[a2] == 1 && G[a2] == 0) { pq.Enqueue(a2); F[a2] = 0; } if (B[a1] == A[a1]) { G[a1] = 1; } else { pq.Enqueue(a1); } } else { F[a1] = 1; } } for (var i = 0; i < N; i++) { if (A[i] != B[i]) { ct = -1; } } Console.WriteLine(ct); } } class PriorityQueue<T> { private readonly List<T> heap; private readonly Comparison<T> compare; private int size; public PriorityQueue() : this(Comparer<T>.Default) {} public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) {} public PriorityQueue(Comparison<T> comparison) : this(16, comparison) {} public PriorityQueue(int capacity, Comparison<T> comparison) { this.heap = new List<T>(capacity); this.compare = comparison; } public void Enqueue(T item) { this.heap.Add(item); var i = size++; while (i > 0) { var p = (i - 1) >> 1; if (compare(this.heap[p], item) <= 0) break; this.heap[i] = heap[p]; i = p; } this.heap[i] = item; } public T Dequeue() { var ret = this.heap[0]; var x = this.heap[--size]; var i = 0; while ((i << 1) + 1 < size) { var a = (i << 1) + 1; var b = (i << 1) + 2; if (b < size && compare(heap[b], heap[a]) < 0) a = b; if (compare(heap[a], x) >= 0) break; heap[i] = heap[a]; i = a; } heap[i] = x; heap.RemoveAt(size); return ret; } public T Peek() { return heap[0]; } public int Count { get { return size; } } public bool Any() { return size > 0; } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(); string[] str2 = Console.ReadLine().Split(); int[] A = new int[N]; int[] B = new int[N]; var pq = new PriorityQueue<int>((x, y) => B[y] - B[x]); for (var i = 0; i < N; i++) { A[i] = int.Parse(str[i]); B[i] = int.Parse(str2[i]); pq.Enqueue(i); } long ct = 0; int[] F = new int[N]; int[] G = new int[N]; while (pq.Count > 0) { int a1 = pq.Dequeue(); bool flag = false; int a0 = (a1 + N - 1) % N; int a2 = (a1 + 1) % N; while (B[a0] + B[a2] <= B[a1] && B[a1] - B[a0] - B[a2] >= A[a1]) { int cta1 = (B[a1] - B[a0] - B[a2] - 1) / (B[a0] + B[a2]) + 1; int cta2 = (B[a1] - A[a1] - 1) / (B[a0] + B[a2]) + 1; int cta = Math.Min(cta1, cta2); B[a1] -= (B[a0] + B[a2]) * cta; ct += cta; flag = true; } if (flag) { if (F[a0] == 1 && G[a0] == 0) { F[a0] = 0; pq.Enqueue(a0); } if (F[a2] == 1 && G[a2] == 0) { pq.Enqueue(a2); F[a2] = 0; } if (B[a1] == A[a1]) { G[a1] = 1; } else { pq.Enqueue(a1); } } else { F[a1] = 1; } } for (var i = 0; i < N; i++) { if (A[i] != B[i]) { ct = -1; } } Console.WriteLine(ct); } } class PriorityQueue<T> { private readonly List<T> heap; private readonly Comparison<T> compare; private int size; public PriorityQueue() : this(Comparer<T>.Default) {} public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) {} public PriorityQueue(Comparison<T> comparison) : this(16, comparison) {} public PriorityQueue(int capacity, Comparison<T> comparison) { this.heap = new List<T>(capacity); this.compare = comparison; } public void Enqueue(T item) { this.heap.Add(item); var i = size++; while (i > 0) { var p = (i - 1) >> 1; if (compare(this.heap[p], item) <= 0) break; this.heap[i] = heap[p]; i = p; } this.heap[i] = item; } public T Dequeue() { var ret = this.heap[0]; var x = this.heap[--size]; var i = 0; while ((i << 1) + 1 < size) { var a = (i << 1) + 1; var b = (i << 1) + 2; if (b < size && compare(heap[b], heap[a]) < 0) a = b; if (compare(heap[a], x) >= 0) break; heap[i] = heap[a]; i = a; } heap[i] = x; heap.RemoveAt(size); return ret; } public T Peek() { return heap[0]; } public int Count { get { return size; } } public bool Any() { return size > 0; } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199]]
8
920
2
using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; // using System.Threading.Tasks; // using System.Text.RegularExpressions; // using System.Globalization; // using System.Diagnostics; using static System.Console; using System.Numerics; using static System.Math; using pair = Pair<int, int>; class Program { static void Main() { // SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); new Program().solve(); Out.Flush(); } readonly Scanner cin = new Scanner(); readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑ readonly int mod = 1000000007; readonly int dom = 998244353; 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; } void solve() { int A = cin.nextint; int B = cin.nextint; int C = cin.nextint; WriteLine(C - (A - B)); } } static class Ex { public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values)); public static string concat<T>(this IEnumerable<T> values) => string.Concat(values); public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); } public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) < 0) low = mid + 1; else high = mid; } return low; } public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) <= 0) low = mid + 1; else high = mid; } return low; } } class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T f; public U s; public Pair(T f, U s) { this.f = f; this.s = s; } public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s); public override string ToString() => $"{f} {s}"; } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan => ReadLine().Split(); public int[] scanint => Array.ConvertAll(scan, int.Parse); public long[] scanlong => Array.ConvertAll(scan, long.Parse); public double[] scandouble => Array.ConvertAll(scan, double.Parse); public string next { get { if (i < s.Length) return s[i++]; string st = ReadLine(); while (st == "") st = ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint => int.Parse(next); public long nextlong => long.Parse(next); public double nextdouble => double.Parse(next); }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; // using System.Threading.Tasks; // using System.Text.RegularExpressions; // using System.Globalization; // using System.Diagnostics; using static System.Console; using System.Numerics; using static System.Math; using pair = Pair<int, int>; class Program { static void Main() { // SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); new Program().solve(); Out.Flush(); } readonly Scanner cin = new Scanner(); readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑ readonly int mod = 1000000007; readonly int dom = 998244353; 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; } void solve() { int A = cin.nextint; int B = cin.nextint; int C = cin.nextint; WriteLine(Max(0, C - (A - B))); } } static class Ex { public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values)); public static string concat<T>(this IEnumerable<T> values) => string.Concat(values); public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); } public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) < 0) low = mid + 1; else high = mid; } return low; } public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T> { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) <= 0) low = mid + 1; else high = mid; } return low; } } class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U> { public T f; public U s; public Pair(T f, U s) { this.f = f; this.s = s; } public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s); public override string ToString() => $"{f} {s}"; } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan => ReadLine().Split(); public int[] scanint => Array.ConvertAll(scan, int.Parse); public long[] scanlong => Array.ConvertAll(scan, long.Parse); public double[] scandouble => Array.ConvertAll(scan, double.Parse); public string next { get { if (i < s.Length) return s[i++]; string st = ReadLine(); while (st == "") st = ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint => int.Parse(next); public long nextlong => long.Parse(next); public double nextdouble => double.Parse(next); }
[["+", 0, 213, 3, 4, 0, 28, 0, 213, 63, 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, 16, 12, 23, 0, 25]]
8
865
5
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); int cnt = 0; for (int i = 1; i <= N; i++) { if (i.ToString().Length % 2 != 0) { cnt++; } else { i *= 10; } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { long N = long.Parse(Console.ReadLine()); int cnt = 0; for (int i = 1; i <= N; i++) { if (i.ToString().Length % 2 != 0) { cnt++; } else { i = i * 10 - 1; } } Console.WriteLine(cnt); } } } // 文字列の入力 // string s = Console.ReadLine(); // 整数の入力 // long n = long.Parse(Console.ReadLine()); // 文字列配列の入力 // string[] inputStrArray = Console.ReadLine().Split(' '); // 整数配列の入力 // long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => // long.Parse(i)).ToArray();
[["-", 75, 196, 0, 1, 0, 11, 0, 202, 0, 108], ["+", 75, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 31, 22], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 17, 48], ["+", 75, 196, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 75, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
130
6
using System; class Prog { static void Main() { int x = int.Parse(Console.ReadLine()); string y = x.ToString(); double a = 0; if (y.Length % 2 != 0) { a += x - Math.Pow(10, (y.Length / 2) + 1) + 1; } for (int i = 0; i < y.Length - 1; i = i + 2) { a += 9 * Math.Pow(10, i); } Console.WriteLine(a); } }
using System; class Prog { static void Main() { int x = int.Parse(Console.ReadLine()); string y = x.ToString(); double a = 0; if (y.Length % 2 != 0) { a += x - Math.Pow(10, y.Length - 1) + 1; } for (int i = 0; i < y.Length - 1; i = i + 2) { a += 9 * Math.Pow(10, i); } Console.WriteLine(a); } }
[["-", 3, 4, 0, 28, 0, 16, 31, 23, 0, 24], ["-", 0, 28, 0, 16, 31, 23, 0, 16, 17, 85], ["-", 0, 28, 0, 16, 31, 23, 0, 16, 12, 203], ["-", 3, 4, 0, 28, 0, 16, 31, 23, 0, 25], ["-", 12, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 12, 213, 3, 4, 0, 28, 0, 16, 17, 33]]
8
120
6
using System; using System.Collections.Generic; using System.Linq; public class AtCoderB { static void Main() { int n = int.Parse(Console.ReadLine()); int count = 0; for (int i = 1; i <= n; i++) { if ((1 <= i && i < 10) || (100 <= i && i < 1000) || (10000 <= i && i < 1000000)) { count++; } } Console.WriteLine(count); } }
using System; using System.Collections.Generic; using System.Linq; public class AtCoderB { static void Main() { int n = int.Parse(Console.ReadLine()); int count = 0; for (int i = 1; i <= n; i++) { if ((1 <= i && i < 10) || (100 <= i && i < 1000) || (10000 <= i && i < 100000)) { count++; } } Console.WriteLine(count); } }
[["-", 15, 16, 12, 23, 0, 16, 12, 16, 12, 203], ["+", 15, 16, 12, 23, 0, 16, 12, 16, 12, 203]]
8
106
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main() { Solve(); Console.ReadKey(); } static void Solve() { var N = IO.GetInt(); var ans = 0; if (N < 10) { ans = N; } else if (N < 100) { ans = 9; } else if (N < 1000) { ans = N - 100 + 1 + 9; } else if (N < 10000) { ans = 999 - 100 + 1 + 9; } else if (N < 100000) { ans = N - 10000 + 1 + 999 - 100 + 9; } else { ans = 99999 - 10000 + 1 + 999 - 100 + 1 + 9; } Console.WriteLine(ans); } } static class IO { static public string GetString() { return Console.ReadLine(); } static public int GetInt() { return int.Parse(Console.ReadLine()); } static public long GetLong() { return long.Parse(Console.ReadLine()); } static public List<string> GetStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> GetIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> GetLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main() { Solve(); Console.ReadKey(); } static void Solve() { var N = IO.GetInt(); var ans = 0; if (N < 10) { ans = N; } else if (N < 100) { ans = 9; } else if (N < 1000) { ans = N - 100 + 1 + 9; } else if (N < 10000) { ans = 999 - 100 + 1 + 9; } else if (N < 100000) { ans = N - 10000 + 1 + 999 - 100 + 1 + 9; } else { ans = 99999 - 10000 + 1 + 999 - 100 + 1 + 9; } Console.WriteLine(ans); } } static class IO { static public string GetString() { return Console.ReadLine(); } static public int GetInt() { return int.Parse(Console.ReadLine()); } static public long GetLong() { return long.Parse(Console.ReadLine()); } static public List<string> GetStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> GetIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> GetLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } } } }
[["+", 0, 1, 0, 11, 12, 16, 31, 16, 12, 203], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 72]]
8
418
2
// ABC136 B - Uneven Numbers // 20190905 using System; using System.Linq; namespace B { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); double OUTPUT = 0; for (int i = 0; i < N; i++) { if (i.ToString().Length % 2 == 1) { OUTPUT++; } } Console.WriteLine(OUTPUT); // int N = int.Parse(Console.ReadLine()); // double OUTPUT = 0; // if (N < 10) // { // OUTPUT = N; // } // else if (N < 100) // { // OUTPUT = 9; // } // else if (N < 1000) // { // OUTPUT = 9 + N - 99; // } // else if (N < 10000) // { // OUTPUT = 9 + 900; // } // else if (N < 100000) // { // OUTPUT = 9 + 900 + N - 9999; // } // else // { // OUTPUT = 9 + 900 + 90000 ; // } // Console.WriteLine(OUTPUT); } } }
// ABC136 B - Uneven Numbers // 20190905 using System; using System.Linq; namespace B { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); double OUTPUT = 0; for (int i = 1; i <= N; i++) { if (i.ToString().Length % 2 == 1) { OUTPUT++; } } Console.WriteLine(OUTPUT); // int N = int.Parse(Console.ReadLine()); // double OUTPUT = 0; // if (N < 10) // { // OUTPUT = N; // } // else if (N < 100) // { // OUTPUT = 9; // } // else if (N < 1000) // { // OUTPUT = 9 + N - 99; // } // else if (N < 10000) // { // OUTPUT = 9 + 900; // } // else if (N < 100000) // { // OUTPUT = 9 + 900 + N - 9999; // } // else // { // OUTPUT = 9 + 900 + 90000 ; // } // Console.WriteLine(OUTPUT); } } }
[["-", 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]]
8
117
4
// ABC136 B - Uneven Numbers // 20190905 using System; using System.Linq; namespace B { class Program { static void Main(string[] args) { // Console.WriteLine("Hello World!"); int N = int.Parse(Console.ReadLine()); double OUTPUT = 0; if (N < 10) { // OUTPUT = (int)(N / 2.0 + 0.5); OUTPUT = N; } else if (N < 100) { // OUTPUT = 5; OUTPUT = 9; } else if (N < 1000) { // OUTPUT = 5 + (int)((N - 100) / 2.0 + 0.5); OUTPUT = 9 + N - 100 + 1; } else if (N < 10000) { // OUTPUT = 5 + 4950; OUTPUT = 9 + 900; } else if (N < 100000) { // OUTPUT = 5 + 4950 + (int)((100000 - 10000) / 2.0 + 0.5); OUTPUT = 9 + 900 + N - 1000 + 1; } else { OUTPUT = 9 + 900 + 90000; } Console.WriteLine(OUTPUT); } } }
// ABC136 B - Uneven Numbers // 20190905 using System; using System.Linq; namespace B { class Program { static void Main(string[] args) { // Console.WriteLine("Hello World!"); int N = int.Parse(Console.ReadLine()); double OUTPUT = 0; if (N < 10) { OUTPUT = N; } else if (N < 100) { OUTPUT = 9; } else if (N < 1000) { OUTPUT = 9 + N - 99; } else if (N < 10000) { OUTPUT = 9 + 900; } else if (N < 100000) { OUTPUT = 9 + 900 + N - 9999; } else { OUTPUT = 9 + 900 + 90000; } Console.WriteLine(OUTPUT); } } }
[["-", 0, 1, 0, 11, 12, 16, 31, 16, 12, 203], ["-", 64, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
152
8
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _136B { class Program { static void Main(string[] args) { string strN = Console.ReadLine(); int n = int.Parse(strN); double ans = 0; int keta = strN.Length; int a = 1; for (; a <= keta; a += 2) { if (Math.Pow(10, a) < n) { ans += (Math.Pow(10, a) - 1) - (Math.Pow(10, a - 1)) + 1; } } if (keta % 2 == 1) { ans += n - (Math.Pow(10, a - 3)) + 1; } Console.WriteLine(ans); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _136B { class Program { static void Main(string[] args) { string strN = Console.ReadLine(); int n = int.Parse(strN); double ans = 0; int keta = strN.Length; int a = 1; for (; a <= keta; a += 2) { if (Math.Pow(10, a) <= n) { ans += (Math.Pow(10, a) - 1) - (Math.Pow(10, a - 1)) + 1; } } if (keta % 2 == 1) { ans += n - (Math.Pow(10, a - 3)) + 1; } Console.WriteLine(ans); } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 19]]
8
176
2
using System; class Task3 { public static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int len = N.ToString().Length; int ans = 0; for (int i = 0; i < len; i++) { if ((i + 1) % 2 != 0) { ans = 9 * Convert.ToInt32(Math.Pow(10, i)); } else { ans += 0; } } if (len % 2 != 0) { ans = (N + 1) - Convert.ToInt32(Math.Pow(10, len)); } Console.WriteLine(ans); } }
using System; class Task3 { public static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int len = N.ToString().Length; int ans = 0; for (int i = 0; i < len; i++) { if ((i + 1) % 2 != 0) { ans += 9 * (Convert.ToInt32(Math.Pow(10, i))); } else { ans += 0; } } if (len % 2 != 0) { ans += (N + 1) - (Convert.ToInt32(Math.Pow(10, len))); } Console.WriteLine(ans); } }
[["-", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 107], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25]]
8
143
8
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Solution { public long Proc(string Source) { long n = Source.Length; long src = long.Parse(Source); long number = 0; if (n % 2 == 0) { for (long i = 1; i < n; i += 2) { number += (long)Math.Pow(10, i) - (long)Math.Pow(10, i - 1); } } else { for (long i = 1; i < n; i += 2) { number += (long)Math.Pow(10, i) - (long)Math.Pow(10, i - 1); } number += (src % (long)Math.Pow(10, n - 1)) + ((src / (long)Math.Pow(10, n - 1) - 1) * (long)Math.Pow(10, n - 1)); } return number; } public long Proc2(string Source) { long src = long.Parse(Source); long count = 0; for (long i = 1; i < src; i++) { if (i.ToString().Length % 2 == 1) count++; } return count; } } public class Program { static void Main(string[] args) { try { var str = Console.ReadLine(); var s = new Solution(); var re = s.Proc2(str); Console.WriteLine(re); // Random rnd = new System.Random(); // while (true) //{ // string src = rnd.Next(1, 10000).ToString(); // var s = new Solution(); // s.Proc2("405"); // s.Proc("405"); // if (s.Proc(src) != s.Proc2(src)) // { // throw new Exception(""); // } // Console.WriteLine("Cp"); //} // var str = Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); } // Console.WriteLine("終了"); // Console.Read(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Solution { public long Proc(string Source) { long n = Source.Length; long src = long.Parse(Source); long number = 0; if (n % 2 == 0) { for (long i = 1; i < n; i += 2) { number += (long)Math.Pow(10, i) - (long)Math.Pow(10, i - 1); } } else { for (long i = 1; i < n; i += 2) { number += (long)Math.Pow(10, i) - (long)Math.Pow(10, i - 1); } number += (src % (long)Math.Pow(10, n - 1)) + ((src / (long)Math.Pow(10, n - 1) - 1) * (long)Math.Pow(10, n - 1)); } return number; } public long Proc2(string Source) { long src = long.Parse(Source); long count = 0; for (long i = 1; i <= src; i++) { if (i.ToString().Length % 2 == 1) count++; } return count; } } public class Program { static void Main(string[] args) { try { var str = Console.ReadLine(); var s = new Solution(); var re = s.Proc2(str); Console.WriteLine(re); // Random rnd = new System.Random(); // while (true) //{ // string src = rnd.Next(1, 10000).ToString(); // var s = new Solution(); // s.Proc2("405"); // s.Proc("405"); // if (s.Proc(src) != s.Proc2(src)) // { // throw new Exception(""); // } // Console.WriteLine("Cp"); //} // var str = Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); } // Console.WriteLine("終了"); // Console.Read(); } } }
[["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 19]]
8
366
2
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { static void Main(string[] args) { MainStream(); } static void MainStream() { Scan sc = new Scan(); write wr = new write(); long n = sc.intarr[0]; if (n <= 9) { wr.wri(n); } else if (n <= 99) { wr.wri("9"); } else if (n <= 999) { wr.wri(n - 99 + 9); } else if (n <= 9999) { wr.wri("9 + 900"); } else if (n <= 99999) { wr.wri(9 + 900 + n - 9999); } else { wr.wri(9 + 900 + 90000); } } } class Scan { public string str => Console.ReadLine(); public string[] strarr => str.Split(' '); public long[] longarr => strarr.Select(long.Parse).ToArray(); public int[] intarr => strarr.Select(int.Parse).ToArray(); public char[] chararr => str.ToArray(); } class write { public void wri<Type>(Type x) { Console.WriteLine(x); } } class Method { static public int max(params int[] nums) { int maxx = nums[0]; for (int i = 1; i < nums.Length; i++) { maxx = Math.Max(maxx, nums[i]); } return maxx; } } }
using System; using System.Linq; using System.Collections.Generic; namespace AtCoder { class Program { static void Main(string[] args) { MainStream(); } static void MainStream() { Scan sc = new Scan(); write wr = new write(); long n = sc.intarr[0]; if (n <= 9) { wr.wri(n); } else if (n <= 99) { wr.wri("9"); } else if (n <= 999) { wr.wri(n - 99 + 9); } else if (n <= 9999) { wr.wri(9 + 900); } else if (n <= 99999) { wr.wri(9 + 900 + n - 9999); } else { wr.wri(9 + 900 + 90000); } } } class Scan { public string str => Console.ReadLine(); public string[] strarr => str.Split(' '); public long[] longarr => strarr.Select(long.Parse).ToArray(); public int[] intarr => strarr.Select(int.Parse).ToArray(); public char[] chararr => str.ToArray(); } class write { public void wri<Type>(Type x) { Console.WriteLine(x); } } class Method { static public int max(params int[] nums) { int maxx = nums[0]; for (int i = 1; i < nums.Length; i++) { maxx = Math.Max(maxx, nums[i]); } return maxx; } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 62], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 31, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
340
6
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Math; namespace AtCodeeeer { class AtCOOOOOOOOOOOder { static void Main(string[] args) { int n = int.Parse(ReadLine()); int ans = 0; for (int i = 0; i < n; i++) { if (cal(i) % 2 == 1) { ans++; } } WriteLine(ans); } static int cal(int num) { int tmp = 0; while (num != 0) { num /= 10; tmp++; } return tmp; } } }
using System; using System.Collections.Generic; using System.Linq; using System.IO; using static System.Console; using static System.Math; namespace AtCodeeeer { class AtCOOOOOOOOOOOder { static void Main(string[] args) { int n = int.Parse(ReadLine()); int ans = 0; for (int i = 1; i <= n; i++) { if (cal(i) % 2 == 1) { ans++; } } WriteLine(ans); } static int cal(int num) { int tmp = 0; while (num != 0) { num /= 10; tmp++; } return tmp; } } }
[["-", 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]]
8
137
4
using System; using System.Linq; using System.Collections.Generic; using static System.Math; using static System.Console; using System.Text; class Program { static void Main(string[] args) { solve(); } static Scanner cin; static int N; static void input() { cin = new Scanner(); N = cin.nextInt(); } static void solve() { input(); int sum = 0; if (N >= 10) sum += 9; else { sum += N; WriteLine(sum); return; } if (N >= 1000) sum += 900; else if (N >= 100) { sum += N - 99; WriteLine(sum); return; } if (N >= 100000) sum += 90000; else if (N >= 10000) { sum += N - 999; WriteLine(sum); return; } WriteLine(sum); } 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[] ArrayDounble(int N, int add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } }
using System; using System.Linq; using System.Collections.Generic; using static System.Math; using static System.Console; using System.Text; class Program { static void Main(string[] args) { solve(); } static Scanner cin; static int N; static void input() { cin = new Scanner(); N = cin.nextInt(); } static void solve() { input(); int sum = 0; if (N >= 10) sum += 9; else { sum += N; WriteLine(sum); return; } if (N >= 1000) sum += 900; else if (N >= 100) { sum += N - 99; WriteLine(sum); return; } if (N >= 100000) sum += 90000; else if (N >= 10000) { sum += N - 9999; WriteLine(sum); return; } WriteLine(sum); } 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[] ArrayDounble(int N, int add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } } }
[["-", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
540
2
using System; namespace AtCoderPractice { class Program { static void Main(string[] args) { string[] line = Console.ReadLine().Split(' '); int n = int.Parse(line[0]); // n以下の // 5桁の数 … 10000~99999 90000個 // 3桁の数 … 100~999 900個 // 1桁の数 … 1~9 9個 int result = 0; if (n <= 100) { result = n >= 9 ? 9 : n; } else if (n <= 10000) { result = 9 + (n >= 999 ? 900 : n - 100 + 1); } else { result = 9 + 900 + (n >= 99999 ? 90000 : n - 10000 + 1); } Console.WriteLine(result); } } }
using System; namespace AtCoderPractice { class Program { static void Main(string[] args) { string[] line = Console.ReadLine().Split(' '); int n = int.Parse(line[0]); // n以下の // 5桁の数 … 10000~99999 90000個 // 3桁の数 … 100~999 900個 // 1桁の数 … 1~9 9個 int result = 0; if (n < 100) { result = n >= 9 ? 9 : n; } else if (n < 10000) { result = 9 + (n >= 999 ? 900 : n - 100 + 1); } else { result = 9 + 900 + (n >= 99999 ? 90000 : n - 10000 + 1); } Console.WriteLine(result); } } }
[["-", 0, 195, 8, 196, 0, 57, 15, 16, 17, 19], ["+", 0, 195, 8, 196, 0, 57, 15, 16, 17, 18], ["-", 8, 196, 0, 57, 75, 57, 15, 16, 17, 19], ["+", 8, 196, 0, 57, 75, 57, 15, 16, 17, 18]]
8
137
4
using System; class ProgramB { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); if (n <= 9) Console.WriteLine(n); else if (n <= 99) Console.WriteLine("9"); else if (n <= 999) Console.WriteLine(n - 90); else if (n <= 9999) Console.WriteLine("909"); else if (n <= 999) Console.WriteLine(n - 9090); else Console.WriteLine("90909"); } }
using System; class ProgramB { static void Main(string[] args) { string input = Console.ReadLine(); int n = int.Parse(input); if (n <= 9) Console.WriteLine(n); else if (n <= 99) Console.WriteLine("9"); else if (n <= 999) Console.WriteLine(n - 90); else if (n <= 9999) Console.WriteLine("909"); else if (n <= 99999) Console.WriteLine(n - 9090); else Console.WriteLine("90909"); } }
[["-", 75, 57, 75, 57, 75, 57, 15, 16, 12, 203], ["+", 75, 57, 75, 57, 75, 57, 15, 16, 12, 203]]
8
124
2
using System; class B_Uneven { static void Main(string[] args) { double N = double.Parse(Console.ReadLine()); int Score = 0; if (Math.Floor(N / 10) > 0) { Score += 9; if (Math.Floor(N / 1000) > 0) { Score += 900; if (N > 10000) Score += ((int)Math.Floor(N) - 9999); if (N >= 100000) Score--; } else if (N >= 100) Score += ((int)Math.Floor(N) - 99); } else Score += (int)Math.Floor(N); Console.WriteLine(Score); } }
using System; class B_Uneven { static void Main(string[] args) { double N = double.Parse(Console.ReadLine()); int Score = 0; if (Math.Floor(N / 10) > 0) { Score += 9; if (Math.Floor(N / 1000) > 0) { Score += 900; if (N >= 10000) Score += ((int)Math.Floor(N) - 9999); if (N >= 100000) Score--; } else if (N >= 100) Score += ((int)Math.Floor(N) - 99); } else Score += (int)Math.Floor(N); Console.WriteLine(Score); } }
[["-", 0, 57, 64, 196, 0, 57, 15, 16, 17, 47], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 17, 20]]
8
149
2
using System; using System.Collections.Generic; class ABC { public static void Main() { string str = Console.ReadLine(); List<char> list = new List<char> {}; foreach (char c in str) { list.Add(c); } switch (list.Count) { case 1: Console.WriteLine(Convert.ToString(list[0])); break; case 2: Console.WriteLine(9); break; case 3: Console.WriteLine(10 + (int.Parse(Convert.ToString(list[0])) - 1) * 100 + int.Parse(Convert.ToString(list[1])) * 10 + int.Parse(Convert.ToString(list[2]))); break; case 4: Console.WriteLine(909); break; case 5: Console.WriteLine(909 + (int.Parse(Convert.ToString(list[0])) - 1) * 10000 + int.Parse(Convert.ToString(list[1])) * 1000 + int.Parse(Convert.ToString(list[2])) * 100 + int.Parse(Convert.ToString(list[3])) * 10 + int.Parse(Convert.ToString(list[4]))); break; case 6: Console.WriteLine(90909); break; } } }
using System; using System.Collections.Generic; class ABC { public static void Main() { string str = Console.ReadLine(); List<char> list = new List<char> {}; foreach (char c in str) { list.Add(c); } switch (list.Count) { case 1: Console.WriteLine(Convert.ToString(list[0])); break; case 2: Console.WriteLine(9); break; case 3: Console.WriteLine(10 + (int.Parse(Convert.ToString(list[0])) - 1) * 100 + int.Parse(Convert.ToString(list[1])) * 10 + int.Parse(Convert.ToString(list[2]))); break; case 4: Console.WriteLine(909); break; case 5: Console.WriteLine(910 + (int.Parse(Convert.ToString(list[0])) - 1) * 10000 + int.Parse(Convert.ToString(list[1])) * 1000 + int.Parse(Convert.ToString(list[2])) * 100 + int.Parse(Convert.ToString(list[3])) * 10 + int.Parse(Convert.ToString(list[4]))); break; case 6: Console.WriteLine(90909); break; } } }
[["-", 31, 16, 31, 16, 31, 16, 31, 16, 31, 203], ["+", 31, 16, 31, 16, 31, 16, 31, 16, 31, 203]]
8
289
2
using System; namespace AC_5 { class Program { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int A = int.Parse(input[0]); if (A < 10) { Console.WriteLine(A); } else if (10 <= A && A < 100) { Console.WriteLine(9); } else if (100 <= A && A < 1000) { Console.WriteLine((A - 100) + 10); } else if (1000 <= A && A < 10000) { Console.WriteLine(909); } else if (10000 <= A && A < 100000) { Console.WriteLine((A - 10000) + 911); } else if (A == 100000) { Console.WriteLine(90909); } } } }
using System; namespace AC_5 { class Program { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int A = int.Parse(input[0]); if (A < 10) { Console.WriteLine(A); } else if (10 <= A && A < 100) { Console.WriteLine(9); } else if (100 <= A && A < 1000) { Console.WriteLine((A - 100) + 10); } else if (1000 <= A && A < 10000) { Console.WriteLine(909); } else if (10000 <= A && A < 100000) { Console.WriteLine((A - 10000) + 910); } else if (A == 100000) { Console.WriteLine(90909); } } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
176
2
using System; namespace question2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int digit = 0; int amari = 0; int[] numbers = new int[7]; int[] all = new int[7]; numbers[0] = 1; numbers[1] = 10; numbers[2] = 100; numbers[3] = 1000; numbers[4] = 10000; numbers[5] = 100000; numbers[6] = 1000000; all[0] = 9; all[1] = 0; all[2] = 900; all[3] = 0; all[4] = 90000; all[5] = 0; all[6] = 9000000; int res = 0; for (int i = 1; i < 7; i++) { if (N < numbers[i]) { amari = N % numbers[i - 1]; if (all[i - 1] != 0) res += 1 + amari; if (i == 1) res = N; break; } res += all[i - 1]; } Console.WriteLine(res.ToString()); // Console.ReadLine(); } } }
using System; namespace question2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int digit = 0; int amari = 0; int[] numbers = new int[7]; int[] all = new int[7]; numbers[0] = 1; numbers[1] = 10; numbers[2] = 100; numbers[3] = 1000; numbers[4] = 10000; numbers[5] = 100000; numbers[6] = 1000000; all[0] = 9; all[1] = 0; all[2] = 900; all[3] = 0; all[4] = 90000; all[5] = 0; all[6] = 9000000; int res = 0; for (int i = 1; i < 7; i++) { if (N < numbers[i]) { amari = N - numbers[i - 1]; if (all[i - 1] != 0) res += 1 + amari; if (i == 1) res = N; break; } res += all[i - 1]; } Console.WriteLine(res.ToString()); // Console.ReadLine(); } } }
[["-", 64, 196, 0, 1, 0, 11, 12, 16, 17, 109], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 33]]
8
259
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace task { class Program { static void Main(string[] args) { bool ifOk = true; int n = Convert.ToInt32(Console.ReadLine()); int[] m = Array.ConvertAll(Console.ReadLine().Split(' '), a => Convert.ToInt32(a)); for (int i = n - 1; i > -1; i--) { if (m[i - 1] <= m[i]) ; else if (m[i - 1] - 1 <= m[i]) { m[i - 1] -= 1; } else { ifOk = false; break; } } if (ifOk) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace task { class Program { static void Main(string[] args) { bool ifOk = true; int n = Convert.ToInt32(Console.ReadLine()); int[] m = Array.ConvertAll(Console.ReadLine().Split(' '), a => Convert.ToInt32(a)); for (int i = n - 1; i > 0; i--) { if (m[i - 1] <= m[i]) ; else if (m[i - 1] - 1 <= m[i]) { m[i - 1] -= 1; } else { ifOk = false; break; } } if (ifOk) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } }
[["-", 8, 196, 0, 7, 15, 16, 12, 241, 0, 33], ["-", 8, 196, 0, 7, 15, 16, 12, 241, 0, 203], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 12, 203]]
8
191
3
using System; using System.IO; class Program { static void Main(string[] args) { var scan = new Scanner(); var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(writer); Console.WriteLine(Solve(scan)); Console.Out.Flush(); } static object Solve(Scanner scan) { var N = scan.NextInt(); if (N == 1) return "Yes"; var H = scan.ArrayInt(N); if (H[1] - H[0] == -1) H[0]--; for (int i = 0; i < N - 1; i++) { if (H[i + 1] - H[i] > 1) H[i + 1]--; if (H[i + 1] - H[i] < 0) return "No"; } return "Yes"; } } class Scanner { string[] s; int i; public Scanner() { s = new string[0]; i = 0; } public string Next() { if (i < s.Length) return s[i++]; string str = Console.ReadLine(); while (str == "") str = Console.ReadLine(); s = str.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return Next(); i = 0; return s[i++]; } public int NextInt() => int.Parse(Next()); public int[] ArrayInt(int len, int add = 0) { var array = new int[len]; for (int i = 0; i < len; i++) array[i] = NextInt() + add; return array; } }
using System; using System.IO; class Program { static void Main(string[] args) { var scan = new Scanner(); var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(writer); Console.WriteLine(Solve(scan)); Console.Out.Flush(); } static object Solve(Scanner scan) { var N = scan.NextInt(); if (N == 1) return "Yes"; var H = scan.ArrayInt(N); if (H[1] - H[0] == -1) H[0]--; for (int i = 0; i < N - 1; i++) { if (H[i + 1] - H[i] > 0) H[i + 1]--; if (H[i + 1] - H[i] < 0) return "No"; } return "Yes"; } } class Scanner { string[] s; int i; public Scanner() { s = new string[0]; i = 0; } public string Next() { if (i < s.Length) return s[i++]; string str = Console.ReadLine(); while (str == "") str = Console.ReadLine(); s = str.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return Next(); i = 0; return s[i++]; } public int NextInt() => int.Parse(Next()); public int[] ArrayInt(int len, int add = 0) { var array = new int[len]; for (int i = 0; i < len; i++) array[i] = NextInt() + add; return array; } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203]]
8
385
2
using Atcoder; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Atcoder { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var inputs = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var ret = 0; for (int i = inputs.Length - 1; i >= 1; i--) { if (inputs[i - 1] - inputs[i] == 1) { inputs[i - 1]--; } else if (inputs[i - 1] - inputs[i] > 1) { Console.WriteLine("No"); } } Console.WriteLine("Yes"); } } }
using Atcoder; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Atcoder { class Program { static void Main(string[] args) { var N = int.Parse(Console.ReadLine()); var inputs = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var ret = 0; for (int i = inputs.Length - 1; i >= 1; i--) { if (inputs[i - 1] - inputs[i] == 1) { inputs[i - 1]--; } else if (inputs[i - 1] - inputs[i] > 1) { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } } }
[["+", 0, 57, 75, 57, 64, 196, 0, 37, 0, 38], ["+", 0, 57, 75, 57, 64, 196, 0, 37, 0, 35]]
8
195
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AtCoder { static class Program { static void Main() { //ここから int N = IO.ReadLineToInt(); var H = IO.ReadLineToIntList(); int max = 0; var ans = "Yes"; for (var i = 0; i < N; i++) { if (max - H[i] > 1) { ans = "No"; break; } max = Math.Max(H[i] - 1, max); } //ここまで Console.WriteLine(ans); Console.ReadKey(); } } static class Func { static long mod = 1000000007; static public void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; } static public long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; } static public long LCM(long a, long b) { return (long)a * b / GCD(a, b); } static public int Factorial(int a) { return a == 1 ? a : a * Factorial(a - 1); } public static double ToAngle(this double radian) { return (double)(radian * 180 / Math.PI); } public static double ToRadian(this double angle) { return (double)(angle * Math.PI / 180); } static public long Conbination(int n, int m) { if (m == 0) return 1; if (n == 0) return 0; return n * Conbination(n - 1, m - 1) / m; } static public void Factoring(long num, ref long[] vs) { if (num == 1) { vs[1]++; } for (long i = 2; i < Math.Sqrt(num); i++) { if (num % i == 0) { vs[i]++; Factoring(num / i, ref vs); } } vs[1]++; } } static class IO { static public string ReadLineToString() { return Console.ReadLine(); } static public int ReadLineToInt() { return int.Parse(Console.ReadLine()); } static public long ReadLineToLong() { return long.Parse(Console.ReadLine()); } static public List<string> ReadLineToStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> ReadLineToIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> ReadLineToLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> ReadLineToDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings(object[] strings) { foreach (var o in strings) { Console.Write(o + " "); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AtCoder { static class Program { static void Main() { //ここから int N = IO.ReadLineToInt(); var H = IO.ReadLineToIntList(); int max = 0; var ans = "Yes"; for (var i = 0; i < N; i++) { if (max - H[i] > 0) { ans = "No"; break; } max = Math.Max(H[i] - 1, max); } //ここまで Console.WriteLine(ans); Console.ReadKey(); } } static class Func { static long mod = 1000000007; static public void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; } static public long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; } static public long LCM(long a, long b) { return (long)a * b / GCD(a, b); } static public int Factorial(int a) { return a == 1 ? a : a * Factorial(a - 1); } public static double ToAngle(this double radian) { return (double)(radian * 180 / Math.PI); } public static double ToRadian(this double angle) { return (double)(angle * Math.PI / 180); } static public long Conbination(int n, int m) { if (m == 0) return 1; if (n == 0) return 0; return n * Conbination(n - 1, m - 1) / m; } static public void Factoring(long num, ref long[] vs) { if (num == 1) { vs[1]++; } for (long i = 2; i < Math.Sqrt(num); i++) { if (num % i == 0) { vs[i]++; Factoring(num / i, ref vs); } } vs[1]++; } } static class IO { static public string ReadLineToString() { return Console.ReadLine(); } static public int ReadLineToInt() { return int.Parse(Console.ReadLine()); } static public long ReadLineToLong() { return long.Parse(Console.ReadLine()); } static public List<string> ReadLineToStringList() { return Console.ReadLine().Split(' ').ToList(); } static public List<int> ReadLineToIntList() { return Console.ReadLine().Split(' ').Select(int.Parse).ToList(); } static public List<long> ReadLineToLongList() { return Console.ReadLine().Split(' ').Select(long.Parse).ToList(); } static public List<double> ReadLineToDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); } static public void WriteStrings(object[] strings) { foreach (var o in strings) { Console.Write(o + " "); } } } }
[["-", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 12, 203]]
8
680
2
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); var H = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); var res = "Yes"; for (int i = 1; i < N; i++) { if (H[i] - H[i - 1] >= 0) { } else if (H[i] - H[i - 1] == -1) { H[i - 1]--; } else { res = "No"; break; } } Console.Write(res); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; using E = System.Linq.Enumerable; class Program { static void Main(string[] args) { //入力を受け取る int N = int.Parse(Console.ReadLine()); var H = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); var res = "Yes"; for (int i = N - 1; i > 0; i--) { if (H[i] - H[i - 1] >= 0) { } else if (H[i] - H[i - 1] == -1) { H[i - 1]--; } else { res = "No"; break; } } Console.Write(res); } }
[["+", 10, 198, 0, 200, 0, 212, 0, 16, 31, 22], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 195, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 195, 8, 196, 0, 7, 15, 16, 17, 47], ["+", 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
178
8
using System; using System.Text.RegularExpressions; class Program { static void Main(string[] arg) { Console.ReadLine(); string[] tmp = Regex.Split(Console.ReadLine(), @"\s+"); int tmpnum = Convert.ToInt32(tmp[tmp.Length - 1]); int tmpvalue; bool flag = true; for (int i = tmp.Length - 2; i >= 0; --i) { tmpvalue = Convert.ToInt32(tmp[i]); if (tmpvalue - tmpnum > 1) { flag = false; break; } else if (tmpvalue - tmpnum > 0) { tmpnum = (tmpvalue - 1); } else { tmpnum = tmpvalue; } Console.WriteLine((flag) ? "Yes" : "No"); } } }
using System; using System.Text.RegularExpressions; class Program { static void Main(string[] arg) { Console.ReadLine(); string[] tmp = Regex.Split(Console.ReadLine(), @"\s+"); int tmpnum = Convert.ToInt32(tmp[tmp.Length - 1]); int tmpvalue; bool flag = true; for (int i = tmp.Length - 2; i >= 0; --i) { tmpvalue = Convert.ToInt32(tmp[i]); if (tmpvalue - tmpnum > 1) { flag = false; break; } else if (tmpvalue - tmpnum > 0) { tmpnum = (tmpvalue - 1); } else { tmpnum = tmpvalue; } } Console.WriteLine((flag) ? "Yes" : "No"); } }
[["+", 0, 195, 8, 196, 0, 7, 8, 196, 0, 46], ["-", 0, 195, 8, 196, 0, 7, 8, 196, 0, 46]]
8
165
2
using System; using System.Collections.Generic; namespace AtCoder190804_C { class Program { static void Main() { String input1 = Console.ReadLine(); int n = int.Parse(input1); String[] input2 = Console.ReadLine().Split(); var hList = new List<int>(n); for (int i = 0; i < n; i++) { hList.Add(int.Parse(input2[i])); } int[] h = hList.ToArray(); bool res = true; int minVal = h[n - 1]; for (int i = 0; i < n; i++) { int current = h[n - i - 1]; if (current > minVal + 1) { res = false; break; } if (current < minVal) { minVal = current; } } string resStr = res ? "YES" : "NO"; Console.WriteLine($"{resStr}"); } } }
using System; using System.Collections.Generic; namespace AtCoder190804_C { class Program { static void Main() { String input1 = Console.ReadLine(); int n = int.Parse(input1); String[] input2 = Console.ReadLine().Split(); var hList = new List<int>(n); for (int i = 0; i < n; i++) { hList.Add(int.Parse(input2[i])); } int[] h = hList.ToArray(); bool res = true; int minVal = h[n - 1]; for (int i = 0; i < n; i++) { int current = h[n - i - 1]; if (current > minVal + 1) { res = false; break; } if (current < minVal) { minVal = current; } } string resStr = res ? "Yes" : "No"; Console.WriteLine($"{resStr}"); } } }
[["-", 0, 200, 0, 212, 0, 41, 64, 5, 0, 222], ["+", 0, 200, 0, 212, 0, 41, 64, 5, 0, 222], ["-", 0, 200, 0, 212, 0, 41, 75, 5, 0, 222], ["+", 0, 200, 0, 212, 0, 41, 75, 5, 0, 222]]
8
208
4
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // 整数の入力 int n = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input = Console.ReadLine().Split(' '); var arr = new List<int>(); foreach (var i in input) { arr.Add(int.Parse(i)); } if (arr.Count < 1) { Console.WriteLine("Yes"); } else { for (int i = arr.Count - 2; i != -1; i--) { if (arr[i] > arr[i + 1]) arr[i]--; if (arr[i] > arr[i + 1]) Console.WriteLine("No"); } } Console.WriteLine("Yes"); } }
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // 整数の入力 int n = int.Parse(Console.ReadLine()); // スペース区切りの整数の入力 string[] input = Console.ReadLine().Split(' '); var arr = new List<int>(); foreach (var i in input) { arr.Add(int.Parse(i)); } if (arr.Count < 1) { Console.WriteLine("Yes"); } else { for (int i = arr.Count - 2; i != -1; i--) { if (arr[i] > arr[i + 1]) arr[i]--; if (arr[i] > arr[i + 1]) { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } }
[["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 37, 0, 38], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
186
4