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.IO; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(t => int.Parse(t)).ToArray(); int a = inpt[0]; int b = inpt[1]; int kaku = inpt[2]; double rad = kaku * (Math.PI / 180); double menseki = 0; double gaisyu = 0; double takasa = b * Math.Sin(rad); menseki = a * takasa / 2; gaisyu = a + b + Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2) - (a * b * Math.Cos(rad) / 2)); Console.WriteLine(menseki); Console.WriteLine(gaisyu); Console.WriteLine(takasa); } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 4 3 90 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }
using System; using System.IO; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(t => int.Parse(t)).ToArray(); int a = inpt[0]; int b = inpt[1]; int kaku = inpt[2]; double rad = kaku * (Math.PI / 180); double menseki = 0; double gaisyu = 0; double takasa = b * Math.Sin(rad); menseki = a * takasa / 2; gaisyu = a + b + Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2) - (a * b * Math.Cos(rad) * 2)); Console.WriteLine(menseki); Console.WriteLine(gaisyu); Console.WriteLine(takasa); } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 100 80 54 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }
[["-", 0, 28, 0, 16, 12, 23, 0, 16, 17, 85], ["+", 0, 28, 0, 16, 12, 23, 0, 16, 17, 48], ["-", 0, 124, 0, 198, 0, 200, 0, 212, 0, 258], ["+", 0, 124, 0, 198, 0, 200, 0, 212, 0, 258]]
8
310
4
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Contexts; using System.Security.Cryptography.X509Certificates; using System.Threading; namespace AOJ { internal class Program { public static void Main(string[] args) { new Program().solve(); } void solve() { Func<double, double> rad = (theta) => theta * (Math.PI) / 180D; Func<double, double, double, double> area = (e1, e2, theta) => (e1 * e2 * Math.Sin(rad(theta))) / 2; Func<double, double, double, double> surroundings = (e1, e2, theta) => e1 + e2 + Math.Sqrt(e1 * e1 + e2 * e2 - 2 * e1 * e2 * Math.Cos(rad(theta))); Func<double, double, double> height = (bottom, theta) => bottom * Math.Sin(rad(theta)); var inputs = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); var a = inputs[0]; var b = inputs[1]; var C = inputs[2]; Console.WriteLine(area(a, b, C)); Console.WriteLine(surroundings(a, b, C)); Console.WriteLine(height(a, C)); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Contexts; using System.Security.Cryptography.X509Certificates; using System.Threading; namespace AOJ { internal class Program { public static void Main(string[] args) { new Program().solve(); } void solve() { Func<double, double> rad = (theta) => theta * (Math.PI) / 180D; Func<double, double, double, double> area = (e1, e2, theta) => (e1 * e2 * Math.Sin(rad(theta))) / 2; Func<double, double, double, double> surroundings = (e1, e2, theta) => e1 + e2 + Math.Sqrt(e1 * e1 + e2 * e2 - 2 * e1 * e2 * Math.Cos(rad(theta))); Func<double, double, double> height = (bottom, theta) => bottom * Math.Sin(rad(theta)); var inputs = Console.ReadLine().Split().Select(v => int.Parse(v)).ToArray(); var a = inputs[0]; var b = inputs[1]; var C = inputs[2]; Console.WriteLine(area(a, b, C)); Console.WriteLine(surroundings(a, b, C)); Console.WriteLine(height(b, C)); } } }
[["-", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22]]
8
308
2
using System; class ITP1_10_B { static void Main() { string[] input = Console.ReadLine().Split(' '); int a = int.Parse(input[0]); int b = int.Parse(input[1]); double rad = int.Parse(input[2]) * Math.PI / 180; double S = a * b * Math.Sin(rad) / 2; Console.WriteLine(S); Console.WriteLine(a + b + Math.Sqrt(a * a + b * b - a * b * Math.Cos(rad))); Console.WriteLine(S / a * 2); } }
using System; class ITP1_10_B { static void Main() { string[] input = Console.ReadLine().Split(' '); int a = int.Parse(input[0]); int b = int.Parse(input[1]); double rad = int.Parse(input[2]) * Math.PI / 180; double S = a * b * Math.Sin(rad) / 2; Console.WriteLine(S); Console.WriteLine(a + b + Math.Sqrt(a * a + b * b - 2 * a * b * Math.Cos(rad))); Console.WriteLine(S / a * 2); } }
[["+", 0, 16, 12, 16, 31, 16, 31, 16, 31, 203], ["+", 0, 16, 12, 16, 31, 16, 31, 16, 17, 48]]
8
144
2
using System; namespace ITP1_10_C_2 { class Program { static void Main(string[] args) { while (true) { int n = int.Parse(Console.ReadLine()); if (n == 0) Environment.Exit(0); string[] data = Console.ReadLine().Split(' '); int[] tokuten = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { tokuten[i] = int.Parse(data[i]); sum += tokuten[i]; } double m = sum / n; //平均 double m2 = Math.Pow(m, 2); //平均の2乗 double sum2 = 0; for (int i = 0; i < n; i++) { double before = Math.Pow(tokuten[i], 2);   //データの2乗 sum2 += before; } double a = sum2 / n;    //データの2乗の平均 double a2 = a - m2; double root = Math.Sqrt(a2); Console.WriteLine("{0:F8}", root); } } } }
using System; namespace ITP1_10_C_2 { class Program { static void Main(string[] args) { while (true) { int n = int.Parse(Console.ReadLine()); if (n == 0) Environment.Exit(0); string[] data = Console.ReadLine().Split(' '); int[] tokuten = new int[n]; double sum = 0; for (int i = 0; i < n; i++) { tokuten[i] = int.Parse(data[i]); sum += tokuten[i]; } double m = sum / n; //平均 double m2 = Math.Pow(m, 2); //平均の2乗 double sum2 = 0; for (int i = 0; i < n; i++) { double before = Math.Pow(tokuten[i], 2);   //データの2乗 sum2 += before; } double a = sum2 / n;    //データの2乗の平均 double a2 = a - m2; double root = Math.Sqrt(a2); Console.WriteLine("{0:F8}", root); } } } }
[["-", 0, 52, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 52, 8, 196, 0, 197, 0, 198, 39, 199]]
8
227
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { while (true) { int n = int.Parse(Console.ReadLine()); if (n == 0) { break; } string[] str = Console.ReadLine().Split(' '); int[] S = new int[n]; int sum1 = 0; for (int i = 0; i < n; ++i) { S[i] = int.Parse(str[i]); sum1 += S[i]; } double m = sum1 / n; double sum2 = 0; for (int i = 0; i < n; ++i) { sum2 += Math.Pow((S[i] - m), 2); } double a = Math.Sqrt(sum2 / n); Console.WriteLine("{0:f8}", a); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { while (true) { int n = int.Parse(Console.ReadLine()); if (n == 0) { break; } string[] str = Console.ReadLine().Split(' '); int[] S = new int[n]; int sum1 = 0; for (int i = 0; i < n; ++i) { S[i] = int.Parse(str[i]); sum1 += S[i]; } double m = (double)sum1 / n; double sum2 = 0; for (int i = 0; i < n; ++i) { sum2 += Math.Pow((S[i] - m), 2); } double a = Math.Sqrt(sum2 / n); Console.WriteLine("{0:f8}", a); } } } }
[["+", 0, 200, 0, 212, 0, 16, 31, 74, 0, 24], ["+", 0, 200, 0, 212, 0, 16, 31, 74, 39, 199], ["+", 0, 200, 0, 212, 0, 16, 31, 74, 0, 25]]
8
210
3
using System; namespace ConsoleApplication26 { class Program { static void Main(string[] args) { while (true) { //?????° string N = Console.ReadLine(); int n = int.Parse(N); int Lav = 0; int Sav = 0; double Lhy = 0; double Shy = 0; if (n == 0) break; //?????° string[] s = Console.ReadLine().Split(' '); //?????? for (int i = 0; i < n; i++) { int av = int.Parse(s[i]); Sav += av; } Lav = Sav / n; //?¨?????????? for (int i = 0; i < n; i++) { int av = int.Parse(s[i]); double hy = Math.Pow(av - Lav, 2); Shy += hy; } Lhy = Math.Sqrt(Shy / n); Console.WriteLine(Lhy); } } } }
using System; namespace ConsoleApplication26 { class Program { static void Main(string[] args) { while (true) { //?????° string N = Console.ReadLine(); int n = int.Parse(N); double Lav = 0; double Sav = 0; double Lhy = 0; double Shy = 0; if (n == 0) break; //?????° string[] s = Console.ReadLine().Split(' '); //?????? for (int i = 0; i < n; i++) { int av = int.Parse(s[i]); Sav += av; } Lav = Sav / n; //?¨?????????? for (int i = 0; i < n; i++) { int av = int.Parse(s[i]); double hy = Math.Pow(av - Lav, 2); Shy += hy; } Lhy = Math.Sqrt(Shy / n); Console.WriteLine(Lhy); } } } }
[["-", 0, 52, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 52, 8, 196, 0, 197, 0, 198, 39, 199]]
8
201
4
using System; using System.Collections.Generic; using System.Linq; namespace ITP1_10_C { class Program { static void Main(string[] args) { int member; var result = new List<double>(); while ((member = int.Parse(Console.ReadLine())) != 0) { int[] input = Console.ReadLine() .Split(' ') .Select(x => Convert.ToInt32(x)) .ToArray(); double ave = input.Sum() / member; double disp = 0.0; foreach (var i in input) disp += Math.Pow((i - ave), 2); result.Add(Math.Sqrt(disp / member)); } foreach (var r in result) Console.WriteLine(r.ToString("f5")); } } }
using System; using System.Collections.Generic; using System.Linq; namespace ITP1_10_C { class Program { static void Main(string[] args) { double member; var result = new List<double>(); while ((member = double.Parse(Console.ReadLine())) != 0.0) { double[] input = Console.ReadLine() .Split(' ') .Select(x => Convert.ToDouble(x)) .ToArray(); double ave = input.Sum() / member; double disp = 0.0; foreach (var i in input) disp += Math.Pow((i - ave), 2); result.Add(Math.Sqrt(disp / member)); } foreach (var r in result) Console.WriteLine(r.ToString("f5")); } } }
[["-", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["+", 0, 195, 8, 196, 0, 197, 0, 198, 39, 199], ["-", 31, 23, 0, 11, 12, 213, 63, 214, 205, 199], ["+", 31, 23, 0, 11, 12, 213, 63, 214, 205, 199], ["-", 0, 195, 8, 196, 0, 52, 15, 16, 12, 203], ["+", 0, 195, 8, 196, 0, 52, 15, 16, 12, 250], ["-", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["+", 8, 196, 0, 197, 0, 198, 39, 224, 39, 199], ["-", 0, 28, 0, 218, 8, 213, 63, 214, 141, 22], ["+", 0, 28, 0, 218, 8, 213, 63, 214, 141, 22]]
8
176
10
using System; public class Class1 { public static void Main(String[] args) { int n = 0; while ((n = int.Parse(Console.ReadLine())) != 0) { string line = Console.ReadLine(); string[] data = line.Split(); double[] score = new double[n]; double sum = 0; double square = 0; for (int i = 0; i < n; i++) { score[i] = double.Parse(data[i]); sum += score[i]; } double aver = sum / n; for (int j = 0; j < n; j++) { square += Math.Pow((aver - score[j]), 2); } Console.WriteLine(Math.Sqrt(square)); } } }
using System; public class Class1 { public static void Main(String[] args) { int n = 0; while ((n = int.Parse(Console.ReadLine())) != 0) { string line = Console.ReadLine(); string[] data = line.Split(); double[] score = new double[n]; double sum = 0; double square = 0; for (int i = 0; i < n; i++) { score[i] = double.Parse(data[i]); sum += score[i]; } double aver = sum / n; for (int j = 0; j < n; j++) { square += Math.Pow((aver - score[j]), 2); } Console.WriteLine(Math.Sqrt(square / n)); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 85], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
178
2
using System; using System.Linq; namespace Math_Functions { class Standard_Deviation { static void Main(string[] args) { while ((int.Parse(Console.ReadLine())) != 0) { var score = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); var score2 = score.Select(s => s * s).ToArray(); Console.WriteLine(score2.Average() - score.Average() * score.Average()); } } } }
using System; using System.Linq; namespace Math_Functions { class Standard_Deviation { static void Main(string[] args) { while ((int.Parse(Console.ReadLine())) != 0) { var score = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); var score2 = score.Select(s => s * s).ToArray(); Console.WriteLine( Math.Sqrt(score2.Average() - score.Average() * score.Average())); } } } }
[["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["+", 0, 16, 12, 16, 12, 213, 3, 4, 0, 25]]
8
119
5
using System; class Cubic { static void Main() { int x = int.Parse(Console.ReadLine()); Console.WriteLine(x); } }
using System; class Cubic { static void Main() { int x = int.Parse(Console.ReadLine()); Console.WriteLine(x * x * x); } }
[["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
35
4
using System; class Hello_World { static void Main() { int n = Convert.ToInt32(System.Console.Read()); Console.WriteLine(n * n * n); } }
using System; class Hello_World { static void Main() { int n = Convert.ToInt32(System.Console.ReadLine()); Console.WriteLine(n * n * n); } }
[["-", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22]]
8
41
2
using System; class Test { static void Main(string[] args) { var i = int.Parse(args[0]); if (!(1 <= i && i <= 100)) return; Console.WriteLine(i * i * i); } }
using System; class Test { static void Main(string[] args) { var i = int.Parse(Console.ReadLine()); if (!(1 <= i && i <= 100)) return; Console.WriteLine(i * i * i); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["-", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 205, 22], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 0, 131], ["+", 3, 4, 0, 28, 0, 213, 63, 214, 141, 22], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 24], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 25]]
8
57
9
using System; class hoge { static void Main(String[] args) { String input = Console.ReadLine(); int x = int.Parse(input); Console.WriteLine(x ^ 3); } }
using System; class hoge { static void Main(String[] args) { String input = Console.ReadLine(); int x = int.Parse(input); Console.WriteLine(x * x * x); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 140], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
46
6
using System; namespace ConsoleApp10 { class Program { static void Main(string[] args) { string str = Console.ReadLine(); int x = int.Parse(str); Console.WriteLine(Math.Pow(3, x)); } } }
using System; namespace ITP1_1_B { class Program { static void Main(string[] args) { string str = Console.ReadLine(); int x = int.Parse(str); Console.WriteLine(Math.Pow(x, 3)); } } }
[["-", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["-", 0, 28, 0, 213, 3, 4, 0, 28, 0, 203], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["+", 0, 28, 0, 213, 3, 4, 0, 28, 0, 203]]
8
55
6
using System; namespace _1_1c { class Program { static void Main(string[] args) { int piyo = 1; int perimeter = 0; string ab; string[] side; ab = Console.ReadLine(); side = ab.Split(' '); foreach (string hoge in side) { piyo = piyo * int.Parse(hoge); perimeter += int.Parse(hoge); } Console.WriteLine("{0} {1}", piyo, perimeter); } } }
using System; namespace _1_1c { class Program { static void Main(string[] args) { int piyo = 1; int perimeter = 0; string ab; string[] side; ab = Console.ReadLine(); side = ab.Split(' '); foreach (string hoge in side) { piyo = piyo * int.Parse(hoge); perimeter += int.Parse(hoge) * 2; } Console.WriteLine("{0} {1}", piyo, perimeter); } } }
[["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203]]
8
101
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(' '); int h = int.Parse(str[0]); int w = int.Parse(str[1]); Console.WriteLine(h * w + " " + 2 * h + 2 * w); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split(' '); int h = int.Parse(str[0]); int w = int.Parse(str[1]); Console.WriteLine(h * w + " " + (2 * h + 2 * w)); } }
[["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
100
2
using System; using System.Linq; using System.Collections.Generic; public class rectangle { public static void Main() { string[] strs = Console.ReadLine().Split(' '); int[] num = new int[strs.Length]; for (int i = 0; i < strs.Length; i++) { num[i] = int.Parse(strs[i]); } System.Console.WriteLine((num[0] * num[1]) + " "); System.Console.WriteLine((num[0] + num[1]) * 2); } }
using System; using System.Linq; using System.Collections.Generic; public class rectangle { public static void Main() { string[] strs = Console.ReadLine().Split(' '); int[] num = new int[strs.Length]; for (int i = 0; i < strs.Length; i++) { num[i] = int.Parse(strs[i]); } System.Console.Write((num[0] * num[1]) + " "); System.Console.WriteLine((num[0] + num[1]) * 2); } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22]]
8
136
2
using System; using System.Linq; namespace AOJ000 { class Program { static void Main(string[] args) { var str = Console.ReadLine(); var a = int.Parse(str.Split(' ')[0]); var b = int.Parse(str.Split(' ')[1]); Console.WriteLine(a * b + " " + a * 2 + b * 2); } } }
using System; using System.Linq; namespace AOJ000 { class Program { static void Main(string[] args) { var str = Console.ReadLine(); var a = int.Parse(str.Split(' ')[0]); var b = int.Parse(str.Split(' ')[1]); Console.WriteLine(a * b + " " + (a * 2 + b * 2)); } } }
[["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
97
2
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { String[] str = Console.ReadLine().Split(' '); int a = int.Parse(str[0]); int b = int.Parse(str[1]); int x = a * b; int y = 2 * (a + b); Console.WriteLine(x + ' ' + y); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { String[] str = Console.ReadLine().Split(' '); int a = int.Parse(str[0]); int b = int.Parse(str[1]); int x = a * b; int y = 2 * (a + b); Console.WriteLine(x + " " + y); } } }
[["-", 0, 28, 0, 16, 31, 16, 12, 252, 0, 104], ["+", 0, 28, 0, 16, 31, 16, 12, 5, 0, 62]]
8
109
4
using System; namespace ConsoleApplication11 { class Program { static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int m = int.Parse(s[0]); int n = int.Parse(s[1]); Console.WriteLine(m * n + " " + (m * n) * 2); } } }
using System; namespace ConsoleApplication11 { class Program { static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int m = int.Parse(s[0]); int n = int.Parse(s[1]); Console.WriteLine(m * n + " " + (m + n) * 2); } } }
[["-", 0, 16, 12, 16, 31, 23, 0, 16, 17, 48], ["+", 0, 16, 12, 16, 31, 23, 0, 16, 17, 72]]
8
87
2
using System; namespace Application { class Program { public static void Main(string[] args) { string line = Console.ReadLine(); string[] vals = line.Split(' '); int a = int.Parse(vals[0]); int b = int.Parse(vals[1]); Console.WriteLine(a * b + " " + a * 2 + b * 2); } } }
using System; namespace Application { class Program { public static void Main(string[] args) { string line = Console.ReadLine(); string[] vals = line.Split(' '); int a = int.Parse(vals[0]); int b = int.Parse(vals[1]); Console.WriteLine(a * b + " " + (a * 2 + b * 2)); } } }
[["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
93
2
using System; namespace p3 { class prg { static void Main() { int a, b; { string[] str = Console.ReadLine().Split(' '); a = int.Parse(str[0]); b = int.Parse(str[1]); } Console.WriteLine((a * b) + (a * 2 + b * 2)); } } }
using System; namespace p3 { class prg { static void Main() { int a, b; { string[] str = Console.ReadLine().Split(' '); a = int.Parse(str[0]); b = int.Parse(str[1]); } Console.WriteLine((a * b) + " " + (a * 2 + b * 2)); } } }
[["+", 0, 28, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 28, 0, 16, 31, 16, 12, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72]]
8
88
4
using System; namespace ITP1_1_C { class Program { static void Main(string[] args) { string[] strs = Console.ReadLine().Split(' '); int a = int.Parse(strs[0]); int b = int.Parse(strs[1]); Console.WriteLine("{0} {1}", a * b, (a + b) >> 1); } } }
using System; namespace ITP1_1_A { class Program { static void Main(string[] args) { string[] strs = Console.ReadLine().Split(' '); int a = int.Parse(strs[0]); int b = int.Parse(strs[1]); Console.WriteLine("{0} {1}", a * b, (a + b) << 1); } } }
[["-", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 209, 141, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 152], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 151]]
8
87
4
using System; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string[] array = Console.ReadLine().Split(' '); int x = int.Parse(array[0]) * int.Parse(array[1]); int z = (int.Parse(array[0]) * 2) + (int.Parse(array[1]) * 2); Console.WriteLine(z + " " + x); } } }
using System; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string[] array = Console.ReadLine().Split(' '); int z = int.Parse(array[0]) * int.Parse(array[1]); int x = (int.Parse(array[0]) * 2) + (int.Parse(array[1]) * 2); Console.WriteLine(z + " " + x); } } }
[["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22]]
8
107
4
using System; using System.Linq; class Program { static void Main(string[] args) { var x_y = Console.ReadLine().Split(' '); var rectangleResult = 0; foreach (var val in x_y) { rectangleResult *= int.Parse(val); } var areaResult = x_y.Select(x => int.Parse(x) * 2).Sum(); Console.WriteLine(rectangleResult + " " + areaResult); } }
using System; using System.Linq; class Program { static void Main(string[] args) { var x_y = Console.ReadLine().Split(' '); var rectangleResult = 1; foreach (var val in x_y) { rectangleResult *= int.Parse(val); } var areaResult = x_y.Select(x => int.Parse(x) * 2).Sum(); Console.WriteLine(rectangleResult + " " + areaResult); } }
[["-", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203], ["+", 0, 197, 0, 198, 0, 200, 0, 212, 0, 203]]
8
98
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170409_AOJ { class Program { static void Main(string[] args) { string[] sArr = Console.ReadLine().Split(' '); int a = int.Parse(sArr[0]); int b = int.Parse(sArr[1]); int s = a * a; int l = 2 * (a + b); Console.WriteLine(string.Format("{0} {1}", s, l)); Console.Read(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170409_AOJ { class Program { static void Main(string[] args) { string[] sArr = Console.ReadLine().Split(' '); int a = int.Parse(sArr[0]); int b = int.Parse(sArr[1]); int s = a * b; int l = 2 * (a + b); Console.WriteLine(string.Format("{0} {1}", s, l)); Console.Read(); } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 16, 12, 22], ["+", 0, 198, 0, 200, 0, 212, 0, 16, 12, 22]]
8
125
2
using System; class Program { static void Main(string[] args) { int a, b, c, d; string[] str = Console.ReadLine().Split(' '); a = int.Parse(str[0]); b = int.Parse(str[1]); c = a + b; d = a * b; Console.WriteLine(d + " " + c); } }
using System; class Program { static void Main(string[] args) { int a, b, c, d; string[] str = Console.ReadLine().Split(' '); a = int.Parse(str[0]); b = int.Parse(str[1]); c = 2 * (a + b); d = a * b; Console.WriteLine(d + " " + c); } }
[["+", 8, 196, 0, 1, 0, 11, 12, 16, 31, 203], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
8
94
4
using System; namespace Getting_Started { class Rectangle { static void Main(string[] args) { string line = Console.ReadLine(); string[] token = line.Split(' '); int a = int.Parse(token[0]); int b = int.Parse(token[1]); Console.WriteLine(a * b + " " + 2 * a + 2 * b); } } }
using System; namespace Getting_Started { class Rectangle { static void Main(string[] args) { string line = Console.ReadLine(); string[] token = line.Split(' '); int a = int.Parse(token[0]); int b = int.Parse(token[1]); Console.WriteLine(a * b + " " + (2 * a + 2 * b)); } } }
[["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 12, 23, 0, 25]]
8
92
2
using System; public class Program { public static void Main(string[] args) { int tate = 0, yoko = 0, menseki = 0, syu = 0; string[] data = Console.ReadLine().Split(); //????????\????????? tate = int.Parse(data[0]); yoko = int.Parse(data[1]); menseki = tate * yoko; //??¢???????±????????????? syu = (tate * 2) + (yoko * 2); //??¨?????????????±????????????? Console.WriteLine("{0},{1}", menseki, syu); } }
using System; public class Test { public static void Main() { int tate = 0, yoko = 0, menseki = 0, syu = 0; string[] data = Console.ReadLine().Split(); //????????\????????? tate = int.Parse(data[0]); yoko = int.Parse(data[1]); menseki = tate * yoko; //??¢???????±????????????? syu = (tate * 2) + (yoko * 2); //??¨?????????????±????????????? Console.WriteLine("{0} {1}", menseki, syu); } }
[["-", 36, 36, 36, 36, 0, 208, 0, 235, 141, 22], ["+", 36, 36, 36, 36, 0, 208, 0, 235, 141, 22], ["-", 0, 195, 54, 55, 0, 220, 39, 224, 39, 199], ["-", 54, 55, 0, 220, 39, 224, 225, 226, 0, 70], ["-", 54, 55, 0, 220, 39, 224, 225, 226, 0, 73], ["-", 8, 201, 0, 195, 54, 55, 0, 220, 141, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
112
8
using System; using System.Linq; class Program { static void Main(string[] args) { short[] l = Console.ReadLine().Split(' ').Select(x => short.Parse(x)).ToArray(); Console.WriteLine(l[1] * l[1] + " " + (l[0] * 2 + l[1] * 2)); } }
using System; using System.Linq; class Program { static void Main(string[] args) { short[] l = Console.ReadLine().Split(' ').Select(x => short.Parse(x)).ToArray(); Console.WriteLine(l[0] * l[1] + " " + (l[0] * 2 + l[1] * 2)); } }
[["-", 31, 16, 31, 204, 206, 207, 0, 28, 0, 203], ["+", 31, 16, 31, 204, 206, 207, 0, 28, 0, 203]]
8
92
2
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split( ' '); // 2?????\?????????????????????????????\???????????? var a = int.Parse(str[0]); //??°?????§????????????????????´?????????????????? var b = int.Parse(str[1]); Console.WriteLine("{0} {1}", a * b, a * b * 2); } } }
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] str = Console.ReadLine().Split( ' '); // 2?????\?????????????????????????????\???????????? var a = int.Parse(str[0]); //??°?????§????????????????????´?????????????????? var b = int.Parse(str[1]); Console.WriteLine("{0} {1}", a * b, (a + b) * 2); } } }
[["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 24], ["-", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 0, 28, 0, 16, 31, 23, 0, 16, 17, 72], ["+", 3, 4, 0, 28, 0, 16, 31, 23, 0, 25]]
8
87
4
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string input = Console.ReadLine(); string[] dw = input.Split(' '); int length = Int32.Parse(dw[0]) * Int32.Parse(dw[1]); int area = (Int32.Parse(dw[0]) + Int32.Parse(dw[1])) * 2; Console.WriteLine("{0} {1}", area, length); } } }
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string input = Console.ReadLine(); string[] dw = input.Split(' '); int area = Int32.Parse(dw[0]) * Int32.Parse(dw[1]); int length = (Int32.Parse(dw[0]) + Int32.Parse(dw[1])) * 2; Console.WriteLine("{0} {1}", area, length); } } }
[["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22]]
8
108
4
using System.IO; using System; class Program { static void Main() { string[] c = Console.ReadLine().Split(); var a = int.Parse(c[0]); var b = int.Parse(c[0]); var area = a * b; var outline = 2 * (a + b); Console.WriteLine("{0} {1}", area, outline); } }
using System.IO; using System; class Program { static void Main() { string[] c = Console.ReadLine().Split(); var a = int.Parse(c[0]); var b = int.Parse(c[1]); var area = a * b; var outline = 2 * (a + b); Console.WriteLine("{0} {1}", area, outline); } }
[["-", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203], ["+", 0, 28, 0, 204, 206, 207, 0, 28, 0, 203]]
8
91
2
using System; namespace ITP1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); double[] x = new double[n]; double[] y = new double[n]; string[] input = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { x[i] = double.Parse(input[i]); } string[] input2 = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { y[i] = double.Parse(input2[i]); } double sum = 0; double sum2 = 0; double sum3 = 0; double max = 0; for (int i = 0; i < n; i++) { double t = Math.Abs(x[i] - y[i]); sum += t; sum2 += t * t; sum3 += t * t * t; if (t > max) max = t; } double ans2 = Math.Sqrt(sum2); double ans3 = Math.Pow(sum3, 1 / 3); Console.WriteLine(sum.ToString("f0")); Console.WriteLine(ans2.ToString("f5")); Console.WriteLine(ans3.ToString("f5")); Console.WriteLine(max.ToString("f0")); } } }
using System; namespace ITP1 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); double[] x = new double[n]; double[] y = new double[n]; string[] input = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { x[i] = double.Parse(input[i]); } string[] input2 = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { y[i] = double.Parse(input2[i]); } double sum = 0; double sum2 = 0; double sum3 = 0; double max = 0; for (int i = 0; i < n; i++) { double t = Math.Abs(x[i] - y[i]); sum += t; sum2 += t * t; sum3 += t * t * t; if (t > max) max = t; } double ans2 = Math.Sqrt(sum2); double ans3 = Math.Pow(sum3, (double)1 / 3); Console.WriteLine(sum.ToString("f0")); Console.WriteLine(ans2.ToString("f5")); Console.WriteLine(ans3.ToString("f5")); Console.WriteLine(max.ToString("f0")); } } }
[["+", 3, 4, 0, 28, 0, 16, 31, 74, 0, 24], ["+", 3, 4, 0, 28, 0, 16, 31, 74, 39, 199], ["+", 3, 4, 0, 28, 0, 16, 31, 74, 0, 25]]
8
318
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { const int diceUpDownSum = 7; static void Main(string[] args) { int id = 0; var diceList = Console.ReadLine() .Split(' ') .Select(n => new Dice(++id, int.Parse(n))) .ToList(); var commandItem = Console.ReadLine().ToArray(); int top = 1; int s = 2; int e = 3; foreach (var command in commandItem) { int work = 0; work = top; switch (command) { case 'S': top = diceUpDownSum - s; s = work; break; case 'E': top = diceUpDownSum - e; e = work; break; case 'W': top = e; e = diceUpDownSum - top; break; case 'N': top = s; s = diceUpDownSum - top; break; } } Console.WriteLine(diceList.SingleOrDefault(n => n.Id == top).Number); Console.ReadKey(); } } class Dice { public int Id { get; set; } public int Number { get; set; } public Dice(int id, int number) { Id = id; Number = number; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { const int diceUpDownSum = 7; static void Main(string[] args) { int id = 0; var diceList = Console.ReadLine() .Split(' ') .Select(n => new Dice(++id, int.Parse(n))) .ToList(); var commandItem = Console.ReadLine().ToArray(); int top = 1; int s = 2; int e = 3; foreach (var command in commandItem) { int work = 0; work = top; switch (command) { case 'S': top = diceUpDownSum - s; s = work; break; case 'E': top = diceUpDownSum - e; e = work; break; case 'W': top = e; e = diceUpDownSum - work; break; case 'N': top = s; s = diceUpDownSum - work; break; } } Console.WriteLine(diceList.SingleOrDefault(n => n.Id == top).Number); Console.ReadKey(); } } class Dice { public int Id { get; set; } public int Number { get; set; } public Dice(int id, int number) { Id = id; Number = number; } } }
[["-", 0, 239, 0, 1, 0, 11, 12, 16, 12, 22], ["+", 0, 239, 0, 1, 0, 11, 12, 16, 12, 22]]
8
276
4
using System; using System.Linq; namespace Structure_and_Class { class Dice_I { static void Main(string[] args) { var dice = new Dice( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); dice.Move(Console.ReadLine()); dice.Show(); } } public class Dice { int[] label = new int[6]; public Dice(int[] label) { this.label = label; } public void Show() { Console.WriteLine(label[0]); } public void Move(string s) { for (int i = 0; i < s.Length; i++) { Move(s[i]); } } public void Move(char c) { int[] res = new int[6]; switch (c) { case 'E': res[0] = label[3]; res[1] = label[1]; res[2] = label[0]; res[3] = label[5]; res[4] = label[4]; res[5] = label[2]; label = res; break; case 'N': res[0] = label[1]; res[1] = label[5]; res[2] = label[2]; res[3] = label[3]; res[4] = label[0]; res[5] = label[4]; break; case 'S': Move('N'); Move('N'); Move('N'); break; case 'W': Move('E'); Move('E'); Move('E'); break; } } } }
using System; using System.Linq; namespace Structure_and_Class { class Dice_I { static void Main(string[] args) { var dice = new Dice( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); dice.Move(Console.ReadLine()); dice.Show(); } } public class Dice { int[] label = new int[6]; public Dice(int[] label) { this.label = label; } public void Show() { Console.WriteLine(label[0]); } public void Move(string s) { for (int i = 0; i < s.Length; i++) { Move(s[i]); } } public void Move(char c) { int[] res = new int[6]; switch (c) { case 'E': res[0] = label[3]; res[1] = label[1]; res[2] = label[0]; res[3] = label[5]; res[4] = label[4]; res[5] = label[2]; label = res; break; case 'N': res[0] = label[1]; res[1] = label[5]; res[2] = label[2]; res[3] = label[3]; res[4] = label[0]; res[5] = label[4]; label = res; break; case 'S': Move('N'); Move('N'); Move('N'); break; case 'W': Move('E'); Move('E'); Move('E'); break; } } } }
[["+", 8, 254, 0, 239, 0, 1, 0, 11, 31, 22], ["+", 0, 239, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 254, 0, 239, 0, 1, 0, 11, 12, 22], ["+", 0, 99, 8, 254, 0, 239, 0, 1, 0, 35]]
8
384
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_B { public class Program { public class Dice { public int top; public int front; public int right; public int left; public int back; public int bottom; public Dice(int[] num) { this.top = num[0]; this.front = num[1]; this.right = num[2]; this.left = num[3]; this.back = num[4]; this.bottom = num[5]; } public void RotateX() // X軸に対し時計回りに回転 { int tmp = this.top; this.top = this.back; this.back = this.bottom; this.bottom = this.front; this.front = tmp; } public void RotateY() // Y軸に対し時計回りに回転 { int tmp = this.right; this.right = this.top; this.top = this.left; this.left = this.bottom; this.bottom = tmp; } public void RotateZ() // Z軸に対し時計回りに回転 { int tmp = this.front; this.front = this.right; this.right = this.back; this.back = this.left; this.left = tmp; } public void Rotate(Char cmd) { switch (cmd) { default: break; case 'X': RotateX(); break; case 'Y': RotateY(); break; case 'Z': RotateZ(); break; } } } public static void Main(string[] args) { int[] line = ReadIntAr(); int n = ReadInt(); Dice dice = new Dice(line); String cmd = "ZZZXYYYZXXXZYYYZXXXYZZZ"; for (int i = 0; i < n; i++) { int[] topFront = ReadIntAr(); for (int j = 0; j <= cmd.Length; j++) { dice.Rotate(cmd[j]); if (topFront[0] == dice.top && topFront[1] == dice.front) { Console.WriteLine(dice.right.ToString()); break; } } } } static string ReadSt() { return Console.ReadLine(); } static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static double ReadDouble() { return double.Parse(Console.ReadLine()); } static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_B { public class Program { public class Dice { public int top; public int front; public int right; public int left; public int back; public int bottom; public Dice(int[] num) { this.top = num[0]; this.front = num[1]; this.right = num[2]; this.left = num[3]; this.back = num[4]; this.bottom = num[5]; } public void RotateX() // X軸に対し時計回りに回転 { int tmp = this.top; this.top = this.back; this.back = this.bottom; this.bottom = this.front; this.front = tmp; } public void RotateY() // Y軸に対し時計回りに回転 { int tmp = this.right; this.right = this.top; this.top = this.left; this.left = this.bottom; this.bottom = tmp; } public void RotateZ() // Z軸に対し時計回りに回転 { int tmp = this.front; this.front = this.right; this.right = this.back; this.back = this.left; this.left = tmp; } public void Rotate(Char cmd) { switch (cmd) { default: break; case 'X': RotateX(); break; case 'Y': RotateY(); break; case 'Z': RotateZ(); break; } } } public static void Main(string[] args) { int[] line = ReadIntAr(); int n = ReadInt(); Dice dice = new Dice(line); String cmd = " ZZZXYYYZXXXZYYYZXXXYZZZ"; for (int i = 0; i < n; i++) { int[] topFront = ReadIntAr(); for (int j = 0; j <= cmd.Length; j++) { dice.Rotate(cmd[j]); if (topFront[0] == dice.top && topFront[1] == dice.front) { Console.WriteLine(dice.right.ToString()); break; } } } } static string ReadSt() { return Console.ReadLine(); } static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static double ReadDouble() { return double.Parse(Console.ReadLine()); } static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222], ["+", 0, 198, 0, 200, 0, 212, 0, 5, 0, 222]]
8
664
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dice2 { class Program { static void Main(string[] args) { string st1 = Console.ReadLine(); string[] dice = st1.Trim().Split(' '); string[] search1 = { "S", "S", "S", "S", "W", "W", "W" }; string[] search2 = { "R", "R", "R", "R" }; bool endflug = false; // string st2 = Console.ReadLine( ); int Qtimes = int.Parse(Console.ReadLine()); int[] ans = new int[Qtimes]; // string ans = ""; for (int i = 0; i < Qtimes; i++) { endflug = false; string st2 = Console.ReadLine(); string[] question = st2.Trim().Split(' '); for (int j = 0; j < 6; j++) { // Console.WriteLine("{0}, {1}(上面揃った?)", question[ 0 ], dice[ 0 ] // ); if (question[0] == dice[0]) { // Console.WriteLine( "上面揃った" ); for (int k = 0; k < 4; k++) { // Console.WriteLine( "{0}, {1}(前面揃った?)", question[ 1 ], dice[ // 1 ] ); dice = Diceroll(dice, search2[k]); if (question[1] == dice[1]) { // Console.WriteLine( "前面揃った" ); ans[i] = int.Parse(dice[2]); endflug = true; break; } } } if (endflug != true) { dice = Diceroll(dice, search1[j]); } else { break; } } } for (int i = 0; i < Qtimes; i++) { Console.WriteLine(ans[i]); } } public static string[] Diceroll(string[] input, string command) { string[] Dice = input; string tmp = ""; switch (command) { case "S": tmp = Dice[0]; Dice[0] = Dice[4]; Dice[4] = Dice[5]; Dice[5] = Dice[1]; Dice[1] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "");/**/ break; case "N": tmp = Dice[0]; Dice[0] = Dice[1]; Dice[1] = Dice[5]; Dice[5] = Dice[4]; Dice[4] = tmp; break; case "W": tmp = Dice[0]; Dice[0] = Dice[2]; Dice[2] = Dice[5]; Dice[5] = Dice[3]; Dice[3] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "" );/**/ break; case "E": tmp = Dice[0]; Dice[0] = Dice[3]; Dice[3] = Dice[5]; Dice[5] = Dice[2]; Dice[2] = tmp; break; case "R": tmp = Dice[1]; Dice[1] = Dice[2]; Dice[2] = Dice[4]; Dice[4] = Dice[3]; Dice[3] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "" );/**/ break; } return (Dice); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dice2 { class Program { static void Main(string[] args) { string st1 = Console.ReadLine(); string[] dice = st1.Trim().Split(' '); string[] search1 = { "S", "S", "S", "S", "W", "W", "W", "W" }; string[] search2 = { "R", "R", "R", "R" }; bool endflug = false; // string st2 = Console.ReadLine( ); int Qtimes = int.Parse(Console.ReadLine()); int[] ans = new int[Qtimes]; // string ans = ""; for (int i = 0; i < Qtimes; i++) { endflug = false; string st2 = Console.ReadLine(); string[] question = st2.Trim().Split(' '); for (int j = 0; j < 8; j++) { // Console.WriteLine("{0}, {1}(上面揃った?)", question[ 0 ], dice[ 0 ] // ); if (question[0] == dice[0]) { // Console.WriteLine( "上面揃った" ); for (int k = 0; k < 4; k++) { // Console.WriteLine( "{0}, {1}(前面揃った?)", question[ 1 ], dice[ // 1 ] ); dice = Diceroll(dice, search2[k]); if (question[1] == dice[1]) { // Console.WriteLine( "前面揃った" ); ans[i] = int.Parse(dice[2]); endflug = true; break; } } } if (endflug != true) { dice = Diceroll(dice, search1[j]); } else { break; } } } for (int i = 0; i < Qtimes; i++) { Console.WriteLine(ans[i]); } } public static string[] Diceroll(string[] input, string command) { string[] Dice = input; string tmp = ""; switch (command) { case "S": tmp = Dice[0]; Dice[0] = Dice[4]; Dice[4] = Dice[5]; Dice[5] = Dice[1]; Dice[1] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "");/**/ break; case "N": tmp = Dice[0]; Dice[0] = Dice[1]; Dice[1] = Dice[5]; Dice[5] = Dice[4]; Dice[4] = tmp; break; case "W": tmp = Dice[0]; Dice[0] = Dice[2]; Dice[2] = Dice[5]; Dice[5] = Dice[3]; Dice[3] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "" );/**/ break; case "E": tmp = Dice[0]; Dice[0] = Dice[3]; Dice[3] = Dice[5]; Dice[5] = Dice[2]; Dice[2] = tmp; break; case "R": tmp = Dice[1]; Dice[1] = Dice[2]; Dice[2] = Dice[4]; Dice[4] = Dice[3]; Dice[3] = tmp; /*for( int i = 0 ; i < 6 ; i++ ) { Console.WriteLine( Dice[ i ] ); } Console.WriteLine( "" );/**/ break; } return (Dice); } } }
[["+", 0, 198, 0, 200, 0, 212, 0, 229, 0, 21], ["+", 0, 200, 0, 212, 0, 229, 0, 5, 0, 62], ["+", 0, 200, 0, 212, 0, 229, 0, 5, 0, 222], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 203]]
8
659
6
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Dice { private int[] dicelabel = new int[6]; //???????????????°?????????± private int temp = 0; public Dice( string num) //???????????????°?????????±??????????????????????????? { var dicenum = num.Split(' '); for (int i = 0; i < 6; i++) { dicelabel[i] = int.Parse(dicenum[i]); } } public void roll(string order) //?????????????????????Switch???§?????? { switch (order[0]) { case 'N': temp = dicelabel[0]; dicelabel[0] = dicelabel[1]; dicelabel[1] = dicelabel[5]; dicelabel[5] = dicelabel[4]; dicelabel[4] = temp; break; case 'S': temp = dicelabel[0]; dicelabel[0] = dicelabel[4]; dicelabel[4] = dicelabel[5]; dicelabel[5] = dicelabel[1]; dicelabel[1] = temp; break; case 'E': temp = dicelabel[0]; dicelabel[0] = dicelabel[3]; dicelabel[3] = dicelabel[5]; dicelabel[5] = dicelabel[2]; dicelabel[2] = temp; break; case 'W': temp = dicelabel[0]; dicelabel[0] = dicelabel[2]; dicelabel[2] = dicelabel[5]; dicelabel[5] = dicelabel[3]; dicelabel[3] = temp; break; case 'R': temp = dicelabel[1]; dicelabel[1] = dicelabel[2]; dicelabel[2] = dicelabel[4]; dicelabel[4] = dicelabel[3]; dicelabel[3] = temp; break; } } public int getRightSide(string topfront) { string[] temps = topfront.Split(' '); int top = int.Parse(temps[0]); int front = int.Parse(temps[1]); bool match = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("N"); } for (int i = 0; i < 4; i++) { if (match) break; for (int j = 0; j < 4; j++) { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) break; } if (match) break; this.roll("E"); } return dicelabel[2]; } public int[] getLabel() { return dicelabel; } } class Program { static void Main(string[] args) { string input = Console.ReadLine(); Dice d = new Dice(input); //???????????????°?????????±????????? int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { string question = Console.ReadLine(); Console.WriteLine(d.getRightSide(question)); } } } }
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Dice { private int[] dicelabel = new int[6]; //???????????????°?????????± private int temp = 0; public Dice( string num) //???????????????°?????????±??????????????????????????? { var dicenum = num.Split(' '); for (int i = 0; i < 6; i++) { dicelabel[i] = int.Parse(dicenum[i]); } } public void roll(string order) //?????????????????????Switch???§?????? { switch (order[0]) { case 'N': temp = dicelabel[0]; dicelabel[0] = dicelabel[1]; dicelabel[1] = dicelabel[5]; dicelabel[5] = dicelabel[4]; dicelabel[4] = temp; break; case 'S': temp = dicelabel[0]; dicelabel[0] = dicelabel[4]; dicelabel[4] = dicelabel[5]; dicelabel[5] = dicelabel[1]; dicelabel[1] = temp; break; case 'E': temp = dicelabel[0]; dicelabel[0] = dicelabel[3]; dicelabel[3] = dicelabel[5]; dicelabel[5] = dicelabel[2]; dicelabel[2] = temp; break; case 'W': temp = dicelabel[0]; dicelabel[0] = dicelabel[2]; dicelabel[2] = dicelabel[5]; dicelabel[5] = dicelabel[3]; dicelabel[3] = temp; break; case 'R': temp = dicelabel[1]; dicelabel[1] = dicelabel[2]; dicelabel[2] = dicelabel[4]; dicelabel[4] = dicelabel[3]; dicelabel[3] = temp; break; } } public int getRightSide(string topfront) { string[] temps = topfront.Split(' '); int top = int.Parse(temps[0]); int front = int.Parse(temps[1]); bool match = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("N"); } for (int i = 0; i < 4; i++) { if (match) break; for (int j = 0; j < 4; j++) { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("E"); } return dicelabel[2]; } public int[] getLabel() { return dicelabel; } } class Program { static void Main(string[] args) { string input = Console.ReadLine(); Dice d = new Dice(input); //???????????????°?????????±????????? int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { string question = Console.ReadLine(); Console.WriteLine(d.getRightSide(question)); } } } }
[["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 64, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 64, 196, 0, 1, 0, 11, 12, 211, 0, 146], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
8
680
6
using System; namespace ITP1_11_B { class Dice { private int[,] adj = new int[6, 4] { { 2, 3, 5, 4 }, { 1, 4, 6, 3 }, { 1, 2, 6, 5 }, { 1, 5, 6, 2 }, { 1, 3, 6, 4 }, { 2, 4, 5, 3 } }; public int Top { get; private set; } public int North { get; private set; } public Dice(int top = 1, int north = 5) { Top = top; North = north; } public int GetRight(int top, int front) { int index = GetIndex(top, front); return adj[top - 1, (index + 1) % 4]; } private int GetIndex(int top, int val) { for (int i = 0; i < 4; i++) if (adj[top - 1, i] == val) return i; throw new Exception(); } } class Program { static void Main(string[] args) { string[] strs = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(strs, int.Parse); int n = int.Parse(Console.ReadLine()); Dice dice = new Dice(); for (int i = 0; i < n; i++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Console.WriteLine( dice.GetRight(GetNum(a, input[0]), GetNum(a, input[1]))); } } static int GetNum(int[] array, int val) { for (int i = 0; i < array.Length; i++) if (array[i] == val) return i + 1; throw new Exception(); } } }
using System; namespace ITP1_11_B { class Dice { private int[,] adj = new int[6, 4] { { 2, 3, 5, 4 }, { 1, 4, 6, 3 }, { 1, 2, 6, 5 }, { 1, 5, 6, 2 }, { 1, 3, 6, 4 }, { 2, 4, 5, 3 } }; public int Top { get; private set; } public int North { get; private set; } public Dice(int top = 1, int north = 5) { Top = top; North = north; } public int GetRight(int top, int front) { int index = GetIndex(top, front); return adj[top - 1, (index + 1) % 4]; } private int GetIndex(int top, int val) { for (int i = 0; i < 4; i++) if (adj[top - 1, i] == val) return i; throw new Exception(); } } class Program { static void Main(string[] args) { string[] strs = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(strs, int.Parse); int n = int.Parse(Console.ReadLine()); Dice dice = new Dice(); for (int i = 0; i < n; i++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Console.WriteLine( a[dice.GetRight(GetNum(a, input[0]), GetNum(a, input[1])) - 1]); } } static int GetNum(int[] array, int val) { for (int i = 0; i < array.Length; i++) if (array[i] == val) return i + 1; throw new Exception(); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 70], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 0, 204, 206, 207, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 204, 206, 207, 0, 73]]
8
410
5
using System; using System.Linq; namespace Structure_and_Class { class Dice_II { static void Main(string[] args) { var dice = new Dice2( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); var q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { var token = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); dice.ShowRight(token[0], token[1]); } } } public class Dice2 { int[] label = new int[6]; private int[,] rightmap; public Dice2(int[] label) { this.label = label; } public void ShowRight(int up, int front) { int upIndex = 0; int frontIndex = 0; for (int i = 0; i < label.Length; i++) { if (label[i] == up) upIndex = i; else if (label[i] == front) frontIndex = i; } rightmap = new int[,] { { -1, 2, 4, 3, 1, -1 }, { 3, -1, 0, 5, -1, 2 }, { 1, 5, -1, -1, 0, 4 }, { 4, 0, -1, -1, 5, 1 }, { 2, -1, 5, 0, -1, 3 }, { -1, 3, 1, 4, 2, -1 } }; Console.WriteLine(label[rightmap[upIndex, frontIndex]]); } } }
using System; using System.Linq; namespace Structure_and_Class { class Dice_II { static void Main(string[] args) { var dice = new Dice2( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); var q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { var token = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); dice.ShowRight(token[0], token[1]); } } } public class Dice2 { int[] label = new int[6]; private int[,] rightmap; public Dice2(int[] label) { this.label = label; } public void ShowRight(int up, int front) { int upIndex = 0; int frontIndex = 0; for (int i = 0; i < label.Length; i++) { if (label[i] == up) upIndex = i; else if (label[i] == front) frontIndex = i; } rightmap = new int[,] { { -1, 2, 4, 1, 3, -1 }, { 3, -1, 0, 5, -1, 2 }, { 1, 5, -1, -1, 0, 4 }, { 4, 0, -1, -1, 5, 1 }, { 2, -1, 5, 0, -1, 3 }, { -1, 3, 1, 4, 2, -1 } }; Console.WriteLine(label[rightmap[upIndex, frontIndex]]); } } }
[["-", 0, 11, 12, 227, 0, 229, 0, 229, 0, 203], ["-", 0, 11, 12, 227, 0, 229, 0, 229, 0, 21], ["+", 0, 11, 12, 227, 0, 229, 0, 229, 0, 21], ["+", 0, 11, 12, 227, 0, 229, 0, 229, 0, 203]]
8
366
4
using System; using System.Collections.Generic; using System.Text; namespace ITP1_11_C { class Program { // faces[] = { 0, 1, 2, 3, 4, 5, 6 }; // 0 // 3 1 2 4 // 5 static void Main(string[] args) { string s1 = Console.ReadLine(); string[] faces1 = s1.Split(); string s2 = Console.ReadLine(); string[] faces2 = s2.Split(); Dice dice1 = new Dice(faces1); bool f = false; for (int i = 0; i < 6; i++) { Dice dice2 = new Dice(faces2); switch (i) { case 0: break; case 1: dice2.MoveS(); break; case 2: dice2.MoveN(); break; case 3: dice2.MoveW(); break; case 4: dice2.MoveE(); break; case 5: dice2.MoveN(); dice2.MoveN(); break; } for (int j = 0; j < 4; j++) { if (dice1.Top == dice2.Top && dice1.Bottom == dice2.Bottom && dice1.North == dice2.North && dice1.South == dice2.South && dice1.East == dice2.East && dice1.West == dice2.West) { f = true; break; } } if (f) { break; } } if (f) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } // Console.WriteLine("Hit Any Key"); // Console.ReadLine(); } } class Dice { // 1 // 4 2 3 5 // 6 public string Top { get; private set; } public string North { get; private set; } public string South { get; private set; } public string East { get; private set; } public string West { get; private set; } public string Bottom { get; private set; } public Dice(string[] faces) { Top = faces[0]; North = faces[4]; South = faces[1]; East = faces[2]; West = faces[3]; Bottom = faces[5]; } public void MoveN() { // T<-S<-B<-N<-T string tmp = Top; Top = South; South = Bottom; Bottom = North; North = tmp; } public void MoveS() { // T<-N<-B<-S<-T string tmp = Top; Top = North; North = Bottom; Bottom = South; South = tmp; } public void MoveE() { // T<-W<-B<-E<-T string tmp = Top; Top = West; West = Bottom; Bottom = East; East = tmp; } public void MoveW() { // T<-E<-B<-W<-T string tmp = Top; Top = East; East = Bottom; Bottom = West; West = tmp; } public void MoveCW() { // N<-W<-S<-E<-N string tmp = North; North = West; West = South; South = East; East = tmp; } public void MoveCCW() { // N<-E<-S<-W<-N string tmp = North; North = East; East = South; South = West; West = tmp; } } }
using System; using System.Collections.Generic; using System.Text; namespace ITP1_11_C { class Program { // faces[] = { 0, 1, 2, 3, 4, 5, 6 }; // 0 // 3 1 2 4 // 5 static void Main(string[] args) { string s1 = Console.ReadLine(); string[] faces1 = s1.Split(); string s2 = Console.ReadLine(); string[] faces2 = s2.Split(); Dice dice1 = new Dice(faces1); bool f = false; for (int i = 0; i < 6; i++) { Dice dice2 = new Dice(faces2); switch (i) { case 0: break; case 1: dice2.MoveS(); break; case 2: dice2.MoveN(); break; case 3: dice2.MoveW(); break; case 4: dice2.MoveE(); break; case 5: dice2.MoveN(); dice2.MoveN(); break; } for (int j = 0; j < 4; j++) { if (dice1.Top == dice2.Top && dice1.Bottom == dice2.Bottom && dice1.North == dice2.North && dice1.South == dice2.South && dice1.East == dice2.East && dice1.West == dice2.West) { f = true; break; } dice2.MoveCW(); } if (f) { break; } } if (f) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } // Console.WriteLine("Hit Any Key"); // Console.ReadLine(); } } class Dice { // 1 // 4 2 3 5 // 6 public string Top { get; private set; } public string North { get; private set; } public string South { get; private set; } public string East { get; private set; } public string West { get; private set; } public string Bottom { get; private set; } public Dice(string[] faces) { Top = faces[0]; North = faces[4]; South = faces[1]; East = faces[2]; West = faces[3]; Bottom = faces[5]; } public void MoveN() { // T<-S<-B<-N<-T string tmp = Top; Top = South; South = Bottom; Bottom = North; North = tmp; } public void MoveS() { // T<-N<-B<-S<-T string tmp = Top; Top = North; North = Bottom; Bottom = South; South = tmp; } public void MoveE() { // T<-W<-B<-E<-T string tmp = Top; Top = West; West = Bottom; Bottom = East; East = tmp; } public void MoveW() { // T<-E<-B<-W<-T string tmp = Top; Top = East; East = Bottom; Bottom = West; West = tmp; } public void MoveCW() { // N<-W<-S<-E<-N string tmp = North; North = West; West = South; South = East; East = tmp; } public void MoveCCW() { // N<-E<-S<-W<-N string tmp = North; North = East; East = South; South = West; West = tmp; } } }
[["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 0, 131], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 25], ["+", 8, 196, 0, 7, 8, 196, 0, 1, 0, 35]]
8
593
6
using System; public class Program { public static void Main() { Dice d1 = new Dice(Console.ReadLine().Split(' ')); Dice d2 = new Dice(Console.ReadLine().Split(' ')); if (d1.IsEquals(d2)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } public class Dice { public string Left { get; private set; } public string Front { get; private set; } public string Right { get; private set; } public string Rear { get; private set; } public string Top { get; private set; } public string Bottom { get; private set; } // 4 // 0 1 2 3 // 5 public Dice(string[] faces) { Left = faces[0]; Front = faces[1]; Right = faces[2]; Rear = faces[3]; Top = faces[4]; Bottom = faces[5]; } public void RollX(bool direction) { var tmp = ""; if (direction) { tmp = Top; Top = Front; Front = Bottom; Bottom = Rear; Rear = tmp; } else { tmp = Top; Top = Rear; Rear = Bottom; Bottom = Front; Front = tmp; } } public void RollY(bool direction) { var tmp = ""; if (direction) { tmp = Top; Top = Right; Right = Bottom; Bottom = Left; Left = tmp; } else { tmp = Top; Top = Left; Left = Bottom; Bottom = Right; Right = tmp; } } public void RollZ(bool direction) { var tmp = ""; if (direction) { tmp = Front; Front = Right; Right = Rear; Rear = Left; Left = tmp; } else { tmp = Front; Front = Left; Left = Rear; Rear = Right; Right = tmp; } } public void Roll(char direction) { var c = char.ToUpper(direction); switch (c) { case 'N': RollX(true); break; case 'S': RollX(false); break; case 'W': RollY(true); break; case 'E': RollY(false); break; case 'U': RollZ(true); break; case 'D': RollZ(false); break; } // Console.WriteLine(this.Left + this.Front + this.Right + this.Rear + // this.Top + this.Bottom); } public bool IsEquals(Dice d) { foreach (var c in "UUUUNUUUUNUUUUNUUUUEUUUUSSUUUU".ToCharArray()) { if (this.Bottom == d.Bottom && this.Front == d.Front && this.Left == d.Left && this.Rear == d.Rear && this.Right == d.Right && this.Top == d.Top) { return true; } this.Roll(c); } return false; } }
using System; public class Program { public static void Main() { Dice d1 = new Dice(Console.ReadLine().Split(' ')); Dice d2 = new Dice(Console.ReadLine().Split(' ')); if (d1.IsEquals(d2)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } public class Dice { public string Left { get; private set; } public string Front { get; private set; } public string Right { get; private set; } public string Rear { get; private set; } public string Top { get; private set; } public string Bottom { get; private set; } // 4 // 3 0 2 5 // 1 public Dice(string[] faces) { Front = faces[0]; Bottom = faces[1]; Right = faces[2]; Left = faces[3]; Top = faces[4]; Rear = faces[5]; } public void RollX(bool direction) { var tmp = ""; if (direction) { tmp = Top; Top = Front; Front = Bottom; Bottom = Rear; Rear = tmp; } else { tmp = Top; Top = Rear; Rear = Bottom; Bottom = Front; Front = tmp; } } public void RollY(bool direction) { var tmp = ""; if (direction) { tmp = Top; Top = Right; Right = Bottom; Bottom = Left; Left = tmp; } else { tmp = Top; Top = Left; Left = Bottom; Bottom = Right; Right = tmp; } } public void RollZ(bool direction) { var tmp = ""; if (direction) { tmp = Front; Front = Right; Right = Rear; Rear = Left; Left = tmp; } else { tmp = Front; Front = Left; Left = Rear; Rear = Right; Right = tmp; } } public void Roll(char direction) { var c = char.ToUpper(direction); switch (c) { case 'N': RollX(true); break; case 'S': RollX(false); break; case 'W': RollY(true); break; case 'E': RollY(false); break; case 'U': RollZ(true); break; case 'D': RollZ(false); break; } // Console.WriteLine(this.Left + this.Front + this.Right + this.Rear + // this.Top + this.Bottom); } public bool IsEquals(Dice d) { foreach (var c in "UUUUNUUUUNUUUUNUUUUEUUUUSSUUUU".ToCharArray()) { if (this.Bottom == d.Bottom && this.Front == d.Front && this.Left == d.Left && this.Rear == d.Rear && this.Right == d.Right && this.Top == d.Top) { return true; } d.Roll(c); } return false; } }
[["-", 0, 263, 8, 196, 0, 1, 0, 11, 31, 22], ["+", 0, 263, 8, 196, 0, 1, 0, 11, 31, 22], ["-", 0, 1, 0, 213, 63, 214, 205, 273, 0, 274], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 205, 22]]
8
584
10
using System; namespace Project { class Dice { public int face; public int N; public int E; public int S; public int W; public int back; public Dice(int a1, int a2, int a3, int a4, int a5, int a6) { face = a1; N = a5; E = a3; S = a2; W = a4; back = a6; } public void roll(char D) { int pface = face; int pN = N; int pE = E; int pW = W; int pS = S; int pback = back; switch (D) { case 'N': face = pS; N = pface; S = pback; back = pN; break; case 'E': face = pW; E = pface; W = pback; back = pE; break; case 'W': face = pE; E = pback; W = pface; back = pW; break; case 'S': face = pN; N = pback; S = pface; back = pS; break; } } } class MainClass { public static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int[] a = new int[6]; for (int i = 0; i < 6; i++) { a[i] = int.Parse(s[i]); } Dice dice1 = new Dice(a[0], a[1], a[2], a[3], a[4], a[5]); s = Console.ReadLine().Split(' '); for (int i = 0; i < 6; i++) { a[i] = int.Parse(s[i]); } Dice dice2 = new Dice(a[0], a[1], a[2], a[3], a[4], a[5]); int count = 0; while (count < 10) { if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (count == 4) dice1.roll('N'); dice1.roll('W'); count++; } if (count == 10) Console.WriteLine("No"); else Console.WriteLine("Yes1"); } public static bool checkdice(Dice dice1, Dice dice2) { if (dice1.face == dice2.face && dice1.N == dice2.N && dice1.E == dice2.E && dice1.S == dice2.S && dice1.W == dice2.W && dice1.back == dice2.back) return true; else return false; } } }
using System; namespace Project { class Dice { public int face; public int N; public int E; public int S; public int W; public int back; public Dice(int a1, int a2, int a3, int a4, int a5, int a6) { face = a1; N = a5; E = a3; S = a2; W = a4; back = a6; } public void roll(char D) { int pface = face; int pN = N; int pE = E; int pW = W; int pS = S; int pback = back; switch (D) { case 'N': face = pS; N = pface; S = pback; back = pN; break; case 'E': face = pW; E = pface; W = pback; back = pE; break; case 'W': face = pE; E = pback; W = pface; back = pW; break; case 'S': face = pN; N = pback; S = pface; back = pS; break; } } } class MainClass { public static void Main(string[] args) { string[] s = Console.ReadLine().Split(' '); int[] a = new int[6]; for (int i = 0; i < 6; i++) { a[i] = int.Parse(s[i]); } Dice dice1 = new Dice(a[0], a[1], a[2], a[3], a[4], a[5]); s = Console.ReadLine().Split(' '); for (int i = 0; i < 6; i++) { a[i] = int.Parse(s[i]); } Dice dice2 = new Dice(a[0], a[1], a[2], a[3], a[4], a[5]); int count = 0; while (count < 10) { if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (checkdice(dice1, dice2)) break; dice1.roll('N'); if (count == 4) dice1.roll('N'); dice1.roll('W'); count++; } if (count == 10) Console.WriteLine("No"); else Console.WriteLine("Yes"); } public static bool checkdice(Dice dice1, Dice dice2) { if (dice1.face == dice2.face && dice1.N == dice2.N && dice1.E == dice2.E && dice1.S == dice2.S && dice1.W == dice2.W && dice1.back == dice2.back) return true; else return false; } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
630
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_D { public class Program { public struct Dice { public int top { get; private set; } public int front { get; private set; } public int right { get; private set; } public int left { get; private set; } public int back { get; private set; } public int bottom { get; private set; } public Dice(int[] num) { this.top = num[0]; this.front = num[1]; this.right = num[2]; this.left = num[3]; this.back = num[4]; this.bottom = num[5]; } public void RotateX() // X軸に対し時計回りに回転 { int tmp = this.top; this.top = this.back; this.back = this.bottom; this.bottom = this.front; this.front = tmp; } public void RotateY() // Y軸に対し時計回りに回転 { int tmp = this.right; this.right = this.top; this.top = this.left; this.left = this.bottom; this.bottom = tmp; } public void RotateZ() // Z軸に対し時計回りに回転 { int tmp = this.front; this.front = this.right; this.right = this.back; this.back = this.left; this.left = tmp; } public void Rotate(Char cmd) { switch (cmd) { default: break; case 'X': RotateX(); break; case 'Y': RotateY(); break; case 'Z': RotateZ(); break; } } } public static void Main(string[] args) { int n = ReadInt(); List<Dice> dices = new List<Dice>(); for (int i = 0; i < n; i++) { dices.Add(new Dice(ReadIntAr())); } for (int i = 0; i < dices.Count; i++) { for (int j = i; j < dices.Count; j++) { if (IsEqualDice(dices[i], dices[j])) { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } public static bool IsEqualDice(Dice diceA, Dice diceB) { String cmd = " ZZZXYYYZXXXZYYYZXXXYZZZ"; for (int j = 0; j < cmd.Length; j++) { diceB.Rotate(cmd[j]); if (diceA.Equals(diceB)) return true; } return false; } static string ReadSt() { return Console.ReadLine(); } static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static double ReadDouble() { return double.Parse(Console.ReadLine()); } static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_D { public class Program { public struct Dice { public int top { get; private set; } public int front { get; private set; } public int right { get; private set; } public int left { get; private set; } public int back { get; private set; } public int bottom { get; private set; } public Dice(int[] num) { this.top = num[0]; this.front = num[1]; this.right = num[2]; this.left = num[3]; this.back = num[4]; this.bottom = num[5]; } public void RotateX() // X軸に対し時計回りに回転 { int tmp = this.top; this.top = this.back; this.back = this.bottom; this.bottom = this.front; this.front = tmp; } public void RotateY() // Y軸に対し時計回りに回転 { int tmp = this.right; this.right = this.top; this.top = this.left; this.left = this.bottom; this.bottom = tmp; } public void RotateZ() // Z軸に対し時計回りに回転 { int tmp = this.front; this.front = this.right; this.right = this.back; this.back = this.left; this.left = tmp; } public void Rotate(Char cmd) { switch (cmd) { default: break; case 'X': RotateX(); break; case 'Y': RotateY(); break; case 'Z': RotateZ(); break; } } } public static void Main(string[] args) { int n = ReadInt(); List<Dice> dices = new List<Dice>(); for (int i = 0; i < n; i++) { dices.Add(new Dice(ReadIntAr())); } for (int i = 0; i < dices.Count; i++) { for (int j = i + 1; j < dices.Count; j++) { if (IsEqualDice(dices[i], dices[j])) { Console.WriteLine("No"); return; } } } Console.WriteLine("Yes"); } public static bool IsEqualDice(Dice diceA, Dice diceB) { String cmd = " ZZZXYYYZXXXZYYYZXXXYZZZ"; for (int j = 0; j < cmd.Length; j++) { diceB.Rotate(cmd[j]); if (diceA.Equals(diceB)) return true; } return false; } static string ReadSt() { return Console.ReadLine(); } static int ReadInt() { return int.Parse(Console.ReadLine()); } static long ReadLong() { return long.Parse(Console.ReadLine()); } static double ReadDouble() { return double.Parse(Console.ReadLine()); } static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); } static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); } static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); } static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); } } }
[["+", 10, 198, 0, 200, 0, 212, 0, 16, 17, 72], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 12, 203]]
8
764
2
using System; namespace Dice { class Program { readonly static Dise[] dise = new Dise[100]; static void Main(string[] args) { //サイコロ数 short n; //サイコロ string result = "Yes"; //サイコロ数 n = short.Parse(Console.ReadLine()); for (short i = 0; i < n; i++) { dise[i] = new Dise(); } //指定サイコロ数入力 for (short i = 0; i < n; i++) { //目を入力 dise[i].InputNumber(); //同じサイコロが存在するか if (result == "Yes" && IsDistance(i)) { result = "No"; break; } } Console.WriteLine(result); } static bool IsDistance(short i) { Dise copy = dise[i]; short k, l, m; for (short j = 0; j < i; j++) { for (k = 0; k < 6; k++) { //てっぺんと同じ数字 if (copy.num[k] == dise[j].num[0]) { break; } } //同じ数字がない if (k < 6) { //てっぺん合わせ copy.RotateTop(k); for (l = 0; l < 6; l++) { //前面と同じ値 if (copy.num[l] == dise[j].num[1]) { break; } } //下面 同じ数字なし if (l != 5) { //前面合わせ copy.RotateFront(l); } //面調べる for (m = 0; m < 6; m++) { //異なる if (dise[j].num[m] != copy.num[m]) { break; } } //全て同じなら if (m == 6) { return true; } } } //全て異なる return false; } } class Dise { public short[] num = new short[6]; public void InputNumber() { string[] str = Console.ReadLine().Split(' '); //計6マス入力 for (short i = 0; i < 6; i++) { num[i] = short.Parse(str[i]); } } public void RotateTop(short pos) { //すでに上面 if (pos == 0) { return; } if (pos == 5) { Swap(ref num[0], ref num[5]); Swap(ref num[1], ref num[4]); return; } //反対側以外 Swap(ref num[pos], ref num[0]); Swap(ref num[pos], ref num[5 - pos]); Swap(ref num[pos], ref num[5]); } public void RotateFront(short pos) { //すでに前 if (pos == 1) { return; } //後ろ面 if (pos == 4) { Swap(ref num[1], ref num[4]); Swap(ref num[2], ref num[3]); return; } Swap(ref num[pos], ref num[1]); Swap(ref num[pos], ref num[5 - pos]); Swap(ref num[pos], ref num[4]); } public void Swap(ref short a, ref short b) { short c = a; a = b; b = c; } }; }
using System; namespace Dice { class Program { readonly static Dise[] dise = new Dise[100]; static void Main(string[] args) { //サイコロ数 short n; //サイコロ string result = "Yes"; //サイコロ数 n = short.Parse(Console.ReadLine()); for (short i = 0; i < n; i++) { dise[i] = new Dise(); } //指定サイコロ数入力 for (short i = 0; i < n; i++) { //目を入力 dise[i].InputNumber(); //同じサイコロが存在するか if (result == "Yes" && IsDistance(i)) { result = "No"; break; } } Console.WriteLine(result); } static bool IsDistance(short i) { Dise copy = dise[i]; short k, l, m; for (short j = 0; j < i; j++) { for (k = 0; k < 6; k++) { //てっぺんと同じ数字 if (copy.num[k] == dise[j].num[0]) { break; } } //同じ数字がない if (k < 6) { //てっぺん合わせ copy.RotateTop(k); for (l = 0; l < 6; l++) { //前面と同じ値 if (copy.num[l] == dise[j].num[1]) { break; } } //下面 同じ数字なし if (l < 5) { //前面合わせ copy.RotateFront(l); } //面調べる for (m = 0; m < 6; m++) { //異なる if (dise[j].num[m] != copy.num[m]) { break; } } //全て同じなら if (m == 6) { return true; } } } //全て異なる return false; } } class Dise { public short[] num = new short[6]; public void InputNumber() { string[] str = Console.ReadLine().Split(' '); //計6マス入力 for (short i = 0; i < 6; i++) { num[i] = short.Parse(str[i]); } } public void RotateTop(short pos) { //すでに上面 if (pos == 0) { return; } if (pos == 5) { Swap(ref num[0], ref num[5]); Swap(ref num[1], ref num[4]); return; } //反対側以外 Swap(ref num[pos], ref num[0]); Swap(ref num[pos], ref num[5 - pos]); Swap(ref num[pos], ref num[5]); } public void RotateFront(short pos) { //すでに前 if (pos == 1) { return; } //後ろ面 if (pos == 4) { Swap(ref num[1], ref num[4]); Swap(ref num[2], ref num[3]); return; } Swap(ref num[pos], ref num[1]); Swap(ref num[pos], ref num[5 - pos]); Swap(ref num[pos], ref num[4]); } public void Swap(ref short a, ref short b) { short c = a; a = b; b = c; } }; }
[["-", 0, 57, 64, 196, 0, 57, 15, 16, 17, 79], ["+", 0, 57, 64, 196, 0, 57, 15, 16, 17, 18]]
8
670
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_D { class Program { public static IEnumerable<string[]> ReadSplitWords(Func<string> reader, char splitChar) { while (true) { yield return reader().Split(splitChar); } } public static bool HasDuplicate<T>(IEnumerable<T> stream, Func<T, T, bool> isEquals) { Func<T, bool> recursiveFunction = _ => false; foreach (var n in stream) { if (recursiveFunction(n)) { return true; } var currentFunc = recursiveFunction; recursiveFunction = (m) => isEquals(m, n) || currentFunc(m); } return false; } public static void Main(string[] args) { var count = int.Parse(Console.ReadLine()); // (1) // var alias = ReadSplitWords(Console.ReadLine, ' ').Take(count).ToArray(); // var hasDuplicate = HasDuplicate(alias, Dice.EqualsAliasOfDice); // (2) var hasDuplicate = true; var diceAliasList = new string[count][]; var h = 0; foreach (var n in ReadSplitWords(Console.ReadLine, ' ').Take(count)) { if (diceAliasList.Take(h).Any(_ => Dice.EqualsAliasOfDice(_, n))) { hasDuplicate = true; break; } // if (Dice.EqualsAliasOfDice(n, diceAliasList[i])) // { // hasDuplicate = true; // break; // } //} if (hasDuplicate == true) { break; } diceAliasList[h] = n; ++h; } Console.WriteLine(hasDuplicate ? "No" : "Yes"); } } class Dice { public Dice() { Top = 0; Front = 1; Right = 2; } public void RotateNorth() { var temp = Inverse(Top); Top = Front; Front = temp; } public void RotateSouth() { var temp = Inverse(Front); Front = Top; Top = temp; } public void RotateEast() { var temp = Inverse(Right); Right = Top; Top = temp; } public void RotateWest() { var temp = Inverse(Top); Top = Right; Right = temp; } public enum Surface { TOP, BOTTOM, FRONT, BACK, RIGHT, LEFT, NONE } public Surface Match(int surface) { if (surface == Top) return Surface.TOP; if (surface == Bottom) return Surface.BOTTOM; if (surface == Front) return Surface.FRONT; if (surface == Back) return Surface.BACK; if (surface == Right) return Surface.RIGHT; if (surface == Left) return Surface.LEFT; return Surface.NONE; } public bool RotateToTop(int top) { switch (Match(top)) { case Surface.TOP: break; case Surface.BOTTOM: RotateNorth(); RotateNorth(); break; case Surface.FRONT: RotateNorth(); break; case Surface.BACK: RotateSouth(); break; case Surface.RIGHT: RotateWest(); break; case Surface.LEFT: RotateEast(); break; } return Top == top; } public bool RotateToTopAndFront(int top, int front) { if (RotateToTop(top) == false) { return false; } switch (Match(front)) { case Surface.RIGHT: RotateRightOfZCoords(); break; case Surface.BACK: RotateRightOfZCoords(); RotateRightOfZCoords(); break; case Surface.LEFT: RotateLeftOfZCoords(); break; } return Top == top && Front == front; } public void RotateRightOfZCoords() { var temp = Back; Front = Right; Right = temp; } public void RotateLeftOfZCoords() { var temp = Left; Right = Front; Front = temp; } public int RightOf(int front) { switch (Match(front)) { case Surface.FRONT: return Right; case Surface.RIGHT: return Back; case Surface.BACK: return Left; case Surface.LEFT: return Front; } return -1; } public int Top { get; private set; } public int Front { get; private set; } public int Right { get; private set; } public int Bottom { get { return Inverse(Top); } } public int Back { get { return Inverse(Front); } } public int Left { get { return Inverse(Right); } } public int Inverse(int surface) { return 5 - surface; } public IEnumerable<int> Surfaces() { yield return Top; yield return Front; yield return Right; yield return Left; yield return Back; yield return Bottom; } public static bool EqualsAliasOfNoDuplication(string[] alias1, string[] alias2) { var dice1 = new Dice(); var dice2 = new Dice(); var top = -1; var front = -1; for (var i = 0; i < alias1.Length; ++i) { if (alias1[0] == alias2[i]) { top = i; } if (alias1[1] == alias2[i]) { front = i; } } if (dice2.RotateToTopAndFront(top, front) == false) { return false; } var orderedAlias1 = dice1.Surfaces().Select(n => alias1[n]); var orderedAlias2 = dice2.Surfaces().Select(n => alias2[n]); return Enumerable.SequenceEqual(orderedAlias1, orderedAlias2); } public static bool EqualsAliasOfDice(string[] alias1, string[] alias2) { var dice1 = new Dice(); var dice2 = new Dice(); var orderedAlias1 = dice1.Surfaces().Select(n => alias1[n]); var orderedAlias2 = dice2.Surfaces().Select(n => alias2[n]); for (var i = 0; i < 6; ++i) { dice2.RotateToTop(i); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; } return false; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ITP1_11_D { class Program { public static IEnumerable<string[]> ReadSplitWords(Func<string> reader, char splitChar) { while (true) { yield return reader().Split(splitChar); } } public static bool HasDuplicate<T>(IEnumerable<T> stream, Func<T, T, bool> isEquals) { Func<T, bool> recursiveFunction = _ => false; foreach (var n in stream) { if (recursiveFunction(n)) { return true; } var currentFunc = recursiveFunction; recursiveFunction = (m) => isEquals(m, n) || currentFunc(m); } return false; } public static void Main(string[] args) { var count = int.Parse(Console.ReadLine()); // (1) // var alias = ReadSplitWords(Console.ReadLine, ' ').Take(count).ToArray(); // var hasDuplicate = HasDuplicate(alias, Dice.EqualsAliasOfDice); // (2) var hasDuplicate = false; var diceAliasList = new string[count][]; var h = 0; foreach (var n in ReadSplitWords(Console.ReadLine, ' ').Take(count)) { if (diceAliasList.Take(h).Any(_ => Dice.EqualsAliasOfDice(_, n))) { hasDuplicate = true; break; } // if (Dice.EqualsAliasOfDice(n, diceAliasList[i])) // { // hasDuplicate = true; // break; // } //} if (hasDuplicate == true) { break; } diceAliasList[h] = n; ++h; } Console.WriteLine(hasDuplicate ? "No" : "Yes"); } } class Dice { public Dice() { Top = 0; Front = 1; Right = 2; } public void RotateNorth() { var temp = Inverse(Top); Top = Front; Front = temp; } public void RotateSouth() { var temp = Inverse(Front); Front = Top; Top = temp; } public void RotateEast() { var temp = Inverse(Right); Right = Top; Top = temp; } public void RotateWest() { var temp = Inverse(Top); Top = Right; Right = temp; } public enum Surface { TOP, BOTTOM, FRONT, BACK, RIGHT, LEFT, NONE } public Surface Match(int surface) { if (surface == Top) return Surface.TOP; if (surface == Bottom) return Surface.BOTTOM; if (surface == Front) return Surface.FRONT; if (surface == Back) return Surface.BACK; if (surface == Right) return Surface.RIGHT; if (surface == Left) return Surface.LEFT; return Surface.NONE; } public bool RotateToTop(int top) { switch (Match(top)) { case Surface.TOP: break; case Surface.BOTTOM: RotateNorth(); RotateNorth(); break; case Surface.FRONT: RotateNorth(); break; case Surface.BACK: RotateSouth(); break; case Surface.RIGHT: RotateWest(); break; case Surface.LEFT: RotateEast(); break; } return Top == top; } public bool RotateToTopAndFront(int top, int front) { if (RotateToTop(top) == false) { return false; } switch (Match(front)) { case Surface.RIGHT: RotateRightOfZCoords(); break; case Surface.BACK: RotateRightOfZCoords(); RotateRightOfZCoords(); break; case Surface.LEFT: RotateLeftOfZCoords(); break; } return Top == top && Front == front; } public void RotateRightOfZCoords() { var temp = Back; Front = Right; Right = temp; } public void RotateLeftOfZCoords() { var temp = Left; Right = Front; Front = temp; } public int RightOf(int front) { switch (Match(front)) { case Surface.FRONT: return Right; case Surface.RIGHT: return Back; case Surface.BACK: return Left; case Surface.LEFT: return Front; } return -1; } public int Top { get; private set; } public int Front { get; private set; } public int Right { get; private set; } public int Bottom { get { return Inverse(Top); } } public int Back { get { return Inverse(Front); } } public int Left { get { return Inverse(Right); } } public int Inverse(int surface) { return 5 - surface; } public IEnumerable<int> Surfaces() { yield return Top; yield return Front; yield return Right; yield return Left; yield return Back; yield return Bottom; } public static bool EqualsAliasOfNoDuplication(string[] alias1, string[] alias2) { var dice1 = new Dice(); var dice2 = new Dice(); var top = -1; var front = -1; for (var i = 0; i < alias1.Length; ++i) { if (alias1[0] == alias2[i]) { top = i; } if (alias1[1] == alias2[i]) { front = i; } } if (dice2.RotateToTopAndFront(top, front) == false) { return false; } var orderedAlias1 = dice1.Surfaces().Select(n => alias1[n]); var orderedAlias2 = dice2.Surfaces().Select(n => alias2[n]); return Enumerable.SequenceEqual(orderedAlias1, orderedAlias2); } public static bool EqualsAliasOfDice(string[] alias1, string[] alias2) { var dice1 = new Dice(); var dice2 = new Dice(); var orderedAlias1 = dice1.Surfaces().Select(n => alias1[n]); var orderedAlias2 = dice2.Surfaces().Select(n => alias2[n]); for (var i = 0; i < 6; ++i) { dice2.RotateToTop(i); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; dice2.RotateLeftOfZCoords(); if (Enumerable.SequenceEqual(orderedAlias1, orderedAlias2)) return true; } return false; } } }
[["-", 0, 198, 0, 200, 0, 212, 0, 211, 0, 146], ["+", 0, 198, 0, 200, 0, 212, 0, 211, 0, 147]]
8
1,229
2
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Dice { private int[] dicelabel = new int[6]; private int temp = 0; public Dice(string num) //???????????????????????°?????\?????? { var dicenum = num.Split(' '); for (int i = 0; i < 6; i++) { dicelabel[i] = int.Parse(dicenum[i]); } } public int[] getDiceLabel //???????????????????????????????????????????????£ { get { return dicelabel; } } public void roll(string order) //?????????switch??§?????? { switch (order[0]) { case 'N': temp = dicelabel[0]; dicelabel[0] = dicelabel[1]; dicelabel[1] = dicelabel[5]; dicelabel[5] = dicelabel[4]; dicelabel[4] = temp; break; case 'S': temp = dicelabel[0]; dicelabel[0] = dicelabel[4]; dicelabel[4] = dicelabel[5]; dicelabel[5] = dicelabel[1]; dicelabel[1] = temp; break; case 'E': temp = dicelabel[0]; dicelabel[0] = dicelabel[3]; dicelabel[3] = dicelabel[5]; dicelabel[5] = dicelabel[2]; dicelabel[2] = temp; break; case 'W': temp = dicelabel[0]; dicelabel[0] = dicelabel[2]; dicelabel[2] = dicelabel[5]; dicelabel[5] = dicelabel[3]; dicelabel[3] = temp; break; case 'R': temp = dicelabel[1]; dicelabel[1] = dicelabel[2]; dicelabel[2] = dicelabel[4]; dicelabel[4] = dicelabel[3]; dicelabel[3] = temp; break; } } public int getRightSide(string topfront) { string[] temps = topfront.Split(' '); int top = int.Parse(temps[0]); //?????¢?????? int front = int.Parse(temps[1]); //?????¢?????? bool match = false; for (int i = 0; i < 4; i++) // N???????????¢???????????????????´¢ { for (int j = 0; j < 4; j++) //????????¢?????¨????????¢????????? { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("N"); } for (int i = 0; i < 4; i++) // E???????????¢???????????????????´¢ { if (match) break; for (int j = 0; j < 4; j++) //????????¢?????¨????????¢????????? { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("E"); // } //?????¢??¨?????¢????????´?????????????´?2(?????´??¢)????????? return dicelabel[2]; } public bool identity(int[] match) //???????????????????????????????????????????????? { bool equal = false; int matchcnt = 0; //???????????¢??????????????? for (int h = 0; h < 4; h++) { for (int i = 0; i < 4; i++) { for ( int j = 0; j < 4; j++) //?????¢??????????????§2????????????????????´????????????????????? { matchcnt = 0; for (int k = 0; k < match.Length; k++) { if (dicelabel[k] == match[k]) { matchcnt++; } } if (matchcnt == 6) // 6????????£??????1~6???????´????????????´?????????????????§ { equal = true; // equal???true????¨???? break; } this.roll("R"); } if (equal) break; this.roll("N"); } if (equal) break; this.roll("E"); } return equal; } class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Dice[] dicelist = new Dice[n]; bool check = true; for (int i = 0; i < n; i++) // n??????????????????????????? { dicelist[i] = new Dice(Console.ReadLine()); } for (int i = 0; i < n - 1; i++) { for (int j = 1; j < n; j++) { if (dicelist[i].identity(dicelist[j].getDiceLabel)) { check = false; } } } if (check) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } } }
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Dice { private int[] dicelabel = new int[6]; private int temp = 0; public Dice(string num) //???????????????????????°?????\?????? { var dicenum = num.Split(' '); for (int i = 0; i < 6; i++) { dicelabel[i] = int.Parse(dicenum[i]); } } public int[] getDiceLabel //???????????????????????????????????????????????£ { get { return dicelabel; } } public void roll(string order) //?????????switch??§?????? { switch (order[0]) { case 'N': temp = dicelabel[0]; dicelabel[0] = dicelabel[1]; dicelabel[1] = dicelabel[5]; dicelabel[5] = dicelabel[4]; dicelabel[4] = temp; break; case 'S': temp = dicelabel[0]; dicelabel[0] = dicelabel[4]; dicelabel[4] = dicelabel[5]; dicelabel[5] = dicelabel[1]; dicelabel[1] = temp; break; case 'E': temp = dicelabel[0]; dicelabel[0] = dicelabel[3]; dicelabel[3] = dicelabel[5]; dicelabel[5] = dicelabel[2]; dicelabel[2] = temp; break; case 'W': temp = dicelabel[0]; dicelabel[0] = dicelabel[2]; dicelabel[2] = dicelabel[5]; dicelabel[5] = dicelabel[3]; dicelabel[3] = temp; break; case 'R': temp = dicelabel[1]; dicelabel[1] = dicelabel[2]; dicelabel[2] = dicelabel[4]; dicelabel[4] = dicelabel[3]; dicelabel[3] = temp; break; } } public int getRightSide(string topfront) { string[] temps = topfront.Split(' '); int top = int.Parse(temps[0]); //?????¢?????? int front = int.Parse(temps[1]); //?????¢?????? bool match = false; for (int i = 0; i < 4; i++) // N???????????¢???????????????????´¢ { for (int j = 0; j < 4; j++) //????????¢?????¨????????¢????????? { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("N"); } for (int i = 0; i < 4; i++) // E???????????¢???????????????????´¢ { if (match) break; for (int j = 0; j < 4; j++) //????????¢?????¨????????¢????????? { this.roll("R"); if (dicelabel[0] == top && dicelabel[1] == front) { match = true; break; } } if (match) break; this.roll("E"); // } //?????¢??¨?????¢????????´?????????????´?2(?????´??¢)????????? return dicelabel[2]; } public bool identity(int[] match) //???????????????????????????????????????????????? { bool equal = false; int matchcnt = 0; //???????????¢??????????????? for (int h = 0; h < 4; h++) { for (int i = 0; i < 4; i++) { for ( int j = 0; j < 4; j++) //?????¢??????????????§2????????????????????´????????????????????? { matchcnt = 0; for (int k = 0; k < match.Length; k++) { if (dicelabel[k] == match[k]) { matchcnt++; } } if (matchcnt == 6) // 6????????£??????1~6???????´????????????´?????????????????§ { equal = true; // equal???true????¨???? break; } this.roll("R"); } if (equal) break; this.roll("N"); } if (equal) break; this.roll("E"); } return equal; } class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Dice[] dicelist = new Dice[n]; bool check = true; for (int i = 0; i < n; i++) // n??????????????????????????? { dicelist[i] = new Dice(Console.ReadLine()); } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { if (dicelist[i].identity(dicelist[j].getDiceLabel)) { check = false; } } } if (check) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } } }
[["+", 10, 198, 0, 200, 0, 212, 0, 16, 31, 22], ["+", 10, 198, 0, 200, 0, 212, 0, 16, 17, 33], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 47], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 196, 0, 7, 26, 223, 0, 29], ["+", 0, 7, 8, 196, 0, 7, 26, 223, 0, 68]]
8
940
8
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Dice : IEquatable<Dice> { protected string[] val = new string[6]; public Dice(string[] str) { Array.Copy(val, str, 6); } public bool Equals(Dice dice2) { if ((val[0] == dice2.val[2] && val[5] == dice2.val[3]) || (val[0] == val[3] && val[5] == dice2.val[2])) { string tmp = dice2.val[1]; dice2.val[1] = dice2.val[2]; dice2.val[2] = dice2.val[4]; dice2.val[4] = dice2.val[3]; dice2.val[3] = tmp; } for (int i = 0; i < 3; i++) { if (val[0] == dice2.val[0] && val[5] == dice2.val[5]) break; string tmp = dice2.val[0]; dice2.val[0] = dice2.val[1]; dice2.val[1] = dice2.val[5]; dice2.val[5] = dice2.val[4]; dice2.val[4] = tmp; } for (int i = 0; i < 3; i++) { if (val[1] == dice2.val[1] && val[4] == dice2.val[4]) break; string tmp = dice2.val[1]; dice2.val[1] = dice2.val[2]; dice2.val[2] = dice2.val[4]; dice2.val[4] = dice2.val[3]; dice2.val[3] = tmp; } if (val[0] == dice2.val[0] && val[1] == dice2.val[1] && val[2] == dice2.val[2] && val[3] == dice2.val[3] && val[4] == dice2.val[4] && val[5] == dice2.val[5]) return true; else return false; } } class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); Dice[] dices = new Dice[num]; for (int i = 0; i < num; i++) { dices[i] = new Dice(Console.ReadLine().Split(' ')); } bool res = true; for (int i = 0; i < num; i++) { for (int j = i + 1; j < num; j++) { if (dices[i].Equals(dices[j])) { res = false; break; } } } Console.WriteLine(res ? "Yes" : "No"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Dice : IEquatable<Dice> { protected string[] val = new string[6]; public Dice(string[] str) { Array.Copy(str, val, 6); } public bool Equals(Dice dice2) { if ((val[0] == dice2.val[2] && val[5] == dice2.val[3]) || (val[0] == dice2.val[3] && val[5] == dice2.val[2])) { string tmp = dice2.val[1]; dice2.val[1] = dice2.val[2]; dice2.val[2] = dice2.val[4]; dice2.val[4] = dice2.val[3]; dice2.val[3] = tmp; } for (int i = 0; i < 3; i++) { if (val[0] == dice2.val[0] && val[5] == dice2.val[5]) break; string tmp = dice2.val[0]; dice2.val[0] = dice2.val[1]; dice2.val[1] = dice2.val[5]; dice2.val[5] = dice2.val[4]; dice2.val[4] = tmp; } for (int i = 0; i < 3; i++) { if (val[1] == dice2.val[1] && val[4] == dice2.val[4]) break; string tmp = dice2.val[1]; dice2.val[1] = dice2.val[2]; dice2.val[2] = dice2.val[4]; dice2.val[4] = dice2.val[3]; dice2.val[3] = tmp; } if (val[0] == dice2.val[0] && val[1] == dice2.val[1] && val[2] == dice2.val[2] && val[3] == dice2.val[3] && val[4] == dice2.val[4] && val[5] == dice2.val[5]) return true; else return false; } } class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); Dice[] dices = new Dice[num]; for (int i = 0; i < num; i++) { dices[i] = new Dice(Console.ReadLine().Split(' ')); } bool res = true; for (int i = 0; i < num; i++) { for (int j = i + 1; j < num; j++) { if (dices[i].Equals(dices[j])) { res = false; break; } } } Console.WriteLine(res ? "Yes" : "No"); } } }
[["-", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 8, 196, 0, 1, 0, 213, 3, 4, 0, 21], ["+", 8, 196, 0, 1, 0, 213, 3, 4, 0, 21], ["+", 0, 1, 0, 213, 3, 4, 0, 28, 0, 22], ["+", 0, 16, 31, 16, 12, 204, 205, 214, 205, 22], ["+", 0, 16, 31, 16, 12, 204, 205, 214, 0, 131]]
8
641
6
using System; using System.Linq; namespace Structure_and_Class { class Dice_IV { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var dices = new Dice4[n]; for (int i = 0; i < n; i++) dices[i] = new Dice4( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); for (int i = 0; i < n; i++) for (int j = i + 1; j < n - 1; j++) if (!dices[i].IsEqual(dices[j])) { Console.WriteLine("No"); return; } Console.WriteLine("Yes"); } } public class Dice4 { public int[] label = new int[6]; public Dice4(int[] label) { this.label = label; } public bool IsEqual(Dice4 diceB) { if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('E'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; return false; } private bool RotaetCheck(Dice4 diceB) { for (int i = 0; i < 4; i++) { diceB.Rotate(); if (label.SequenceEqual(diceB.label)) return true; } return false; } public void Move(char c) { int[] res = new int[6]; switch (c) { case 'E': res[0] = label[3]; res[1] = label[1]; res[2] = label[0]; res[3] = label[5]; res[4] = label[4]; res[5] = label[2]; label = res; break; case 'N': res[0] = label[1]; res[1] = label[5]; res[2] = label[2]; res[3] = label[3]; res[4] = label[0]; res[5] = label[4]; label = res; break; case 'S': Move('N'); Move('N'); Move('N'); break; case 'W': Move('E'); Move('E'); Move('E'); break; } } public void Rotate() { int[] res = new int[6]; res[0] = label[0]; res[1] = label[3]; res[2] = label[1]; res[3] = label[4]; res[4] = label[2]; res[5] = label[5]; label = res; } } }
using System; using System.Linq; namespace Structure_and_Class { class Dice_IV { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var dices = new Dice4[n]; for (int i = 0; i < n; i++) dices[i] = new Dice4( Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (dices[i].IsEqual(dices[j])) { Console.WriteLine("No"); return; } Console.WriteLine("Yes"); } } public class Dice4 { public int[] label = new int[6]; public Dice4(int[] label) { this.label = label; } public bool IsEqual(Dice4 diceB) { if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('E'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; diceB.Move('N'); if (RotaetCheck(diceB)) return true; return false; } private bool RotaetCheck(Dice4 diceB) { for (int i = 0; i < 4; i++) { diceB.Rotate(); if (label.SequenceEqual(diceB.label)) return true; } return false; } public void Move(char c) { int[] res = new int[6]; switch (c) { case 'E': res[0] = label[3]; res[1] = label[1]; res[2] = label[0]; res[3] = label[5]; res[4] = label[4]; res[5] = label[2]; label = res; break; case 'N': res[0] = label[1]; res[1] = label[5]; res[2] = label[2]; res[3] = label[3]; res[4] = label[0]; res[5] = label[4]; label = res; break; case 'S': Move('N'); Move('N'); Move('N'); break; case 'W': Move('E'); Move('E'); Move('E'); break; } } public void Rotate() { int[] res = new int[6]; res[0] = label[0]; res[1] = label[3]; res[2] = label[1]; res[3] = label[4]; res[4] = label[2]; res[5] = label[5]; label = res; } } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 203], ["-", 0, 7, 8, 7, 15, 16, 12, 16, 17, 33], ["-", 0, 7, 8, 7, 15, 16, 12, 16, 12, 203], ["-", 0, 7, 8, 7, 8, 57, 15, 241, 0, 111]]
8
676
5
using System; using System.Collections.Generic; using System.Linq; namespace Aizu { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var diceList = new Dictionary<string, List<Dice>>(); // さいころを作成し、構成する数値が同じもの同士でまとめる for (int i = 0; i < n; i++) { var diceValue = Console.ReadLine(); var dice = new Dice(diceValue); // 同じ数値の数を構成しているさいころでまとめる var diceValueSort = string.Join( " ", (string[])diceValue.Split(' ').OrderBy(d => d).ToArray()); if (!diceList.ContainsKey(diceValueSort)) { diceList.Add(diceValueSort, new List<Dice>()); } diceList[diceValueSort].Add(dice); } // 全て一致しないことをチェック var flag = false; foreach (string key in diceList.Keys) { // 同じ数値(個数)が一致するさいころが一つしかない場合は、一致しようがないので次のサイコロ if (diceList[key].Count < 2) { continue; } for (int i = 0; i < diceList[key].Count; i++) { for (int j = i + 1; j < diceList[key].Count; j++) { flag = Dice.Judge(diceList[key][i], diceList[key][j]); if (flag) { break; } } if (flag) { break; } } if (flag) { break; } } Console.WriteLine(flag ? "Yes" : "No"); } } class Dice { public int Top { get; private set; } public int Bottom { get; private set; } public int Right { get; private set; } public int Left { get; private set; } public int Front { get; private set; } public int Back { get; private set; } public int[] AllValue { get; set; } public Dice(string value) { var temp = value.Split(' '); this.Top = int.Parse(temp[0]); this.Front = int.Parse(temp[1]); this.Right = int.Parse(temp[2]); this.Left = int.Parse(temp[3]); this.Back = int.Parse(temp[4]); this.Bottom = int.Parse(temp[5]); this.AllValue = new int[] { this.Top, this.Front, this.Right, this.Left, this.Back, this.Bottom }; } /// <summary> /// 受け取ったダイスのTopとBottomに一致する箇所を調べその時の側面が一致するかチェックする /// </summary> /// <param name="dice">比較対象のダイス</param> /// <returns>True:一致, False:不一致</returns> public static bool Judge(Dice dice1, Dice dice2) { bool judge = false; int[] diceSide = new int[] { dice1.Front, dice1.Right, dice1.Back, dice1.Left }; // Top, Bottom if (dice1.Top == dice2.Top && dice1.Bottom == dice2.Bottom) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Right, dice2.Back, dice2.Left }; judge = JudgeSide(diceSide, thisDiceSide); } // Bottom, Top if (dice1.Top == dice2.Bottom && dice1.Bottom == dice2.Top) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Left, dice2.Back, dice2.Right }; judge = JudgeSide(diceSide, thisDiceSide); } // Right, Left if (dice1.Top == dice2.Right && dice1.Bottom == dice2.Left) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Bottom, dice2.Back, dice2.Top }; judge = JudgeSide(diceSide, thisDiceSide); } // Left, Right if (dice1.Top == dice2.Left && dice1.Bottom == dice2.Right) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Top, dice2.Back, dice2.Bottom }; judge = JudgeSide(diceSide, thisDiceSide); } // Front, Back if (dice1.Top == dice2.Front && dice1.Bottom == dice2.Back) { int[] thisDiceSide = new int[] { dice2.Bottom, dice2.Right, dice2.Top, dice2.Left }; judge = JudgeSide(diceSide, thisDiceSide); } // Back, Front if (dice1.Top == dice2.Back && dice1.Bottom == dice2.Front) { int[] thisDiceSide = new int[] { dice2.Bottom, dice2.Left, dice2.Top, dice2.Right }; judge = JudgeSide(diceSide, thisDiceSide); } return judge; } /// <summary> /// 側面を比較する /// </summary> /// <param name="dice1">さいころ1</param> /// <param name="dice2">さいころ2</param> /// <returns>true:同じ配置のさいころ, false:配置がことなるさいころ</returns> private static bool JudgeSide(int[] dice1, int[] dice2) { for (int ds = 0; ds < 4; ds++) { int start = 0; for (int tds = start; tds < 4; tds++) { if (dice1[ds % 4] == dice2[tds % 4] && dice1[(ds + 1) % 4] == dice2[(tds + 1) % 4] && dice1[(ds + 2) % 4] == dice2[(tds + 2) % 4] && dice1[(ds + 3) % 4] == dice2[(tds + 3) % 4]) { return true; } start++; } } return false; } } }
using System; using System.Collections.Generic; using System.Linq; namespace Aizu { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var diceList = new Dictionary<string, List<Dice>>(); // さいころを作成し、構成する数値が同じもの同士でまとめる for (int i = 0; i < n; i++) { var diceValue = Console.ReadLine(); var dice = new Dice(diceValue); // 同じ数値の数を構成しているさいころでまとめる var diceValueSort = string.Join( " ", (string[])diceValue.Split(' ').OrderBy(d => d).ToArray()); if (!diceList.ContainsKey(diceValueSort)) { diceList.Add(diceValueSort, new List<Dice>()); } diceList[diceValueSort].Add(dice); } // 全て一致しないことをチェック var flag = false; foreach (string key in diceList.Keys) { // 同じ数値(個数)が一致するさいころが一つしかない場合は、一致しようがないので次のサイコロ if (diceList[key].Count < 2) { continue; } for (int i = 0; i < diceList[key].Count; i++) { for (int j = i + 1; j < diceList[key].Count; j++) { flag = Dice.Judge(diceList[key][i], diceList[key][j]); if (flag) { break; } } if (flag) { break; } } if (flag) { break; } } Console.WriteLine(flag ? "No" : "Yes"); } } class Dice { public int Top { get; private set; } public int Bottom { get; private set; } public int Right { get; private set; } public int Left { get; private set; } public int Front { get; private set; } public int Back { get; private set; } public int[] AllValue { get; set; } public Dice(string value) { var temp = value.Split(' '); this.Top = int.Parse(temp[0]); this.Front = int.Parse(temp[1]); this.Right = int.Parse(temp[2]); this.Left = int.Parse(temp[3]); this.Back = int.Parse(temp[4]); this.Bottom = int.Parse(temp[5]); this.AllValue = new int[] { this.Top, this.Front, this.Right, this.Left, this.Back, this.Bottom }; } /// <summary> /// 受け取ったダイスのTopとBottomに一致する箇所を調べその時の側面が一致するかチェックする /// </summary> /// <param name="dice">比較対象のダイス</param> /// <returns>True:一致, False:不一致</returns> public static bool Judge(Dice dice1, Dice dice2) { bool judge = false; int[] diceSide = new int[] { dice1.Front, dice1.Right, dice1.Back, dice1.Left }; // Top, Bottom if (dice1.Top == dice2.Top && dice1.Bottom == dice2.Bottom) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Right, dice2.Back, dice2.Left }; judge = JudgeSide(diceSide, thisDiceSide); } // Bottom, Top if (dice1.Top == dice2.Bottom && dice1.Bottom == dice2.Top) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Left, dice2.Back, dice2.Right }; judge = JudgeSide(diceSide, thisDiceSide); } // Right, Left if (dice1.Top == dice2.Right && dice1.Bottom == dice2.Left) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Bottom, dice2.Back, dice2.Top }; judge = JudgeSide(diceSide, thisDiceSide); } // Left, Right if (dice1.Top == dice2.Left && dice1.Bottom == dice2.Right) { int[] thisDiceSide = new int[] { dice2.Front, dice2.Top, dice2.Back, dice2.Bottom }; judge = JudgeSide(diceSide, thisDiceSide); } // Front, Back if (dice1.Top == dice2.Front && dice1.Bottom == dice2.Back) { int[] thisDiceSide = new int[] { dice2.Bottom, dice2.Right, dice2.Top, dice2.Left }; judge = JudgeSide(diceSide, thisDiceSide); } // Back, Front if (dice1.Top == dice2.Back && dice1.Bottom == dice2.Front) { int[] thisDiceSide = new int[] { dice2.Bottom, dice2.Left, dice2.Top, dice2.Right }; judge = JudgeSide(diceSide, thisDiceSide); } return judge; } /// <summary> /// 側面を比較する /// </summary> /// <param name="dice1">さいころ1</param> /// <param name="dice2">さいころ2</param> /// <returns>true:同じ配置のさいころ, false:配置がことなるさいころ</returns> private static bool JudgeSide(int[] dice1, int[] dice2) { for (int ds = 0; ds < 4; ds++) { int start = 0; for (int tds = start; tds < 4; tds++) { if (dice1[ds % 4] == dice2[tds % 4] && dice1[(ds + 1) % 4] == dice2[(tds + 1) % 4] && dice1[(ds + 2) % 4] == dice2[(tds + 2) % 4] && dice1[(ds + 3) % 4] == dice2[(tds + 3) % 4]) { return true; } start++; } } return false; } } }
[["-", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 64, 5, 0, 222], ["-", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222]]
8
1,074
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AOJ1_1_B { class Program { static void Main(string[] args) { int x; x = int.Parse(Console.ReadLine()); Console.WriteLine(x ^ 3); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AOJ1_1_B { class Program { static void Main(string[] args) { int x; x = int.Parse(Console.ReadLine()); Console.WriteLine(x * x * x); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 140], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
71
6
using System; class Program { static void Main() { int x, y; x = int.Parse(Console.ReadLine()); y = x ^ 3; Console.WriteLine(y); } }
using System; class Program { static void Main() { int x, y; x = int.Parse(Console.ReadLine()); y = x * x * x; Console.WriteLine(y); } }
[["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 140], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 12, 203], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 12, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 12, 22]]
8
45
6
using System; namespace helloworld { class Program { static void Main(string[] args) { var x = int.Parse(Console.ReadLine()); Console.WriteLine(x); } } }
using System; namespace helloworld { class Program { static void Main(string[] args) { var x = int.Parse(Console.ReadLine()); Console.WriteLine(x * x * x); } } }
[["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
43
4
using System; namespace ConsoleApplication34 { class Program { static void Main(string[] args) { int x = int.Parse(Console.ReadLine()); x = 2; Console.WriteLine(x * x * x); } } }
using System; namespace ConsoleApplication34 { class Program { static void Main(string[] args) { int x = int.Parse(Console.ReadLine()); Console.WriteLine(x * x * x); } } }
[["-", 0, 195, 8, 196, 0, 1, 0, 11, 31, 22], ["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["-", 0, 195, 8, 196, 0, 1, 0, 11, 12, 203], ["-", 8, 201, 0, 195, 8, 196, 0, 1, 0, 35]]
8
51
4
using System; public class Test { public static void Main() { // your code goes here var n = int.Parse(Console.ReadLine()); Console.WriteLine(n ^ 3); } }
using System; public class Test { public static void Main() { // your code goes here var n = int.Parse(Console.ReadLine()); Console.WriteLine(n * n * n); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 140], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
40
6
using System; class ITP1_1_B { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Console.WriteLine(n ^ 3); } }
using System; class ITP1_1_B { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Console.WriteLine(n * n * n); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 140], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
41
6
using System; class Cubic { static void Main() { int x = int.Parse(Console.ReadLine()); Console.WriteLine(x * 3); } }
using System; class Cubic { static void Main() { int x = int.Parse(Console.ReadLine()); Console.WriteLine(x * x * x); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 48], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 22]]
8
37
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Practice { class Program { public struct Card { public char c; public int d; } static void Main(string[] args) { SolveA(args); // SolveB(args); // SolveC(args); // SolveD(args); } static void SolveA(string[] args) { int n = int.Parse(Console.ReadLine()); Card[] mergeSort = new Card[n]; Card[] s = new Card[n]; for (int i = 0; i < n; ++i) { var c = new Card(); string[] szarr = Console.ReadLine().Split(' '); c.c = szarr[0][0]; c.d = int.Parse(szarr[1]); mergeSort[i] = c; s[i] = c; } MergeSort(mergeSort, 0, s.Length - 1); QuickSort(s, 0, s.Length - 1); bool stable = true; for (int i = 0; i < n; ++i) { if (mergeSort[i].c != s[i].c || mergeSort[i].d != s[i].d) { stable = false; break; } } Console.WriteLine(stable ? "Stable" : "Not stable"); for (int i = 0; i < s.Length; ++i) { var c = s[i]; Console.WriteLine(c.c + " " + c.d); } } static void QuickSort(Card[] a, int p, int r) { if (p < r) { int q = Partition(a, p, r); QuickSort(a, p, q - 1); QuickSort(a, q + 1, r); } } static int Partition(Card[] a, int p, int r) { Card x = a[r]; int i = p - 1; for (int j = p; j < r; ++j) { if (a[j].d <= x.d) { i = i + 1; Card tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } Card tmp2 = a[i + 1]; a[i + 1] = a[r]; a[r] = tmp2; return i + 1; } static void Merge(Card[] a, int left, int mid, int right) { int n1 = mid - left; int n2 = right - mid; Card[] L = new Card[n1 + 1]; Card[] R = new Card[n2 + 1]; for (int i = 0; i < n1; ++i) { L[i] = a[left + i]; } for (int i = 0; i < n2; ++i) { R[i] = a[mid + i]; } L[n1].d = int.MaxValue; R[n2].d = int.MaxValue; { int i = 0; int j = 0; for (int k = left; k < right; ++k) { if (L[i].d <= R[j].d) { a[k] = L[i]; i = i + 1; } else { a[k] = R[j]; j = j + 1; } } } } static void MergeSort(Card[] a, int left, int right) { if (left + 1 < right) { int mid = (left + right) / 2; MergeSort(a, left, mid); MergeSort(a, mid, right); Merge(a, left, mid, right); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Practice { class Program { public struct Card { public char c; public int d; } static void Main(string[] args) { SolveA(args); // SolveB(args); // SolveC(args); // SolveD(args); } static void SolveA(string[] args) { int n = int.Parse(Console.ReadLine()); Card[] mergeSort = new Card[n]; Card[] s = new Card[n]; for (int i = 0; i < n; ++i) { var c = new Card(); string[] szarr = Console.ReadLine().Split(' '); c.c = szarr[0][0]; c.d = int.Parse(szarr[1]); mergeSort[i] = c; s[i] = c; } MergeSort(mergeSort, 0, mergeSort.Length); QuickSort(s, 0, s.Length - 1); bool stable = true; for (int i = 0; i < n; ++i) { if (mergeSort[i].c != s[i].c || mergeSort[i].d != s[i].d) { stable = false; break; } } Console.WriteLine(stable ? "Stable" : "Not stable"); for (int i = 0; i < s.Length; ++i) { var c = s[i]; Console.WriteLine(c.c + " " + c.d); } } static void QuickSort(Card[] a, int p, int r) { if (p < r) { int q = Partition(a, p, r); QuickSort(a, p, q - 1); QuickSort(a, q + 1, r); } } static int Partition(Card[] a, int p, int r) { Card x = a[r]; int i = p - 1; for (int j = p; j < r; ++j) { if (a[j].d <= x.d) { i = i + 1; Card tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } Card tmp2 = a[i + 1]; a[i + 1] = a[r]; a[r] = tmp2; return i + 1; } static void Merge(Card[] a, int left, int mid, int right) { int n1 = mid - left; int n2 = right - mid; Card[] L = new Card[n1 + 1]; Card[] R = new Card[n2 + 1]; for (int i = 0; i < n1; ++i) { L[i] = a[left + i]; } for (int i = 0; i < n2; ++i) { R[i] = a[mid + i]; } L[n1].d = int.MaxValue; R[n2].d = int.MaxValue; { int i = 0; int j = 0; for (int k = left; k < right; ++k) { if (L[i].d <= R[j].d) { a[k] = L[i]; i = i + 1; } else { a[k] = R[j]; j = j + 1; } } } } static void MergeSort(Card[] a, int left, int right) { if (left + 1 < right) { int mid = (left + right) / 2; MergeSort(a, left, mid); MergeSort(a, mid, right); Merge(a, left, mid, right); } } } }
[["-", 3, 4, 0, 28, 0, 16, 31, 214, 205, 22], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 205, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
805
4
using System; using System.Collections.Generic; public class my { public static void Main() { var n = my.GetInt(); var A = new Card[n]; for (int i = 0; i < n; i++) { A[i] = new Card(Console.ReadLine()); } Card[] B = new Card[n]; A.CopyTo(B, 0); my.QuickSort(A, 0, n - 1); my.MergeSort(B, 0, n - 1); // stablity check bool isStable = true; for (int i = 0; i < A.Length; i++) { if (B[i].ToString() != A[i].ToString()) { isStable = false; break; } } Console.WriteLine(isStable ? "Stable" : "Not stable"); for (int i = 0; i < n; i++) { Console.WriteLine(A[i].ToString()); } } public static void BubbleSort(Card[] A) { bool flag = true; while (flag) { flag = false; for (int i = A.Length - 1; i > 0; i--) { if (A[i - 1].CompareTo(A[i]) > 0) { Card.Swap(ref A[i], ref A[i - 1]); flag = true; } } } } private static void QuickSort(Card[] A, int p, int r) { if (p < r) { var q = my.Partition(A, p, r); my.QuickSort(A, p, q - 1); my.QuickSort(A, q + 1, r); } } private static int Partition(Card[] A, int p, int r) { var x = A[r]; var i = p - 1; for (int j = p; j < r; j++) { if (A[j].CompareTo(x) <= 0) { Card.Swap(A, ++i, j); } } Card.Swap(A, i + 1, r); return i + 1; // pivot index } private static int Partition(int[] A, int p, int r) { var x = A[r]; var i = p - 1; for (int j = p; j < r; j++) { if (A[j] <= x) { my.Swap(A, ++i, j); } } my.Swap(A, i + 1, r); return i + 1; // pivot index } private static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { int mid = (left + right) / 2; my.MergeSort(A, left, mid); my.MergeSort(A, mid, right); my.Merge(A, left, mid, right); } } private static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; Array.Copy(A, left, L, 0, n1); var R = new Card[n2 + 1]; // for sentinel Array.Copy(A, mid, R, 0, n2); L[n1] = new Card(int.MaxValue); R[n2] = new Card(int.MaxValue); int i = 0, j = 0; for (int k = left; k < right; k++) { if (L[i].CompareTo(R[j]) <= 0) { A[k] = L[i++]; } else { A[k] = R[j++]; } } } private static void Swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } private static int GetInt() { return int.Parse(Console.ReadLine()); } private static int[] GetIntArray() { return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } private static int[] GetIntArray(int n) { var A = new int[n]; var s = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { A[i] = int.Parse(s[i]); } return A; } private static int[] GetIntArray(int n, string t) { var A = new int[n]; var s = t.Split(' '); for (int i = 0; i < n; i++) { A[i] = int.Parse(s[i]); } return A; } private static int[] GetIntArray(char c) { return Array.ConvertAll(Console.ReadLine().Split(c), int.Parse); } private static int[] GetIntArray(char c, string s) { return Array.ConvertAll(s.Split(c), int.Parse); } private static int[] GetIntArray(string s) { return Array.ConvertAll(s.Split(' '), int.Parse); } private static double[] GetDoubleArray() { return Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); } public static string Reverse(string s) { var c = s.ToCharArray(); Array.Reverse(c); return new String(c); } private static void WriteArray(int[] a) { Console.WriteLine(string.Join(" ", Array.ConvertAll(a, x => x.ToString()))); } private static void WriteArray(long[] a) { Console.WriteLine(string.Join(" ", Array.ConvertAll(a, x => x.ToString()))); } private static void WriteVector(Vector v) { Console.WriteLine(string.Format("{0:f8} {1:f8}", v.X, v.Y)); } } public struct Vector { public double X, Y; } public class Card { public int Number { get; private set; } public char Suit { get; private set; } public Card(string s) { this.Suit = s[0]; this.Number = int.Parse(s.Substring(2)); } public Card(int n) { this.Suit = '*'; this.Number = n; } public int CompareTo(Card c) { return this.Number - c.Number; } public override string ToString() { return Suit.ToString() + " " + Number.ToString(); } public static void Swap(ref Card a, ref Card b) { Card tmp; tmp = a; a = b; b = tmp; } public static void Swap(Card[] A, int i, int j) { Card tmp = A[i]; A[i] = A[j]; A[j] = tmp; } }
using System; using System.Collections.Generic; public class my { public static void Main() { var n = my.GetInt(); var A = new Card[n]; for (int i = 0; i < n; i++) { A[i] = new Card(Console.ReadLine()); } Card[] B = new Card[n]; A.CopyTo(B, 0); my.QuickSort(A, 0, n - 1); my.MergeSort(B, 0, n); // stablity check bool isStable = true; for (int i = 0; i < A.Length; i++) { if (B[i].ToString() != A[i].ToString()) { isStable = false; break; } } Console.WriteLine(isStable ? "Stable" : "Not stable"); for (int i = 0; i < n; i++) { Console.WriteLine(A[i].ToString()); } } public static void BubbleSort(Card[] A) { bool flag = true; while (flag) { flag = false; for (int i = A.Length - 1; i > 0; i--) { if (A[i - 1].CompareTo(A[i]) > 0) { Card.Swap(ref A[i], ref A[i - 1]); flag = true; } } } } private static void QuickSort(Card[] A, int p, int r) { if (p < r) { var q = my.Partition(A, p, r); my.QuickSort(A, p, q - 1); my.QuickSort(A, q + 1, r); } } private static int Partition(Card[] A, int p, int r) { var x = A[r]; var i = p - 1; for (int j = p; j < r; j++) { if (A[j].CompareTo(x) <= 0) { Card.Swap(A, ++i, j); } } Card.Swap(A, i + 1, r); return i + 1; // pivot index } private static int Partition(int[] A, int p, int r) { var x = A[r]; var i = p - 1; for (int j = p; j < r; j++) { if (A[j] <= x) { my.Swap(A, ++i, j); } } my.Swap(A, i + 1, r); return i + 1; // pivot index } private static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { int mid = (left + right) / 2; my.MergeSort(A, left, mid); my.MergeSort(A, mid, right); my.Merge(A, left, mid, right); } } private static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; Array.Copy(A, left, L, 0, n1); var R = new Card[n2 + 1]; // for sentinel Array.Copy(A, mid, R, 0, n2); L[n1] = new Card(int.MaxValue); R[n2] = new Card(int.MaxValue); int i = 0, j = 0; for (int k = left; k < right; k++) { if (L[i].CompareTo(R[j]) <= 0) { A[k] = L[i++]; } else { A[k] = R[j++]; } } } private static void Swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } private static int GetInt() { return int.Parse(Console.ReadLine()); } private static int[] GetIntArray() { return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } private static int[] GetIntArray(int n) { var A = new int[n]; var s = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { A[i] = int.Parse(s[i]); } return A; } private static int[] GetIntArray(int n, string t) { var A = new int[n]; var s = t.Split(' '); for (int i = 0; i < n; i++) { A[i] = int.Parse(s[i]); } return A; } private static int[] GetIntArray(char c) { return Array.ConvertAll(Console.ReadLine().Split(c), int.Parse); } private static int[] GetIntArray(char c, string s) { return Array.ConvertAll(s.Split(c), int.Parse); } private static int[] GetIntArray(string s) { return Array.ConvertAll(s.Split(' '), int.Parse); } private static double[] GetDoubleArray() { return Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse); } public static string Reverse(string s) { var c = s.ToCharArray(); Array.Reverse(c); return new String(c); } private static void WriteArray(int[] a) { Console.WriteLine(string.Join(" ", Array.ConvertAll(a, x => x.ToString()))); } private static void WriteArray(long[] a) { Console.WriteLine(string.Join(" ", Array.ConvertAll(a, x => x.ToString()))); } private static void WriteVector(Vector v) { Console.WriteLine(string.Format("{0:f8} {1:f8}", v.X, v.Y)); } } public struct Vector { public double X, Y; } public class Card { public int Number { get; private set; } public char Suit { get; private set; } public Card(string s) { this.Suit = s[0]; this.Number = int.Parse(s.Substring(2)); } public Card(int n) { this.Suit = '*'; this.Number = n; } public int CompareTo(Card c) { return this.Number - c.Number; } public override string ToString() { return Suit.ToString() + " " + Number.ToString(); } public static void Swap(ref Card a, ref Card b) { Card tmp; tmp = a; a = b; b = tmp; } public static void Swap(Card[] A, int i, int j) { Card tmp = A[i]; A[i] = A[j]; A[j] = tmp; } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
1,536
2
using System; class Program { class Card { public int value; public char suit; public Card(char suit, int value) { this.suit = suit; this.value = value; } } static void Swap(Card[] A, int a, int b) { var tmp = A[a]; A[a] = A[b]; A[b] = tmp; } static int Partition(Card[] A, int p, int r) { var x = A[r - 1].value; var i = p - 1; for (var j = p; j < r - 1; j++) { if (A[j].value <= x) { i++; Swap(A, i, j); } } Swap(A, i + 1, r - 1); return i + 1; } static void QuickSort(Card[] A, int p, int r) { if (p + 1 < r) { var q = Partition(A, p, r); QuickSort(A, p, q - 1); QuickSort(A, q, r); } } static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; var R = new Card[n2 + 1]; for (var i = 0; i < n1; i++) { L[i] = A[left + i]; } for (var i = 0; i < n2; i++) { R[i] = A[mid + i]; } L[n1] = new Card('X', 1000 * 1000 * 1000 + 1); R[n2] = new Card('X', 1000 * 1000 * 1000 + 1); var l = 0; var j = 0; for (var k = left; k < right; k++) { if (L[l].value <= R[j].value) { A[k] = L[l]; l++; } else { A[k] = R[j]; j++; } } } static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { var mid = (left + right) / 2; MergeSort(A, left, mid); MergeSort(A, mid, right); Merge(A, left, mid, right); } } public static void Main() { var n = int.Parse(Console.ReadLine()); var A = new Card[n]; var B = new Card[n]; for (var i = 0; i < n; i++) { var s = Console.ReadLine().Split(' '); A[i] = new Card(s[0][0], int.Parse(s[1])); B[i] = new Card(s[0][0], int.Parse(s[1])); } QuickSort(A, 0, n); MergeSort(B, 0, n); var isStable = true; for (var i = 0; i < n; i++) { if (A[i].suit != B[i].suit) { isStable = false; break; } } var sout = new System.Text.StringBuilder(); sout.Append(isStable ? "Stable\n" : "Not stable\n"); for (var i = 0; i < n; i++) { sout.Append(A[i].suit + " " + A[i].value + "\n"); } Console.Write(sout); } }
using System; class Program { class Card { public int value; public char suit; public Card(char suit, int value) { this.suit = suit; this.value = value; } } static void Swap(Card[] A, int a, int b) { var tmp = A[a]; A[a] = A[b]; A[b] = tmp; } static int Partition(Card[] A, int p, int r) { var x = A[r - 1].value; var i = p - 1; for (var j = p; j < r - 1; j++) { if (A[j].value <= x) { i++; Swap(A, i, j); } } Swap(A, i + 1, r - 1); return i + 1; } static void QuickSort(Card[] A, int p, int r) { if (p + 1 < r) { var q = Partition(A, p, r); QuickSort(A, p, q); QuickSort(A, q, r); } } static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; var R = new Card[n2 + 1]; for (var i = 0; i < n1; i++) { L[i] = A[left + i]; } for (var i = 0; i < n2; i++) { R[i] = A[mid + i]; } L[n1] = new Card('X', 1000 * 1000 * 1000 + 1); R[n2] = new Card('X', 1000 * 1000 * 1000 + 1); var l = 0; var j = 0; for (var k = left; k < right; k++) { if (L[l].value <= R[j].value) { A[k] = L[l]; l++; } else { A[k] = R[j]; j++; } } } static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { var mid = (left + right) / 2; MergeSort(A, left, mid); MergeSort(A, mid, right); Merge(A, left, mid, right); } } public static void Main() { var n = int.Parse(Console.ReadLine()); var A = new Card[n]; var B = new Card[n]; for (var i = 0; i < n; i++) { var s = Console.ReadLine().Split(' '); A[i] = new Card(s[0][0], int.Parse(s[1])); B[i] = new Card(s[0][0], int.Parse(s[1])); } QuickSort(A, 0, n); MergeSort(B, 0, n); var isStable = true; for (var i = 0; i < n; i++) { if (A[i].suit != B[i].suit) { isStable = false; break; } } var sout = new System.Text.StringBuilder(); sout.Append(isStable ? "Stable\n" : "Not stable\n"); for (var i = 0; i < n; i++) { sout.Append(A[i].suit + " " + A[i].value + "\n"); } Console.Write(sout); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 33], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
802
2
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); Tuple<char, int>[] cards = new Tuple<char, int>[n]; for (int i = 0; i < n; i++) { string[] inputs = Console.ReadLine().Split(' '); cards[i] = new Tuple<char, int>(inputs[0][0], int.Parse(inputs[1])); } Tuple<char, int>[] origin = new Tuple<char, int>[n]; cards.CopyTo(origin, 0); QuickSort(ref cards, 0, cards.Length - 1); Console.WriteLine(IsStable(cards, origin) ? "Stable" : "Not Stable"); for (int i = 0; i < n; i++) { Console.WriteLine(cards[i].ToString()); } } static void QuickSort(ref Tuple<char, int>[] A, int p, int r) { if (p < r) { int q = Partition(ref A, p, r); QuickSort(ref A, p, q - 1); QuickSort(ref A, q + 1, r); } } static int Partition(ref Tuple<char, int>[] A, int p, int r) { int x = A[r].Item2; int i = p - 1; for (int j = p; j < r; j++) { if (A[j].Item2 <= x) { i++; Swap(ref A, i, j); } } Swap(ref A, i + 1, r); return i + 1; } static void Swap(ref Tuple<char, int>[] A, int i, int j) { try { Tuple<char, int> tmp = A[i]; A[i] = A[j]; A[j] = tmp; } catch (IndexOutOfRangeException ex) { Console.Error.WriteLine(ex.Message); } } static bool IsStable(Tuple<char, int>[] A, Tuple<char, int>[] B) { if (A.Length != B.Length) return false; Tuple<char, int>[] stableA = B.OrderBy(elem => elem.Item2).ToArray(); for (int i = 0; i < A.Length; i++) { if (A[i] != stableA[i]) return false; } return true; } } class Tuple<T, U> { public T Item1 { get; set; } public U Item2 { get; set; } public Tuple(T item1, U item2) { this.Item1 = item1; this.Item2 = item2; } public override string ToString() { return string.Format("{0} {1}", this.Item1, this.Item2); } }
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); Tuple<char, int>[] cards = new Tuple<char, int>[n]; for (int i = 0; i < n; i++) { string[] inputs = Console.ReadLine().Split(' '); cards[i] = new Tuple<char, int>(inputs[0][0], int.Parse(inputs[1])); } Tuple<char, int>[] origin = new Tuple<char, int>[n]; cards.CopyTo(origin, 0); QuickSort(ref cards, 0, cards.Length - 1); Console.WriteLine(IsStable(cards, origin) ? "Stable" : "Not stable"); for (int i = 0; i < n; i++) { Console.WriteLine(cards[i].ToString()); } } static void QuickSort(ref Tuple<char, int>[] A, int p, int r) { if (p < r) { int q = Partition(ref A, p, r); QuickSort(ref A, p, q - 1); QuickSort(ref A, q + 1, r); } } static int Partition(ref Tuple<char, int>[] A, int p, int r) { int x = A[r].Item2; int i = p - 1; for (int j = p; j < r; j++) { if (A[j].Item2 <= x) { i++; Swap(ref A, i, j); } } Swap(ref A, i + 1, r); return i + 1; } static void Swap(ref Tuple<char, int>[] A, int i, int j) { try { Tuple<char, int> tmp = A[i]; A[i] = A[j]; A[j] = tmp; } catch (IndexOutOfRangeException ex) { Console.Error.WriteLine(ex.Message); } } static bool IsStable(Tuple<char, int>[] A, Tuple<char, int>[] B) { if (A.Length != B.Length) return false; Tuple<char, int>[] stableA = B.OrderBy(elem => elem.Item2).ToArray(); for (int i = 0; i < A.Length; i++) { if (A[i] != stableA[i]) return false; } return true; } } class Tuple<T, U> { public T Item1 { get; set; } public U Item2 { get; set; } public Tuple(T item1, U item2) { this.Item1 = item1; this.Item2 = item2; } public override string ToString() { return string.Format("{0} {1}", this.Item1, this.Item2); } }
[["-", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222]]
8
653
2
using System; using System.IO; namespace Sort_II { class Quick_Sort { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var A = new Card[n]; var B = new Card[n]; for (int i = 0; i < n; i++) { var token = Console.ReadLine().Split(' '); A[i] = B[i] = new Card(token[0][0], int.Parse(token[1])); } var p = 0; var r = A.Length - 1; var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); QuickSort(A, p, r); MergeSort(B, p, r + 1); if (IsStable(A, B)) Console.WriteLine("Stable"); else Console.WriteLine("Not Stable"); for (int i = 0; i < r; i++) Console.WriteLine(A[i]); Console.WriteLine(A[r]); Console.Out.Flush(); } static bool IsStable(Card[] A, Card[] B) { var f = true; for (int i = 0; i < A.Length; i++) { if (!A[i].Equals(B[i])) f = false; } return f; } static public void QuickSort(Card[] A, int p, int r) { if (p < r) { var q = Partition(A, p, r); QuickSort(A, p, q - 1); QuickSort(A, q + 1, r); } } static public int Partition(Card[] A, int p, int r) { var x = A[r].rank; var i = p - 1; for (int j = p; j < r; j++) if (A[j].rank <= x) { i++; var temp = A[i]; A[i] = A[j]; A[j] = temp; } var tmp = A[i + 1]; A[i + 1] = A[r]; A[r] = tmp; return i + 1; } public struct Card { public int rank; char suit; public Card(char c, int r) { rank = r; suit = c; } public override string ToString() { return suit + " " + rank; } } static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; var R = new Card[n2 + 1]; for (int i = 0; i < n1; i++) L[i] = A[left + i]; for (int i = 0; i < n2; i++) R[i] = A[mid + i]; L[n1] = R[n2] = new Card('s', int.MaxValue); var p = 0; var q = 0; for (int k = left; k < right; k++) if (L[p].rank <= R[q].rank) A[k] = L[p++]; else A[k] = R[q++]; } static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { var mid = (left + right) / 2; MergeSort(A, left, mid); MergeSort(A, mid, right); Merge(A, left, mid, right); } } } }
using System; using System.IO; namespace Sort_II { class Quick_Sort { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var A = new Card[n]; var B = new Card[n]; for (int i = 0; i < n; i++) { var token = Console.ReadLine().Split(' '); A[i] = B[i] = new Card(token[0][0], int.Parse(token[1])); } var p = 0; var r = A.Length - 1; var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); QuickSort(A, p, r); MergeSort(B, p, r + 1); if (IsStable(A, B)) Console.WriteLine("Stable"); else Console.WriteLine("Not stable"); for (int i = 0; i < r; i++) Console.WriteLine(A[i]); Console.WriteLine(A[r]); Console.Out.Flush(); } static bool IsStable(Card[] A, Card[] B) { var f = true; for (int i = 0; i < A.Length; i++) { if (!A[i].Equals(B[i])) f = false; } return f; } static public void QuickSort(Card[] A, int p, int r) { if (p < r) { var q = Partition(A, p, r); QuickSort(A, p, q - 1); QuickSort(A, q + 1, r); } } static public int Partition(Card[] A, int p, int r) { var x = A[r].rank; var i = p - 1; for (int j = p; j < r; j++) if (A[j].rank <= x) { i++; var temp = A[i]; A[i] = A[j]; A[j] = temp; } var tmp = A[i + 1]; A[i + 1] = A[r]; A[r] = tmp; return i + 1; } public struct Card { public int rank; char suit; public Card(char c, int r) { rank = r; suit = c; } public override string ToString() { return suit + " " + rank; } } static void Merge(Card[] A, int left, int mid, int right) { var n1 = mid - left; var n2 = right - mid; var L = new Card[n1 + 1]; var R = new Card[n2 + 1]; for (int i = 0; i < n1; i++) L[i] = A[left + i]; for (int i = 0; i < n2; i++) R[i] = A[mid + i]; L[n1] = R[n2] = new Card('s', int.MaxValue); var p = 0; var q = 0; for (int k = left; k < right; k++) if (L[p].rank <= R[q].rank) A[k] = L[p++]; else A[k] = R[q++]; } static void MergeSort(Card[] A, int left, int right) { if (left + 1 < right) { var mid = (left + right) / 2; MergeSort(A, left, mid); MergeSort(A, mid, right); Merge(A, left, mid, right); } } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
806
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Aoj.ALDS.Chapter6C { class Card : IComparable<Card> { public string Suit { get; set; } public int Number { get; set; } public Card(string suit, int number) { Suit = suit; Number = number; } public int CompareTo(Card other) { return Number - other.Number; } public override string ToString() { return string.Format("{0} {1}", Suit, Number); } } class Program { /// <summary> /// array???????????????????????????????????????(??´?£??????????)??? /// </summary> /// <param name="array"></param> /// <param name="left"></param> /// <param name="right">(????°????????´???????????????????)</param> public static void MergeSort<T>(List<T> array, int left, int right) where T : IComparable<T> { if (left + 1 < right) { int mid = (left + right) / 2; MergeSort(array, left, mid); MergeSort(array, mid, right); Merge(array, left, mid, right); } } private static void Merge<T>(List<T> array, int left, int mid, int right) where T : IComparable<T> { int n1 = mid - left; int n2 = right - mid; if (n1 < 0 || n2 < 0) { throw new System.ArgumentException(""); } var leftArray = new List<T>(); var rightArray = new List<T>(); for (int i = 0; i < n1; i++) { leftArray.Add(array[left + i]); } for (int i = 0; i < n2; i++) { rightArray.Add(array[mid + i]); } int lc = 0; int rc = 0; for (int i = left; i < right; i++) { if (rc >= n2 || (lc < n1 && leftArray[lc].CompareTo(rightArray[rc]) <= 0)) { array[i] = leftArray[lc]; lc++; } else { array[i] = rightArray[rc]; rc++; } } } public static void QuickSort<T>(List<T> array, int first, int last) where T : IComparable<T> { if (first >= last || last < 0) return; // Partition??§????????????(??????)??? int pIndex = Partition(array, first, last); // ????????¨?????¨????????¨??????????????????????????¨??? // (??????????????§array????????´??????????????§?????±??????????????????????????????????????????) QuickSort(array, first, pIndex - 1); QuickSort(array, pIndex + 1, last); } /// <summary> /// partition??§?????????????????????????????? /// ??????????????????????´?????????????????????\?????? /// ??????????????????????´???????????????????????????§?????????????????????array?????????????????? /// (array???????????´?£???????????????????)??? /// </summary> /// <param name="array"></param> /// <param name="first">??°??????????????¨??????????´??????????</param> /// <param /// name="last">??°??????????????¨??????????´???????(??????)???</param> /// <param /// name="partition">partition??¨??????????????????????´??????????</param> /// <returns>partition??????????±??????????????´??????????</returns> private static int Partition<T>(List<T> array, int first, int last, int partition = -1) where T : IComparable<T> { if (first < 0 || last >= array.Count) throw new System.ArgumentException(""); if (partition == -1) // ?????????????????§???????°????????´?????????????????????£?????§??????????????? partition = last; // ???????????£?????§???????±???¨?????????(last?????????????§????????????????)??? T x = array[partition]; Swap(array, partition, last); int i = first; for (int j = first; j < last; j++) { if (array[j].CompareTo(x) <= 0) { Swap(array, i, j); // ?????°i???Swap???????????´??°???????????????????????????????????¨????¢????????????????????????????????????? i++; } } // ????°?????????????????????????x?????????????????£?????§??????????±??????????Swap??? Swap(array, i, last); return i; } private static void Swap<T>(List<T> array, int i, int j) { T tmp = array[i]; array[i] = array[j]; array[j] = tmp; } public static void Solve() { int n = int.Parse(Console.ReadLine()); var m_cards = new List<Card>(); var q_cards = new List<Card>(); for (int i = 0; i < n; i++) { string[] suit_and_num = Console.ReadLine().Split(' '); var card = new Card(suit_and_num[0], int.Parse(suit_and_num[1])); // MEMO: // ????????????????????????????????????????????´?£?????????????????????????????????§?????????????????? // ?????????list????????????????´?????????§????????£??????????????¨?????¨?????? m_cards.Add(card); q_cards.Add(card); } // ?????????????????§?????????????????? MergeSort(m_cards, 0, n); QuickSort(q_cards, 0, n - 1); // ????????????????????¨???????????????????????? // ?????????????????????????????????????????¨?¨?????????? bool stable = true; for (int i = 0; i < n; i++) { stable = stable && m_cards[i].Suit == q_cards[i].Suit && m_cards[i].Number == q_cards[i].Number; } Console.WriteLine("{0}", stable ? "Stable" : "Not Stable"); // Console.WriteLine("Merge Sorted Cards:"); // m_cards.ForEach(card => Console.WriteLine(card)); // Console.WriteLine("Quick Sorted Cards:"); q_cards.ForEach(card => Console.WriteLine(card)); } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/06_Sort_C_02.txt")); Solve(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Aoj.ALDS.Chapter6C { class Card : IComparable<Card> { public string Suit { get; set; } public int Number { get; set; } public Card(string suit, int number) { Suit = suit; Number = number; } public int CompareTo(Card other) { return Number - other.Number; } public override string ToString() { return string.Format("{0} {1}", Suit, Number); } } class Program { /// <summary> /// array???????????????????????????????????????(??´?£??????????)??? /// </summary> /// <param name="array"></param> /// <param name="left"></param> /// <param name="right">(????°????????´???????????????????)</param> public static void MergeSort<T>(List<T> array, int left, int right) where T : IComparable<T> { if (left + 1 < right) { int mid = (left + right) / 2; MergeSort(array, left, mid); MergeSort(array, mid, right); Merge(array, left, mid, right); } } private static void Merge<T>(List<T> array, int left, int mid, int right) where T : IComparable<T> { int n1 = mid - left; int n2 = right - mid; if (n1 < 0 || n2 < 0) { throw new System.ArgumentException(""); } var leftArray = new List<T>(); var rightArray = new List<T>(); for (int i = 0; i < n1; i++) { leftArray.Add(array[left + i]); } for (int i = 0; i < n2; i++) { rightArray.Add(array[mid + i]); } int lc = 0; int rc = 0; for (int i = left; i < right; i++) { if (rc >= n2 || (lc < n1 && leftArray[lc].CompareTo(rightArray[rc]) <= 0)) { array[i] = leftArray[lc]; lc++; } else { array[i] = rightArray[rc]; rc++; } } } public static void QuickSort<T>(List<T> array, int first, int last) where T : IComparable<T> { if (first >= last || last < 0) return; // Partition??§????????????(??????)??? int pIndex = Partition(array, first, last); // ????????¨?????¨????????¨??????????????????????????¨??? // (??????????????§array????????´??????????????§?????±??????????????????????????????????????????) QuickSort(array, first, pIndex - 1); QuickSort(array, pIndex + 1, last); } /// <summary> /// partition??§?????????????????????????????? /// ??????????????????????´?????????????????????\?????? /// ??????????????????????´???????????????????????????§?????????????????????array?????????????????? /// (array???????????´?£???????????????????)??? /// </summary> /// <param name="array"></param> /// <param name="first">??°??????????????¨??????????´??????????</param> /// <param /// name="last">??°??????????????¨??????????´???????(??????)???</param> /// <param /// name="partition">partition??¨??????????????????????´??????????</param> /// <returns>partition??????????±??????????????´??????????</returns> private static int Partition<T>(List<T> array, int first, int last, int partition = -1) where T : IComparable<T> { if (first < 0 || last >= array.Count) throw new System.ArgumentException(""); if (partition == -1) // ?????????????????§???????°????????´?????????????????????£?????§??????????????? partition = last; // ???????????£?????§???????±???¨?????????(last?????????????§????????????????)??? T x = array[partition]; Swap(array, partition, last); int i = first; for (int j = first; j < last; j++) { if (array[j].CompareTo(x) <= 0) { Swap(array, i, j); // ?????°i???Swap???????????´??°???????????????????????????????????¨????¢????????????????????????????????????? i++; } } // ????°?????????????????????????x?????????????????£?????§??????????±??????????Swap??? Swap(array, i, last); return i; } private static void Swap<T>(List<T> array, int i, int j) { T tmp = array[i]; array[i] = array[j]; array[j] = tmp; } public static void Solve() { int n = int.Parse(Console.ReadLine()); var m_cards = new List<Card>(); var q_cards = new List<Card>(); for (int i = 0; i < n; i++) { string[] suit_and_num = Console.ReadLine().Split(' '); var card = new Card(suit_and_num[0], int.Parse(suit_and_num[1])); // MEMO: // ????????????????????????????????????????????´?£?????????????????????????????????§?????????????????? // ?????????list????????????????´?????????§????????£??????????????¨?????¨?????? m_cards.Add(card); q_cards.Add(card); } // ?????????????????§?????????????????? MergeSort(m_cards, 0, n); QuickSort(q_cards, 0, n - 1); // ????????????????????¨???????????????????????? // ?????????????????????????????????????????¨?¨?????????? bool stable = true; for (int i = 0; i < n; i++) { stable = stable && m_cards[i].Suit == q_cards[i].Suit && m_cards[i].Number == q_cards[i].Number; } Console.WriteLine("{0}", stable ? "Stable" : "Not stable"); // Console.WriteLine("Merge Sorted Cards:"); // m_cards.ForEach(card => Console.WriteLine(card)); // Console.WriteLine("Quick Sorted Cards:"); q_cards.ForEach(card => Console.WriteLine(card)); } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/06_Sort_C_03.txt")); Solve(); } } }
[["-", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 41, 75, 5, 0, 222]]
8
962
2
using System; using System.Linq; using System.Text; namespace test { class Program { struct Card { internal char suit; internal int value; } static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Card[] A1 = new Card[n]; Card[] A2 = new Card[n]; string[] input; for (int i = 0; i < n; i++) { input = Console.ReadLine().Split(' '); A1[i].suit = input[0][0]; A2[i].suit = input[0][0]; A1[i].value = int.Parse(input[1]); A2[i].value = int.Parse(input[1]); } QuickSort(A1, 0, n - 1); A2 = A2.OrderBy(a => a.value).ToArray(); StringBuilder sb = new StringBuilder(); sb.AppendLine(IsStabel(A1, A2, n) ? "Stable" : "Not stable"); foreach (var a in A1) { sb.AppendLine(a.suit + " " + a.value); } Console.WriteLine(sb); // Console.ReadLine(); } static void QuickSort(Card[] A, int p, int r) { if (p < r) { int q = Partition(A, p, r); // 前パーテーションを再度クイックソート QuickSort(A, p, q - 1); // 後ろパーテーションを再度クイックソート QuickSort(A, q + 1, r); } } static int Partition(Card[] A, int p, int r) { int x = A[r].value; // iが前パーテーションの末尾 int i = p - 1; // jが後ろパーテーションの末尾 for (int j = p; j < r; j++) { if (A[j].value <= x) { i++; var tmp = A[j]; A[j] = A[i]; A[i] = tmp; } } var tmp2 = A[i + 1]; A[i + 1] = A[r]; A[r] = tmp2; // パーテーションで区切られた場所のkeyを返す return i + 1; } static bool IsStabel(Card[] A1, Card[] A2, int n) { for (int i = 0; i < n; i++) { if (A1[i].suit == A2[i].suit) return false; } return true; } } }
using System; using System.Linq; using System.Text; namespace test { class Program { struct Card { internal char suit; internal int value; } static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Card[] A1 = new Card[n]; Card[] A2 = new Card[n]; string[] input; for (int i = 0; i < n; i++) { input = Console.ReadLine().Split(' '); A1[i].suit = input[0][0]; A2[i].suit = input[0][0]; A1[i].value = int.Parse(input[1]); A2[i].value = int.Parse(input[1]); } QuickSort(A1, 0, n - 1); A2 = A2.OrderBy(a => a.value).ToArray(); StringBuilder sb = new StringBuilder(); sb.AppendLine(IsStabel(A1, A2, n) ? "Stable" : "Not stable"); foreach (var a in A1) { sb.AppendLine(a.suit + " " + a.value); } Console.Write(sb); } static void QuickSort(Card[] A, int p, int r) { if (p < r) { int q = Partition(A, p, r); // 前パーテーションを再度クイックソート QuickSort(A, p, q - 1); // 後ろパーテーションを再度クイックソート QuickSort(A, q + 1, r); } } static int Partition(Card[] A, int p, int r) { int x = A[r].value; // iが前パーテーションの末尾 int i = p - 1; // jが後ろパーテーションの末尾 for (int j = p; j < r; j++) { if (A[j].value <= x) { i++; var tmp = A[j]; A[j] = A[i]; A[i] = tmp; } } var tmp2 = A[i + 1]; A[i + 1] = A[r]; A[r] = tmp2; // パーテーションで区切られた場所のkeyを返す return i + 1; } static bool IsStabel(Card[] A1, Card[] A2, int n) { for (int i = 0; i < n; i++) { if (A1[i].suit != A2[i].suit) return false; } return true; } } }
[["-", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["+", 8, 196, 0, 1, 0, 213, 63, 214, 141, 22], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 79]]
8
518
4
using System; namespace B { class Check { public int dep(Node[] node, int id) { if (node[id].p == -1) { return 0; } return dep(node, node[id].p) + 1; } public int height(Node[] node, int id) { if (id == -1) { return 0; } return Math.Max(height(node, node[id].left), height(node, node[id].right)) + 1; } } class Node { public int id; public int p; public int s; public int deg; public int dep; public int h; public string type; public int left; public int right; public void node() { id = 0; p = -1; s = -1; deg = 0; dep = 0; h = 0; type = "none"; left = 0; right = 0; } } class B { static void Main(string[] args) { string input1 = Console.ReadLine(); int n = int.Parse(input1), i, nowid; Node[] node = new Node[n]; string[] input2 = new string[100000]; Check check = new Check(); for (i = 0; i < n; i++) { node[i] = new Node(); node[i].id = i; node[i].p = -1; node[i].s = -1; } for (i = 0; i < n; i++) { input2 = Console.ReadLine().Split(' '); nowid = int.Parse(input2[0]); node[nowid].left = int.Parse(input2[1]); node[nowid].right = int.Parse(input2[2]); if (node[nowid].left == -1 && node[nowid].right == -1) { node[nowid].type = "leaf"; } if (node[nowid].left != -1) { node[nowid].deg++; } if (node[nowid].right != -1) { node[nowid].deg++; } } for (i = 0; i < n; i++) { if (node[i].left != -1) { node[node[i].left].p = node[i].id; } if (node[i].right != -1) { node[node[i].right].p = node[i].id; } } for (i = 0; i < n; i++) { node[i].dep = check.dep(node, i); node[i].h = check.height(node, i); if (node[i].left != -1 && node[i].right != -1) { node[node[i].left].s = node[i].right; node[node[i].right].s = node[i].left; } } //出力 for (i = 0; i < n; i++) { if (node[i].dep == 0) { node[i].type = "root"; } else if (node[i].type != "leaf") { node[i].type = "internal node"; } Console.WriteLine("node " + node[i].id + ": parent = " + node[i].p + ", sibling = " + node[i].s + ", degree = " + node[i].deg + ", depth = " + node[i].dep + ", height = " + node[i].h + ", " + node[i].type); } } } }
using System; namespace B { class Check { public int dep(Node[] node, int id) { if (node[id].p == -1) { return 0; } return dep(node, node[id].p) + 1; } public int height(Node[] node, int id) { if (id == -1) { return -1; } return Math.Max(height(node, node[id].left), height(node, node[id].right)) + 1; } } class Node { public int id; public int p; public int s; public int deg; public int dep; public int h; public string type; public int left; public int right; public void node() { id = 0; p = -1; s = -1; deg = 0; dep = 0; h = 0; type = "none"; left = 0; right = 0; } } class B { static void Main(string[] args) { string input1 = Console.ReadLine(); int n = int.Parse(input1), i, nowid; Node[] node = new Node[n]; string[] input2 = new string[100000]; Check check = new Check(); for (i = 0; i < n; i++) { node[i] = new Node(); node[i].id = i; node[i].p = -1; node[i].s = -1; } for (i = 0; i < n; i++) { input2 = Console.ReadLine().Split(' '); nowid = int.Parse(input2[0]); node[nowid].left = int.Parse(input2[1]); node[nowid].right = int.Parse(input2[2]); if (node[nowid].left == -1 && node[nowid].right == -1) { node[nowid].type = "leaf"; } if (node[nowid].left != -1) { node[nowid].deg++; } if (node[nowid].right != -1) { node[nowid].deg++; } } for (i = 0; i < n; i++) { if (node[i].left != -1) { node[node[i].left].p = node[i].id; } if (node[i].right != -1) { node[node[i].right].p = node[i].id; } } for (i = 0; i < n; i++) { node[i].dep = check.dep(node, i); node[i].h = check.height(node, i); if (node[i].left != -1 && node[i].right != -1) { node[node[i].left].s = node[i].right; node[node[i].right].s = node[i].left; } } //出力 for (i = 0; i < n; i++) { if (node[i].dep == 0) { node[i].type = "root"; } else if (node[i].type != "leaf") { node[i].type = "internal node"; } Console.WriteLine("node " + node[i].id + ": parent = " + node[i].p + ", sibling = " + node[i].s + ", degree = " + node[i].deg + ", depth = " + node[i].dep + ", height = " + node[i].h + ", " + node[i].type); } } } }
[["-", 8, 196, 0, 57, 64, 196, 0, 37, 0, 203], ["+", 0, 57, 64, 196, 0, 37, 0, 241, 0, 33], ["+", 0, 57, 64, 196, 0, 37, 0, 241, 0, 203]]
8
818
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Practice { class Program { public class Node { public int id; public int parent = -1; public int left = -1; public int right = -1; public int sibling = -1; public int height; public int depth; } static void Main(string[] args) { SolveA(args); // SolveB(args); // SolveC(args); // SolveD(args); } static void SolveA(string[] args) { int n = int.Parse(Console.ReadLine()); Node[] nodes = new Node[n]; for (int i = 0; i < nodes.Length; ++i) { nodes[i] = new Node(); } for (int i = 0; i < n; ++i) { int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int id = arr[0]; int l = arr[1]; int r = arr[2]; nodes[id].id = id; nodes[id].left = l; nodes[id].right = r; if (l != -1) { nodes[l].parent = id; nodes[l].sibling = r; } if (r != -1) { nodes[r].parent = id; nodes[r].sibling = l; } } Node rootnode = nodes.First(p => p.parent == -1); SetDepth(nodes, rootnode); SetHeight(nodes, rootnode); foreach (Node node in nodes) { string type = null; if (node.depth == 0) { type = "root"; } else if (node.left != -1 || node.right != -1) { type = "internal node"; } else { type = "leaf"; } int degree = 0; if (node.left != -1) { degree++; } if (node.right != -1) { degree++; } string sz = string.Format( "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, hright = {5}, {6}", node.id, node.parent, node.sibling, degree, node.depth, node.height, type); Console.WriteLine(sz); } } private static void SetDepth(Node[] nodes, Node parent) { if (parent.left != -1) { nodes[parent.left].depth = parent.depth + 1; SetDepth(nodes, nodes[parent.left]); } if (parent.right != -1) { nodes[parent.right].depth = parent.depth + 1; SetDepth(nodes, nodes[parent.right]); } } private static int SetHeight(Node[] nodes, Node parent) { int h1 = 0; int h2 = 0; if (parent.left != -1) { h1 = SetHeight(nodes, nodes[parent.left]) + 1; } if (parent.right != -1) { h2 = SetHeight(nodes, nodes[parent.right]) + 1; } int h = Math.Max(h1, h2); parent.height = h; return h; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Practice { class Program { public class Node { public int id; public int parent = -1; public int left = -1; public int right = -1; public int sibling = -1; public int height; public int depth; } static void Main(string[] args) { SolveA(args); // SolveB(args); // SolveC(args); // SolveD(args); } static void SolveA(string[] args) { int n = int.Parse(Console.ReadLine()); Node[] nodes = new Node[n]; for (int i = 0; i < nodes.Length; ++i) { nodes[i] = new Node(); } for (int i = 0; i < n; ++i) { int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int id = arr[0]; int l = arr[1]; int r = arr[2]; nodes[id].id = id; nodes[id].left = l; nodes[id].right = r; if (l != -1) { nodes[l].parent = id; nodes[l].sibling = r; } if (r != -1) { nodes[r].parent = id; nodes[r].sibling = l; } } Node rootnode = nodes.First(p => p.parent == -1); SetDepth(nodes, rootnode); SetHeight(nodes, rootnode); foreach (Node node in nodes) { string type = null; if (node.depth == 0) { type = "root"; } else if (node.left != -1 || node.right != -1) { type = "internal node"; } else { type = "leaf"; } int degree = 0; if (node.left != -1) { degree++; } if (node.right != -1) { degree++; } string sz = string.Format( "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}", node.id, node.parent, node.sibling, degree, node.depth, node.height, type); Console.WriteLine(sz); } } private static void SetDepth(Node[] nodes, Node parent) { if (parent.left != -1) { nodes[parent.left].depth = parent.depth + 1; SetDepth(nodes, nodes[parent.left]); } if (parent.right != -1) { nodes[parent.right].depth = parent.depth + 1; SetDepth(nodes, nodes[parent.right]); } } private static int SetHeight(Node[] nodes, Node parent) { int h1 = 0; int h2 = 0; if (parent.left != -1) { h1 = SetHeight(nodes, nodes[parent.left]) + 1; } if (parent.right != -1) { h2 = SetHeight(nodes, nodes[parent.right]) + 1; } int h = Math.Max(h1, h2); parent.height = h; return h; } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
666
2
using System; using System.Linq; class A7 { struct Tree { public int id; public int parent; public int depth; public int height; public int left, right; public int sibling; public int degree; public string type; } public static void Main() { int n = int.Parse(Console.ReadLine()); var tree = new Tree[n]; for (int i = 0; i < n; i++) { tree[i].parent = -1; } for (int i = 0; i < n; i++) { var input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var id = input[0]; var l = input[1]; var r = input[2]; tree[id].id = id; tree[id].left = l; tree[id].right = r; tree[id].type = "leaf"; if (l != -1) { tree[id].type = "internal node"; tree[id].degree++; tree[l].parent = id; tree[l].sibling = r; } if (r != -1) { tree[id].type = "internal node"; tree[id].degree++; tree[r].parent = id; tree[r].sibling = l; } } for (int i = 0; i < n; i++) if (tree[i].parent == -1) { tree[i].type = "root"; tree[i].sibling = -1; Depth(tree, i, 0); break; } for (int j = 0; j < n; j++) { Console.Write("node "); Console.Write(tree[j].id); Console.Write(":parent = "); Console.Write(tree[j].parent); Console.Write(",sibling = "); Console.Write(tree[j].sibling); Console.Write(",degree = "); Console.Write(tree[j].degree); Console.Write(",depth = "); Console.Write(tree[j].depth); Console.Write(",height = "); Console.Write(tree[j].height); Console.Write(","); Console.WriteLine(tree[j].type); } } static int Depth(Tree[] tree, int id, int depth) { if (id == -1) return depth - 1; tree[id].depth = depth; var lh = Depth(tree, tree[id].left, depth + 1); var rh = Depth(tree, tree[id].right, depth + 1); var h = Math.Max(lh, rh); tree[id].height = h - depth; return h; } }
using System; using System.Linq; class A7 { struct Tree { public int id; public int parent; public int depth; public int height; public int left, right; public int sibling; public int degree; public string type; } public static void Main() { int n = int.Parse(Console.ReadLine()); var tree = new Tree[n]; for (int i = 0; i < n; i++) { tree[i].parent = -1; } for (int i = 0; i < n; i++) { var input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var id = input[0]; var l = input[1]; var r = input[2]; tree[id].id = id; tree[id].left = l; tree[id].right = r; tree[id].type = "leaf"; if (l != -1) { tree[id].type = "internal node"; tree[id].degree++; tree[l].parent = id; tree[l].sibling = r; } if (r != -1) { tree[id].type = "internal node"; tree[id].degree++; tree[r].parent = id; tree[r].sibling = l; } } for (int i = 0; i < n; i++) if (tree[i].parent == -1) { tree[i].type = "root"; tree[i].sibling = -1; Depth(tree, i, 0); break; } for (int j = 0; j < n; j++) { Console.Write("node "); Console.Write(tree[j].id); Console.Write(": parent = "); Console.Write(tree[j].parent); Console.Write(", sibling = "); Console.Write(tree[j].sibling); Console.Write(", degree = "); Console.Write(tree[j].degree); Console.Write(", depth = "); Console.Write(tree[j].depth); Console.Write(", height = "); Console.Write(tree[j].height); Console.Write(", "); Console.WriteLine(tree[j].type); } } static int Depth(Tree[] tree, int id, int depth) { if (id == -1) return depth - 1; tree[id].depth = depth; var lh = Depth(tree, tree[id].left, depth + 1); var rh = Depth(tree, tree[id].right, depth + 1); var h = Math.Max(lh, rh); tree[id].height = h - depth; return h; } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
627
12
using System; using System.Text; namespace ALDS1_7_B { public struct node { public int parent; public int left; public int right; public node(int parent, int left, int right) { this.parent = parent; this.left = left; this.right = right; } } class Program { public static int nil = -1; static int[] S = new int[25]; static int[] de = new int[25]; static int getdepth(int u, node[] T) { int d = 0; while (T[u].parent != nil) { u = T[u].parent; d++; } return d; } static int[] D = new int[25]; static void setdepth(int u, int p, node[] T) { D[u] = p; if (T[u].right != nil) { setdepth(T[u].right, p + 1, T); } if (T[u].left != nil) { setdepth(T[u].left, p + 1, T); } } static int[] H = new int[25]; static int setheight(int u, node[] T) { int h1 = 0; int h2 = 0; if (T[u].left != nil) { h1 = setheight(T[u].left, T) + 1; } if (T[u].right != nil) { h2 = setheight(T[u].right, T) + 1; } H[u] = Math.Max(h1, h2); return H[u]; } static StringBuilder sbchildren(int u, node[] T) { var sb = new StringBuilder(); sb.Append(" ["); int c = T[u].left; while (c != nil) { sb.Append(c + ", "); c = T[c].right; } sb.Remove(sb.Length - 2, 2); return sb; } static int rootchecker(node[] T) { for (int i = 0; i < T.Length; i++) { if (T[i].parent == nil) { return i; } } return 0; } static void tstart(node[] T, int n) { for (int i = 0; i < n; i++) { T[i].parent = nil; T[i].left = nil; T[i].right = nil; S[i] = nil; } for (int i = 0; i < n; i++) { var input = getsplit(); int[] a = Array.ConvertAll(input, int.Parse); int count = 0; if (a[1] != -1) { T[a[0]].left = a[1]; T[a[1]].parent = a[0]; count++; if (a[2] != -1) { S[a[1]] = a[2]; } } if (a[2] != -1) { T[a[0]].right = a[2]; T[a[2]].parent = a[0]; count++; if (a[1] != -1) { S[a[2]] = a[1]; } } de[a[0]] = count; } } static void Main(string[] args) { int n = getint(); node[] T = new node[n]; tstart(T, n); int r = rootchecker(T); setdepth(r, 0, T); setheight(r, T); for (int i = 0; i < n; i++) { var sb = new StringBuilder(); sb.Append("node " + i + ": "); sb.Append("parent = " + T[i].parent + ", "); sb.Append("sibling = " + S[i] + ", "); sb.Append("degrgee = " + de[i] + ", "); sb.Append("depth = " + D[i] + ", "); sb.Append("height = " + H[i] + ", "); if (D[i] == 0) { sb.Append("root"); } else { if (de[i] == 0) { sb.Append("leaf"); } else { sb.Append("internal node"); } } Console.WriteLine(sb); } } public static string[] getsplit() { string[] x = Console.ReadLine().Split(' '); return x; } public static int getint() { int x = int.Parse(Console.ReadLine()); return x; } } }
using System; using System.Text; namespace ALDS1_7_B { public struct node { public int parent; public int left; public int right; public node(int parent, int left, int right) { this.parent = parent; this.left = left; this.right = right; } } class Program { public static int nil = -1; static int[] S = new int[25]; static int[] de = new int[25]; static int getdepth(int u, node[] T) { int d = 0; while (T[u].parent != nil) { u = T[u].parent; d++; } return d; } static int[] D = new int[25]; static void setdepth(int u, int p, node[] T) { D[u] = p; if (T[u].right != nil) { setdepth(T[u].right, p + 1, T); } if (T[u].left != nil) { setdepth(T[u].left, p + 1, T); } } static int[] H = new int[25]; static int setheight(int u, node[] T) { int h1 = 0; int h2 = 0; if (T[u].left != nil) { h1 = setheight(T[u].left, T) + 1; } if (T[u].right != nil) { h2 = setheight(T[u].right, T) + 1; } H[u] = Math.Max(h1, h2); return H[u]; } static StringBuilder sbchildren(int u, node[] T) { var sb = new StringBuilder(); sb.Append(" ["); int c = T[u].left; while (c != nil) { sb.Append(c + ", "); c = T[c].right; } sb.Remove(sb.Length - 2, 2); return sb; } static int rootchecker(node[] T) { for (int i = 0; i < T.Length; i++) { if (T[i].parent == nil) { return i; } } return 0; } static void tstart(node[] T, int n) { for (int i = 0; i < n; i++) { T[i].parent = nil; T[i].left = nil; T[i].right = nil; S[i] = nil; } for (int i = 0; i < n; i++) { var input = getsplit(); int[] a = Array.ConvertAll(input, int.Parse); int count = 0; if (a[1] != -1) { T[a[0]].left = a[1]; T[a[1]].parent = a[0]; count++; if (a[2] != -1) { S[a[1]] = a[2]; } } if (a[2] != -1) { T[a[0]].right = a[2]; T[a[2]].parent = a[0]; count++; if (a[1] != -1) { S[a[2]] = a[1]; } } de[a[0]] = count; } } static void Main(string[] args) { int n = getint(); node[] T = new node[n]; tstart(T, n); int r = rootchecker(T); setdepth(r, 0, T); setheight(r, T); for (int i = 0; i < n; i++) { var sb = new StringBuilder(); sb.Append("node " + i + ": "); sb.Append("parent = " + T[i].parent + ", "); sb.Append("sibling = " + S[i] + ", "); sb.Append("degree = " + de[i] + ", "); sb.Append("depth = " + D[i] + ", "); sb.Append("height = " + H[i] + ", "); if (D[i] == 0) { sb.Append("root"); } else { if (de[i] == 0) { sb.Append("leaf"); } else { sb.Append("internal node"); } } Console.WriteLine(sb); } } public static string[] getsplit() { string[] x = Console.ReadLine().Split(' '); return x; } public static int getint() { int x = int.Parse(Console.ReadLine()); return x; } } }
[["-", 0, 28, 0, 16, 31, 16, 31, 5, 0, 222], ["+", 0, 28, 0, 16, 31, 16, 31, 5, 0, 222]]
8
1,052
2
using System; using System.Collections.Generic; using System.Linq; namespace ALDS1_7B { public class Program { public static void Main(string[] args) { var sr = new StreamReader(); //--------------------------------- var N = sr.Next<int>(); var tree = new Tree(N); for (var i = 0; i < N; i++) { var id = sr.Next<int>(); var children = sr.Next<int>(2).Where(x => x != -1).ToArray(); tree.Add(id, children); } foreach (var node in tree.Nodes) { Console.Write("node {0}: ", node.Id); Console.Write("parent = {0}, ", node.ParentId); Console.Write("sibling = {0}, ", node.SiblingId.DefaultIfEmpty(-1).First()); Console.Write("degree = {0}, ", node.Children.Count); Console.Write("depth = {0},", node.Depth); Console.Write("height = {0}, ", node.Height); Console.Write("{0}", node.Type); Console.WriteLine(); } //--------------------------------- } } public class Tree { public readonly Node[] Nodes; public Tree(int cap) { Nodes = new Node[cap]; for (var i = 0; i < cap; i++) { Nodes[i] = new Node(i); } } public void Add(int id, int[] children) { foreach (var child in children) { Nodes[id].Children.Add(Nodes[child]); Nodes[child].Parent = Nodes[id]; } } } public class Node { private static int NIL = -1; public int Id; public Node Parent; public List<Node> Children = new List<Node>(); public int Depth { get { if (Parent == null) return 0; if (_depth != NIL) return _depth; return _depth = Parent.Depth + 1; } } public int Height { get { if (Children.Count == 0) return 0; if (_height != NIL) return _height; return _height = Children.Max(n => n.Height) + 1; } } public string Type { get { if (Parent == null) return "root"; if (Children.Count == 0) return "leaf"; return "internal node"; } } public int ParentId { get { if (Parent == null) return -1; return Parent.Id; } } public int[] ChildrenId { get { return Children.Select(n => n.Id).ToArray(); } } public int[] SiblingId { get { if (Parent == null) return new int[] {}; return Parent.ChildrenId.Where(x => x != Id).ToArray(); } } private int _depth = NIL; private int _height = NIL; public Node(int id) { Id = id; } } public class StreamReader { private readonly char[] _c = { ' ' }; private int _index = -1; private string[] _input = new string[0]; public T Next<T>() { if (_index == _input.Length - 1) { _index = -1; while (true) { string rl = Console.ReadLine(); if (rl == null) { if (typeof(T).IsClass) return default(T); return (T) typeof(T).GetField("MinValue").GetValue(null); } if (rl != "") { _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries); break; } } } return (T)Convert.ChangeType( _input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture); } public T[] Next<T>(int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next<T>(); return ret; } public T[][] Next<T>(int y, int x) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next<T>(x); return ret; } } }
using System; using System.Collections.Generic; using System.Linq; namespace ALDS1_7B { public class Program { public static void Main(string[] args) { var sr = new StreamReader(); //--------------------------------- var N = sr.Next<int>(); var tree = new Tree(N); for (var i = 0; i < N; i++) { var id = sr.Next<int>(); var children = sr.Next<int>(2).Where(x => x != -1).ToArray(); tree.Add(id, children); } foreach (var node in tree.Nodes) { Console.Write("node {0}: ", node.Id); Console.Write("parent = {0}, ", node.ParentId); Console.Write("sibling = {0}, ", node.SiblingId.DefaultIfEmpty(-1).First()); Console.Write("degree = {0}, ", node.Children.Count); Console.Write("depth = {0}, ", node.Depth); Console.Write("height = {0}, ", node.Height); Console.Write("{0}", node.Type); Console.WriteLine(); } //--------------------------------- } } public class Tree { public readonly Node[] Nodes; public Tree(int cap) { Nodes = new Node[cap]; for (var i = 0; i < cap; i++) { Nodes[i] = new Node(i); } } public void Add(int id, int[] children) { foreach (var child in children) { Nodes[id].Children.Add(Nodes[child]); Nodes[child].Parent = Nodes[id]; } } } public class Node { private static int NIL = -1; public int Id; public Node Parent; public List<Node> Children = new List<Node>(); public int Depth { get { if (Parent == null) return 0; if (_depth != NIL) return _depth; return _depth = Parent.Depth + 1; } } public int Height { get { if (Children.Count == 0) return 0; if (_height != NIL) return _height; return _height = Children.Max(n => n.Height) + 1; } } public string Type { get { if (Parent == null) return "root"; if (Children.Count == 0) return "leaf"; return "internal node"; } } public int ParentId { get { if (Parent == null) return -1; return Parent.Id; } } public int[] ChildrenId { get { return Children.Select(n => n.Id).ToArray(); } } public int[] SiblingId { get { if (Parent == null) return new int[] {}; return Parent.ChildrenId.Where(x => x != Id).ToArray(); } } private int _depth = NIL; private int _height = NIL; public Node(int id) { Id = id; } } public class StreamReader { private readonly char[] _c = { ' ' }; private int _index = -1; private string[] _input = new string[0]; public T Next<T>() { if (_index == _input.Length - 1) { _index = -1; while (true) { string rl = Console.ReadLine(); if (rl == null) { if (typeof(T).IsClass) return default(T); return (T) typeof(T).GetField("MinValue").GetValue(null); } if (rl != "") { _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries); break; } } } return (T)Convert.ChangeType( _input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture); } public T[] Next<T>(int x) { var ret = new T[x]; for (var i = 0; i < x; ++i) ret[i] = Next<T>(); return ret; } public T[][] Next<T>(int y, int x) { var ret = new T[y][]; for (var i = 0; i < y; ++i) ret[i] = Next<T>(x); return ret; } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
910
2
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); BinaryTree btree = new BinaryTree(); for (int i = 0; i < n; i++) { int[] node = Console.ReadLine().Split(' ').Select(val => int.Parse(val)).ToArray(); btree.Insert(node[0], node[1], node[2]); } btree.DisplayAll(); } } class BinaryTree { #region class Node define internal class Node { #region Node's fields and properties internal const int MaxValue = 25; public int Value { get; internal set; } Node parent, left, right; internal int _depth = 0, _height = 0; internal int Depth { get { if (_depth != 0) return _depth; else if (parent == null) return _depth = 0; else return this._depth = this.parent.Depth + 1; } } internal int Height { get { if (_height != 0) return _height; else { if (this.left != null) this._height = this.left.Height + 1; if (this.right != null) return this._height = Math.Max(_height, this.right.Height + 1); } return this._height = 0; } } string Type { get { if (parent == null) return "root"; else if (left == null && right == null) return "leaf"; else return "internal node"; } } #endregion #region Node's constructor public Node(int val) : this(val, null, "null") {} public Node(int val, Node parent, string direction) { this.Value = val; this.parent = parent; switch (direction) { case "right": this.parent.right = this; break; case "left": this.parent.left = this; break; } } public Node(int val, Node left, Node right) { this.Value = val; this.left = left; this.right = right; this.left = this.right = this; } #endregion #region Node's methods internal static void ConnectNodes(Node parent, Node child, string direction) { child.parent = parent; switch (direction) { case "right": parent.right = child; break; case "left": parent.left = child; break; } } internal void Display() { int parentVal = this.parent == null ? -1 : this.parent.Value; int siblingVal = -1; if (this.parent != null && this.parent.left != null && this.parent.right != null) { siblingVal = this.parent.left == this ? this.parent.right.Value : this.parent.left.Value; } int degree = 0; if (this.left != null) degree++; if (this.right != null) degree++; Console.WriteLine(string.Format( "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}", this.Value, parentVal, siblingVal, degree, this.Depth, this.Height, this.Type)); } #endregion } #endregion // class Node define public int Count { get; internal set; } private Node[] nodes = new Node[Node.MaxValue]; internal Node this[int i] { get { return nodes[i]; } private set { nodes[i] = value; } } public void Insert(int thisval, int leftval, int rightval) { this.Count++; if (this[thisval] == null) this[thisval] = new Node(thisval); if (leftval == -1) { } else if (this[leftval] == null) this[leftval] = new Node(leftval, this[thisval], "left"); else Node.ConnectNodes(this[thisval], this[leftval], "left"); if (rightval == -1) { } else if (this[rightval] == null) this[rightval] = new Node(rightval, this[thisval], "right"); else Node.ConnectNodes(this[thisval], this[rightval], "right"); } public void DisplayAll() { for (int i = 0; i < this.Count; i++) { this[i].Display(); } } }
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); BinaryTree btree = new BinaryTree(); for (int i = 0; i < n; i++) { int[] node = Console.ReadLine().Split(' ').Select(val => int.Parse(val)).ToArray(); btree.Insert(node[0], node[1], node[2]); } btree.DisplayAll(); } } class BinaryTree { #region class Node define internal class Node { #region Node's fields and properties internal const int MaxValue = 25; public int Value { get; internal set; } Node parent, left, right; internal int _depth = 0, _height = 0; internal int Depth { get { if (_depth != 0) return _depth; else if (parent == null) return _depth = 0; else return this._depth = this.parent.Depth + 1; } } internal int Height { get { if (_height != 0) return _height; else { if (this.left != null) this._height = this.left.Height + 1; if (this.right != null) this._height = Math.Max(_height, this.right.Height + 1); return Math.Max(this._height, 0); } } } string Type { get { if (parent == null) return "root"; else if (left == null && right == null) return "leaf"; else return "internal node"; } } #endregion #region Node's constructor public Node(int val) : this(val, null, "null") {} public Node(int val, Node parent, string direction) { this.Value = val; this.parent = parent; switch (direction) { case "right": this.parent.right = this; break; case "left": this.parent.left = this; break; } } public Node(int val, Node left, Node right) { this.Value = val; this.left = left; this.right = right; this.left = this.right = this; } #endregion #region Node's methods internal static void ConnectNodes(Node parent, Node child, string direction) { child.parent = parent; switch (direction) { case "right": parent.right = child; break; case "left": parent.left = child; break; } } internal void Display() { int parentVal = this.parent == null ? -1 : this.parent.Value; int siblingVal = -1; if (this.parent != null && this.parent.left != null && this.parent.right != null) { siblingVal = this.parent.left == this ? this.parent.right.Value : this.parent.left.Value; } int degree = 0; if (this.left != null) degree++; if (this.right != null) degree++; Console.WriteLine(string.Format( "node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}", this.Value, parentVal, siblingVal, degree, this.Depth, this.Height, this.Type)); } #endregion } #endregion // class Node define public int Count { get; internal set; } private Node[] nodes = new Node[Node.MaxValue]; internal Node this[int i] { get { return nodes[i]; } private set { nodes[i] = value; } } public void Insert(int thisval, int leftval, int rightval) { this.Count++; if (this[thisval] == null) this[thisval] = new Node(thisval); if (leftval == -1) { } else if (this[leftval] == null) this[leftval] = new Node(leftval, this[thisval], "left"); else Node.ConnectNodes(this[thisval], this[leftval], "left"); if (rightval == -1) { } else if (this[rightval] == null) this[rightval] = new Node(rightval, this[thisval], "right"); else Node.ConnectNodes(this[thisval], this[rightval], "right"); } public void DisplayAll() { for (int i = 0; i < this.Count; i++) { this[i].Display(); } } }
[["-", 0, 57, 75, 196, 0, 57, 64, 37, 0, 38], ["-", 0, 280, 8, 196, 0, 57, 75, 196, 0, 46], ["+", 75, 196, 0, 37, 0, 213, 63, 214, 205, 22], ["+", 75, 196, 0, 37, 0, 213, 63, 214, 0, 131], ["+", 75, 196, 0, 37, 0, 213, 63, 214, 141, 22], ["+", 75, 196, 0, 37, 0, 213, 3, 4, 0, 24], ["-", 8, 196, 0, 37, 0, 11, 0, 202, 0, 32], ["+", 75, 196, 0, 37, 0, 213, 3, 4, 0, 21], ["+", 75, 196, 0, 37, 0, 213, 3, 4, 0, 25], ["+", 0, 280, 8, 196, 0, 57, 75, 196, 0, 46]]
8
929
10
using System; using System.Collections.Generic; namespace alds17c { class node { public int id; public node parent; public node left; public node right; public node(int setid) { id = setid; } public void setleft(node tgt) { left = tgt; tgt.parent = this; } public void setright(node tgt) { right = tgt; tgt.parent = this; } public void writepre() { Console.Write(" "); Console.Write(id); if (left != null) left.writepre(); if (right != null) right.writepre(); } public void writein() { if (left != null) left.writein(); Console.Write(" "); Console.Write(id); if (right != null) right.writein(); } public void writepost() { if (left != null) left.writepost(); if (right != null) right.writepost(); Console.Write(" "); Console.Write(id); } } class main { static void Main() { List<node> all = new List<node>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { all.Add(new node(i)); } for (int i = 0; i < n; i++) { string[] input = Console.ReadLine().Split(' '); int tgt = int.Parse(input[0]); int left = int.Parse(input[1]); int right = int.Parse(input[2]); if (left != -1) all[tgt].setleft(all[left]); if (right != -1) all[tgt].setright(all[right]); } node root = all[0]; foreach (node tgt in all) { if (tgt.parent == null) { root = tgt; break; } } Console.WriteLine("Preoder"); root.writepre(); Console.WriteLine(); Console.WriteLine("Inoder"); root.writein(); Console.WriteLine(); Console.WriteLine("Postoder"); root.writepost(); Console.WriteLine(); } } }
using System; using System.Collections.Generic; namespace alds17c { class node { public int id; public node parent; public node left; public node right; public node(int setid) { id = setid; } public void setleft(node tgt) { left = tgt; tgt.parent = this; } public void setright(node tgt) { right = tgt; tgt.parent = this; } public void writepre() { Console.Write(" "); Console.Write(id); if (left != null) left.writepre(); if (right != null) right.writepre(); } public void writein() { if (left != null) left.writein(); Console.Write(" "); Console.Write(id); if (right != null) right.writein(); } public void writepost() { if (left != null) left.writepost(); if (right != null) right.writepost(); Console.Write(" "); Console.Write(id); } } class main { static void Main() { List<node> all = new List<node>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { all.Add(new node(i)); } for (int i = 0; i < n; i++) { string[] input = Console.ReadLine().Split(' '); int tgt = int.Parse(input[0]); int left = int.Parse(input[1]); int right = int.Parse(input[2]); if (left != -1) all[tgt].setleft(all[left]); if (right != -1) all[tgt].setright(all[right]); } node root = all[0]; foreach (node tgt in all) { if (tgt.parent == null) { root = tgt; break; } } Console.WriteLine("Preorder"); root.writepre(); Console.WriteLine(); Console.WriteLine("Inorder"); root.writein(); Console.WriteLine(); Console.WriteLine("Postorder"); root.writepost(); Console.WriteLine(); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
500
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170410_AOJ_ALDS1_7D { class ALDS1_7D { static void Main(string[] args) { for (;;) { string line = Console.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } int n = int.Parse(line); TreeNode[] arr = new TreeNode[n + 1]; TreeNode parent = null; int[] ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); for (int i = 0; i < n; i++) { TreeNode tn = new TreeNode(); tn.Id = ns[i]; arr[tn.Id] = tn; if (parent != null) { parent.Left = tn; tn.Parent = parent; } parent = tn; } ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); TreeNode restRoot = arr[1].GetRoot(); for (int i = 0; i < n; i++) { if (restRoot == null) { break; } TreeNode tn = arr[ns[i]]; if (restRoot == tn) { restRoot = tn.Left; tn.Left = null; tn.Right = restRoot; } else if (restRoot.Contains(tn)) { restRoot = tn.Left; if (restRoot != null) { tn.Left = null; restRoot.Parent = null; } } else { if (restRoot.Parent != null) { restRoot.Parent.Right = null; } restRoot.Parent = tn; tn.Right = restRoot; } } TreeNode root = arr[1].GetRoot(); WriteOrder(root); } } static void WriteOrder(TreeNode tn) { List<TreeNode> list = new List<TreeNode>(); PostOrder(list, tn); Console.WriteLine( string.Join(" ", list.Select(x => x.Id.ToString()).ToArray())); } static void PostOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { PostOrder(list, tn.Left); } if (tn.Right != null) { PostOrder(list, tn.Right); } list.Add(tn); } } class TreeNode { public int Id { get; set; } public TreeNode Parent { get; set; } public TreeNode Left { get; set; } public TreeNode Right { get; set; } public int Depth { get; internal set; } public int Height { get; internal set; } public TreeNode() {} public TreeNode GetRoot() { TreeNode temp = this; while (temp.Parent != null) { temp = temp.Parent; } return temp; } public bool Contains(TreeNode tn) { bool rv = false; rv = (this == tn || (Left != null && Left.Contains(tn)) || (Right != null && Right.Contains(tn))); return rv; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170410_AOJ_ALDS1_7D { class ALDS1_7D { static void Main(string[] args) { for (;;) { string line = Console.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } int n = int.Parse(line); TreeNode[] arr = new TreeNode[n + 1]; TreeNode parent = null; int[] ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); for (int i = 0; i < n; i++) { TreeNode tn = new TreeNode(); tn.Id = ns[i]; arr[tn.Id] = tn; if (parent != null) { parent.Left = tn; tn.Parent = parent; } parent = tn; } ns = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); TreeNode restRoot = arr[1].GetRoot(); for (int i = 0; i < n; i++) { if (restRoot == null) { break; } TreeNode tn = arr[ns[i]]; if (restRoot == tn) { restRoot = tn.Left; tn.Left = null; tn.Right = restRoot; } else if (restRoot.Contains(tn)) { restRoot = tn.Left; if (restRoot != null) { tn.Left = null; tn.Right = restRoot; } } else { if (restRoot.Parent != null) { restRoot.Parent.Right = null; } restRoot.Parent = tn; tn.Right = restRoot; } } TreeNode root = arr[1].GetRoot(); WriteOrder(root); } } static void WriteOrder(TreeNode tn) { List<TreeNode> list = new List<TreeNode>(); PostOrder(list, tn); Console.WriteLine( string.Join(" ", list.Select(x => x.Id.ToString()).ToArray())); } static void PostOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { PostOrder(list, tn.Left); } if (tn.Right != null) { PostOrder(list, tn.Right); } list.Add(tn); } } class TreeNode { public int Id { get; set; } public TreeNode Parent { get; set; } public TreeNode Left { get; set; } public TreeNode Right { get; set; } public int Depth { get; internal set; } public int Height { get; internal set; } public TreeNode() {} public TreeNode GetRoot() { TreeNode temp = this; while (temp.Parent != null) { temp = temp.Parent; } return temp; } public bool Contains(TreeNode tn) { bool rv = false; rv = (this == tn || (Left != null && Left.Contains(tn)) || (Right != null && Right.Contains(tn))); return rv; } } }
[["-", 64, 196, 0, 1, 0, 11, 31, 214, 205, 22], ["+", 64, 196, 0, 1, 0, 11, 31, 214, 205, 22], ["-", 64, 196, 0, 1, 0, 11, 31, 214, 141, 22], ["+", 64, 196, 0, 1, 0, 11, 31, 214, 141, 22], ["-", 0, 57, 64, 196, 0, 1, 0, 11, 12, 215], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 22]]
8
661
6
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Aoj.ALDS.Chapter8C { class Node { public int Key { get; set; } public Node Parent { get; set; } public Node Left { get; set; } public Node Right { get; set; } public Node(int key) { Key = key; Parent = null; Left = null; Right = null; } public bool IsRoot() { return Parent == null; } public bool IsLeaf() { return !IsRoot() && (Left == null && Right == null); } public int Degree() { int degree = 0; if (Left != null) degree++; if (Right != null) degree++; return degree; } public Node Sibling() { if (Parent == null) return null; if (Parent.Right != null && this.Key < Parent.Right.Key) // ????????????????????? return Parent.Right; if (Parent.Left != null && Parent.Left.Key < this.Key) // ????????????????????? return Parent.Left; return null; } // public override string ToString() // { // return string.Format( // "key {0}: parent = {1}, sibling = {2}, left = {3}, right = {4} // degree = {5}, nodeType = {6}" , Key , // OtherNodeKeyOrDefault(Parent) , OtherNodeKeyOrDefault(Sibling()) // , OtherNodeKeyOrDefault(Left) // , OtherNodeKeyOrDefault(Right) // , Degree() // , NodeType() // ); // } protected int OtherNodeKeyOrDefault(Node node, int defaultKey = -1) { return (node != null) ? node.Key : defaultKey; } protected string NodeType() { if (IsRoot()) return "root"; if (IsLeaf()) return "leaf"; return "internal node"; } } class BinarySearchTree { public Node Root { get; set; } public BinarySearchTree() { Root = null; } public void Insert(Node node) { Node parent = null; Node current = Root; while (current != null) { parent = current; if (node.Key < current.Key) current = current.Left; else current = current.Right; } if (parent == null) { Root = node; } else if (node.Key < parent.Key) { parent.Left = node; node.Parent = parent; } else { parent.Right = node; node.Parent = parent; } } public Node Find(Node node) { Node current = Root; while (current != null) { if (node.Key == current.Key) break; if (node.Key < current.Key) current = current.Left; else current = current.Right; } return current; } /// <summary> /// ????????¢?´¢??¨?????????????????\????????¢?????´????????????????????? /// 1.???????????????: ????±?????????????????????? /// 2.????????????: /// ????±??????????????????????????????¨???????????????????????? /// 3.????????????: /// ????±??????????????¬??????????????????????????±??????????????????????????¬?????????????????????£??\????????? /// ????¬???????: /// ??¨??????????????????????????´?????????????±??????????????¬???????????????? /// </summary> public void Delete(Node node) { Node deleted = null; if (node.Left == null || node.Right == null) // ??????????????\????????´?????????node???????????????????±???? deleted = node; else // ?????????????????´??????????¬????????????????????±???? deleted = GetSuccessor(node); Node child = null; if (deleted.Left != null) child = deleted.Left; else child = deleted.Right; if (child != null) // (?????????????????´????????????????????????) child.Parent = deleted.Parent; if (deleted.Parent == null) Root = child; else if (deleted == deleted.Parent.Left) deleted.Parent.Left = child; else deleted.Parent.Right = child; if (deleted != node) // (?????????????????´???) // ???????????????????§????????????? node.Key = deleted.Key; } /// <summary> /// ????????????????????¨???????¬???????????±?????????? /// </summary> /// <returns></returns> protected Node GetSuccessor(Node node) { if (node.Right != null) // ????????????????????´???????????¨?????¨???????°????????¬?????????? return GetMinimum(node); // ??????????????????????????´????????????????????????????????????????????????????±?????????? Node parent = node.Parent; while (parent != null && node == parent.Right) { node = parent; parent = parent.Parent; } return parent; } protected Node GetMinimum(Node node) { if (node.Left == null) return node; return GetMinimum(node.Left); } public void ApplyPreorder(Node node, Action<Node> func) { if (node == null) return; func(node); ApplyPreorder(node.Left, func); ApplyPreorder(node.Right, func); } public void ApplyInorder(Node node, Action<Node> func) { if (node == null) return; ApplyInorder(node.Left, func); func(node); ApplyInorder(node.Right, func); } public void ApplyPostorder(Node node, Action<Node> func) { if (node == null) return; ApplyPostorder(node.Left, func); ApplyPostorder(node.Right, func); func(node); } } class Program { public static void Solve() { var tree = new BinarySearchTree(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] inputs = Console.ReadLine().Split(' '); int key = 0; Node foundNode = null; switch (inputs[0]) { case "insert": key = int.Parse(inputs[1]); tree.Insert(new Node(key)); break; case "find": key = int.Parse(inputs[1]); foundNode = tree.Find(new Node(key)); Console.WriteLine(foundNode != null ? "yes" : "no"); break; case "delete": key = int.Parse(inputs[1]); foundNode = tree.Find(new Node(key)); tree.Delete(foundNode); break; case "print": tree.ApplyInorder( tree.Root, (Node node) => { Console.Write(" {0}", node.Key); }); Console.WriteLine(""); tree.ApplyPreorder( tree.Root, (Node node) => { Console.Write(" {0}", node.Key); }); Console.WriteLine(""); break; default: throw new System.IO.IOException(""); } } } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/08_BinarySearchTree_C_01.txt")); // var sw = new Stopwatch(); // sw.Start(); Solve(); // Console.WriteLine("{0}ms",sw.ElapsedMilliseconds); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Aoj.ALDS.Chapter8C { class Node { public int Key { get; set; } public Node Parent { get; set; } public Node Left { get; set; } public Node Right { get; set; } public Node(int key) { Key = key; Parent = null; Left = null; Right = null; } public bool IsRoot() { return Parent == null; } public bool IsLeaf() { return !IsRoot() && (Left == null && Right == null); } public int Degree() { int degree = 0; if (Left != null) degree++; if (Right != null) degree++; return degree; } public Node Sibling() { if (Parent == null) return null; if (Parent.Right != null && this.Key < Parent.Right.Key) // ????????????????????? return Parent.Right; if (Parent.Left != null && Parent.Left.Key < this.Key) // ????????????????????? return Parent.Left; return null; } // public override string ToString() // { // return string.Format( // "key {0}: parent = {1}, sibling = {2}, left = {3}, right = {4} // degree = {5}, nodeType = {6}" , Key , // OtherNodeKeyOrDefault(Parent) , OtherNodeKeyOrDefault(Sibling()) // , OtherNodeKeyOrDefault(Left) // , OtherNodeKeyOrDefault(Right) // , Degree() // , NodeType() // ); // } protected int OtherNodeKeyOrDefault(Node node, int defaultKey = -1) { return (node != null) ? node.Key : defaultKey; } protected string NodeType() { if (IsRoot()) return "root"; if (IsLeaf()) return "leaf"; return "internal node"; } } class BinarySearchTree { public Node Root { get; set; } public BinarySearchTree() { Root = null; } public void Insert(Node node) { Node parent = null; Node current = Root; while (current != null) { parent = current; if (node.Key < current.Key) current = current.Left; else current = current.Right; } if (parent == null) { Root = node; } else if (node.Key < parent.Key) { parent.Left = node; node.Parent = parent; } else { parent.Right = node; node.Parent = parent; } } public Node Find(Node node) { Node current = Root; while (current != null) { if (node.Key == current.Key) break; if (node.Key < current.Key) current = current.Left; else current = current.Right; } return current; } /// <summary> /// ????????¢?´¢??¨?????????????????\????????¢?????´????????????????????? /// 1.???????????????: ????±?????????????????????? /// 2.????????????: /// ????±??????????????????????????????¨???????????????????????? /// 3.????????????: /// ????±??????????????¬??????????????????????????±??????????????????????????¬?????????????????????£??\????????? /// ????¬???????: /// ??¨??????????????????????????´?????????????±??????????????¬???????????????? /// </summary> public void Delete(Node node) { Node deleted = null; if (node.Left == null || node.Right == null) // ??????????????\????????´?????????node???????????????????±???? deleted = node; else // ?????????????????´??????????¬????????????????????±???? deleted = GetSuccessor(node); Node child = null; if (deleted.Left != null) child = deleted.Left; else child = deleted.Right; if (child != null) // (?????????????????´????????????????????????) child.Parent = deleted.Parent; if (deleted.Parent == null) Root = child; else if (deleted == deleted.Parent.Left) deleted.Parent.Left = child; else deleted.Parent.Right = child; if (deleted != node) // (?????????????????´???) // ???????????????????§????????????? node.Key = deleted.Key; } /// <summary> /// ????????????????????¨???????¬???????????±?????????? /// </summary> /// <returns></returns> protected Node GetSuccessor(Node node) { if (node.Right != null) // ????????????????????´???????????¨?????¨???????°????????¬?????????? return GetMinimum(node.Right); // ??????????????????????????´????????????????????????????????????????????????????±?????????? Node parent = node.Parent; while (parent != null && node == parent.Right) { node = parent; parent = parent.Parent; } return parent; } protected Node GetMinimum(Node node) { if (node.Left == null) return node; return GetMinimum(node.Left); } public void ApplyPreorder(Node node, Action<Node> func) { if (node == null) return; func(node); ApplyPreorder(node.Left, func); ApplyPreorder(node.Right, func); } public void ApplyInorder(Node node, Action<Node> func) { if (node == null) return; ApplyInorder(node.Left, func); func(node); ApplyInorder(node.Right, func); } public void ApplyPostorder(Node node, Action<Node> func) { if (node == null) return; ApplyPostorder(node.Left, func); ApplyPostorder(node.Right, func); func(node); } } class Program { public static void Solve() { var tree = new BinarySearchTree(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] inputs = Console.ReadLine().Split(' '); int key = 0; Node foundNode = null; switch (inputs[0]) { case "insert": key = int.Parse(inputs[1]); tree.Insert(new Node(key)); break; case "find": key = int.Parse(inputs[1]); foundNode = tree.Find(new Node(key)); Console.WriteLine(foundNode != null ? "yes" : "no"); break; case "delete": key = int.Parse(inputs[1]); foundNode = tree.Find(new Node(key)); tree.Delete(foundNode); break; case "print": tree.ApplyInorder( tree.Root, (Node node) => { Console.Write(" {0}", node.Key); }); Console.WriteLine(""); tree.ApplyPreorder( tree.Root, (Node node) => { Console.Write(" {0}", node.Key); }); Console.WriteLine(""); break; default: throw new System.IO.IOException(""); } } } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/08_BinarySearchTree_C_02.txt")); // var sw = new Stopwatch(); // sw.Start(); Solve(); // Console.WriteLine("{0}ms",sw.ElapsedMilliseconds); } } }
[["+", 0, 213, 3, 4, 0, 28, 0, 214, 0, 131], ["+", 0, 213, 3, 4, 0, 28, 0, 214, 141, 22]]
8
1,192
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170410_AOJ_ALDS1_8A { class ALDS1_8A { static void Main(string[] args) { for (;;) { string line = Console.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } BinaryTree bt = new BinaryTree(); int n = int.Parse(line); for (int i = 0; i < n; i++) { string[] command = Console.ReadLine().Split(' '); int key; switch (command[0]) { case "delete": key = int.Parse(command[1]); bt.Delete(key); break; case "find": key = int.Parse(command[1]); TreeNode tn = bt.Find(key); if (tn != null) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } break; case "insert": key = int.Parse(command[1]); bt.Insert(key); break; case "print": Console.Write(" "); Console.WriteLine(string.Join( " ", bt.InOrder().Select(x => x.Key.ToString()).ToArray())); Console.Write(" "); Console.WriteLine(string.Join( " ", bt.PreOrder().Select(x => x.Key.ToString()).ToArray())); break; } } } } } class BinaryTree { List<TreeNode> list; public TreeNode Root { get { return GetRoot(); } } public BinaryTree() { list = new List<TreeNode>(); } private TreeNode GetRoot() { if (list.Count == 0) return null; TreeNode temp = list[0]; while (temp.Parent != null) { temp = temp.Parent; } return temp; } public void Delete(int key) { TreeNode tn = Find(key); Delete(tn); } void Delete(TreeNode tn) { if (tn == null) return; if (tn.Left == null && tn.Right == null) { if (tn.Parent.Left == tn) { tn.Parent.Left = null; } else if (tn.Parent.Right == tn) { tn.Parent.Right = null; } list.Remove(tn); } else if ((tn.Left == null) ^ (tn.Right != null)) { TreeNode child = (tn.Left != null) ? tn.Left : tn.Right; if (tn.Parent.Left == tn) { tn.Parent.Left = child; } else if (tn.Parent.Right == tn) { tn.Parent.Right = child; } child.Parent = tn.Parent; list.Remove(tn); } else { List<TreeNode> rightTree = new List<TreeNode>(); InOrder(rightTree, tn.Right); TreeNode nextNode = rightTree[0]; tn.Key = nextNode.Key; Delete(nextNode); } } public TreeNode Find(int key) { return Find(Root, key); } static TreeNode Find(TreeNode tn, int key) { TreeNode rv = null; if (tn != null) { if (key < tn.Key) { rv = Find(tn.Left, key); } else if (key > tn.Key) { rv = Find(tn.Right, key); } else { rv = tn; } } return rv; } public void Insert(int z) { TreeNode tn = new TreeNode(); tn.Key = z; Insert(tn); } public void Insert(TreeNode z) { TreeNode y = null; TreeNode x = Root; while (x != null) { y = x; x = (z.Key < x.Key) ? x.Left : x.Right; } z.Parent = y; list.Add(z); if (y == null) { } else if (z.Key < y.Key) { y.Left = z; z.Parent = y; } else { y.Right = z; z.Parent = y; } } public TreeNode[] PreOrder() { List<TreeNode> list = new List<TreeNode>(); PreOrder(list, Root); return list.ToArray(); } static void PreOrder(List<TreeNode> list, TreeNode tn) { list.Add(tn); if (tn.Left != null) { PreOrder(list, tn.Left); } if (tn.Right != null) { PreOrder(list, tn.Right); } } public TreeNode[] InOrder() { List<TreeNode> list = new List<TreeNode>(); InOrder(list, Root); return list.ToArray(); } static void InOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { InOrder(list, tn.Left); } list.Add(tn); if (tn.Right != null) { InOrder(list, tn.Right); } } static void PostOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { PostOrder(list, tn.Left); } if (tn.Right != null) { PostOrder(list, tn.Right); } list.Add(tn); } } class TreeNode { public int Key { get; set; } public TreeNode Parent { get; set; } public TreeNode Left { get; set; } public TreeNode Right { get; set; } public TreeNode() {} } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _20170410_AOJ_ALDS1_8A { class ALDS1_8A { static void Main(string[] args) { for (;;) { string line = Console.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } BinaryTree bt = new BinaryTree(); int n = int.Parse(line); for (int i = 0; i < n; i++) { string[] command = Console.ReadLine().Split(' '); int key; switch (command[0]) { case "delete": key = int.Parse(command[1]); bt.Delete(key); break; case "find": key = int.Parse(command[1]); TreeNode tn = bt.Find(key); if (tn != null) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } break; case "insert": key = int.Parse(command[1]); bt.Insert(key); break; case "print": Console.Write(" "); Console.WriteLine(string.Join( " ", bt.InOrder().Select(x => x.Key.ToString()).ToArray())); Console.Write(" "); Console.WriteLine(string.Join( " ", bt.PreOrder().Select(x => x.Key.ToString()).ToArray())); break; } } } } } class BinaryTree { List<TreeNode> list; public TreeNode Root { get { return GetRoot(); } } public BinaryTree() { list = new List<TreeNode>(); } private TreeNode GetRoot() { if (list.Count == 0) return null; TreeNode temp = list[0]; while (temp.Parent != null) { temp = temp.Parent; } return temp; } public void Delete(int key) { TreeNode tn = Find(key); Delete(tn); } void Delete(TreeNode tn) { if (tn == null) return; if (tn.Left == null && tn.Right == null) { if (tn.Parent.Left == tn) { tn.Parent.Left = null; } else if (tn.Parent.Right == tn) { tn.Parent.Right = null; } list.Remove(tn); } else if ((tn.Left == null) ^ (tn.Right == null)) { TreeNode child = (tn.Left != null) ? tn.Left : tn.Right; if (tn.Parent.Left == tn) { tn.Parent.Left = child; } else if (tn.Parent.Right == tn) { tn.Parent.Right = child; } child.Parent = tn.Parent; list.Remove(tn); } else { List<TreeNode> rightTree = new List<TreeNode>(); InOrder(rightTree, tn.Right); TreeNode nextNode = rightTree[0]; tn.Key = nextNode.Key; Delete(nextNode); } } public TreeNode Find(int key) { return Find(Root, key); } static TreeNode Find(TreeNode tn, int key) { TreeNode rv = null; if (tn != null) { if (key < tn.Key) { rv = Find(tn.Left, key); } else if (key > tn.Key) { rv = Find(tn.Right, key); } else { rv = tn; } } return rv; } public void Insert(int z) { TreeNode tn = new TreeNode(); tn.Key = z; Insert(tn); } public void Insert(TreeNode z) { TreeNode y = null; TreeNode x = Root; while (x != null) { y = x; x = (z.Key < x.Key) ? x.Left : x.Right; } z.Parent = y; list.Add(z); if (y == null) { } else if (z.Key < y.Key) { y.Left = z; z.Parent = y; } else { y.Right = z; z.Parent = y; } } public TreeNode[] PreOrder() { List<TreeNode> list = new List<TreeNode>(); PreOrder(list, Root); return list.ToArray(); } static void PreOrder(List<TreeNode> list, TreeNode tn) { list.Add(tn); if (tn.Left != null) { PreOrder(list, tn.Left); } if (tn.Right != null) { PreOrder(list, tn.Right); } } public TreeNode[] InOrder() { List<TreeNode> list = new List<TreeNode>(); InOrder(list, Root); return list.ToArray(); } static void InOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { InOrder(list, tn.Left); } list.Add(tn); if (tn.Right != null) { InOrder(list, tn.Right); } } static void PostOrder(List<TreeNode> list, TreeNode tn) { if (tn.Left != null) { PostOrder(list, tn.Left); } if (tn.Right != null) { PostOrder(list, tn.Right); } list.Add(tn); } } class TreeNode { public int Key { get; set; } public TreeNode Parent { get; set; } public TreeNode Left { get; set; } public TreeNode Right { get; set; } public TreeNode() {} } }
[["-", 75, 57, 15, 16, 12, 23, 0, 16, 17, 79], ["+", 75, 57, 15, 16, 12, 23, 0, 16, 17, 60]]
8
1,205
2
using System; using System.Text; namespace ALDS1_9_A { class Program { static int parent(int i) { return i / 2; } static int left(int i) { return 2 * i; } static int right(int i) { return 2 * i + 1; } static void Main(string[] args) { var sb = new StringBuilder(); var H = scan()[0]; var A = new int[1000]; var b = scan(); for (int i = 1; i <= H; i++) { A[i] = b[i - 1]; } for (int i = 1; i <= H; i++) { sb.Append("node " + i + "; key = " + A[i] + ", "); if (parent(i) >= 1) sb.Append("parent key = " + A[parent(i)] + ", "); if (left(i) <= H) sb.Append("left key = " + A[left(i)] + ", "); if (right(i) <= H) sb.Append("right key = " + A[right(i)] + ", "); sb.AppendLine(); } Console.Write(sb); } static int[] scan() { return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } } }
using System; using System.Text; namespace ALDS1_9_A { class Program { static int parent(int i) { return i / 2; } static int left(int i) { return 2 * i; } static int right(int i) { return 2 * i + 1; } static void Main(string[] args) { var sb = new StringBuilder(); var H = scan()[0]; var A = new int[1000]; var b = scan(); for (int i = 1; i <= H; i++) { A[i] = b[i - 1]; } for (int i = 1; i <= H; i++) { sb.Append("node " + i + ": key = " + A[i] + ", "); if (parent(i) >= 1) sb.Append("parent key = " + A[parent(i)] + ", "); if (left(i) <= H) sb.Append("left key = " + A[left(i)] + ", "); if (right(i) <= H) sb.Append("right key = " + A[right(i)] + ", "); sb.AppendLine(); } Console.Write(sb); } static int[] scan() { return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); } } }
[["-", 0, 16, 31, 16, 31, 16, 12, 5, 0, 222], ["+", 0, 16, 31, 16, 31, 16, 12, 5, 0, 222]]
8
308
2
using System; class Program { public static void Main() { var H = int.Parse(Console.ReadLine()); var nodes = new int[H]; var s = Console.ReadLine().Split(' '); for (var i = 0; i < H; i++) { nodes[i] = int.Parse(s[i]); } for (var i = 0; i < H; i++) { Console.Write("node " + (i + 1) + ": key = " + nodes[i] + ", "); if (i != 0) { Console.Write("parent key = " + nodes[(i - 1) / 2] + ","); } if (i * 2 + 1 < H) { Console.Write("left key = " + nodes[i * 2 + 1] + ","); } if ((i + 1) * 2 < H) { Console.Write("right key = " + nodes[(i + 1) * 2] + ","); } Console.WriteLine(); } } }
using System; class Program { public static void Main() { var H = int.Parse(Console.ReadLine()); var nodes = new int[H]; var s = Console.ReadLine().Split(' '); for (var i = 0; i < H; i++) { nodes[i] = int.Parse(s[i]); } for (var i = 0; i < H; i++) { Console.Write("node " + (i + 1) + ": key = " + nodes[i] + ", "); if (i != 0) { Console.Write("parent key = " + nodes[(i - 1) / 2] + ", "); } if (i * 2 + 1 < H) { Console.Write("left key = " + nodes[i * 2 + 1] + ", "); } if ((i + 1) * 2 < H) { Console.Write("right key = " + nodes[(i + 1) * 2] + ", "); } Console.WriteLine(); } } }
[["-", 3, 4, 0, 28, 0, 16, 12, 5, 0, 222], ["+", 3, 4, 0, 28, 0, 16, 12, 5, 0, 222]]
8
239
6
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int l = int.Parse(Console.ReadLine()); int[] heap = Console.ReadLine().Split(' ').Select(val => int.Parse(val)).ToArray(); for (int i = 1; i < l + 1; i++) { Console.Write("node {0}: key = {1}, ", i, heap[i - 1]); if ((i / 2) > 0) Console.Write("parent key = {0}, ", heap[i / 2 - 1]); if ((2 * i) < l) Console.Write("left key = {0}, ", heap[2 * i - 1]); if (((2 * i + 1) < l)) Console.Write("right key = {0}, ", heap[2 * i]); Console.WriteLine(); } } }
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; class Program { static void Main() { int l = int.Parse(Console.ReadLine()); int[] heap = Console.ReadLine().Split(' ').Select(val => int.Parse(val)).ToArray(); for (int i = 1; i < l + 1; i++) { Console.Write("node {0}: key = {1}, ", i, heap[i - 1]); if ((i / 2) > 0) Console.Write("parent key = {0}, ", heap[i / 2 - 1]); if ((2 * i) < l + 1) Console.Write("left key = {0}, ", heap[2 * i - 1]); if (((2 * i + 1) < l + 1)) Console.Write("right key = {0}, ", heap[2 * i]); Console.WriteLine(); } } }
[["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 72], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 12, 203], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 17, 72], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 203]]
8
217
4
using System; namespace A { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var array = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); for (int i = 1; i <= n; i++) { Console.Write("node {0}: key = {1},", i, array[i - 1]); if ((i >> 1) > 0) Console.Write("parent key = {0},", array[(i >> 1) - 1]); if ((i << 1) <= n) Console.Write("left key = {0},", array[(i << 1) - 1]); if (((i << 1) + 1) <= n) Console.Write("right key = {0},", array[i << 1]); Console.WriteLine(); } } } }
using System; namespace A { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var array = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); for (int i = 1; i <= n; i++) { Console.Write("node {0}: key = {1}, ", i, array[i - 1]); if ((i >> 1) > 0) Console.Write("parent key = {0}, ", array[(i >> 1) - 1]); if ((i << 1) <= n) Console.Write("left key = {0}, ", array[(i << 1) - 1]); if (((i << 1) + 1) <= n) Console.Write("right key = {0}, ", array[i << 1]); Console.WriteLine(); } } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
191
8
using System; using System.Collections.Generic; using System.Linq; namespace AizuOnline_DataStructure { public class Node { public int id; public bool isPassed = false; public int groupId; public int cost = int.MaxValue; } internal class Program { private static void Main(string[] args) { var nodes = new List<Node>(); var n = Convert.ToInt32(Console.ReadLine()); var p = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { var node = new Node() { id = i, cost = Convert.ToInt32(p[i]) }; nodes.Add(node); } foreach (var node in nodes) { Console.Write("node " + (node.id + 1) + ": key = " + node.cost + ", "); var maxIndex = nodes.Count(); if (node.id != 0) { Console.Write("parent key = " + p[node.id / 2] + ", "); } if (node.id * 2 + 1 < maxIndex) { Console.Write("left key = " + p[node.id * 2 + 1] + ", "); } if (node.id * 2 + 2 < maxIndex) { Console.Write("right key = " + p[node.id * 2 + 2] + ", "); } Console.WriteLine(); } Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; namespace AizuOnline_DataStructure { public class Node { public int id; public bool isPassed = false; public int groupId; public int cost = int.MaxValue; } internal class Program { private static void Main(string[] args) { var nodes = new List<Node>(); var n = Convert.ToInt32(Console.ReadLine()); var p = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { var node = new Node() { id = i, cost = Convert.ToInt32(p[i]) }; nodes.Add(node); } foreach (var node in nodes) { Console.Write("node " + (node.id + 1) + ": key = " + node.cost + ", "); var maxIndex = nodes.Count(); if (node.id != 0) { Console.Write("parent key = " + p[(node.id - 1) / 2] + ", "); } if (node.id * 2 + 1 < maxIndex) { Console.Write("left key = " + p[node.id * 2 + 1] + ", "); } if (node.id * 2 + 2 < maxIndex) { Console.Write("right key = " + p[node.id * 2 + 2] + ", "); } Console.WriteLine(); } Console.ReadLine(); } } }
[["+", 206, 207, 0, 28, 0, 16, 31, 23, 0, 24], ["+", 0, 28, 0, 16, 31, 23, 0, 16, 17, 33], ["+", 0, 28, 0, 16, 31, 23, 0, 16, 12, 203], ["+", 206, 207, 0, 28, 0, 16, 31, 23, 0, 25]]
8
319
4
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace B { class Heap<T> : IEnumerable<T> where T : IComparable<T> { private List<T> data = new List<T>() { default(T) }; public Heap() {} public Heap(T[] array) { data.AddRange(array); } public IEnumerator<T> GetEnumerator() { return data.Skip(1).GetEnumerator(); } public void MaxHeapify() { for (int i = data.Count / 2; i > 0; i--) MaxHeapify(i); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } private void MaxHeapify(int index) { var left = index << 1; var right = left + 1; int largest = index; if (left < data.Count && data[left].CompareTo(data[largest]) > 0) largest = left; if (right < data.Count && data[right].CompareTo(data[largest]) > 0) largest = right; if (largest != index) { var tmp = data[largest]; data[largest] = data[index]; data[index] = tmp; MaxHeapify(largest); } } } class Program { static void Main(string[] args) { var array = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var heap = new Heap<int>(array); heap.MaxHeapify(); foreach (var item in heap) Console.Write(" " + item); Console.WriteLine(); } } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace B { class Heap<T> : IEnumerable<T> where T : IComparable<T> { private List<T> data = new List<T>() { default(T) }; public Heap() {} public Heap(T[] array) { data.AddRange(array); } public IEnumerator<T> GetEnumerator() { return data.Skip(1).GetEnumerator(); } public void MaxHeapify() { for (int i = data.Count / 2; i > 0; i--) MaxHeapify(i); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } private void MaxHeapify(int index) { var left = index << 1; var right = left + 1; int largest = index; if (left < data.Count && data[left].CompareTo(data[largest]) > 0) largest = left; if (right < data.Count && data[right].CompareTo(data[largest]) > 0) largest = right; if (largest != index) { var tmp = data[largest]; data[largest] = data[index]; data[index] = tmp; MaxHeapify(largest); } } } class Program { static void Main(string[] args) { Console.ReadLine(); var array = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); var heap = new Heap<int>(array); heap.MaxHeapify(); foreach (var item in heap) Console.Write(" " + item); Console.WriteLine(); } } }
[["+", 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
354
6
using System; using System.Linq; namespace geometry { public class Point { public double x, y; public Point() { this.x = this.y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y); } public static Point operator -(Point p1, Point p2) { return new Point(p1.x - p2.x, p1.y - p2.y); } public static Point operator *(Point p1, double a) { return new Point(p1.x * a, p1.y * a); } public static Point operator /(Point p1, double a) { return new Point(p1.x / a, p1.y / a); } public double norm() { return this.x * this.x + this.y * this.y; } public double abs() { return Math.Sqrt(norm()); } public Point Clone() { Point clone = new Point(this.x, this.y); return clone; } } public class Line { public Point p1, p2; public Line() { this.p1 = new Point(); this.p2 = new Point(); } public Line(double p1x, double p1y, double p2x, double p2y) { this.p1 = new Point(p1x, p1y); this.p2 = new Point(p2x, p2y); } public Line(Point p1, Point p2) { this.p1 = p1.Clone(); this.p2 = p2.Clone(); } public Line Clone() { Line clone = new Line(this.p1, this.p2); return clone; } } public class Circle { public Point c; public double r; public Circle() { this.c = new Point(); this.r = 0.0; } public Circle(double cx, double cy, double r) { this.c.x = cx; this.c.y = cy; this.r = r; } public Circle(Point c, double r) { this.c = c.Clone(); this.r = r; } public Circle Clone() { Circle clone = new Circle(this.c, this.r); return clone; } } class MainClass { static bool is_equal(double a, double b) { if (Math.Abs(a - b) < 0.000001) { return true; } else { return false; } } static double dot(Point v1, Point v2) { return v1.x * v2.x + v1.y * v2.y; } static double cross(Point v1, Point v2) { return v1.x * v2.y - v1.y * v2.x; } // ??´????????? static bool is_orthogonal(Line s1, Line s2) { return is_equal(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } // ???????????? static bool is_parallel(Line s1, Line s2) { return is_equal(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } /* static bool is_intersect_sl(Line s, Line sl){ return true; } */ // ?°???±??¢??° static Point project(Line s, Point p) { Point base_v = s.p2 - s.p1; double r = dot(p - s.p1, base_v) / base_v.norm(); return s.p1 + base_v * r; } // ????°???¢??° static Point reflect(Line s, Point p) { return p + (project(s, p) - p) * 2; } static double get_distance_p2p(Point p1, Point p2) { return (p2 - p1).abs(); } static double get_distance_l2p(Line l, Point p) { return Math.Abs(cross(l.p2 - l.p1, p - l.p1)) / (l.p2 - l.p1).abs(); } static double get_distance_s2p(Line s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) { return (p - s.p1).abs(); } else if (dot(s.p1 - s.p2, p - s.p2) < 0.0) { return (p - s.p2).abs(); } return get_distance_l2p(s, p); } /* static double get_distance_s2s(Line s, Line s){ if ( } */ static double[] input_line() { string[] s = Console.ReadLine().Split(' '); double[] d = s.Select(i => double.Parse(i)).ToArray(); return d; } // ?°???± static void CGL_1_A() { double[] d = input_line(); Line l = new Line(d[0], d[1], d[2], d[3]); int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] p_pos = input_line(); Point p1 = new Point(p_pos[0], p_pos[1]); Point p2 = project(l, p1); Console.WriteLine("{0}, {1}", p2.x, p2.y); } } // ????°? static void CGL_1_B() { double[] d = input_line(); Line l = new Line(d[0], d[1], d[2], d[3]); int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] p_pos = input_line(); Point p1 = new Point(p_pos[0], p_pos[1]); Point p2 = reflect(l, p1); Console.WriteLine("{0}, {1}", p2.x, p2.y); } } // ??´????????´???/???????????? static void CGL_2_A() { int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] s_pos = input_line(); Line s1 = new Line(s_pos[0], s_pos[1], s_pos[2], s_pos[3]); Line s2 = new Line(s_pos[4], s_pos[5], s_pos[6], s_pos[7]); if (is_parallel(s1, s2)) { Console.Write("2\n"); } else if (is_orthogonal(s1, s2)) { Console.Write("1\n"); } else { Console.Write("0\n"); } } } /* static void CGL_2_D(){ int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] s_pos = input_line (); Line s1 = new Line(s_pos[0], s_pos[1], s_pos[2], s_pos[3]); Line s2 = new Line(s_pos[4], s_pos[5], s_pos[6], s_pos[7]); } } */ public static void Main(string[] args) { // ?°???± CGL_1_A(); // ????°? // CGL_1_B (); // ??´????????´???/???????????? // CGL_2_A (); } } }
using System; using System.Linq; namespace geometry { public class Point { public double x, y; public Point() { this.x = this.y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y); } public static Point operator -(Point p1, Point p2) { return new Point(p1.x - p2.x, p1.y - p2.y); } public static Point operator *(Point p1, double a) { return new Point(p1.x * a, p1.y * a); } public static Point operator /(Point p1, double a) { return new Point(p1.x / a, p1.y / a); } public double norm() { return this.x * this.x + this.y * this.y; } public double abs() { return Math.Sqrt(norm()); } public Point Clone() { Point clone = new Point(this.x, this.y); return clone; } } public class Line { public Point p1, p2; public Line() { this.p1 = new Point(); this.p2 = new Point(); } public Line(double p1x, double p1y, double p2x, double p2y) { this.p1 = new Point(p1x, p1y); this.p2 = new Point(p2x, p2y); } public Line(Point p1, Point p2) { this.p1 = p1.Clone(); this.p2 = p2.Clone(); } public Line Clone() { Line clone = new Line(this.p1, this.p2); return clone; } } public class Circle { public Point c; public double r; public Circle() { this.c = new Point(); this.r = 0.0; } public Circle(double cx, double cy, double r) { this.c.x = cx; this.c.y = cy; this.r = r; } public Circle(Point c, double r) { this.c = c.Clone(); this.r = r; } public Circle Clone() { Circle clone = new Circle(this.c, this.r); return clone; } } class MainClass { static bool is_equal(double a, double b) { if (Math.Abs(a - b) < 0.000001) { return true; } else { return false; } } static double dot(Point v1, Point v2) { return v1.x * v2.x + v1.y * v2.y; } static double cross(Point v1, Point v2) { return v1.x * v2.y - v1.y * v2.x; } // ??´????????? static bool is_orthogonal(Line s1, Line s2) { return is_equal(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } // ???????????? static bool is_parallel(Line s1, Line s2) { return is_equal(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } /* static bool is_intersect_sl(Line s, Line sl){ return true; } */ // ?°???±??¢??° static Point project(Line s, Point p) { Point base_v = s.p2 - s.p1; double r = dot(p - s.p1, base_v) / base_v.norm(); return s.p1 + base_v * r; } // ????°???¢??° static Point reflect(Line s, Point p) { return p + (project(s, p) - p) * 2; } static double get_distance_p2p(Point p1, Point p2) { return (p2 - p1).abs(); } static double get_distance_l2p(Line l, Point p) { return Math.Abs(cross(l.p2 - l.p1, p - l.p1)) / (l.p2 - l.p1).abs(); } static double get_distance_s2p(Line s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) { return (p - s.p1).abs(); } else if (dot(s.p1 - s.p2, p - s.p2) < 0.0) { return (p - s.p2).abs(); } return get_distance_l2p(s, p); } /* static double get_distance_s2s(Line s, Line s){ if ( } */ static double[] input_line() { string[] s = Console.ReadLine().Split(' '); double[] d = s.Select(i => double.Parse(i)).ToArray(); return d; } // ?°???± static void CGL_1_A() { double[] d = input_line(); Line l = new Line(d[0], d[1], d[2], d[3]); int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] p_pos = input_line(); Point p1 = new Point(p_pos[0], p_pos[1]); Point p2 = project(l, p1); Console.WriteLine("{0} {1}", p2.x, p2.y); } } // ????°? static void CGL_1_B() { double[] d = input_line(); Line l = new Line(d[0], d[1], d[2], d[3]); int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] p_pos = input_line(); Point p1 = new Point(p_pos[0], p_pos[1]); Point p2 = reflect(l, p1); Console.WriteLine("{0} {1}", p2.x, p2.y); } } // ??´????????´???/???????????? static void CGL_2_A() { int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] s_pos = input_line(); Line s1 = new Line(s_pos[0], s_pos[1], s_pos[2], s_pos[3]); Line s2 = new Line(s_pos[4], s_pos[5], s_pos[6], s_pos[7]); if (is_parallel(s1, s2)) { Console.Write("2\n"); } else if (is_orthogonal(s1, s2)) { Console.Write("1\n"); } else { Console.Write("0\n"); } } } /* static void CGL_2_D(){ int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { double[] s_pos = input_line (); Line s1 = new Line(s_pos[0], s_pos[1], s_pos[2], s_pos[3]); Line s2 = new Line(s_pos[4], s_pos[5], s_pos[6], s_pos[7]); } } */ public static void Main(string[] args) { // ?°???± CGL_1_A(); // ????°? // CGL_1_B (); // ??´????????´???/???????????? // CGL_2_A (); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
1,423
4
using System; class Program { public struct Vector { public int x, y; } public static double norm(Vector v) { return v.x * v.x + v.y * v.y; } public static double dot(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y; } public static void Main() { string[] s1 = Console.ReadLine().Split(' '); Vector baseV; baseV.x = int.Parse(s1[2]) - int.Parse(s1[0]); baseV.y = int.Parse(s1[3]) - int.Parse(s1[1]); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] s2 = Console.ReadLine().Split(' '); Vector v; v.x = int.Parse(s2[0]) - int.Parse(s1[0]); v.y = int.Parse(s2[1]) - int.Parse(s1[1]); double r = dot(v, baseV) / norm(baseV); double x = int.Parse(s1[0]) + baseV.x * r; double y = int.Parse(s1[1]) + baseV.y * r; Console.WriteLine("result: {0:f10} {1:f10}", x, y); } } }
using System; class Program { public struct Vector { public int x, y; } public static double norm(Vector v) { return v.x * v.x + v.y * v.y; } public static double dot(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y; } public static void Main() { string[] s1 = Console.ReadLine().Split(' '); Vector baseV; baseV.x = int.Parse(s1[2]) - int.Parse(s1[0]); baseV.y = int.Parse(s1[3]) - int.Parse(s1[1]); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] s2 = Console.ReadLine().Split(' '); Vector v; v.x = int.Parse(s2[0]) - int.Parse(s1[0]); v.y = int.Parse(s2[1]) - int.Parse(s1[1]); double r = dot(v, baseV) / norm(baseV); double x = int.Parse(s1[0]) + baseV.x * r; double y = int.Parse(s1[1]) + baseV.y * r; Console.WriteLine("{0:f10} {1:f10}", x, y); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
317
2
using System; using System.Collections.Generic; using System.Linq; /* Points/Vectors - Projection http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A ?????????????°???±????±????????????? ??¨???: Vec2f ??? float ??§?¨????????????¨??????????????? */ class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); } static string[] ReadStrings() { return Console.ReadLine().Split(); } // a, b ????????´??????????????????p // ????????????????°???±?????????????±??????? static void Calc(Vec2f p, Vec2f a, Vec2f b) { var ab = b - a; var ap = p - a; var d = ab.Normalized().Dot(ap); var m = a + ab.Normalized() * d; var pm = m - p; var px = 2 * pm; var x = p + px; Console.WriteLine("{0} {1}", x.X, x.Y); } static void Main() { var xs = ReadInts(); var p1 = new Vec2f(xs[0], xs[1]); var p2 = new Vec2f(xs[2], xs[3]); int q = ReadInt(); for (int i = 0; i < q; i++) { var xy = ReadInts(); var p = new Vec2f(xy[0], xy[1]); Calc(p, p1, p2); } } } struct Vec2f { public double X { get; private set; } public double Y { get; private set; } public Vec2f(double x, double y) : this() { X = x; Y = y; } public override string ToString() { return string.Format("({0}, {1})", X, Y); } public static Vec2f operator +(Vec2f a, Vec2f b) { return new Vec2f(a.X + b.X, a.Y + b.Y); } public static Vec2f operator -(Vec2f a, Vec2f b) { return new Vec2f(a.X - b.X, a.Y - b.Y); } public static Vec2f operator *(Vec2f a, double t) { return new Vec2f(a.X * t, a.Y * t); } public static Vec2f operator *(double t, Vec2f a) { return new Vec2f(a.X * t, a.Y * t); } // a???b = |a||b|cos?? public double Dot(Vec2f other) { return X * other.X + Y * other.Y; } public double Cross(Vec2f other) { return X * other.Y - Y * other.X; } public Vec2f Normalized() { var m = Magnitude(); if (m != 0 && m != 1) { return new Vec2f(X / m, Y / m); } return this; } public double Magnitude() { return Math.Sqrt(X * X + Y * Y); } public double SqrMagnitude() { return X * X + Y * Y; } public static double Distance(Vec2f a, Vec2f b) { return (a - b).Magnitude(); } }
using System; using System.Collections.Generic; using System.Linq; /* Points/Vectors - Projection http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A ?????????????°???±????±????????????? ??¨???: Vec2f ??? float ??§?¨????????????¨??????????????? */ class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); } static string[] ReadStrings() { return Console.ReadLine().Split(); } // a, b ????????´??????????????????p // ????????????????°???±?????????????±??????? static void Calc(Vec2f p, Vec2f a, Vec2f b) { var ab = b - a; var ap = p - a; var d = ab.Normalized().Dot(ap); var m = a + ab.Normalized() * d; var pm = m - p; var px = 2 * pm; var x = p + px; Console.WriteLine("{0:f8} {1:f8}", x.X, x.Y); } static void Main() { var xs = ReadInts(); var p1 = new Vec2f(xs[0], xs[1]); var p2 = new Vec2f(xs[2], xs[3]); int q = ReadInt(); for (int i = 0; i < q; i++) { var xy = ReadInts(); var p = new Vec2f(xy[0], xy[1]); Calc(p, p1, p2); } } } struct Vec2f { public double X { get; private set; } public double Y { get; private set; } public Vec2f(double x, double y) : this() { X = x; Y = y; } public override string ToString() { return string.Format("({0}, {1})", X, Y); } public static Vec2f operator +(Vec2f a, Vec2f b) { return new Vec2f(a.X + b.X, a.Y + b.Y); } public static Vec2f operator -(Vec2f a, Vec2f b) { return new Vec2f(a.X - b.X, a.Y - b.Y); } public static Vec2f operator *(Vec2f a, double t) { return new Vec2f(a.X * t, a.Y * t); } public static Vec2f operator *(double t, Vec2f a) { return new Vec2f(a.X * t, a.Y * t); } // a???b = |a||b|cos?? public double Dot(Vec2f other) { return X * other.X + Y * other.Y; } public double Cross(Vec2f other) { return X * other.Y - Y * other.X; } public Vec2f Normalized() { var m = Magnitude(); if (m != 0 && m != 1) { return new Vec2f(X / m, Y / m); } return this; } public double Magnitude() { return Math.Sqrt(X * X + Y * Y); } public double SqrMagnitude() { return X * X + Y * Y; } public static double Distance(Vec2f a, Vec2f b) { return (a - b).Magnitude(); } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
639
2
using System; // using System.Collections.Generic; // using System.Linq; // using Vector = CGL.Point; // using Line = CGL.Segment; // using Polygon = System.Collections.Generic.List<CGL.Point>; namespace CGL { class Program { static void Main(string[] args) { var t = scan; var q = scan[0]; var p0 = new Point(t[0], t[1]); var p1 = new Point(t[2], t[3]); for (int i = 0; i < q; i++) { t = scan; var p2 = new Point(t[0], t[1]); var k = (p1 - p0).CCW(p2 - p0); if (k == 1) Console.WriteLine("COUNTER_CLOCKWISE"); if (k == -1) Console.WriteLine("CLOCKWISE"); if (k == 2) Console.WriteLine("ONLINE_BACK"); if (k == -2) Console.WriteLine("ONLINE_FRONT"); else Console.WriteLine("ON_SEGMENT"); } /*var q = scan[0]; for (int i = 0; i < q; i++) { var t = scan; var P = new Point[4]; for (int j = 0; j < 4; j++) { P[j] = new Point(t[j * 2], t[j * 2 + 1]); } var s1 = new Segment(P[0], P[1]); var s2 = new Segment(P[2], P[3]); Console.WriteLine(s1.GetDistance(s2)); }*/ } static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } } struct Point { static readonly double EPS = 1E-10; public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(double k, Point a) { return new Point(k * a.x, k * a.y); } public static Point operator /(double k, Point a) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } } struct Segment { public Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + r * b; } public Point Reflection(Point p) { return p + 2 * (Projection(p) - p); } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public double GetDistance(Segment s) { // if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } } /*struct Circle { public Point c; double r; public Circle(Point c, double r) { this.c = c; this.r = r; } }*/ }
using System; // using System.Collections.Generic; // using System.Linq; // using Vector = CGL.Point; // using Line = CGL.Segment; // using Polygon = System.Collections.Generic.List<CGL.Point>; namespace CGL { class Program { static void Main(string[] args) { var t = scan; var q = scan[0]; var p0 = new Point(t[0], t[1]); var p1 = new Point(t[2], t[3]); for (int i = 0; i < q; i++) { t = scan; var p2 = new Point(t[0], t[1]); var k = (p1 - p0).CCW(p2 - p0); if (k == 1) Console.WriteLine("COUNTER_CLOCKWISE"); else if (k == -1) Console.WriteLine("CLOCKWISE"); else if (k == 2) Console.WriteLine("ONLINE_BACK"); else if (k == -2) Console.WriteLine("ONLINE_FRONT"); else Console.WriteLine("ON_SEGMENT"); } /*var q = scan[0]; for (int i = 0; i < q; i++) { var t = scan; var P = new Point[4]; for (int j = 0; j < 4; j++) { P[j] = new Point(t[j * 2], t[j * 2 + 1]); } var s1 = new Segment(P[0], P[1]); var s2 = new Segment(P[2], P[3]); Console.WriteLine(s1.GetDistance(s2)); }*/ } static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } } struct Point { static readonly double EPS = 1E-10; public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(double k, Point a) { return new Point(k * a.x, k * a.y); } public static Point operator /(double k, Point a) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } } struct Segment { public Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + r * b; } public Point Reflection(Point p) { return p + 2 * (Projection(p) - p); } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public double GetDistance(Segment s) { // if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } } /*struct Circle { public Point c; double r; public Circle(Point c, double r) { this.c = c; this.r = r; } }*/ }
[["+", 8, 196, 0, 7, 8, 196, 0, 57, 0, 95], ["+", 0, 7, 8, 196, 0, 57, 75, 57, 0, 95], ["+", 8, 196, 0, 57, 75, 57, 75, 57, 0, 95]]
8
1,063
3
using System; using System.Collections.Generic; using System.Linq; class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); } static string[] ReadStrings() { return Console.ReadLine().Split(); } static int Sign(double x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } static string Calc(Vec2f a, Vec2f b, Vec2f c) { var ab = b - a; var ac = c - a; var q = ab.Cross(ac); if (q > 0) return "COUNTER_CLOCKWISE"; if (q < 0) return "CLOCKWISE"; // when q = 0 // c ??? a ????????? b ??¨????????? if ((a.X == c.X && a.Y == c.Y) || (b.X == c.X && b.Y == c.Y)) return "ONLINE_SEGMENT"; // ???????????????????????? if (Sign(ab.X) == Sign(ac.X) && Sign(ab.Y) == Sign(ac.Y)) { if (ab.SqrMagnitude() >= ac.SqrMagnitude()) return "ONLINE_SEGMENT"; return "ONLINE_FRONT"; } else { return "ONLINE_BACK"; } } static void Main() { var xs = ReadInts(); var p0 = new Vec2f(xs[0], xs[1]); var p1 = new Vec2f(xs[2], xs[3]); int q = ReadInt(); for (int i = 0; i < q; i++) { var xy = ReadInts(); var p2 = new Vec2f(xy[0], xy[1]); var ans = Calc(p0, p1, p2); Console.WriteLine(ans); } } } struct Vec2f { public double X { get; private set; } public double Y { get; private set; } public Vec2f(double x, double y) : this() { X = x; Y = y; } public override string ToString() { return string.Format("({0}, {1})", X, Y); } public static Vec2f operator +(Vec2f a, Vec2f b) { return new Vec2f(a.X + b.X, a.Y + b.Y); } public static Vec2f operator -(Vec2f a, Vec2f b) { return new Vec2f(a.X - b.X, a.Y - b.Y); } public static Vec2f operator *(Vec2f a, double t) { return new Vec2f(a.X * t, a.Y * t); } public static Vec2f operator *(double t, Vec2f a) { return new Vec2f(a.X * t, a.Y * t); } // a???b = |a||b|cos?? public double Dot(Vec2f other) { return X * other.X + Y * other.Y; } public double Cross(Vec2f other) { return X * other.Y - Y * other.X; } public Vec2f Normalized() { var m = Magnitude(); if (m != 0 && m != 1) { return new Vec2f(X / m, Y / m); } return this; } public double Magnitude() { return Math.Sqrt(X * X + Y * Y); } public double SqrMagnitude() { return X * X + Y * Y; } public static double Distance(Vec2f a, Vec2f b) { return (a - b).Magnitude(); } }
using System; using System.Collections.Generic; using System.Linq; class Program { static int ReadInt() { return int.Parse(Console.ReadLine()); } static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); } static string[] ReadStrings() { return Console.ReadLine().Split(); } static int Sign(double x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } static string Calc(Vec2f a, Vec2f b, Vec2f c) { var ab = b - a; var ac = c - a; var q = ab.Cross(ac); if (q > 0) return "COUNTER_CLOCKWISE"; if (q < 0) return "CLOCKWISE"; // when q = 0 // c ??? a ????????? b ??¨????????? if ((a.X == c.X && a.Y == c.Y) || (b.X == c.X && b.Y == c.Y)) return "ON_SEGMENT"; // ???????????????????????? if (Sign(ab.X) == Sign(ac.X) && Sign(ab.Y) == Sign(ac.Y)) { if (ab.SqrMagnitude() >= ac.SqrMagnitude()) return "ON_SEGMENT"; return "ONLINE_FRONT"; } else { return "ONLINE_BACK"; } } static void Main() { var xs = ReadInts(); var p0 = new Vec2f(xs[0], xs[1]); var p1 = new Vec2f(xs[2], xs[3]); int q = ReadInt(); for (int i = 0; i < q; i++) { var xy = ReadInts(); var p2 = new Vec2f(xy[0], xy[1]); var ans = Calc(p0, p1, p2); Console.WriteLine(ans); } } } struct Vec2f { public double X { get; private set; } public double Y { get; private set; } public Vec2f(double x, double y) : this() { X = x; Y = y; } public override string ToString() { return string.Format("({0}, {1})", X, Y); } public static Vec2f operator +(Vec2f a, Vec2f b) { return new Vec2f(a.X + b.X, a.Y + b.Y); } public static Vec2f operator -(Vec2f a, Vec2f b) { return new Vec2f(a.X - b.X, a.Y - b.Y); } public static Vec2f operator *(Vec2f a, double t) { return new Vec2f(a.X * t, a.Y * t); } public static Vec2f operator *(double t, Vec2f a) { return new Vec2f(a.X * t, a.Y * t); } // a???b = |a||b|cos?? public double Dot(Vec2f other) { return X * other.X + Y * other.Y; } public double Cross(Vec2f other) { return X * other.Y - Y * other.X; } public Vec2f Normalized() { var m = Magnitude(); if (m != 0 && m != 1) { return new Vec2f(X / m, Y / m); } return this; } public double Magnitude() { return Math.Sqrt(X * X + Y * Y); } public double SqrMagnitude() { return X * X + Y * Y; } public static double Distance(Vec2f a, Vec2f b) { return (a - b).Magnitude(); } }
[["-", 8, 196, 0, 57, 64, 37, 0, 5, 0, 222], ["+", 8, 196, 0, 57, 64, 37, 0, 5, 0, 222], ["-", 64, 196, 0, 57, 64, 37, 0, 5, 0, 222], ["+", 64, 196, 0, 57, 64, 37, 0, 5, 0, 222]]
8
754
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Aoj.CGL.Lib; namespace Aoj.CGL.Chapter1C { class Program { public static string ConvertCcwToStr(Ccw ccw) { var map = new Dictionary<Ccw, string>() { { Ccw.CounterClockwise, "COUNTER_CLOCKWISE" }, { Ccw.Clockwise, "CLOCKWISE" }, { Ccw.OnLineBack, "ON_LINE_BACK" }, { Ccw.OnSegment, "ON_SEGMENT" }, { Ccw.OnLineFront, "ON_LINE_FRONT" }, }; return map[ccw]; } public static void Solve() { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector v1 = new Vector(input[0], input[1]); Vector v2 = new Vector(input[2], input[3]); Vector baseV = v2 - v1; int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { int[] x_y = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector v = new Vector(x_y[0], x_y[1]) - v1; Ccw ccw = Vector.CountrClockWise(baseV, v); Console.WriteLine("{0}", ConvertCcwToStr(ccw)); } } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("ComputationalGeometry/Input/01_Projection_C_03.txt")); // var sw = new Stopwatch(); // sw.Start(); Solve(); // Console.WriteLine("{0}ms", sw.ElapsedMilliseconds); } } } namespace Aoj.CGL.Lib { using Line = Segment; using Polygon = List<Point>; public enum Ccw { CounterClockwise, Clockwise, OnLineBack, OnSegment, OnLineFront, } ; public struct Point { private double _x; private double _y; public double X { get { return _x; } } public double Y { get { return _y; } } public Point(double x, double y) { _x = x; _y = y; } public static Point operator +(Point p1, Point p2) { return new Point(p1.X + p2.X, p1.Y + p2.Y); } public static Point operator -(Point p1, Point p2) { return new Point(p1.X - p2.X, p1.Y - p2.Y); } public static Point operator *(Point p, double k) { return new Point(p.X * k, p.Y * k); } public static Point operator *(double k, Point p) { return p * k; } public static explicit operator Vector(Point p) { return new Vector(p.X, p.Y); } public static bool IsOrthogonal(Point a1, Point a2, Point b1, Point b2) { return Vector.IsOrthogonal((Vector)(a1 - a2), (Vector)(b1 - b2)); } public static bool IsParallel(Point a1, Point a2, Point b1, Point b2) { return Vector.IsParallel((Vector)(a1 - a2), (Vector)(b1 - b2)); } public override string ToString() { return string.Format("x:{0},y:{1}", X, Y); } } public struct Segment { private Point _p1; private Point _p2; public Point P1 { get { return _p1; } } public Point P2 { get { return _p2; } } public Segment(Point p1, Point p2) { _p1 = p1; _p2 = p2; } public Segment(double x1, double y1, double x2, double y2) { _p1 = new Point(x1, y1); _p2 = new Point(x2, y2); } public static bool IsOrthogonal(Segment s1, Segment s2) { return Vector.IsOrthogonal((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2)); } public static bool IsParallel(Segment s1, Segment s2) { return Vector.IsParallel((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2)); } public override string ToString() { return string.Format("P1x:{0},P1y:{1}, P2x:{2},P2y:{3}", P1.X, P1.Y, P2.X, P2.Y); } } public struct Circle { private Point _p; private double _r; public Point P { get { return _p; } } public double R { get { return _r; } } public Circle(Point p, double r) { _p = p; _r = r; } public override string ToString() { return string.Format("px:{0},py:{1},r:{2}", P.X, P.Y, R); } } public struct Vector { private double _x; private double _y; public double X { get { return _x; } } public double Y { get { return _y; } } public Vector(double x, double y) { _x = x; _y = y; } public double Norm() { return X * X + Y * Y; } public double Abs() { return Math.Sqrt(Norm()); } public Vector Normalize() { double mag = Abs(); return new Vector(X * mag, Y * mag); } public static Vector operator +(Vector v1, Vector v2) { return new Vector(v1.X + v2.X, v1.Y + v2.Y); } public static Vector operator -(Vector v1, Vector v2) { return new Vector(v1.X - v2.X, v1.Y - v2.Y); } public static Vector operator *(Vector v, double k) { return new Vector(v.X * k, v.Y * k); } public static Vector operator *(double k, Vector v) { return v * k; } public static double Dot(Vector v1, Vector v2) { return v1.X * v2.X + v1.Y * v2.Y; } public static double Cross(Vector v1, Vector v2) { return v1.X * v2.Y - v1.Y * v2.X; } public static bool IsOrthogonal(Vector v1, Vector v2) { return Dot(v1, v2) == 0.0; } public static bool IsParallel(Vector v1, Vector v2) { return Cross(v1, v2) == 0.0; } public static Vector Project(Vector v, Vector onNormal) { // mag = |b|cos?? / |a| // = |a||b|cos?? / |a|^2 double mag = Dot(v, onNormal) / onNormal.Norm(); return new Vector(onNormal.X * mag, onNormal.Y * mag); } public static Vector Reflect(Vector v, Vector inNormal) { // TODO Unity????????¨???????????????????????????????????? Vector p = Project(v, inNormal); return 2.0 * p - v; } public static Ccw CountrClockWise(Vector baseV, Vector v) { double cross = Cross(baseV, v); if (cross > 0.0) return Ccw.CounterClockwise; else if (cross < 0.0) return Ccw.Clockwise; else if (Dot(baseV, v) < 0.0) return Ccw.OnLineBack; else if (baseV.Norm() < v.Norm()) return Ccw.OnLineFront; else return Ccw.OnSegment; } public override string ToString() { return string.Format("x:{0},y:{1}", X, Y); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Aoj.CGL.Lib; namespace Aoj.CGL.Chapter1C { class Program { public static string ConvertCcwToStr(Ccw ccw) { var map = new Dictionary<Ccw, string>() { { Ccw.CounterClockwise, "COUNTER_CLOCKWISE" }, { Ccw.Clockwise, "CLOCKWISE" }, { Ccw.OnLineBack, "ONLINE_BACK" }, { Ccw.OnSegment, "ON_SEGMENT" }, { Ccw.OnLineFront, "ONLINE_FRONT" }, }; return map[ccw]; } public static void Solve() { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector v1 = new Vector(input[0], input[1]); Vector v2 = new Vector(input[2], input[3]); Vector baseV = v2 - v1; int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { int[] x_y = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector v = new Vector(x_y[0], x_y[1]) - v1; Ccw ccw = Vector.CountrClockWise(baseV, v); Console.WriteLine("{0}", ConvertCcwToStr(ccw)); } } public static void Main() { // Console.SetIn(new // System.IO.StreamReader("ComputationalGeometry/Input/01_Projection_C_03.txt")); // var sw = new Stopwatch(); // sw.Start(); Solve(); // Console.WriteLine("{0}ms", sw.ElapsedMilliseconds); } } } namespace Aoj.CGL.Lib { using Line = Segment; using Polygon = List<Point>; public enum Ccw { CounterClockwise, Clockwise, OnLineBack, OnSegment, OnLineFront, } ; public struct Point { private double _x; private double _y; public double X { get { return _x; } } public double Y { get { return _y; } } public Point(double x, double y) { _x = x; _y = y; } public static Point operator +(Point p1, Point p2) { return new Point(p1.X + p2.X, p1.Y + p2.Y); } public static Point operator -(Point p1, Point p2) { return new Point(p1.X - p2.X, p1.Y - p2.Y); } public static Point operator *(Point p, double k) { return new Point(p.X * k, p.Y * k); } public static Point operator *(double k, Point p) { return p * k; } public static explicit operator Vector(Point p) { return new Vector(p.X, p.Y); } public static bool IsOrthogonal(Point a1, Point a2, Point b1, Point b2) { return Vector.IsOrthogonal((Vector)(a1 - a2), (Vector)(b1 - b2)); } public static bool IsParallel(Point a1, Point a2, Point b1, Point b2) { return Vector.IsParallel((Vector)(a1 - a2), (Vector)(b1 - b2)); } public override string ToString() { return string.Format("x:{0},y:{1}", X, Y); } } public struct Segment { private Point _p1; private Point _p2; public Point P1 { get { return _p1; } } public Point P2 { get { return _p2; } } public Segment(Point p1, Point p2) { _p1 = p1; _p2 = p2; } public Segment(double x1, double y1, double x2, double y2) { _p1 = new Point(x1, y1); _p2 = new Point(x2, y2); } public static bool IsOrthogonal(Segment s1, Segment s2) { return Vector.IsOrthogonal((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2)); } public static bool IsParallel(Segment s1, Segment s2) { return Vector.IsParallel((Vector)(s1.P1 - s1.P2), (Vector)(s2.P1 - s2.P2)); } public override string ToString() { return string.Format("P1x:{0},P1y:{1}, P2x:{2},P2y:{3}", P1.X, P1.Y, P2.X, P2.Y); } } public struct Circle { private Point _p; private double _r; public Point P { get { return _p; } } public double R { get { return _r; } } public Circle(Point p, double r) { _p = p; _r = r; } public override string ToString() { return string.Format("px:{0},py:{1},r:{2}", P.X, P.Y, R); } } public struct Vector { private double _x; private double _y; public double X { get { return _x; } } public double Y { get { return _y; } } public Vector(double x, double y) { _x = x; _y = y; } public double Norm() { return X * X + Y * Y; } public double Abs() { return Math.Sqrt(Norm()); } public Vector Normalize() { double mag = Abs(); return new Vector(X * mag, Y * mag); } public static Vector operator +(Vector v1, Vector v2) { return new Vector(v1.X + v2.X, v1.Y + v2.Y); } public static Vector operator -(Vector v1, Vector v2) { return new Vector(v1.X - v2.X, v1.Y - v2.Y); } public static Vector operator *(Vector v, double k) { return new Vector(v.X * k, v.Y * k); } public static Vector operator *(double k, Vector v) { return v * k; } public static double Dot(Vector v1, Vector v2) { return v1.X * v2.X + v1.Y * v2.Y; } public static double Cross(Vector v1, Vector v2) { return v1.X * v2.Y - v1.Y * v2.X; } public static bool IsOrthogonal(Vector v1, Vector v2) { return Dot(v1, v2) == 0.0; } public static bool IsParallel(Vector v1, Vector v2) { return Cross(v1, v2) == 0.0; } public static Vector Project(Vector v, Vector onNormal) { // mag = |b|cos?? / |a| // = |a||b|cos?? / |a|^2 double mag = Dot(v, onNormal) / onNormal.Norm(); return new Vector(onNormal.X * mag, onNormal.Y * mag); } public static Vector Reflect(Vector v, Vector inNormal) { // TODO Unity?????????¨???????????????????????????????????? Vector p = Project(v, inNormal); return 2.0 * p - v; } public static Ccw CountrClockWise(Vector baseV, Vector v) { double cross = Cross(baseV, v); if (cross > 0.0) return Ccw.CounterClockwise; else if (cross < 0.0) return Ccw.Clockwise; else if (Dot(baseV, v) < 0.0) return Ccw.OnLineBack; else if (baseV.Norm() < v.Norm()) return Ccw.OnLineFront; else return Ccw.OnSegment; } public override string ToString() { return string.Format("x:{0},y:{1}", X, Y); } } }
[["-", 0, 230, 10, 229, 0, 229, 0, 5, 0, 222], ["+", 0, 230, 10, 229, 0, 229, 0, 5, 0, 222]]
8
1,490
4
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Pair = System.Collections.Generic.KeyValuePair<int, int>; using P_Pair = System.Collections.Generic.KeyValuePair<Point, Point>; using Polygon = System.Collections.Generic.List<Point>; class Program { static void Main() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); new Program().solve(); Console.Out.Flush(); } Scanner cin = new Scanner(); int n; void solve() { n = cin.nextint; var PO = new Polygon(); for (int i = 0; i < n; i++) { PO.Add(new Point(cin.nextint, cin.nextint)); } int flag = 1; for (int i = 0; i < n; i++) { int p2 = (i + 1 >= n) ? i + 1 - n : i + 1; int p3 = (i + 2 >= n) ? i + 2 - n : i + 2; var c = (PO[p2] - PO[i]).CCW(PO[p3] - PO[i]); if (c != 1 && c != -2) { flag = 0; break; } } Console.WriteLine(flag); } } struct Point { static readonly double EPS = 1E-10; internal double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(Point a, double k) { return new Point(k * a.x, k * a.y); } public static Point operator /(Point a, double k) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public double Arg { get { return Math.Atan2(y, x); } } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } public int Contains(Polygon g) { int n = g.Count; bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - this, b = g[(i + 1) % n] - this; if (Math.Abs(a.Cross(b)) < EPS && a.Dot(b) < EPS) return 1; if (a.y > b.y) { Point t = a; a = b; b = t; } if (a.y < EPS && EPS < b.y && a.Cross(b) > EPS) x = !x; } return (x ? 2 : 0); } } struct Segment { internal Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + b * r; } public Point Reflection(Point p) { return p + (Projection(p) - p) * 2; } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public bool Intersect(Segment s) { return (p2 - p1).CCW(s.p1 - p1) * (p2 - p1).CCW(s.p2 - p1) <= 0 && (s.p2 - s.p1).CCW(p1 - s.p1) * (s.p2 - s.p1).CCW(p2 - s.p1) <= 0; } public double GetDistance(Segment s) { if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } public Point GetCrossPoint(Segment s) { Point b = s.p2 - s.p1; double d1 = Math.Abs(b.Cross(p1 - s.p1)); double d2 = Math.Abs(b.Cross(p2 - s.p1)); double t = d1 / (d1 + d2); return p1 + (p2 - p1) * t; } } class Circle { internal Point c; internal double r; public Circle(Point c, double r) { this.c = c; this.r = r; } public bool Intersect(Segment l) { return l.GetD_LP(c) <= r; } public P_Pair GetCrossPoints(Segment l) { Point pr = l.Projection(c); Point e = (l.p2 - l.p1) / (l.p2 - l.p1).Abs; double b = Math.Sqrt(r * r - (pr - c).Norm); return new P_Pair(pr + e * b, pr - e * b); } public P_Pair GetCrossPoints(Circle c2) { double d = (c - c2.c).Abs; double a = Math.Acos((r * r + d * d - c2.r * c2.r) / (2 * r * d)); double t = (c2.c - c).Arg; Point p1 = c + new Point(Math.Cos(t + a) * r, Math.Sin(t + a) * r); Point p2 = c + new Point(Math.Cos(t - a) * r, Math.Sin(t - a) * r); return new P_Pair(p1, p2); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan { get { return Console.ReadLine().Split(); } } public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } } public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } } public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } } public string next { get { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint { get { return int.Parse(next); } } public long nextlong { get { return long.Parse(next); } } public double nextdouble { get { return double.Parse(next); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Pair = System.Collections.Generic.KeyValuePair<int, int>; using P_Pair = System.Collections.Generic.KeyValuePair<Point, Point>; using Polygon = System.Collections.Generic.List<Point>; class Program { static void Main() { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); new Program().solve(); Console.Out.Flush(); } Scanner cin = new Scanner(); int n; void solve() { n = cin.nextint; var PO = new Polygon(); for (int i = 0; i < n; i++) { PO.Add(new Point(cin.nextint, cin.nextint)); } int flag = 1; for (int i = 0; i < n; i++) { int p2 = (i + 1 >= n) ? i + 1 - n : i + 1; int p3 = (i + 2 >= n) ? i + 2 - n : i + 2; var c = (PO[p2] - PO[i]).CCW(PO[p3] - PO[i]); if (c == -1) { flag = 0; break; } } Console.WriteLine(flag); } } struct Point { static readonly double EPS = 1E-10; internal double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(Point a, double k) { return new Point(k * a.x, k * a.y); } public static Point operator /(Point a, double k) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public double Arg { get { return Math.Atan2(y, x); } } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } public int Contains(Polygon g) { int n = g.Count; bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - this, b = g[(i + 1) % n] - this; if (Math.Abs(a.Cross(b)) < EPS && a.Dot(b) < EPS) return 1; if (a.y > b.y) { Point t = a; a = b; b = t; } if (a.y < EPS && EPS < b.y && a.Cross(b) > EPS) x = !x; } return (x ? 2 : 0); } } struct Segment { internal Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + b * r; } public Point Reflection(Point p) { return p + (Projection(p) - p) * 2; } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public bool Intersect(Segment s) { return (p2 - p1).CCW(s.p1 - p1) * (p2 - p1).CCW(s.p2 - p1) <= 0 && (s.p2 - s.p1).CCW(p1 - s.p1) * (s.p2 - s.p1).CCW(p2 - s.p1) <= 0; } public double GetDistance(Segment s) { if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } public Point GetCrossPoint(Segment s) { Point b = s.p2 - s.p1; double d1 = Math.Abs(b.Cross(p1 - s.p1)); double d2 = Math.Abs(b.Cross(p2 - s.p1)); double t = d1 / (d1 + d2); return p1 + (p2 - p1) * t; } } class Circle { internal Point c; internal double r; public Circle(Point c, double r) { this.c = c; this.r = r; } public bool Intersect(Segment l) { return l.GetD_LP(c) <= r; } public P_Pair GetCrossPoints(Segment l) { Point pr = l.Projection(c); Point e = (l.p2 - l.p1) / (l.p2 - l.p1).Abs; double b = Math.Sqrt(r * r - (pr - c).Norm); return new P_Pair(pr + e * b, pr - e * b); } public P_Pair GetCrossPoints(Circle c2) { double d = (c - c2.c).Abs; double a = Math.Acos((r * r + d * d - c2.r * c2.r) / (2 * r * d)); double t = (c2.c - c).Arg; Point p1 = c + new Point(Math.Cos(t + a) * r, Math.Sin(t + a) * r); Point p2 = c + new Point(Math.Cos(t - a) * r, Math.Sin(t - a) * r); return new P_Pair(p1, p2); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan { get { return Console.ReadLine().Split(); } } public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } } public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } } public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } } public string next { get { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint { get { return int.Parse(next); } } public long nextlong { get { return long.Parse(next); } } public double nextdouble { get { return double.Parse(next); } } }
[["-", 8, 196, 0, 57, 15, 16, 31, 16, 17, 79], ["-", 8, 196, 0, 57, 15, 16, 31, 16, 12, 203], ["-", 0, 7, 8, 196, 0, 57, 15, 16, 17, 98], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 31, 22], ["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 79], ["+", 0, 7, 8, 196, 0, 57, 15, 16, 17, 60], ["-", 0, 57, 15, 16, 12, 16, 12, 241, 0, 203], ["+", 8, 196, 0, 57, 15, 16, 12, 241, 0, 203]]
8
2,002
8
using System; namespace CGL_3_C_Polygon_Point_Containment { class Program { static void Main(string[] args) { int inputCount = int.Parse(Console.ReadLine()); Vector2[] polygon = new Vector2[inputCount]; for (int lp = 0; lp < inputCount; lp++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); polygon[lp] = new Vector2(input[0], input[1]); } inputCount = int.Parse(Console.ReadLine()); for (int lp = 0; lp < inputCount; lp++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector2 point = new Vector2(input[0], input[1]); var result = Vector2.Contains(polygon, point); Console.WriteLine(result); } } } class Vector2 { public Vector2(decimal x, decimal y) { this.X = x; this.Y = y; } public decimal X { private set; get; } public decimal Y { private set; get; } public static Vector2 operator -(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X - ob2.X, ob1.Y - ob2.Y); } public static decimal DotProduct(Vector2 line1, Vector2 line2) { return line1.X * line2.X + line1.Y * line2.Y; } public static decimal CrossProduct(Vector2 line1, Vector2 line2) { return line1.X * line2.Y - line1.Y * line2.X; } public const int Contain = 2; public const int On = 1; public const int Other = 0; public static int Contains(Vector2[] polygon, Vector2 point) { int length = polygon.Length; bool isContain = false; for (int lp = 0; lp < length; lp++) { Vector2 a = polygon[lp] - point; Vector2 b = polygon[(lp + 1) % length] - point; if (Math.Abs(Vector2.CrossProduct(a, b)) == 0m && Vector2.DotProduct(a, b) < 0m) { return On; } if (a.Y > b.Y) { var tmp = new Vector2(a.X, a.Y); a = new Vector2(b.X, b.Y); b = new Vector2(tmp.X, tmp.Y); } if (a.Y < 0m && 0m <= b.Y && 0m < Vector2.CrossProduct(a, b)) { isContain = !isContain; } } return isContain ? Contain : Other; } } }
using System; namespace CGL_3_C_Polygon_Point_Containment { class Program { static void Main(string[] args) { int inputCount = int.Parse(Console.ReadLine()); Vector2[] polygon = new Vector2[inputCount]; for (int lp = 0; lp < inputCount; lp++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); polygon[lp] = new Vector2(input[0], input[1]); } inputCount = int.Parse(Console.ReadLine()); for (int lp = 0; lp < inputCount; lp++) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector2 point = new Vector2(input[0], input[1]); var result = Vector2.Contains(polygon, point); Console.WriteLine(result); } } } class Vector2 { public Vector2(decimal x, decimal y) { this.X = x; this.Y = y; } public decimal X { private set; get; } public decimal Y { private set; get; } public static Vector2 operator -(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X - ob2.X, ob1.Y - ob2.Y); } public static decimal DotProduct(Vector2 line1, Vector2 line2) { return line1.X * line2.X + line1.Y * line2.Y; } public static decimal CrossProduct(Vector2 line1, Vector2 line2) { return line1.X * line2.Y - line1.Y * line2.X; } public const int Contain = 2; public const int On = 1; public const int Other = 0; public static int Contains(Vector2[] polygon, Vector2 point) { int length = polygon.Length; bool isContain = false; for (int lp = 0; lp < length; lp++) { Vector2 a = polygon[lp] - point; Vector2 b = polygon[(lp + 1) % length] - point; if (Math.Abs(Vector2.CrossProduct(a, b)) == 0m && Vector2.DotProduct(a, b) <= 0m) { return On; } if (a.Y > b.Y) { var tmp = new Vector2(a.X, a.Y); a = new Vector2(b.X, b.Y); b = new Vector2(tmp.X, tmp.Y); } if (a.Y < 0m && 0m <= b.Y && 0m < Vector2.CrossProduct(a, b)) { isContain = !isContain; } } return isContain ? Contain : Other; } } }
[["-", 8, 196, 0, 57, 15, 16, 12, 16, 17, 18], ["+", 8, 196, 0, 57, 15, 16, 12, 16, 17, 19]]
8
561
2
using System; using System.Collections.Generic; // using System.Linq; // using Vector = CGL.Point; // using Line = CGL.Segment; // using Polygon = System.Collections.Generic.List<CGL.Point>; namespace CGL { class Program { static void Main(string[] args) { var k = scan; var C = new Circle(new Point(k[0], k[1]), k[2]); var q = scan[0]; for (int i = 0; i < q; i++) { var t = scan; var P = new Point[2]; for (int j = 0; j < 2; j++) { P[j] = new Point(t[j * 2], t[j * 2 + 1]); } var s1 = new Segment(P[0], P[1]); var p = C.GetCrossPoints(s1); if (p.Key < p.Value) { Console.WriteLine("{0} {1} {2} {3}", p.Key.x, p.Key.y, p.Value.x, p.Value.y); } else { Console.WriteLine("{0} {1} {2} {3}", p.Value.x, p.Value.y, p.Key.x, p.Key.y); } } } static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } } struct Point { static readonly double EPS = 1E-10; public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(Point a, double k) { return new Point(k * a.x, k * a.y); } public static Point operator /(Point a, double k) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } } struct Segment { public Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + b * r; } public Point Reflection(Point p) { return p + (Projection(p) - p) * 2; } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public bool Intersect(Segment s) { return (p2 - p1).CCW(s.p1 - p1) * (p2 - p1).CCW(s.p2 - p1) <= 0 && (s.p2 - s.p1).CCW(p1 - s.p1) * (s.p2 - s.p1).CCW(p2 - s.p1) <= 0; } public double GetDistance(Segment s) { if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } public Point GetCrossPoint(Segment s) { Point b = s.p2 - s.p1; double d1 = Math.Abs(b.Cross(p1 - s.p1)); double d2 = Math.Abs(b.Cross(p2 - s.p1)); double t = d1 / (d1 + d2); return p1 + (p2 - p1) * t; } } struct Circle { public Point c; double r; public Circle(Point c, double r) { this.c = c; this.r = r; } public bool Intersect(Segment l) { return l.GetD_LP(c) <= r; } public KeyValuePair<Point, Point> GetCrossPoints(Segment l) { Point pr = l.Projection(c); Point e = (l.p2 - l.p1) / (l.p2 - l.p1).Abs; double b = Math.Sqrt(r * r - (pr - c).Norm); return new KeyValuePair<Point, Point>(pr + e * b, pr - e * b); } } }
using System; using System.Collections.Generic; // using System.Linq; // using Vector = CGL.Point; // using Line = CGL.Segment; // using Polygon = System.Collections.Generic.List<CGL.Point>; namespace CGL { class Program { static void Main(string[] args) { var k = scan; var C = new Circle(new Point(k[0], k[1]), k[2]); var q = scan[0]; for (int i = 0; i < q; i++) { var t = scan; var P = new Point[2]; for (int j = 0; j < 2; j++) { P[j] = new Point(t[j * 2], t[j * 2 + 1]); } var s1 = new Segment(P[0], P[1]); var p = C.GetCrossPoints(s1); if (p.Key < p.Value) { Console.WriteLine("{0:F7} {1:F7} {2:F7} {3:F7}", p.Key.x, p.Key.y, p.Value.x, p.Value.y); } else { Console.WriteLine("{0:F7} {1:F7} {2:F7} {3:F7}", p.Value.x, p.Value.y, p.Key.x, p.Key.y); } } } static int[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } } } struct Point { static readonly double EPS = 1E-10; public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator *(Point a, double k) { return new Point(k * a.x, k * a.y); } public static Point operator /(Point a, double k) { return new Point(a.x / k, a.y / k); } public double Abs { get { return Math.Sqrt(Norm); } } public double Norm { get { return x * x + y * y; } } public static bool operator<(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; } public static bool operator>(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; } public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; } public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; } public override bool Equals(object p) { return Math.Abs(x - ((Point)p).x) < EPS && Math.Abs(y - ((Point)p).y) < EPS; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } public double Dot(Point p) { return x * p.x + y * p.y; } public double Cross(Point p) { return x * p.y - y * p.x; } public double GetDistance(Point p) { return (this - p).Abs; } public int CCW(Point b) { if (Cross(b) > EPS) return 1; if (Cross(b) < -EPS) return -1; if (Dot(b) < -EPS) return 2; if (Norm < b.Norm) return -2; return 0; } } struct Segment { public Point p1, p2; public Segment(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public Point Projection(Point p) { Point b = p2 - p1; double r = b.Dot(p - p1) / b.Norm; return p1 + b * r; } public Point Reflection(Point p) { return p + (Projection(p) - p) * 2; } public double GetD_LP(Point p) { return Math.Abs((p - p1).Cross(p2 - p1) / (p2 - p1).Abs); } public double GetD_SP(Point p) { if ((p - p1).Dot(p2 - p1) < 0) return (p - p1).Abs; if ((p - p2).Dot(p1 - p2) < 0) return (p - p2).Abs; return GetD_LP(p); } public bool Intersect(Segment s) { return (p2 - p1).CCW(s.p1 - p1) * (p2 - p1).CCW(s.p2 - p1) <= 0 && (s.p2 - s.p1).CCW(p1 - s.p1) * (s.p2 - s.p1).CCW(p2 - s.p1) <= 0; } public double GetDistance(Segment s) { if (this.Intersect(s)) return 0; return Math.Min(Math.Min(GetD_SP(s.p1), GetD_SP(s.p2)), Math.Min(s.GetD_SP(p1), s.GetD_SP(p2))); } public Point GetCrossPoint(Segment s) { Point b = s.p2 - s.p1; double d1 = Math.Abs(b.Cross(p1 - s.p1)); double d2 = Math.Abs(b.Cross(p2 - s.p1)); double t = d1 / (d1 + d2); return p1 + (p2 - p1) * t; } } struct Circle { public Point c; double r; public Circle(Point c, double r) { this.c = c; this.r = r; } public bool Intersect(Segment l) { return l.GetD_LP(c) <= r; } public KeyValuePair<Point, Point> GetCrossPoints(Segment l) { Point pr = l.Projection(c); Point e = (l.p2 - l.p1) / (l.p2 - l.p1).Abs; double b = Math.Sqrt(r * r - (pr - c).Norm); return new KeyValuePair<Point, Point>(pr + e * b, pr - e * b); } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
1,430
4
using System; using System.Text; namespace CGL_1_C_Counter_Clockwise { class Program { static void Main(string[] args) { int[] inputNum = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int inputCount = int.Parse(Console.ReadLine()); StringBuilder output = new StringBuilder(inputCount * 18); Vector2 point1 = new Vector2(inputNum[0], inputNum[1]); Vector2 point2 = new Vector2(inputNum[2], inputNum[3]); for (int lp = 0; lp < inputCount; lp++) { inputNum = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector2 point3 = new Vector2(inputNum[0], inputNum[1]); int result = Vector2.PointState(point1, point2, point3); switch (result) { case Vector2.Counter_Clockwise: output.AppendLine("COUNTER_CLOCKWISE"); break; case Vector2.Clockwise: output.AppendLine("CLOCKWISE"); break; case Vector2.OnlineBack: output.AppendLine("ONLINE_BACK"); break; case Vector2.OnlineFront: output.AppendLine("ON_SEGMENT"); break; case Vector2.OnSegment: output.AppendLine("ONLINE_FRONT"); break; } } Console.Write(output); } } class Vector2 { public Vector2(decimal x, decimal y) { this.X = x; this.Y = y; } public decimal X { private set; get; } public decimal Y { private set; get; } public static Vector2 operator +(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X + ob2.X, ob1.Y + ob2.Y); } public static Vector2 operator -(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X - ob2.X, ob1.Y - ob2.Y); } public static Vector2 operator *(Vector2 ob, decimal scalar) { return new Vector2(ob.X * scalar, ob.Y * scalar); } public decimal Length() { return X * X + Y * Y; } public static decimal DotProduct(Vector2 ob1, Vector2 ob2) { return ob1.X * ob2.X + ob1.Y * ob2.Y; } public static decimal CrossProduct(Vector2 ob1, Vector2 ob2) { return ob1.X * ob2.Y - ob1.Y * ob2.X; } public const int Counter_Clockwise = 1; public const int Clockwise = -1; public const int OnlineBack = 2; public const int OnlineFront = -2; public const int OnSegment = 0; public static int PointState(Vector2 point1, Vector2 point2, Vector2 point3) { Vector2 line1 = point2 - point1; Vector2 line2 = point3 - point1; decimal crossProduct = CrossProduct(line1, line2); if (crossProduct > 0m) { return Counter_Clockwise; } else if (crossProduct < 0m) { return Clockwise; } else if (DotProduct(line1, line2) < 0m) { return OnlineBack; } else if (line1.Length() < line2.Length()) { return OnlineFront; } return OnSegment; } } }
using System; using System.Text; namespace CGL_1_C_Counter_Clockwise { class Program { static void Main(string[] args) { int[] inputNum = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int inputCount = int.Parse(Console.ReadLine()); StringBuilder output = new StringBuilder(inputCount * 18); Vector2 point1 = new Vector2(inputNum[0], inputNum[1]); Vector2 point2 = new Vector2(inputNum[2], inputNum[3]); for (int lp = 0; lp < inputCount; lp++) { inputNum = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); Vector2 point3 = new Vector2(inputNum[0], inputNum[1]); int result = Vector2.PointState(point1, point2, point3); switch (result) { case Vector2.Counter_Clockwise: output.AppendLine("COUNTER_CLOCKWISE"); break; case Vector2.Clockwise: output.AppendLine("CLOCKWISE"); break; case Vector2.OnlineBack: output.AppendLine("ONLINE_BACK"); break; case Vector2.OnlineFront: output.AppendLine("ONLINE_FRONT"); break; case Vector2.OnSegment: output.AppendLine("ON_SEGMENT"); break; } } Console.Write(output); } } class Vector2 { public Vector2(decimal x, decimal y) { this.X = x; this.Y = y; } public decimal X { private set; get; } public decimal Y { private set; get; } public static Vector2 operator +(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X + ob2.X, ob1.Y + ob2.Y); } public static Vector2 operator -(Vector2 ob1, Vector2 ob2) { return new Vector2(ob1.X - ob2.X, ob1.Y - ob2.Y); } public static Vector2 operator *(Vector2 ob, decimal scalar) { return new Vector2(ob.X * scalar, ob.Y * scalar); } public decimal Length() { return X * X + Y * Y; } public static decimal DotProduct(Vector2 ob1, Vector2 ob2) { return ob1.X * ob2.X + ob1.Y * ob2.Y; } public static decimal CrossProduct(Vector2 ob1, Vector2 ob2) { return ob1.X * ob2.Y - ob1.Y * ob2.X; } public const int Counter_Clockwise = 1; public const int Clockwise = -1; public const int OnlineBack = 2; public const int OnlineFront = -2; public const int OnSegment = 0; public static int PointState(Vector2 point1, Vector2 point2, Vector2 point3) { Vector2 line1 = point2 - point1; Vector2 line2 = point3 - point1; decimal crossProduct = CrossProduct(line1, line2); if (crossProduct > 0m) { return Counter_Clockwise; } else if (crossProduct < 0m) { return Clockwise; } else if (DotProduct(line1, line2) < 0m) { return OnlineBack; } else if (line1.Length() < line2.Length()) { return OnlineFront; } return OnSegment; } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222], ["+", 0, 213, 3, 4, 0, 28, 0, 5, 0, 222]]
8
641
4
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; class Program { static int n; static int m; static int[] cs; static int[] minCounts; static void Main(string[] args) { string[] first = Console.ReadLine().Split(); n = int.Parse(first[0]); m = int.Parse(first[1]); cs = Console.ReadLine().Split().Select(str => int.Parse(str)).ToArray(); minCounts = new int[n]; // コイン1枚で実現できる金額には枚数1を入れておく for (int ci = 0; ci < m; ci++) { minCounts[cs[ci]] = 1; } // 1から順番に辿る for (int i = 1; i <= n; i++) { minCounts[i] = GetMinCounts(i); } System.Console.WriteLine(minCounts[n]); } // Kを実現する最小コイン枚数を返す static int GetMinCounts(int k) { // 記録済み if (minCounts[k] > 0) return minCounts[k]; // 頭と後ろからの組み合わせで最小値を計算 int min = k; for (int j = 1; j < k; j++) { if (k - j < j) return min; int current = minCounts[k - j] + minCounts[j]; if (current < min) min = current; } return min; } }
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; class Program { static int n; static int m; static int[] cs; static int[] minCounts; static void Main(string[] args) { string[] nm = Console.ReadLine().Split(); n = int.Parse(nm[0]); m = int.Parse(nm[1]); cs = Console.ReadLine().Split().Select(str => int.Parse(str)).ToArray(); minCounts = new int[50001]; // コイン1枚で実現できる金額には枚数1を入れておく for (int ci = 0; ci < m; ci++) { minCounts[cs[ci]] = 1; } // 1から順番に辿る for (int i = 1; i <= n; i++) { minCounts[i] = GetMinCounts(i); } System.Console.WriteLine(minCounts[n]); } // Kを実現する最小コイン枚数を返す static int GetMinCounts(int k) { // 記録済み if (minCounts[k] > 0) return minCounts[k]; // 頭と後ろからの組み合わせで最小値を計算 int min = k; for (int j = 1; j < k; j++) { if (k - j < j) return min; int current = minCounts[k - j] + minCounts[j]; if (current < min) min = current; } return min; } }
[["-", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["+", 8, 196, 0, 197, 0, 198, 0, 200, 141, 22], ["-", 12, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["+", 12, 213, 3, 4, 0, 28, 0, 204, 205, 22], ["-", 0, 11, 12, 227, 39, 224, 225, 226, 0, 22], ["+", 0, 11, 12, 227, 39, 224, 225, 226, 0, 203]]
8
288
8
using System; class Program { private static int[] a = new int[50]; static void Main() { string[] s = Console.ReadLine().Split(' '); int n = int.Parse(s[0]); int m = int.Parse(s[1]); s = Console.ReadLine().Split(' '); int[] g = new int[m]; for (int i = 0; i < m; i++) g[i] = int.Parse(s[i]); Console.WriteLine(Coin(n, g, m)); } static int Coin(int n, int[] g, int m) { int g_min = 10000; //額面最大値 if (a[n] != 0) return a[n]; if (n == 0) return 0; for (int i = 0; i < m; i++) { if (n - g[i] >= 0) { int r = Coin(n - g[i], g, m) + 1; if (g_min > r) g_min = r; } } a[n] = g_min; return a[n]; } }
using System; class Program { // 最小値保持 private static int[] a = new int[50001]; static void Main() { string[] s = Console.ReadLine().Split(' '); int n = int.Parse(s[0]); int m = int.Parse(s[1]); s = Console.ReadLine().Split(' '); int[] g = new int[m]; for (int i = 0; i < m; i++) g[i] = int.Parse(s[i]); Console.WriteLine(Coin(n, g, m)); } static int Coin(int n, int[] g, int m) { int g_min = 10000; //額面最大値 if (a[n] != 0) return a[n]; if (n == 0) return 0; for (int i = 0; i < m; i++) { if (n - g[i] >= 0) { int r = Coin(n - g[i], g, m) + 1; if (g_min > r) g_min = r; } } a[n] = g_min; return a[n]; } }
[["-", 0, 212, 0, 227, 39, 224, 225, 226, 0, 203], ["+", 0, 212, 0, 227, 39, 224, 225, 226, 0, 203]]
8
258
2
using System; using System.Linq; using System.Collections.Generic; using System.IO; public class Program { public static void Main() { string filename = ""; TextReader reader; if (filename.Count() == 0) reader = Console.In; else reader = new StreamReader(filename); var s = reader.ReadLine(); var t = reader.ReadLine(); var dp = new int[s.Count() + 1, t.Count() + 1]; for (int i = 1; i < t.Count() + 1; i++) dp[0, i] = i; for (int j = 1; j < s.Count() + 1; j++) dp[j, 0] = j; for (int i = 1; i < dp.GetLength(0); i++) { for (int j = 1; j < dp.GetLength(1); j++) { //?????????????????????????????¨ if (s[i - 1] == t[j - 1]) dp[i, j] = dp[i - 1, j - 1]; else { //?????????????????????????????????????????????????????????????????????????°?????????? dp[i, j] = Math.Min(dp[i - 1, j - 1] + 1, Math.Min(dp[i - 1, j] + 1, dp[i, j - 1])); } } } Console.WriteLine(dp[s.Count(), t.Count()]); } }
using System; using System.Linq; using System.Collections.Generic; using System.IO; public class Program { public static void Main() { string filename = ""; TextReader reader; if (filename.Count() == 0) reader = Console.In; else reader = new StreamReader(filename); var s = reader.ReadLine(); var t = reader.ReadLine(); var dp = new int[s.Count() + 1, t.Count() + 1]; for (int i = 1; i < t.Count() + 1; i++) dp[0, i] = i; for (int j = 1; j < s.Count() + 1; j++) dp[j, 0] = j; for (int i = 1; i < dp.GetLength(0); i++) { for (int j = 1; j < dp.GetLength(1); j++) { //?????????????????????????????¨ if (s[i - 1] == t[j - 1]) dp[i, j] = dp[i - 1, j - 1]; else { //?????????????????????????????????????????????????????????????????????????°?????????? dp[i, j] = Math.Min(dp[i - 1, j - 1] + 1, Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1)); } } } Console.WriteLine(dp[s.Count(), t.Count()]); } }
[["+", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["+", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203]]
8
317
2
using System; using System.Text; using System.Collections.Generic; using System.Linq; class Solve { public Solve() {} StringBuilder sb; public static int Main() { new Solve().Run(); return 0; } void Run() { sb = new StringBuilder(); Calc(); Console.Write(sb.ToString()); } void Calc() { string[] str = Console.ReadLine().Split(' '); int H = int.Parse(str[0]); int W = int.Parse(str[1]); bool[,] b = new bool[H, W]; for (int i = 0; i < H; i++) { str = Console.ReadLine().Split(' '); for (int j = 0; j < W; j++) { b[i, j] = str[j] == "1"; } } int[,] L = new int[H, W]; for (int i = 0; i < H; i++) { int p = 0; for (int j = 0; j < W; j++) { if (b[i, j]) { p = j + 1; } L[i, j] = p; } } int[,] U = new int[H, W]; for (int i = 0; i < W; i++) { int p = 0; for (int j = 0; j < H; j++) { if (b[j, i]) { p = j + 1; } U[j, i] = p; } } int[,] DP = new int[H, W]; int max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (!b[i, j]) { if (i == 0 || j == 0) { DP[i, j] = 1; } else { int min = DP[i - 1, j - 1] + 1; min = Math.Min(min, j - L[i, j] + 1); min = Math.Min(min, i - U[i, j] + 1); DP[i, j] = min; } max = Math.Max(max, DP[i, j]); } } } sb.Append(max + "\n"); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; class Solve { public Solve() {} StringBuilder sb; public static int Main() { new Solve().Run(); return 0; } void Run() { sb = new StringBuilder(); Calc(); Console.Write(sb.ToString()); } void Calc() { string[] str = Console.ReadLine().Split(' '); int H = int.Parse(str[0]); int W = int.Parse(str[1]); bool[,] b = new bool[H, W]; for (int i = 0; i < H; i++) { str = Console.ReadLine().Split(' '); for (int j = 0; j < W; j++) { b[i, j] = str[j] == "1"; } } int[,] L = new int[H, W]; for (int i = 0; i < H; i++) { int p = 0; for (int j = 0; j < W; j++) { if (b[i, j]) { p = j + 1; } L[i, j] = p; } } int[,] U = new int[H, W]; for (int i = 0; i < W; i++) { int p = 0; for (int j = 0; j < H; j++) { if (b[j, i]) { p = j + 1; } U[j, i] = p; } } int[,] DP = new int[H, W]; int max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (!b[i, j]) { if (i == 0 || j == 0) { DP[i, j] = 1; } else { int min = DP[i - 1, j - 1] + 1; min = Math.Min(min, j - L[i, j] + 1); min = Math.Min(min, i - U[i, j] + 1); DP[i, j] = min; } max = Math.Max(max, DP[i, j]); } } } sb.Append(max * max + "\n"); } }
[["+", 3, 4, 0, 28, 0, 16, 31, 16, 17, 48], ["+", 3, 4, 0, 28, 0, 16, 31, 16, 12, 22]]
8
541
2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { struct Rectangle { internal int height, pos; } class Program { static void Main(string[] args) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int H = input[0]; int W = input[1]; int[,] dp = new int[H, W]; for (int i = 0; i < H; i++) { input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); for (int j = 0; j < W; j++) { if (input[j] == 1) dp[i, j] = 0; else dp[i, j] = i > 0 ? dp[i - 1, j] + 1 : 1; } } Stack<Rectangle> stk = new Stack<Rectangle>(); int maxArea = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { Rectangle rect; rect.height = dp[i, j]; rect.pos = j; if (stk.Count == 0) { stk.Push(rect); } else { if (stk.Peek().height < rect.height) { stk.Push(rect); } else if (stk.Peek().height > rect.height) { int target = j; while (stk.Count > 0 && stk.Peek().height >= rect.height) { Rectangle pre = stk.Pop(); int area = pre.height * (j - pre.pos); maxArea = Math.Max(maxArea, area); target = pre.pos; } rect.pos = target; stk.Push(rect); } } } } Console.WriteLine(maxArea); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { struct Rectangle { internal int height, pos; } class Program { static void Main(string[] args) { int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int H = input[0]; int W = input[1]; int[,] dp = new int[H, W + 1]; for (int i = 0; i < H; i++) { input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); for (int j = 0; j < W; j++) { if (input[j] == 1) dp[i, j] = 0; else dp[i, j] = i > 0 ? dp[i - 1, j] + 1 : 1; } } Stack<Rectangle> stk = new Stack<Rectangle>(); int maxArea = 0; for (int i = 0; i < H; i++) { for (int j = 0; j <= W; j++) { Rectangle rect; rect.height = dp[i, j]; rect.pos = j; if (stk.Count == 0) { stk.Push(rect); } else { if (stk.Peek().height < rect.height) { stk.Push(rect); } else if (stk.Peek().height > rect.height) { int target = j; while (stk.Count > 0 && stk.Peek().height >= rect.height) { Rectangle pre = stk.Pop(); int area = pre.height * (j - pre.pos); maxArea = Math.Max(maxArea, area); target = pre.pos; } rect.pos = target; stk.Push(rect); } } } } Console.WriteLine(maxArea); } } }
[["+", 0, 227, 39, 224, 225, 226, 0, 16, 17, 72], ["+", 0, 227, 39, 224, 225, 226, 0, 16, 12, 203], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
8
424
4
using System; using System.Text; using System.Collections.Generic; using System.Linq; class Solve { public Solve() {} StringBuilder sb; public static int Main() { new Solve().Run(); return 0; } void Run() { sb = new StringBuilder(); Calc(); Console.Write(sb.ToString()); } void Calc() { string[] str = Console.ReadLine().Split(' '); int H = int.Parse(str[0]); int W = int.Parse(str[1]); bool[,] b = new bool[H, W]; for (int i = 0; i < H; i++) { str = Console.ReadLine().Split(' '); for (int j = 0; j < W; j++) { b[i, j] = str[j] == "1"; } } int[,] U = new int[H, W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (b[i, j]) { U[i, j] = 0; } else { U[i, j] = i == 0 ? 1 : U[i - 1, j] + 1; } } } int max = 0; for (int i = 0; i < H; i++) { int[] stack = new int[W + 1]; int[] point = new int[W + 1]; int p = 1; for (int j = 0; j < W; j++) { int h = U[i, j]; while (p > 1 && stack[p - 1] >= h) { max = Math.Max(max, (j - point[p - 2]) * (stack[p - 1])); p--; } stack[p] = h; point[p] = j + 1; p++; } { for (int j = 1; j < p; j++) { max = Math.Max(max, (point[j] - point[j - 1]) * (stack[j])); } } } sb.Append(max + "\n"); } }
using System; using System.Text; using System.Collections.Generic; using System.Linq; class Solve { public Solve() {} StringBuilder sb; public static int Main() { new Solve().Run(); return 0; } void Run() { sb = new StringBuilder(); Calc(); Console.Write(sb.ToString()); } void Calc() { string[] str = Console.ReadLine().Split(' '); int H = int.Parse(str[0]); int W = int.Parse(str[1]); bool[,] b = new bool[H, W]; for (int i = 0; i < H; i++) { str = Console.ReadLine().Split(' '); for (int j = 0; j < W; j++) { b[i, j] = str[j] == "1"; } } int[,] U = new int[H, W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (b[i, j]) { U[i, j] = 0; } else { U[i, j] = i == 0 ? 1 : U[i - 1, j] + 1; } } } int max = 0; for (int i = 0; i < H; i++) { int[] stack = new int[W + 1]; int[] point = new int[W + 1]; int p = 1; for (int j = 0; j < W; j++) { int h = U[i, j]; while (p > 1 && stack[p - 1] >= h) { max = Math.Max(max, (j - point[p - 2]) * (stack[p - 1])); p--; } stack[p] = h; point[p] = j + 1; p++; } { for (int j = 1; j < p; j++) { max = Math.Max(max, (point[p - 1] - point[j - 1]) * (stack[j])); } } } sb.Append(max + "\n"); } }
[["-", 0, 16, 31, 204, 206, 207, 0, 28, 0, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 31, 22], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 17, 33], ["+", 31, 204, 206, 207, 0, 28, 0, 16, 12, 203]]
8
506
4
using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; // 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 N = cin.nextint; int K = cin.nextint; WriteLine(powmod(N, K, mod)); } // a^b(mod p) solution works in O(logb). long powmod(long a, long b, long p) { if (b == 0) return 1; if (b % 2 == 0) { long d = powmod(a, b / 2, p); return d * d % p; } else { return a % p * powmod(a, b - 1, p) % p; } } } 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.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 N = cin.nextint; int K = cin.nextint; WriteLine(powmod(K, N, mod)); } // a^b(mod p) solution works in O(logb). long powmod(long a, long b, long p) { if (b == 0) return 1; if (b % 2 == 0) { long d = powmod(a, b / 2, p); return d * d % p; } else { return a % p * powmod(a, b - 1, p) % p; } } } 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, 28, 0, 213, 3, 4, 0, 28, 0, 22], ["-", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["+", 3, 4, 0, 28, 0, 213, 3, 4, 0, 21], ["+", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22]]
8
934
4
using System; using System.Linq; namespace ACTest { class AOJDSL2A { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int num = int.Parse(input[0]); int query = int.Parse(input[1]); int[] com = new int[query]; int[] x = new int[query]; int[] y = new int[query]; for (int i = 0; i < query; i++) { input = Console.ReadLine().Split(' '); com[i] = int.Parse(input[0]); x[i] = int.Parse(input[1]); y[i] = int.Parse(input[2]); } // make set int tree = 1; while (tree < num + 1) { tree *= 2; } int[] dat = new int[2 * tree - 1]; dat = Enumerable.Repeat<int>(2147483647, 2 * tree - 1).ToArray(); for (int i = 0; i < query; i++) { if (com[i] == 0) { update(tree, x[i], y[i], dat); } else if (com[i] == 1) { Console.WriteLine(findQuery(x[i], y[i] + 1, 0, 0, num + 1, dat)); } } // while (true) ; } /// <summary> /// ?????¨??? com0 /// </summary> /// <param /// name="n">?????????????????°num????????§??????????°????2????????????</param> /// <param name="k">k?????????</param> /// <param name="a">a????????????</param> /// <param name="dat">?????¨2?????¨</param> static void update(int n, int k, int a, int[] dat) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = Math.Min(dat[2 * k + 1], dat[2 * k + 2]); } } static int findQuery(int a, int b, int k, int l, int r, int[] dat) { if (r <= a || b <= l) { return 2147483647; } if (a <= l && r <= b) { return dat[k]; } else { int vl = findQuery(a, b, 2 * k + 1, l, (l + r) / 2, dat); int vr = findQuery(a, b, 2 * k + 2, (l + r) / 2, r, dat); return Math.Min(vl, vr); } } } }
using System; using System.Linq; namespace ACTest { class AOJDSL2A { static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); int num = int.Parse(input[0]); int query = int.Parse(input[1]); int[] com = new int[query]; int[] x = new int[query]; int[] y = new int[query]; for (int i = 0; i < query; i++) { input = Console.ReadLine().Split(' '); com[i] = int.Parse(input[0]); x[i] = int.Parse(input[1]); y[i] = int.Parse(input[2]); } // make set int tree = 1; while (tree < num + 1) { tree *= 2; } int[] dat = new int[2 * tree - 1]; dat = Enumerable.Repeat<int>(2147483647, 2 * tree - 1).ToArray(); for (int i = 0; i < query; i++) { if (com[i] == 0) { update(tree, x[i], y[i], dat); } else if (com[i] == 1) { Console.WriteLine(findQuery(x[i], y[i] + 1, 0, 0, tree, dat)); } } // while (true) ; } /// <summary> /// ?????¨??? com0 /// </summary> /// <param /// name="n">?????????????????°num????????§??????????°????2????????????</param> /// <param name="k">k?????????</param> /// <param name="a">a????????????</param> /// <param name="dat">?????¨2?????¨</param> static void update(int n, int k, int a, int[] dat) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = Math.Min(dat[2 * k + 1], dat[2 * k + 2]); } } static int findQuery(int a, int b, int k, int l, int r, int[] dat) { if (r <= a || b <= l) { return 2147483647; } if (a <= l && r <= b) { return dat[k]; } else { int vl = findQuery(a, b, 2 * k + 1, l, (l + r) / 2, dat); int vr = findQuery(a, b, 2 * k + 2, (l + r) / 2, r, dat); return Math.Min(vl, vr); } } } }
[["-", 0, 213, 3, 4, 0, 28, 0, 16, 31, 22], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 17, 72], ["-", 0, 213, 3, 4, 0, 28, 0, 16, 12, 203], ["+", 0, 28, 0, 213, 3, 4, 0, 28, 0, 22]]
8
539
4
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Competitive { internal class Solution { public int N, Q; public void Run() { { var line = Input.ReadIntArray(); N = line[0]; Q = line[1]; } var S = new SegTree(N); for (int i = 0; i < Q; i++) { var line = Input.ReadIntArray(); int C = line[0]; if (C == 0) { int id = line[1] - 1; int a = line[2]; if (C == 0) S.Update(id, a); } else { int begin = line[1] - 1; int last = line[2] - 1; Console.WriteLine(S.Query(begin, last + 1)); } } } } // libs ---------- internal class SegTree { public int N; public int NCells; public int[] Data; public int[] KL; // 範囲のbegin public int[] KR; // 範囲のend(そのセルは含まない) public const int Dummy = 2147483647; // 2^31-1 public SegTree(int _n) { Init(_n); } private int GetN(int _n) { int n = 1; while (n < _n) n *= 2; return n; } private void Init(int _n, int initialValue = 0) { N = GetN(_n); NCells = 2 * N - 1; Data = new int[NCells]; KL = new int[NCells]; KR = new int[NCells]; // KL, KRの初期化 for (int k = 0; k < NCells; k++) { if (IsRoot(k)) { KL[k] = 0; KR[k] = N; } else { if (IsChildL(k)) { KL[k] = KL[Parent(k)]; KR[k] = (KL[Parent(k)] + KR[Parent(k)]) / 2; } else { KL[k] = (KL[Parent(k)] + KR[Parent(k)]) / 2; KR[k] = KR[Parent(k)]; } } } // 値の初期化 for (int k = 0; k < NCells; k++) Data[k] = initialValue; } // i番目のみの値を保持するセル public int Pos(int i) { return i + N - 1; } // 親セル・子セル public int Parent(int k) { return (k - 1) / 2; } public int ChildL(int k) { return k * 2 + 1; } public int ChildR(int k) { return k * 2 + 2; } // 根かどうか public bool IsRoot(int k) { return k == 0; } // 左側の子かどうか public bool IsChildL(int k) { if (IsRoot(k)) return false; return ChildL(Parent(k)) == k; } // Update - bottom up public void Update(int i, int a) { int k = Pos(i); // Update Logic Data[k] = a; while (!IsRoot(k)) { // Update Logic k = Parent(k); Data[k] = Data[ChildL(k)] + Data[ChildR(k)]; } } // Query - top down public int Query(int il, int ir) { return query(il, ir, 0); } private int query(int il, int ir, int k) { int kl = KL[k]; int kr = KR[k]; // [Query Logic] if (kr <= il || ir <= kl) return 0; if (il <= kl && kr <= ir) return Data[k]; else { int vl = query(il, ir, ChildL(k)); int vr = query(il, ir, ChildR(k)); return vl + vr; } } } // common ---------- internal static class Input { public static string ReadString() { string line = Console.ReadLine(); return line; } public static int ReadInt() { string line = Console.ReadLine(); return int.Parse(line); } public static int[] ReadIntArray() { string line = Console.ReadLine(); return line.Split(' ').Select(int.Parse).ToArray(); } public static long ReadLong() { string line = Console.ReadLine(); return long.Parse(line); } public static long[] ReadLongArray() { string line = Console.ReadLine(); return line.Split(' ').Select(long.Parse).ToArray(); } public static double[] ReadDoubleArray() { string line = Console.ReadLine(); return line.Split(' ').Select(double.Parse).ToArray(); } } internal class Program { public static void Main(string[] args) { var s = new Solution(); s.Run(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Competitive { internal class Solution { public int N, Q; public void Run() { { var line = Input.ReadIntArray(); N = line[0]; Q = line[1]; } var S = new SegTree(N); for (int i = 0; i < Q; i++) { var line = Input.ReadIntArray(); int C = line[0]; if (C == 0) { int id = line[1] - 1; int a = line[2]; if (C == 0) S.Update(id, a); } else { int begin = line[1] - 1; int last = line[2] - 1; Console.WriteLine(S.Query(begin, last + 1)); } } } } // libs ---------- internal class SegTree { public int N; public int NCells; public int[] Data; public int[] KL; // 範囲のbegin public int[] KR; // 範囲のend(そのセルは含まない) public const int Dummy = 2147483647; // 2^31-1 public SegTree(int _n) { Init(_n); } private int GetN(int _n) { int n = 1; while (n < _n) n *= 2; return n; } private void Init(int _n, int initialValue = 0) { N = GetN(_n); NCells = 2 * N - 1; Data = new int[NCells]; KL = new int[NCells]; KR = new int[NCells]; // KL, KRの初期化 for (int k = 0; k < NCells; k++) { if (IsRoot(k)) { KL[k] = 0; KR[k] = N; } else { if (IsChildL(k)) { KL[k] = KL[Parent(k)]; KR[k] = (KL[Parent(k)] + KR[Parent(k)]) / 2; } else { KL[k] = (KL[Parent(k)] + KR[Parent(k)]) / 2; KR[k] = KR[Parent(k)]; } } } // 値の初期化 for (int k = 0; k < NCells; k++) Data[k] = initialValue; } // i番目のみの値を保持するセル public int Pos(int i) { return i + N - 1; } // 親セル・子セル public int Parent(int k) { return (k - 1) / 2; } public int ChildL(int k) { return k * 2 + 1; } public int ChildR(int k) { return k * 2 + 2; } // 根かどうか public bool IsRoot(int k) { return k == 0; } // 左側の子かどうか public bool IsChildL(int k) { if (IsRoot(k)) return false; return ChildL(Parent(k)) == k; } // Update - bottom up public void Update(int i, int a) { int k = Pos(i); // Update Logic Data[k] += a; while (!IsRoot(k)) { // Update Logic k = Parent(k); Data[k] = Data[ChildL(k)] + Data[ChildR(k)]; } } // Query - top down public int Query(int il, int ir) { return query(il, ir, 0); } private int query(int il, int ir, int k) { int kl = KL[k]; int kr = KR[k]; // [Query Logic] if (kr <= il || ir <= kl) return 0; if (il <= kl && kr <= ir) return Data[k]; else { int vl = query(il, ir, ChildL(k)); int vr = query(il, ir, ChildR(k)); return vl + vr; } } } // common ---------- internal static class Input { public static string ReadString() { string line = Console.ReadLine(); return line; } public static int ReadInt() { string line = Console.ReadLine(); return int.Parse(line); } public static int[] ReadIntArray() { string line = Console.ReadLine(); return line.Split(' ').Select(int.Parse).ToArray(); } public static long ReadLong() { string line = Console.ReadLine(); return long.Parse(line); } public static long[] ReadLongArray() { string line = Console.ReadLine(); return line.Split(' ').Select(long.Parse).ToArray(); } public static double[] ReadDoubleArray() { string line = Console.ReadLine(); return line.Split(' ').Select(double.Parse).ToArray(); } } internal class Program { public static void Main(string[] args) { var s = new Solution(); s.Run(); } } }
[["-", 8, 196, 0, 1, 0, 11, 0, 202, 0, 32], ["+", 8, 196, 0, 1, 0, 11, 0, 202, 0, 107]]
8
1,009
2