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. Docker学习系列(一):windows下安装docker(转载)

    本文目录如下: windows按照docker的基本要求 具体安装步骤 开始使用 安装远程连接工具连接docker 安装中遇到的问题 Docker的更新 Docker中的jupyter windows ...

  2. JS高级——词法作用域

    作用域 1.js中没有块级作用域 2.如果有块级作用域,那么下面代码将会是undefined undefined <script> for (var i = 0; i < 10; i ...

  3. 【译】x86程序员手册01

    Intel 80386 Reference Programmer's Manual 80386程序员参考手册 Chapter 1 -- Introduction to the 80386 第1章 - ...

  4. php用户注册常用检测、写入

    // 判断数据库是否已经存在 $check_sql = "select * from user where idNumber='$idNumber'"; $check_query ...

  5. linux下vim命令汇总

    一. 进入vi的命令 vi filename : 打开或新建文件,并将光标置于第一行首 vi +n filename : 打开文件,并将光标置于第n行首 vi + filename : 打开文件,并将 ...

  6. Caffe2:段错误(核心 已转储)

    测试Caffe的时候, cd ~ && python -c 'from caffe2.python import core' 2>/dev/null && ech ...

  7. centos 设置 ip地址

    linux设置ip,主要是修改/etc/sysconfig/network-scripts/ifcfg-** 里面的网卡配置文件,然后命令 service network restart 生效 自动获 ...

  8. Linux基础篇(1)

    1.计算机硬件的五大单元 五大单元:输入单元,输出单元,CPU内部的控制单元,算术逻辑单元与内存等五大部分. (Ps:CPU实际要处理的数据完全来自于内存) 2.计算机上常用的计算单位 (1)容量单位 ...

  9. Django - 路由对应关系

    1.对url路由关系命名,以后可以根据此名称,生成自己想要的url urls.py中: url('^fdsaafdf(\d+)/',views.index,name='indexx') url('^f ...

  10. yum插件

    参考文章: http://www.linuxfly.org/post/297/ [root@dnstest07.tbc /home/ahao.mah] #rpm -qa |grep yum yum-p ...