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, but the same friend might make multiple posts.
Sample Input 1
2 4
1 2
1 1 WhoDoesntLoveChefBook
2 2 WinterIsComing
3 10 TheseViolentDelightsHaveViolentEnds
4 3 ComeAtTheKingBestNotMiss
Sample Output 1
WinterIsComing
WhoDoesntLoveChefBook
TheseViolentDelightsHaveViolentEnds
ComeAtTheKingBestNotMiss
//C language answer
#include<stdio.h>
struct post{
int f;
int p;
char p_str[105];
};
int place(struct post *pst,struct post temp_post,int p,int q){
int end = q,j,start=0;
while(p<=q){
if(temp_post.p < pst[(p+q)/2].p){
p=(p+q)/2 + 1;
}
else{
q=(p+q)/2 – 1;
}
}
for(j=end;j>=p && j>0;j–){
pst[j]=pst[j-1];
}
pst[p]=temp_post;
return end+1;
}
void display(struct post *posts , int p,int q){
int i;
for(i=p;i<q;i++){
printf(“%s\n”,posts[i].p_str);
}
}
main(){
struct post splist[1000],plist[1000];
int n,m,sp[1000],temp,i,j,s=0,e=0;
scanf(“%d %d”,&n,&m);
for(i=0;i<n;i++){
scanf(“%d”,&temp);
sp[temp]=1;
}
for(j=0;j<m;j++){
struct post temp_post;
scanf(“%d %d %s”,&temp_post.f,&temp_post.p,&temp_post.p_str);
if(sp[temp_post.f]==1)
s=place(splist,temp_post,0,s);
else
e=place(plist,temp_post,0,e);
}
display(splist,0,s);
display(plist,0,e);
}