ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1196. History Exam

two different solutions
Posted by Nodirbek Islomov 27 Dec 2014 12:08
#include <cstdio>
#include <map>
std:: map <int, int> a;
int n, m, q, i, x, ans;
void solve(){
    scanf("%d", &n);
    for(i = 0 ; i < n ; i ++) scanf("%d", &x), a[x] ++;
    scanf("%d", &m);
    for(i = 0 ; i < m ; i ++){
        scanf("%d", &q);
        if(a[q]) ans ++;
    }
    printf("%d", ans);
}
int main(){
    solve();
}
Re: two different solutions
Posted by Nodirbek Islomov 27 Dec 2014 12:09
#include <cstdio>
#include <algorithm>
int a[15051];
int n, m, q, i;
int ans;
int binary(){
    int left = 0, right = n-1, middle;
    while(left <= right){
        middle = (left + right) >> 1;
        if(a[middle] == q) return middle;
        if(a[middle] > q) right = middle - 1;
        if(a[middle] < q) left = middle + 1;
    }
    return -1;
}
void solve(){
    scanf("%d", &n);
    for(i = 0 ; i < n ; i ++) scanf("%d", a + i);
    std:: sort(a, a + n);
    scanf("%d", &m);
    while(m --){
        scanf("%d", &q);
        if(binary() != -1) ans ++;
    }
    printf("%d", ans);
}
int main(){
    solve();
}
Re: two different solutions
Posted by ELDVN 4 Nov 2015 00:37
Me too use the STL, its a simple and easy for write) And not more to time of run. 1.450 for map && 1.405 for binary_search..