Skip to content

Instantly share code, notes, and snippets.

@taross-f
Created January 6, 2017 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taross-f/39a2fa10727ae1e1461860148582096b to your computer and use it in GitHub Desktop.
Save taross-f/39a2fa10727ae1e1461860148582096b to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\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