Problem Code:PROXYC

I am getting segmentation faults when i run my code can anyone explain me why this is happening though i have tried to look for any errors but couldn’t find it.

the link to problem is : PROXYC Problem - CodeChef

my solution is here :

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int T;
cin>>T;
for(int k =0;k<T;k++){
int D;
cin>>D;
char S[D];
for (int i=0; i<D; i++){
    cin>>S[i];
}

int count =0;
int P =0;
for (int i=0; i<2;i++){
    if (S[i]=='P')
    P++;
}
for (int i=2;i<=(D-3);i++){
    if (S[i]=='A') {
     if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
     count++;
     }
    }
else {
    P++;
}
}
for (int i=(D-2); i<D; i++){
    if (S[i]=='P')
    P++; 
}
int req = ceil((0.75*D) - P);
if (count>= req)
cout<<req;
else
cout<<-1;
}
    return 0;
    
}

Consider the testcase:

1
26
AAPPPPPAPAPPPPPPPPAPPPPPPP

1 Like

is it that since here no. of presents is already above 75% so output must be zero
which in my case is coming out to be -1 because i didn’t consider a case for that…

1 Like

i implemented that part but again couldn’t submit my answer

but this problem occurs when i try to click on submit

my code is here :

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int T;
cin>>T;
if (T<=200 && T>=1){
for(int k =0;k<T;k++){
int D;
cin>>D;
if (D<=1000){
char S[D];
for (int i=0; i<D; i++){
    cin>>S[i];
}

int count =0;
int P =0;
for (int i=0; i<2;i++){
    if (S[i]=='P')
    P++;
}
for (int i=2;i<=(D-3);i++){
    if (S[i]=='A') {
     if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
     count++;
     }
    }
else {
    P++;
}
}
for (int i=(D-2); i<D; i++){
    if (S[i]=='P')
    P++; 
}

int req = ceil((0.75*D) - P);
if (count>= req && P<req)
cout<<req;
else if(P>=req)
cout<<0;
else
cout<<-1;
}
}
}
    return 0;
    
}

That fails on the sample testcase :slight_smile: Always check that your solution passes the sample testcase before submitting :slight_smile:

2 Likes

now updated the code but again the problem is occuring as in the last message

updated codes source code

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int T;
cin>>T;
if (T<=200 && T>=1){
for(int k =0;k<T;k++){
int D;
cin>>D;
if (D<=1000){
char S[D];
for (int i=0; i<D; i++){
    cin>>S[i];
}

int count =0;
int P =0;
for (int i=0; i<2;i++){
    if (S[i]=='P')
    P++;
}
for (int i=2;i<=(D-3);i++){
    if (S[i]=='A') {
     if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
     count++;
     }
    }
else {
    P++;
}
}
for (int i=(D-2); i<D; i++){
    if (S[i]=='P')
    P++; 
}

int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req)
cout<<proxy;
else 
cout<< -1;
}
}
}
    return 0;
    
}

i m really sorry a noob like me is eating up a lot of time of yours.

Don’t worry; we were all noobs once, and it only takes a minute or so to find a testcase :slight_smile:

This fails on the testcase:

1
25
AAAAAAAAAAAAPAAAAAAAAAAAP
2 Likes

okay fixed that
now the thing is again same problem is occurring
Have tested the sample case - working fine output is 1
have tested the 26 days case working fine output is 0
have tested he 25 case and output is -1

still on submitting my code getting that problem

updated code :
#include
#include
using namespace std;

int main() {
int T;
cin>>T;
if (T<=200 && T>=1){
for(int k =0;k<T;k++){
int D;
cin>>D;
if (D<=1000){
char S[D];
for (int i=0; i<D; i++){
    cin>>S[i];
}

int count =0;
int P =0;
for (int i=0; i<2;i++){
    if (S[i]=='P')
    P++;
}
for (int i=2;i<=(D-3);i++){
    if (S[i]=='A') {
     if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
     count++;
     }
    }
else {
    P++;
}
}
for (int i=(D-2); i<D; i++){
    if (S[i]=='P')
    P++; 
}

int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req && count>= proxy)
cout<<proxy;
else 
cout<< -1;
}
}
}
    return 0;
    
} 

can you specifically find out the mistake over this segment of my code :

int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req && count>= proxy)
cout<<proxy;
else
cout<< -1;
I strongly feel something is wrong over here…

I personally generally prefer to make people debug their own code - it’s an invaluable skill :slight_smile:

Here’s another testcase:

1
2
AP

Oh - and I’ve only just noticed that you’re not printing a newline after each testcase answer!

2 Likes

Thnx… a lot for helping me out and guiding me to find out my mistakes on my own.
Finally this one got submitted.
But how did you find those test cases so quickly which i couldn’t ?

anyways be prepared cause i will be bugging you from next time when i fail. I hope you won’t mind :grin::grin:
but i will try out a lot before approaching you, that’s a promise

1 Like

I usually write a random testcase generator to go with my solutions, and so have 10000 or so testcases (with known correct solutions) which I can test other people’s code against :slight_smile:

1 Like

and may i know the secret how do you write that random test case generator ?

It varies from problem to problem: here’s my original (messy) solution to PROXYC, complete with testcase generator:

