Please review my code here in C... there are errors occurring and I'm not sure why. Input will begin with a line containing 2 integers, n and e (1 ≤ n ≤ 500,000; 1 ≤ e ≤ 500,000), representing the number of orangutans and the number of events. The following e lines each contain a single event description. An event description will be one of the following three, ●1 n a - which represents a feeding, where n is the name of the orangutan at the time of the feeding and a represents the
Please review my code here in C... there are errors occurring and I'm not sure why.
Input will begin with a line containing 2 integers, n and e (1 ≤ n ≤ 500,000; 1 ≤ e ≤ 500,000), representing
the number of orangutans and the number of events. The following e lines each contain a single event
description. An event description will be one of the following three,
●1 n a - which represents a feeding, where n is the name of the orangutan at the time of the feeding
and a represents the amount of mangos given to the orangutan. (1 ≤ a ≤ 100)
●2 o n - which represents a name change, where o represents the old name of the orangutan and n
represents the new name.
●3 p - which represents an inquiry as to the number of mangos eaten by orangutans whose name
starts with the sequence of characters p.
Each name and character sequence will contain at most 20 characters. All names will be strictly
uppercase Latin characters (‘A’ through ‘Z’). No name will contain whitespace. No orangutan will
change their name to an already existing name.
Assume that no orangutan has eaten prior to the given events
Output: For each input event that represents an inquiry print the number of mangos eaten by orangutans whose
name starts with the given input character sequence.
Sample input:
10 6
1 BOB 5
1 BETTY 3
3 B
3 ALICE
2 BETTY ALICE
3 B
Sample output:
8
0
5
Sample input:
4 8
1 WILLIAM 4
1 WILL 6
3 WILLI
1 WILLIAN 9
1 WILLY 10
2 WILL MATT
1 WILLIAN 2
3 WILL
Sample output:
4
25
Code below:
#include<stdio.h>
#include<limits.h>
// ans variable will store the minimum noise generated
int ans=INT_MAX;
// swap function
void swap(int *a, int *b){
int tmp=*a;
*a=*b;
*b=tmp;
}
// min and max function
int min(int a,int b){
if(a<=b) return a;
else return b;
}
int max(int a,int b){
if(a<=b) return b;
else return a;
}
// recursive function generates all possible arrangement of the animals
void recursive(int ch[],int pos,int n,int noise[n][n]){
// for each possible arrangement
if(pos==n){
// calculate noise generated for this arrangement
int tmp=0;
for(int i=0;i<n;i++){
for(int j=max(0,i-2);j<=min(n-1,i+2);j++){
tmp+=noise[ch[i]-1][ch[j]-1];
}
}
// recheck if ans variable stores minimum possible value of noise generation
ans=min(ans,tmp);
return;
}
else{
// Using backtracking for generated all permutation
for(int j=pos;j<n;j++){
swap(&ch[j],&ch[pos]);
recursive(ch,pos+1,n,noise);
// backtracking
swap(&ch[j],&ch[pos]);
}
}
}
int main(){
// taking input from the user
int n;
scanf("%d",&n);
int noise[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&noise[i][j]);
}
}
// ch array will store the position of the animals
int ch[n];
int pos=0;
for(int i=1;i<=n;i++){
ch[i-1]=i;
}
// calling our recursive function
recursive(ch,0,n,noise);
// output the minimum possible answer
printf("The minimum noise generated is : %d",ans);
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images