1196. History Exam

Time limit: 1.5 second

Memory 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 integer
not 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 (二分)的更多相关文章

  1. URAL 2048 History 蔡勒公式

     HistoryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  2. 二分法&三分法

    ural History Exam    二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...

  3. Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】

    A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...

  4. UVa 111 - History Grading (by 最长公共子序列 )

     History Grading  Background Many problems in Computer Science involve maximizing some measure accor ...

  5. URAL 1873. GOV Chronicles

    唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. ...

  6. Codeforces Round #561 (Div. 2) C. A Tale of Two Lands

    链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...

  7. uva111动态规划之最长公共子序列

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=74662#problem/C     A B C D E C - Largest Rect ...

  8. UVA 111(LCS问题)

     History Grading  Background Many problems in Computer Science involve maximizing some measure accor ...

  9. UVA 111 (复习dp, 14.07.09)

     History Grading  Background Many problems in Computer Science involve maximizing some measure accor ...

随机推荐

  1. 327 Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  2. [转]asp.net MVC 常见安全问题及解决方案

    本文转自:http://www.cnblogs.com/Jessy/p/3539564.html asp.net MVC 常见安全问题及解决方案 一.CSRF (Cross-site request ...

  3. Mybatis的Dao向mapper传多个参数(三种解决方案)

    第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...

  4. FullCalendar日程设置

    顺序很重要!!!不然会报错,后面的文件会引用前面的一些东西. shili1:  http://blog.csdn.net/lizai22/article/details/53522523 shili ...

  5. Java&Xml教程(五)使用SAX方式解析XML文件

    Java SAX解析机制为我们提供了一系列的API来处理XML文件,SAX解析和DOM解析方式不太一样,它并不是將XML文件内容一次性全部加载,而是连续的部分加载. javax.xml.parsers ...

  6. 安卓socket 心跳和信鸽自定义提示音

    /** * 连接socket 和心跳 */ public class SocketService extends Service { private static addNewOrderInterfa ...

  7. CSS3利用box-shadow实现相框效果

    CSS3利用box-shadow实现相框效果 <style> html { overflow: hidden; background-color: #653845; background- ...

  8. C# 执行sql语句批量更新

    int x = db.Database.ExecuteSqlCommand(string.Format("update T_Pension SET UnitType = '{0}' WHER ...

  9. PHP 之递归遍历目录与删除

    /** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...

  10. CAD保存DWG文件,设置保存的文件版本号和密码

    主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgEx 保存DWG文件,可以设置保存的文件版本号和密码,详细说明如下: 参数 说明 IN CString sFile ...