大一合宿--c++map


map
テーマ1:P 1687
#include 
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
"
,vis[k]); } return 0; }

テーマ2:P 1678
#include 
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;
}

テーマ3:P 1686
#include 
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;
}

テーマ4:P 1677
#include 
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;
}