SPIT1 - Editorial

PROBLEM LINK:

Practice

Contest

Author: Vikesh Tiwari

Editorialist: Vikesh Tiwari

DIFFICULTY:

Simple

PREREQUISITES:

maps,Hashing, Implementation

PROBLEM

There are N rounds and there are many participants. In each round participants gets few points. If the point is negative means participant has lost in that round. If two or more players have maximum number of points (say it ‘m’) then, winner is the one who scored ‘m’ points first.

EXPLANATION

Best way solve this problem is to use maps.

  • At the first pass calculate the sum of points for each player at game end. Let M be the maximum of these sums.
  • At the second pass check every round. If current player X has not less than M points and his final score is equal to M then he is the winner.

C++ Code:

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

	map<string, int> f, g;
	string part[1000];
	int poi[1000], n;

	int i, m = 0;

	int main() {
    	cin >> n;
    	for (i = 0; i < n; ++i) cin >> part[i] >> poi[i], f[part[i]] += poi[i];

    	for (i = 0; i < n; ++i)
        if (m < f[part[i]]) m = f[part[i]];
    	for (i = 0; f[part[i]] < m || (g[part[i]] += poi[i]) < m; ++i);

    	cout << part[i];
    	return 0;
	}

when will be the editorial of SPIT05 available ?

1 Like

As a far as i understand the problem “We have to find the winner based on max score and time as well”.
I don’t have any idea how to use map in c++ because i know only C language.
So i used to array one for name i.e names[1000][32] and another for time i.e time[1000].
when the name is already present in the name array then to that corresponding name let say name[i] then update points in point array at point[i] and as well as time in time at time[i].

and finally search for player who have max score at min time but code is giving right answer on my machine but when i try to submit it says wrong answer.
somebody please help me to figure out what is wrong in code.
Thanks in advance.

please find the code below:


#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    int points[1001]={0};
    char name[1001][32];
    int i,j,tpoint,flag=0,n_name=1,k;
    int time_array[1001]={0};
    char tname[33];
    int max_point,min_time;
    char winner[33];
    scanf("%d",&n);
   for(i=0;i<n;i++)
    {
        scanf("%s",&tname);
        scanf("%d",&tpoint);
        if(i==0)
        {
            strcpy(name[0],tname);
            time_array[0]=0;
            points[0]=tpoint;
           // max_point=tpoint;
           // min_time=0;
        }
        if(i!=0)
        { flag=0;
            for(j=0;j<=i;j++)
            {
                k=strcmp(tname,name[j]);
                if(k==0)
                {
                    time_array[j]=i;                    
                    points[j]=points[j]+ tpoint; 
                   // printf("points of %s=%d",name[j],points[j]);                  
 
                    flag=1;
                    break;
                }
 
            }
            if(flag==0)
            {
                strcpy(name[i],tname);
                time_array[i]=i;
                points[i]=tpoint;
                n_name++;
            }
        }
    }
    min_time=time_array[0];
    max_point=points[0];
    strcpy(winner,name[0]);
    for(i=0;i<n_name;i++)
    {
        if(max_point<=points[i])
        {
            max_point=points[i];
            strcpy(winner,name[i]);
            if(min_time>time_array[i])
            {
                max_point=points[i];
                min_time= time_array[i];
                strcpy(winner,name[i]);
            }
        }
    }
    printf("%s\n",winner);
    return 0;
 
}