Thursday, September 29, 2016

UVA 484 - The Department of Redundancy Department Solution

problem link-click here

This problem can't be solved without using STL MAP. the general rule or using array may  result in TLE. so learn map for solving this problem. for map go to the link below:

C++ Tutorial for Beginners 45 - C++ Map

an accepted code is as follows:

#include<iostream>
#include<map>
#include<vector>
using namespace std;

vector<int> v;
map<int,int> m;

int main()
{
    int n;

    while(cin>>n)
    {
        if(m[n]==0)
        {
            v.push_back(n);
            m[n]=1;
        }
        else
        {
        m[n]=m[n]+1;
        }
    }

    for(int i=0;i<v.size();i++)
    {
        cout<<v[i]<<" "<<m[v[i]]<<endl;
    }

    return 0;
}

No comments:

Post a Comment