// Simon St James (ssjgz) - 2019-11-16
// 
// Solution to: https://www.codechef.com/problems/PROXYC
//
//#define SUBMISSION
#define BRUTE_FORCE
#ifdef SUBMISSION
#undef BRUTE_FORCE
#define NDEBUG
#endif
#include <iostream>
#include <vector>
#include <algorithm>

#include <cassert>

#include <sys/time.h> // TODO - this is only for random testcase generation.  Remove it when you don't need new random testcases!

using namespace std;

template <typename T>
T read()
{
    T toRead;
    cin >> toRead;
    assert(cin);
    return toRead;
}

bool canBeMarkedAsProxy(const int dayIndex, const string& attendance)
{
    if (attendance[dayIndex] == 'P')
        return false;
    if (dayIndex - 2 < 0 || dayIndex + 2 >= attendance.size())
        return false;

    if ((attendance[dayIndex - 2] == 'P' || attendance[dayIndex - 1] == 'P') &&
        (attendance[dayIndex + 1] == 'P' || attendance[dayIndex + 2] == 'P'))
    {
        return true;
    }
    return false;
}

#if 1
int solveBruteForce(int D, const string& attendance)
{
    const int numAttendancesFor75 = (D % 4 == 0) ? (3 * D / 4)  : (3 * D / 4 + 1);
    const int numActualAttendances = count(attendance.begin(), attendance.end(), 'P');

    int numPossibleProxyAttendances = 0;
    for (int dayIndex = 0; dayIndex < D; dayIndex++)
    {
        //cout << "dayIndex: " << dayIndex << " ";
        if (canBeMarkedAsProxy(dayIndex, attendance))
        {
            //cout << "canBeMarkedAsProxy" << endl;
            numPossibleProxyAttendances++;
        }
        else
        {
            //cout << " can not be marked as proxy" << endl;
        }
    }
    //cout << "numActualAttendances: " << numActualAttendances << " numAttendancesFor75: " << numAttendancesFor75 << " numPossibleProxyAttendances: " << numPossibleProxyAttendances << endl;


    if (numActualAttendances < numAttendancesFor75)
    {
        if (numPossibleProxyAttendances + numActualAttendances >= numAttendancesFor75)
        {
            return numAttendancesFor75 - numActualAttendances;
        }
        else
        {
            return -1;
        }
    }
    
    return 0;
}
#endif

#if 0
SolutionType solveOptimised()
{
    SolutionType result;
    
    return result;
}
#endif


int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    if (argc == 2 && string(argv[1]) == "--test")
    {
        struct timeval time;
        gettimeofday(&time,NULL);
        srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
        // TODO - generate randomised test.
        //const int T = rand() % 100 + 1;
        const int T = 1;
        cout << T << endl;

        for (int t = 0; t < T; t++)
        {
            const int D = rand() % 1000 + 1;
            cout << D << endl;
            const int numPresent = rand() % (D + 1);
            string attendance;
            for (int i = 0; i < D; i++)
            {
                if (i < numPresent)
                    attendance += "P";
                else
                    attendance += "A";
            }
            random_shuffle(attendance.begin(), attendance.end());
            cout << attendance << endl;
            assert(attendance.size() == D && count(attendance.begin(), attendance.end(), 'P') == numPresent);
        }

        return 0;
    }
    
    // TODO - read in testcase.
    const auto T = read<int>();

    for (int t = 0; t < T; t++)
    {
        const int D = read<int>();
        const string S = read<string>();
        assert(S.length() == D);


        const auto solutionBruteForce = solveBruteForce(D, S);
        cout << solutionBruteForce << endl;
    }

    assert(cin);
}
1 Like

also do you know what to do with the test cases given in the problem set ?


like over here what is the role of subtasks ? and even the constraints ?

Here’s my post(s) on Subtasks and Constraints: CONSTRAINTS - #2 by ssjgz

hi if you are free for a sec can you plz help me sort out this new problem on which i have got stuck ?

the problem link is : CodeChef: Practical coding for everyone

i wrote a code
#include
using namespace std;
int main(){
int T;
cin>>T;
for (int k=0;k<T;k++){
int N;
cin>>N;
N=N*2;

int ar[N];
// loop 1 to take values in the array as ques 1,score,ques 2,score and goes on....
for (int i =0; i<N;i++){
    cin>>ar[i];
}
int score = 0;
// loop2 begins with entire purpose of calculating the total score
for (int i=1;i<N;i=i+2){
    if (ar[i-1]<=8){
        score = score + ar[i];
        if (i>=3){
        //loop 2.1 begins here
        for (int j = 0; j<i-1;j=j+2){
            if (ar[j]==ar[i-1]) //chck if the ques no. has been repeated in the previous entries
            {
                if (ar[j+1]>ar[i]){
                    score = score -ar[i];
                }
                else {
                    score = score - ar[j+1];
                }
            }
        }
        
        } // counter a problem when only one set of entry is  made in the array
    } // this condition is to ensure that entire scoring is considered for scorable problems
}
cout<<score<<endl;
}
    return 0;
    
}

but when i tried running custom inputs i got the correct output as shown in the picture

but when i tried the other run option (not with the custom inputs)
i got two things to share : First one is a segmentation fault as show in image below

And the other problem is that its just partially correct (why ??)

are you available for a little bit of time ? need your help…