URAL 1196. History Exam (二分)
2015-03-12 17:37
281 查看
1196. History Exam
Time limit: 1.5 secondMemory limit: 64 MB
Professor of history decided to simplify the examination process. At the exam, every student should write a list of historic dates she knows (she should write the years only and, of course, must be
able to explain what event took place in this or that year). Professor has a list of dates that students must know. In order to decide upon the student's mark, Professor counts the number of dates in the student's list that are also present in his list. The
student gets her mark according to the number of coincidences.
Your task is to automatize this process. Write a program that would count the number of dates in the student's list that also occur in Professor's list.
Input
The first line contains the number N of dates in Professor's list, 1 ≤ N ≤ 15000. The following Nlines contain this list, one number per line. Each date is a positive integernot exceeding 109. Professor's list is sorted in non-descending order. The following line contains the number M of dates in the student's list, 1 ≤ M ≤ 106.
Then there is the list itself; it is unsorted. The dates here satisfy the same restriction. Both in Professor's and in the student's lists dates can appear more than once.
Output
Output the number of dates in the student's that are also contained in Professor's list.Sample
input | output |
---|---|
2 1054 1492 4 1492 65536 1492 100 | 2 |
解析:由于第一个有序的,所以我们遍历第二个序列的同时对第一个序列二分搜索答案。
PS:本题有个很诡异的现象:G++跑了1.5s+,但是VC++竟然才跑0.343s。。。貌似只有VC++才能过。
AC代码:
#include <cstdio> using namespace std; int a[15002]; int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk int n, m, ans, foo; while(scanf("%d", &n)==1){ ans = 0; for(int i=0; i<n; i++) scanf("%d", &a[i]); scanf("%d", &m); for(int i=0; i<m; i++){ scanf("%d", &foo); int l = 0, r = n - 1, m; if(foo < a[0] || foo > a[n-1]) continue; else if(foo == a[0] || foo == a[n-1]){ ans ++; continue; } while(l <= r){ m = (r - l) / 2 + l; if(a[m] == foo){ ans ++; break; } if(a[m] < foo) l = m + 1; else r = m - 1; } } printf("%d\n", ans); } return 0; }
相关文章推荐
- URAL 1196. History Exam (二分)
- [最小极差生成树 LCT || 二分答案 CDQ分治 并查集] Ural 2055 Urban Geography
- URAL 2052 . Physical Education (数位DP + 二分)
- URAL 1822. Hugo II's War 树的结构+二分
- URAL1917_Titan Ruins: Better late than killed_二分搜索
- bzoj 1196: [HNOI2006]公路修建问题 二分+并查集
- URAL 1822. Hugo II's War 树的结构+二分
- ural 1209. 1, 10, 100, 1000... 二分
- BZOJ 1196 HNOI2006 公路修建问题 二分答案+Kruskal
- URAL - 1066 Garland(二分)
- ural1486 二分+二维哈希
- URAL 1613. For Fans of Statistics 二分+stl
- URAL-1997 Those are not the droids you're looking for 二分匹配
- BZOJ 1196 HNOI 2006 公路修建问题 二分答案+并查集
- URAL-1998 The old Padawan 二分
- BZOJ1196 [HNOI2006]公路修建问题 【二分 + Kruskal】
- BZOJ-1196 公路修建问题 最小生成树Kruskal+(二分??)
- URAL 1998 The old Padawan 二分
- URAL 1153. Supercomputer 二分求根
- bzoj 1196 二分+生成树判定