PROXYC - EDITORIAL

Hello,

i executed my code with two different ways to calculate the minimum of days he has to be attendant.

timeDuration = All Lessons he should have attended.

  1.  int x = (int) Math.ceil(((timeDuration/100.0)*75));
    
  2.         int y =    (int) Math.ceil(timeDuration*0.75);
    

With way 1 i got a wrong answer, way 2 worked. i tested both ways with some numbers and 1 and 2 output allways the same.

Can you give me an example where 1 is wrong ?

Why am I getting Wrong Answer
#include <bits/stdc++.h>
using namespace std;

int main()
{
        int t,n,present_count,count,flag,i;
        float attendence;
        string s;
        cin >> t;
        while(t--)
        {
            cin >> n;
            cin >> s;

            present_count = 0,count = 0,flag = 0;

            for(i=0; i<n; i++)
            {
                if(s[i] == 'P')
                    present_count++;
            }
            attendence = (float)present_count/n;
            if(attendence >= 0.75)
            {
                cout << 0 << "\n";
                    continue;
            }
            else
            {
            for(i=2; i<n-2; i++)
            {
                if(s[i] == 'A')
                {
                    if( (s[i-1] == 'P' || s[i-2] == 'P') && (s[i+1] == 'P' || s[i+2] == 'P') )
                    {
                        s[i] = 'P';
                        count++; present_count++;
                        attendence = (float)present_count/n;
                        if(attendence >= 0.75)
                        {
                            flag = 1;
                            break;
                        }
                    }
                }
            }
            if(flag == 0)
                cout << -1 << "\n";
            else
                cout << count << "\n";
            }
        }
}

What is is wrong in this code?
#include
#include<string.h>
using namespace std;
int main()
{
int D,T,i,j,P,change=0;
char S[1000];
cin>>T;
if(T>2000)
{
exit(0);
}
for(i=0;i<T;i++)
{
cin>>D>>S;
if(D>1000)
{
exit(0);
}
for(j=0;j<D;j++)
{
if(S[j]!=β€˜A’&&S[j]!=β€˜P’)
{
exit(0);
}
}
for(j=0;j<D;j++)
{
if(S[j]==β€˜P’)
{
P++;
}
}

	if((P*100)/D>=75)
	{
		cout<<0;
	}
	else
	{
		for(j=2;j<D-2;j++)
		{
			if(S[j]=='A')
			{
				
				if(S[j-2]=='P'||S[j-1]=='P'||S[j+2]=='P'||S[j+1]=='P')
				{
					change++;
					P=P+1;
					if(((P*100)/D)>=75)
					{
						cout<<change;
						break;			
					}
				}
			}
		}

	}
}

}

for x in range(int(input())):
d = int(input())
s = input()
a = s.count(β€˜P’)
b = 0
if a/d >= 0.75:
print(-1)
else:
for y in range(2,d-2):
if s[y] == β€˜A’ and (s[y-1] == β€˜P’ or s[y-2] == β€˜P’) and (s[y+1] == β€˜P’ or s[y+2] == β€˜P’):
a += 1
b += 1
if a/d >= 0.75:
print(b)
break
else:
print(-1)

What’s Wrong With My Code…

What’s wrong with this code?
https://www.codechef.com/viewsolution/29123229

You have an out-of-bounds access here:

        s[d]='\0';
2 Likes

https://www.codechef.com/viewsolution/29305865

Can somebody find a test case for which this solution is wrong
I have tried all the test cases provided in this discussion thread.

Consider the testcase:

1
13
PAPAPAPPAPAAA

2 Likes

Thank you
found the error

1 Like

All within 3 minutes :slight_smile:

2 Likes

please help me where i am wrong :shushing_face:
#include <bits/stdc++.h>
using namespace std;
using ll=long long int;
int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n,i,p=0,prox=0,np=0;
string s;
double per;
cin>>n;
cin>>s;
if(n==1)
{
if(s[0]==β€˜A’)
cout<<1<<endl;
else
cout<<0<<endl;
}
else if(n==2)
{
if((s[0]==β€˜A’) && (s[1]==β€˜A’))
cout<<2<<endl;
else if(((s[0]==β€˜A’) && (s[1]==β€˜P’))|| ((s[0]==β€˜P’) && (s[1]==β€˜A’)))
cout<<1<<endl;
else
cout<<0<<endl;
}
else
{
for(i=0;i<n;i++)
{
if(s[i]==β€˜P’)
p++;
}
per=(double)p/n;
if(per>=0.75)
cout<<0<<endl;
else
{
for(i=0;i<n;i++)
{
if(i==0)
{
if((s[i]==β€˜A’) && ((s[i+1]==β€˜P’) || (s[i+1]==β€˜P’)))
{
p++;
np++;
s[i]=β€˜P’;
}
}
else if(i==1)
{
if((s[i]==β€˜A’) && ((s[i+1]==β€˜P’) || (s[i+2]==β€˜P’) ||(s[i-1]==β€˜P’)))
{
p++;
np++;
s[i]=β€˜P’;
}
}
else
{
if((s[i]==β€˜A’) && ((s[i+1]==β€˜P’) || (s[i+2]==β€˜P’) ||(s[i-1]==β€˜P’) || (s[i-2]==β€˜P’)))
{
p++;
np++;
s[i]=β€˜P’;
}
}
per=(double)p/n;
if(per>=0.75)
break;
}
//cout<<β€œper=”<<per<<β€œnp=”<<np<<endl;
if(per>=0.75)
cout<<np<<endl;
else
cout<<-1<<endl;
}
}
}
return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

here is link
https://www.codechef.com/viewsolution/36147739

1 Like

Thanks! The following test input causes an out-of-bounds access in your solution:

1
5
AAAAA
2 Likes

It gives -1 as output

That is correct output,i think.

For this input i get -1 as output that is correct

https://www.codechef.com/viewsolution/37764805

Please help me out with the code.Dummy testcases have passed.If anybody can find me some test cases which are outputting wrong from my code.
Thanks in advance.

Consider the test input:

1
4
PAAP
2 Likes

Thanks for replying ,i understood where i did mistake.

1 Like