【二分orSet】History Exam_output

來源:互聯網
上載者:User
Time Limit: 5 secondMemory Limit: 1000KDiscription 曆史老師決定簡化考試的過程。每個學生只需寫下著名事件發生的年份就行了。老師有自己的年份表。學生得多少分取決於他寫下的年份有多少是包含在老師的年份表中。 Input Format 首行為N,是老師年份表中的年數。 1 <= N <= 15000,對下N行包含老師的年份表,每一個年份不超過10^9。以升序排列。 再下來M行,是學生的年份表的年數。1 <= M <= 1000000,對下M行包含學生的年份表。這個表沒有排序,年份可以不止一次出現。 Output Format 輸出一個數,即學生的年份表中有多少個是包含在老師的表中。 Sample Input 21054149241492655361492100Sample Output 2

二分

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <ctime>#include <cmath>#define loc #define mxn 15000+10using namespace std;int n,m;int tc[mxn];int bs(int x){    int l=0,r=n;    while (l<=r)    {          int m=(l+r)>>1;          if (tc[m]==x) return 1;          if (tc[m]>x) r=m-1;          else l=m+1;    }            return 0;}int main(){    freopen("exam.in","r",stdin);    freopen("exam.out","w",stdout);        scanf("%d",&n);    for (int i=1;i<=n;++i)    {        scanf("%d",tc+i);    }        int tot=0,tm;    scanf("%d",&m);    for (int i=1;i<=m;++i)    {        scanf("%d",&tm);        tot+=bs(tm);    }        printf("%d",tot);    return 0;}

Set

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <ctime>#include <cmath>#include <set>#define loc#define mxn#define mxmusing namespace std;int n,m,t;set<int>S;int main(){    freopen("exam.in","r",stdin);    freopen("exam.out","w",stdout);        scanf("%d",&n);    for (int i=1;i<=n;++i)    {        scanf("%d",&t);        S.insert(t);                    }        int tot=0;    scanf("%d",&m);    for (int i=1;i<=m;++i)    {        scanf("%d",&t);        if (S.find(t)!=S.end()) ++tot;    }        printf("%d",tot);    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.