Most of the people here are talking about why they are getting a WA. The link below is my solution which has been accepted. It has detailed comments about everything which is happening in the code. You all can refer to my code and check your mistake.
Hope this helps.
Lang: C++
I am getting TLE in this solution. I tried removing loop to see which one of them gave the error but I failed.
It would be really helpful if somebody could point out the mistake. https://www.codechef.com/viewsolution/24942568
Encountered the same problem but after the contest
Saw a solution with String Builder class and it got accepted with old Reader Class as well https://www.codechef.com/viewsolution/24941683
Also you might need to change your data types from int to long int at desired positions.
You can have a look at your code in action here: CodeChef: Practical coding for everyone
Well, Without that, we can have two type of drinks, one where drinking the favorite drink is beneficial (F > B), and one where favorite drink cause loss (F < B). (We can treat drinks with F = B at the end, it doesn’t matter anyway.)
There can be some solution based on priorityQueue sorting drinks in descending order of |F-B| on basis of which we can make choices whether we can satisfy this constraint or not. Greedy should work.
#include <bits/stdc++.h>
using namespace std;
struct s
{ int d,f,b,ch,idx; };
bool compf(struct s a, struct s b)
{ return a.f > b.f; }
bool compb(struct s a, struct s b)
{ return a.b> b.b; }
bool compx(struct s a, struct s b)
{ return a.idx <b.idx; }
int main()
{
int t;cin>>t;while(t–)
{
int n,m; cin>>n>>m;struct s st[1000000];
int c[m+1],sum=0;
for(int i=1;i<=m;i++) { cin>>c[i];}//cout<<c[i]<<" “; }cout<<endl;
for(int i=0;i<n;i++)
{ cin>>st[i].d>>st[i].f>>st[i].b;st[i].ch=-1;
st[i].idx=i;
}
// sort(st,st+n,compf);
for(int i=0;i<n;i++)
{
if(c[st[i].d]>0)
{sum+=st[i].f;
c[st[i].d]–;
st[i].ch=st[i].d;}
//cout<<st[i].d<<” “<<c[st[i].d]<<endl;
}
//for(int i=1;i<=m;i++) cout<<c[i]<<” "; cout<<endl;
Can anyone explain me this ? . In order to not miss favorite slush of persons coming later would be to wait until the end in order to assign a person slush whose favorite slush is not available . So that way we maximize the profit right . So the thing that got me confused is . If we are waiting till end can we not wait till a person comes whose Fi is more among all those with same favourite slush . Like in this test case .
1
5 3
1 2 3
2 6 3
2 10 7
2 50 3
1 10 5
1 7 4
can we not make people wait until the third person with Fi value of 50 comes so , we sell him the slush and sell second person with Fi 10 . I’m confused with waiting and assigning them the slush