Contest 2 - Hints to Problems [OFFICIAL]

For COMPILER prob, how is this <<>><> valid? If this is valid, the count should be 4 right?

1 Like

The count should be 6 because there are 3 valid pairs each having length 2.
First << after this when > comes we can pair <> and only < left. Then > comes and paired.
Then <> comes together and paired.

6

what is the ans for this case <<<<> ???

ans - 2

// you can understand by it…
while( n>0 ){
if( ch[i] == ‘<’ )
st.push(’<’);
else{
st.pop(); }
count += 2; }

It’s ans should be 0 since there is no valid pair from start.
If the input is like <><<< then it’s output should be 2

3 Likes
**how come this solution is wrong atleast for subtasks?** code - CHFQUEUE
    int ans = 1;
    for (int i = 0; i < n; i++)
    {
        int j = i;
        while (j < n)
        {
            if (arr[j] < arr[i])
            {
                ans = ans * (j - i + 1);
                // std::cout<<"index i- " << i<<" index j - " << j <<"aaj -"<< j-i+1 <<" ans- "<<ans<< std::endl;
                break;
            }
            j++;
        }
    }
    std::cout << ans % 1000000007 << std::endl;
2 Likes

I don’t get where I am wrong in PSHOT

try:
    for i in range(int(input())):
        n = int(input())
        s = input()
        s = list(s)
        c = 0
        d = 0
        e = n
        g = n
   
        for i in range(len(s)):
            if(i%2 == 0):
                c += int(s[i])
                e -= 1
            else:
                d += int(s[i])
                g -= 1
            if(c > d + g):
                print(i+1)
                break
            elif(d > c + g):
                print(i+1)
                break
            elif(c == d and i == 2*n-1):
                print(i+1)
                break
            
            
except EOFError:
    pass

please help me

Whats wrong in my code of COMPILER

for i in range(int(input())):
    s = input()
    a = 0
    b = 0
    c = 0
    if(s[0] == '>'):
        a = 0
    else:
        for i in s:
            if(i == '<'):
                b += 1
            else:
                c += 1
        if(b == c):
            a = b + c
        else:
            d = min(b, c)
            a = 2*d
    print(a)

A doubt in hint 1 of problem Compilers and parsers.
All prefixes ending at location j such that j<i should be invalid na?
Or am I interpreting it wrongly?
say for example
<<> is an invalid prefix. (j = 2, indexing from 0, i does not exist)
however
<>> is a valid prefix, right? (j = 2 and i =2)
Thank you.

PLEASE TELL ME WHERE AM I GOING WRONG ?

#include <iostream>
#include <bits/stdc++.h>
#include <limits>
#include <unordered_map>
#include <vector>
#include <string>

using namespace std;

int main()
{
    int t;
cin >> t;
while (t--)
{
    int n;
    string str;
    cin >> n >> str;
    int a = 0, b = 0;
    int i = 0;
    for (i = 1; i <= str.length(); i++)
    {
        if (((i) % 2 != 0) && str[i - 1] == '1')
        {
            a++;
        }
        if (((i) % 2 == 0) && str[i - 1] == '1')
        {
            b++;
        }

        if (i % 2 == 0)
        {
            int tmp = str.length() - i;
            int e = tmp / 2;
            int o = tmp / 2;
            if (a > e)
            {
                break;
            }
            if (b > e)
            {
                break;
            }
        }
        else
        {
            int tmp = str.length() - i;
            int e = (tmp / 2) + 1;
            int o = tmp / 2;
            if (a > e)
            {
                break;
            }
                if (b > e)
            {
                break;
            }
        }
    }
    cout << i << endl;
}
return 0;
}

When i run below solution for NOTALLFL problem than it shows me an partially corrected solution. It will be great help if someone will help out with this problem.
# cook your dish here

def main():
    for T in range(int(input())):
        N,K = list(map(int,input().split()))
        segment = dict()
        max_segment_length = 0 
        for i in input().split():
            if int(i)<=K :
                if segment.get(i) is None:
                    if len(segment)+1 < K:
                        segment[i]  =  1
                    else:
                        # print(segment)
                        t = sum(segment.values())
                        max_segment_length= max(max_segment_length , t)
                        segment = dict()
                        segment[i] = 1
                else:
                    segment[i] += 1
        # print(segment)
        t = sum(segment.values())
        max_segment_length= max(max_segment_length , t)
        print(max_segment_length)        

                    
                    
if __name__ == '__main__':
    main()  

As I had run through possible testcase but still it is working. i can not able to troubleshoot.

this is wrong because you are calculating the total answer first and than taking its mod. In such a case the variable with data type int(or long long) will already shoot out of bounds and will start storing garbage values.
(ab)%N=(a%Nb%N)%N
use this property and change ans=((ans%N)*(j-i+1)%N)%N to keep int within bounds

