URAL 1196. History Exam (二分)

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;
}
时间: 2024-10-26 22:24:22

URAL 1196. History Exam (二分)的相关文章

ural 1133 Fibonacci Sequence 二分枚举

给出Fibonacci的第i项和第j项.求第n项. Input The input contains five integers in the following order: i, Fi, j,Fj, n.−1000 ≤ i, j, n ≤ 1000, i ≠ j,−2·109 ≤ Fk ≤ 2·109 (k = min(i, j, n), …, max(i, j, n)). Output The output consists of a single integer, which is th

uva 111 History Grading(DP初步应用)

uva 111 History Grading Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all

uva 101 History Grading

Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events co

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

uva111 History Grading

History Grading Description Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students

URAL 1873. GOV Chronicles

唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. Well wrapped up in a coat, a man is rapidly walking along a gray street. This is the Tradition Keeper of the ACM club in Ural State University. Drizzl

uva111 (复习dp, 14.07.09)

 History Grading  Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order

URAL 1822. Hugo II&#39;s War 树的结构+二分

1822. Hugo II's War Time limit: 0.5 second Memory limit: 64 MB The glorious King Hugo II has declared a war-a war that is holy, victorious, almost bloodless, but ruinous! Right after declaring the war the king has started summoning the army. He plans

BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1196 [题目大意] 对于每条可能维修的公路可选择修一级公路或者二级公路,价值不同 要求图连通,且至少有k条一级公路时最大价值公路价值最小. [题解] 二分答案,从一级公路开始处理,利用并查集验证两个条件. [代码] #include <cstdio> #include <algorithm> using namespace std; const int N=20005;