Created
January 6, 2017 10:40
-
-
Save taross-f/39a2fa10727ae1e1461860148582096b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Web.dll</Reference> | |
<NuGetReference>Microsoft.Net.Http</NuGetReference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<NuGetReference>Sendgrid</NuGetReference> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>Newtonsoft.Json.Bson</Namespace> | |
<Namespace>Newtonsoft.Json.Converters</Namespace> | |
<Namespace>Newtonsoft.Json.Linq</Namespace> | |
<Namespace>Newtonsoft.Json.Schema</Namespace> | |
<Namespace>Newtonsoft.Json.Serialization</Namespace> | |
<Namespace>SendGrid.SmtpApi</Namespace> | |
<Namespace>System.IO</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>System.Web.Mail</Namespace> | |
</Query> | |
//using System; | |
//using System.Linq; | |
//using System.Collections.Generic; | |
public class BinarySearch | |
{ | |
public static void Main() | |
{ | |
Console.ReadLine(); | |
var s = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); | |
Console.ReadLine(); | |
var t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); | |
SearchWithHashSet(s, t); | |
//SearchWithBinarySearch(s, t); | |
} | |
private static void SearchWithHashSet(int[] s, int[] t) | |
{ | |
// Contains用にHashset生成 | |
var hashSet = new HashSet<int>(s); | |
var c = t.Count(x => hashSet.Contains(x)); | |
// AIZU ONLINEがmonoなのでParallel使えない。。。使えたらこっちもあり | |
//ThreadPool.SetMinThreads(32, 32); | |
//var c = t.AsParallel().Count(x => hashSet.Contains(x)); | |
Console.WriteLine(c); | |
} | |
private static void SearchWithBinarySearch(int[] s, int[] t) | |
{ | |
var c = t.Count(x => BinarySearchCore(s, x) > 0); | |
Console.WriteLine(c); | |
} | |
// 擬似コードをそのまま写してみました。 | |
private static int BinarySearchCore(int[] s, int key) | |
{ | |
var left = 0; | |
var right = s.Count(); | |
while (left < right) | |
{ | |
var mid = (left + right) / 2; | |
if (s[mid] == key) | |
return mid; | |
if (key < s[mid]) | |
right = mid; | |
else | |
left = mid + 1; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment