Created
December 15, 2017 06:43
-
-
Save HinataYukari/54851028329b50db2900d1ef157537c1 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
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
#define rep(n, i) for(int i = 0; i < n; i++) | |
#define MAX(a, b) ((a) > (b) ? (a) : (b)) | |
#define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
void solve(void){ | |
int n, q; | |
scanf("%d\n", &n); | |
int s[n]; | |
rep(n, i) scanf("%d ", &s[i]); | |
scanf("\n"); | |
scanf("%d\n", &q); | |
int t[q]; | |
rep(q, i) scanf("%d ", &t[i]); | |
int bools[q]; | |
rep(q, i) bools[i] = binarysearch(t[i], s, n); | |
int sum = 0; | |
rep(q, i) sum += bools[i]; | |
printf("%d\n", sum); | |
} | |
int binarysearch(int a, int* xs, int len){ | |
if (len == 0) return 0; | |
if (len == 1) { | |
if (xs[0] == a) return 1; else return 0; | |
} | |
int mid = len / 2 + 1; | |
int p = xs[mid-1]; | |
if (p == a) return 1; | |
if (p > a) return binarysearch(a, &xs[0], mid - 1); | |
else return binarysearch(a, &xs[mid], len - mid); | |
} | |
int main(void){ | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment