EATTWICE - EDITORIAL

My approach was using a vector of pairs using greedy.

ll t,n,m;
cin>>t;
while(t–){
cin>>n>>m;
vector<pair<ll,ll>> vp;
for(ll i=0;i<n;i++){
ll a,b;
cin>>a>>b;
vp.push_back(make_pair(b,a));
}
sort(vp.begin(),vp.end(),sortinrev);
ll taste=vp[0].first,i=1;
while(vp[0].second==vp[i].second)i++;
taste+=vp[i].first;
cout<<taste<<"\n";
}

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 thought solution was to find maximum and second maximum with a constraint that day should be different .
My code

#include<bits/stdc++.h>
using namespace std;
struct token
{
int d,t;
};
int main()
{
int t;
cin>>t;
while(t–)
{
int n,m;
cin>>n>>m;
int max1=INT_MIN,max2=INT_MIN;
token x[n];
for(int i=0;i<n;i++)
{
cin>>x[i].d>>x[i].t;
}
int z=0;
for(int i=0;i<n;i++)
{
if(max1<x[i].t)
{
max1=x[i].t;
z=x[i].d;
}
}
x[z].t=INT_MIN;
for(int i=0;i<n;i++)
{
if(max2<x[i].t && z!=x[i].d)
{
max2=x[i].t;
}
}
cout<<max1+max2<<endl;
}
}

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… :slight_smile:

1 Like

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…

Thanks I got you Nice approach .

1 Like

This is the O(n) approach:

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.

  1. Run a loop for the dishes.
  2. 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.
  3. 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.
  4. Finally, output the value T1 + T2.

Here is my code:
(R1SYB0 - Online C++0x Compiler & Debugging Tool - Ideone.com)

What a solution bro…:heart_eyes:

Here is my approach:

#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define point(n) fixed<<setprecision(n)
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;

int main()
{
    fastIO
    int t;
    cin>>t;
    while(t--){
    	ll n,m;
    	cin>>n>>m;
    	vector<ll> v(n),d(n);
    	map<ll, ll> mp;
    	for(int i=0;i<n;i++){
    		cin>>d[i]>>v[i];
    		if(mp[d[i]] < v[i])
    			mp[d[i]]=v[i];
    	}
    	vector<ll> res;
    	for(auto& i:mp){
    		res.push_back(i.second);
    	}
    	sort(res.begin(),res.end(),greater<int>());
    	cout<<res[0]+res[1]<<'\n';
    }
    return 0;
}

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 :smile: Hope you have a great day ahead.

1 Like

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 .

1 Like

Exactly. My main intention of posting the code was for newbies. Glad you liked it. Happy Coding

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));

    }
    
  pair<int,int>p = pq.top();
  pq.pop();
   k = p.first;
   count =  p.second;
     while(!pq.empty())
    {
        p= pq.top();
        pq.pop();
        if(p.first!=k)
        {
            count = count + p.second;
            break;
        }
    }
    cout<<count<<endl;
}

return 0;

}

https://www.codechef.com/viewsolution/24920208
Can you get the error?
Logic is same but still getting wa , don’t know why?

You forgot to add the case where v=v1 :smiley:
replace :
if(v>v1&&d!=d1)
to
if(v>=v1&&d!=d1)

Happy Coding :smile:

This code works. Check the bugs - mainly you gave output inside the dish query loop, which should have been outside the for loop.
(3JexJd - Online C Compiler & Debugging Tool - Ideone.com)

cook your dish here

import sys
q=int(input())

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)

i have passed the sample test cases, can anybody help me with this code

One way to do using map and priority queue…

code

#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;
}