3 Likes

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename
enable_if<sizeof dud(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge range(c i, c j) { return rge{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(…);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << “(” << d.first << ", " << d.second << “)”;
}
sim dor(rge d) {
*this << “[”;
for (auto it = d.b; it != d.e; ++it)
*this << “, " + 2 * (it == d.b) << *it;
ris << “]”;
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(…) " [” << #VA_ARGS ": " << (VA_ARGS) << "] "
using ll = long long;

void test_case() {
int n;cin>>n;
string s;cin >> s;
vector hash;
string k="";
for(int i=0;i<n;++i)
{
if(s[i] == ‘+’ || s[i] == ‘-’)
{
if(hash.empty()) hash.push_back(s[i]);
else
{
while(true)
{
if(hash.empty()){
hash.push_back(s[i]);
break;
}
else if(hash.back() == ‘(’)
{
hash.push_back(s[i]);
break;
}
else
{
k += hash.back();
hash.pop_back();
}
}
}
}
else if(s[i] == ‘*’ || s[i] == ‘/’)
{
if(hash.empty()) hash.push_back(s[i]);
else
{
if(hash.back() == ‘+’ || hash.back() == ‘-’ || hash.back() == ‘(’)
hash.push_back(s[i]);
else
{
while(true)
{
if(hash.empty()){
hash.push_back(s[i]);
break;
}
else if(hash.back() == ‘(’ || hash.back() == ‘+’ || hash.back() == ‘-’)
{
hash.push_back(s[i]);
break;
}
else
{
k += hash.back();
hash.pop_back();
}

				}
			}
		}
	}
	else if(s[i] == '^')
	{
		hash.push_back(s[i]);
	}
	else if(s[i] == '(')
		hash.push_back(s[i]);
	else if(s[i] == ')'){
		while(true){
			if(hash.back() == '('){
				hash.pop_back();
				break;
			}
			else
			{
				k+= hash.back();
				hash.pop_back();
			}
		}
	}
	else
	{
		k+=s[i];
	}
	
}
int size = hash.size();
for(int i = size-1;i >= 0;--i)
{
	k += hash[i];
}
cout << k << '\n';

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while(T–){
test_case();
}
}

can any one tell me what is wrong in my solution?

https://www.codechef.com/viewsolution/33614457
I am getting WA. can anyone help me?
Thanks in advance.

in Chef queue question for the given input n = 4, k = 2, a = [1,2,1,2] shouldn’t answer should be 5
For a[3] = fearfulness 1, a[2] = fearfulness 1, a[1] = fearfulness 2, a[0] = fearfulness 1,
total fearfulness = 5.
What am I missing?

// Wormholes
/* subtask 1 test case 4 and testcase 6 showing wrong answer /
/
i m not able to understand whats wrong with my code plz any one can help me out …*/

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

int main() {
// your code goes here
int n,x,y;
cin>>n>>x>>y;
vector<pair<int, int>> element;
int arr1[x];
int arr2[y];
long long ans=INT_MAX;
for(int i=0;i<n;i++)
{
int s,e;
cin>>s>>e;
element.push_back(make_pair(s,e));
}
for(int i=0;i<x;i++)
cin>>arr1[i];
for(int i=0;i<y;i++)
cin>>arr2[i];
sort(arr1,arr1+x);
sort(arr2,arr2+y);
for(int i=0;i<n;i++)
{
int u=upper_bound(arr1,arr1+x,element[i].first)-arr1;
int l=lower_bound(arr2,arr2+y,element[i].second)-arr2;
if(u<x && l<y&& u>=0)
{
long long k=arr2[l]-arr1[u-1]+1;
//cout<<arr1[u-1]<<" “<<arr2[l]<<”\n";
ans=min(ans,k);
}

}
cout<<ans<<"\n";
return 0;

}

don’t use INT_MAX use 1000001 as max value I got WA for 4 cases only because I was using INT_MAX

1 Like

i tried to use 1000001 instead of INT_MAX …bt still not working

Why is my Answer Wrong in the Question Rectangle ?

Here’s my Code:
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define pb push_back
#define b 100000
#define l 500
void solve()
{
ll n,x,y; cin>>n;
vector vec;
ll ans=1;
rep(i,0,n)
{
cin>>x>>y;
vec.pb(x);
}sort(all(vec));
rep(i,0,n-1)
{
ans=max(ans,(vec[i+1]-vec[i])*l);
}cout<<max(ans,(b-vec[n-1])*l);
}

Please Check my Logic… I think it’s correct but I am getting Wrong Answer. Please Tell any Example Test Case Where my Logic fails ??

1 Like