This will not get AC because you are not considering of the case in which max and second max tastiness or both max tastiness have same day then you have to look for 3rd max.
I got AC for this .
I stored the descending sorted tastes with there day number .
Stored the maximum taste as sum
Now iterated while we have same days ahead
So after the iteration you will get maximum taste but with a different date .
So…
I was wondering whats wrong with this logic: create an unordered map of pairs and for each di, only store the highest vi. This way the unordered map would only have the highest tastiness for each date. Loop through for max tastiness, store in first max. Delete this key. Find max again store in second max. Yes I know this is not efficient but like I thoght at least it would give the right answer…
Maintain a D1 (Day1) and D2 (Day2) as days of largest and second largest tastiness days (they should have different days, obviously, i.e. D1 != D2) respectively, and their corresponding tastiness as T1 and T2.
Run a loop for the dishes.
If the dish has tastiness greater than equal to T1, also if the day of the dish served is not equal to D1, then make T2=T1 and D2=D1, and the corresponding dish day and tastiness is to be assigned to D1 and T1. Else, assign D1 and T1 to corresponding dish day and tastiness.
Else if, dish tastiness is greater than T2 and corresponding dish day is not D1, then the corresponding dish day and tastiness is to be assigned to D2 and T2.
I am extremely overwhelmed that you liked my solution. I am not sure that whether you meant sarcasm or not but just to clarify you, I got AC for that. I will explain my code for you, as I see you are at 1 star.
The case of 2nd max,3rd max doesn’t arise, because I have used Di as the index and for the same day, I have taken account of the largest value. I hope this is clear and study code a little further before commenting. Thank you Hope you have a great day ahead.
Not at all bro I just loved your solution I am just looking at others solution and people are using pair vector and so many things Your code is sweet and simple .
My algorithm is similar to setters algorithm, but it has produced TLE as output.I cant understand What mistake I did .Can an one help me?
This is my program.Thanks!
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
days=[ ]
taste=[ ]
for i in range(n):
d,t=map(int,input().split())
if d not in days:
days.append(d)
taste.append(t)
else:
taste[days.index(d)]=max(taste[days.index(d)],t)
taste.sort()
print(taste[-1]+taste[-2])
I was getting a runtime error in below code. I have used the priority queue with comparator and pair to store day and tastiness. Please help me to fix it out #include #include
using namespace std;
struct compare
{
bool operator()(pair<int,int>p1,pair<int,int>p2)
{return (p1.second<p2.second);
}
};
int main() {
// your code goes here
int t,n,m,d,v,i;
int count=0;
int k;
cin>>t;
priority_queue<pair<int,int>,vector<pair<int,int>>,compare>pq;
while(t–)
{
cin>>n>>m;
for(i=0;i<n;i++)
{
cin>>d>>v;
pq.push(make_pair(d,v));
for _ in range(q):
n,m = map(int,input().split())
v=[]
d=[]
for i in range(n):
d1,v1=map(int,input().split())
d.append(d1)
v.append(v1)
v, d = zip(*sorted(zip(v, d)))
flag=0
for i in range(1,n):
if(d[n-i]==d[n-i-1]):
continue
else:
flag=v[n-1]+v[n-i-1]
break;
if(flag==0):
print(v[n-1])
else:
print(flag)
#include
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
long long int n,m1,d,v,i,a,b,ans=0;
map<long long int,long long int>m;
priority_queue q;
map<long long int,long long int>::iterator it;
cin>>n>>m1;
for(i=0;i<n;i++)
{
cin>>d>>v;
if(m[d]<v)
m[d]=v;
}
for(it=m.begin();it!=m.end();it++)
q.push(it->second);
ans+=q.top();
q.pop();
if(!q.empty())
ans+=q.top();
cout<<ans<<endl;
}
return 0;
}