0

here is the link to the problem https://www.codechef.com/JUNE20B/problems/CHFICRM I have two written two different codes both are working fine according to me but still getting wrong answer this is my second approach. https://www.codechef.com/JUNE20B/problems/CHFICRM please some can help me ou.....

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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n;
        k=5;
        stack<int> s;
       int b=1;
        for(int i=0;i<n;i++)
        {
            int d;
            cin>>d;
            int y;
            y=d-k;
            if(y==0)
            {
                s.push(d);
                continue;
            }
            if(s.empty() && d>5)
            {
                b--;
                cout<<"NO"<<endl;
                break;
            }
           while(!s.empty())
           {
               int z=y-s.top();
               if(z==0)
               {
                   s.pop();
                   s.push(d);
                   break;
               }
               else if(z>0){
                   s.pop();
                   if(s.empty())
                   {
                       cout<<"NO"<<endl;
                       b--;
                       break;
                   }
                   continue;
               }
               else if(z<0){
                   cout<<"NO"<<endl;
                   b--;
                   break;
               }
           }
           if(b==0)
           {
               break;
           }
        }
        if(b)
        cout<<"YES"<<endl;
    }
    return 0;
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • Your solution seems overcomplicated to me. You only need to memorize the numbers of 5 and 10 coins available at a given instant – Damien Jun 08 '20 at 18:29
  • 2
    If we talk about standard C++ or at least decent coding practices `#include ` is [the first error](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Lukas-T Jun 08 '20 at 18:29
  • Not gonna lie here. I'd be much more likely to help if you wrote readable code. – Taekahn Jun 08 '20 at 18:32
  • can you help with a demonstration @Damien – Code_it_out Jun 08 '20 at 18:32
  • 1
    Welcome to Stack Overflow! You'll likely get more useful answers if you can describe your problem a bit more. Try to make the title shorter and describe what problem you're having (rather than the title saying "Please help" or something generic). It might also help to describe the what you're trying to do & the errors or problems you're having (links to problem sets don't always make this clear). That way you can get a useful answer to help you understand your code. Many on SO are more likely to help that way, and they won't feel like you're asking them to do your homework for you. – bjg222 Jun 08 '20 at 20:08

1 Answers1

0

In practice, a simple iterative procedure seems needed.

At a given instant, just count the number of "5" coins and "10" coins available.

#include    <iostream>
#include    <vector>

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            std::cin >> arr[i];
        }
        int c5 = 0, c10 = 0;
        bool success = true;
        for (int i = 0; (i < n) && success; ++i) {
            switch (arr[i]) {
                case(5):
                    c5++;
                    break;
                case (10):
                    c10++;
                    c5--;
                    break;
                case (15): 
                    if (c10) {
                        c10--;
                    } else {
                        c5 -= 2;
                    }
            }
            success = (c5 >= 0) && (c10 >= 0);
        }
        if (success) std::cout << "YES\n";
        else std::cout << "NO\n";
    }
}
Damien
  • 4,809
  • 4
  • 15
  • 20