大一集训--c++ map

map

题目一:P1687

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    map<int,int>vis;
    scanf("%d",&n);
    int x;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        vis[x]=i;
    }
    int q;
    scanf("%d",&q);
    int k;
    while(q--)
    {
        scanf("%d",&k);
        printf("%d\n",vis[k]);
    }
    return 0;
}

题目二:P1678

#include <bits/stdc++.h>
using namespace std;
int main()
{
    map<string,int>word;
    int n;
    cin >> n;
    string s;
    while(n--)
    {
        cin >> s;
        cin >> word[s];
    }
    int q;
    cin >> q;
    while(q--)
    {
        cin >> s;
        cout << word[s] << endl ;
    }
    return 0;
}

题目三:P1686

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin >> n >> m;
    queue<int>que;
    map<int,int>ma;
    int x;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        que.push(x);
    }
    for(int i=1;i<=m;i++)
    {
        cin >> x;
        ma[x]++;
    }
    int flag=0;
    while(!que.empty())
    {
        if(ma[que.front()])
        {
            if(!flag)
            {
                printf("%d",que.front());
                flag++;
            }
            else
            {
                printf(" %d",que.front());
            }
        }
        que.pop();
    }
    return 0;
}

题目四:P1677

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    map<int,int>m;
    cin >> n;
    int x;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        m[x]++;
    }
    map<int,int>::iterator it;
    for(it=m.begin();it!=m.end();)
    {
        int t=1;
        if(it->second>=2)
        {
            m[it->first+1]+=it->second/2;
            it->second=(it->second)%2;
            if(it->second==0)
            {
                m.erase(it++);
                t=0;
            }
        }
        if(t)
        {
            it++;
        }
    }
    int flag=0;
    it=m.begin();
    flag=flag+it->first;
    for(it=m.begin();it!=m.end();it++)
    {
        if(it!=m.begin())
        {
            flag=flag+it->first-(--it)->first-1;
            ++it;
        }
    }
    cout << flag << endl ;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值