URAL 1196. History Exam (二分)
1196. History Exam
Memory limit: 64 MB
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.
Input
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
Sample
input | output |
---|---|
2 |
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 (二分)的更多相关文章
- URAL 2048 History 蔡勒公式
HistoryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...
- 二分法&三分法
ural History Exam 二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...
- 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 ...
- UVa 111 - History Grading (by 最长公共子序列 )
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- URAL 1873. GOV Chronicles
唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. ...
- 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 ...
- uva111动态规划之最长公共子序列
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=74662#problem/C A B C D E C - Largest Rect ...
- UVA 111(LCS问题)
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- UVA 111 (复习dp, 14.07.09)
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
随机推荐
- Android中有四大组件的简单总结
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...
- swift单例创建的几种方法
//单例方法1 class SingleTonOne{ static var sharedInstanceOne:SingleTonOne{ struct SingleTonStruct { stat ...
- [转][IPC通信]基于管道的popen和pclose函数
标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由popen启动的进程称为子进程. popen函数还创建一个管道用于父子进 ...
- [Windows Server 2012] Discuz X3安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★[护卫神·V课堂]是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Discu ...
- Effective Java中文版
译者序 序 前言 第一章 引言 第二章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造函数 第2条:使用私有构造函数强化singleton属性 第3条:通过私有构造函数强化不可实例化属性 第4条: ...
- 11-c++虚拟函数
虚拟函数 #include "stdio.h" class A{ public: void print() { printf("%s","this i ...
- jdbcUrl is required with driverClassName错误解决
jdbcUrl is required with driverClassName springboot2.0配置多数据源: spring.datasource.primary.url=jdbc:mys ...
- ThinkPHP框架表单验证AJAX
验证有两种方式:静态验证与动态验证. 一.静态验证 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 验证时要在test表的Model里面加验证条件:新建testModel.class. ...
- Jenkins+Ant+Jmeter自动化集成测试实例
通过学习Jmeter自动化测试,接触到了Ant命令和其构建文件build.xml文件的编写,与此同时,通过将测试项目集成在jenkins上,进一步学习了jenkins的一些环境配置知识.以下是自己的初 ...
- [luogu2154 SDOI2009] 虔诚的墓主人(树状数组+组合数)
传送门 Solution 显然每个点的权值可以由当前点上下左右的树的数量用组合数\(O(1)\)求出,但这样枚举会T 那么我们考虑一段连续区间,对于一行中两个常青树中间的部分左右树的数量一定,我们可用 ...