Codechef ChefWatson uses Social Network

      No Comments on Codechef ChefWatson uses Social Network

Codechef ChefWatson uses Social Network .Problem Code: BOOKCHEF

Codechef ChefWatson uses Social Network – problem statement

Chef Watson uses a social network called ChefBook, which has a new feed consisting of posts by his friends. Each post can be characterized by f – the identifier of the friend who created the post, p – the popularity of the post(which is pre-calculated by ChefBook platform using some machine learning algorithm) and s – the contents of the post which is a string of lower and uppercase English alphabets.

Also, Chef has some friends, which he has marked as special.

The algorithm used by ChefBook for determining the order of posts in news feed is as follows:

Posts of special friends should be shown first, irrespective of popularity. Among all such posts the popular ones should be shown earlier.
Among all other posts, popular posts should be shown earlier.
Given, a list of identifiers of Chef’s special friends and a list of posts, you have to implement this algorithm for engineers of ChefBook and output the correct ordering of posts in the new feed.

Input

First line contains N, number of special friends of Chef and M, the number of posts. Next line contains N integers A1, A2, …, AN denoting the identifiers of special friends of Chef. Each of the next M lines contains a pair of integers and a string denoting f, p and s, identifier of the friend who created the post, the popularity of the post and the contents of the post, respectively. It is guaranteed that no two posts have same popularity.

Output

Output correct ordering of posts in news feed in M lines. Output only the contents of a post.

Constraints

  • 1 ≤ N, M ≤ 103
  • 1 ≤ Ai, f, p ≤ 105
  • 1 ≤ length(s) ≤ 100

Code – C

#include<stdio.h>
#include<string.h>
int main()
{
	int n,m,i,j,temp;
	scanf("%d %d \n",&n,&m);
	int a[n],f[m],p[m],flag=0;
	char s[m][101];
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(j=0;j<m;j++)
	{
		scanf("\n%d %d %s",&f[j],&p[j],&s[j]);
	}
	for(i=0;i<m-1;++i)
	{
		for(j=0;j<m-i-1;++j)
		{
			 if(p[j]<p[j+1])
             {
                temp=p[j];
                p[j]=p[j+1];
                p[j+1]=temp;
                temp=f[j];
                f[j]=f[j+1];
                f[j+1]=temp;
                char str[101];
                strcpy(str,s[j]);
                strcpy(s[j],s[j+1]);
                strcpy(s[j+1],str);

             }
		}
	}
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
		 if(a[j]==f[i])
		 {
			printf("%s\n",s[i]);
		 }
	    }
	}
	for(i=0;i<m;i++)
	{
		flag=0;
		for(j=0;j<n;j++)
		{
		 if(a[j]==f[i])
		 {
			flag=1;
		 }
	    }
		if(flag==0)
		{
		   	printf("%s\n",s[i]);	
		}
	}
	return 0;

}

Test Case

2 4
1 2
1 1 WhoDoesntLoveChefBook
2 2 WinterIsComing
3 10 TheseViolentDelightsHaveViolentEnds
4 3 ComeAtTheKingBestNotMiss

Output:
Codechef ChefWatson uses Social Network

Summary
Codechef ChefWatson uses Social Network
Article Name
Codechef ChefWatson uses Social Network
Description
Codechef Chef Watson uses a social network called ChefBook, which has a new feed consisting of posts by his friends. Each post can be characterized by f - the identifier of the friend who created the post, p - the popularity of the post(which is pre-calculated by ChefBook platform using some machine learning algorithm) and s - the contents of the post which is a string of lower and uppercase English alphabets.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *