thebeginner123's blog

By thebeginner123, 10 years ago, In English

DEVUCLASS MARCH CODECHEF http://www.codechef.com/MARCH15/problems/DEVCLASS getting runtime error (when b1==g1)

Your code here...
Y#include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
typedef vector <int> vec;
int type;
//int v1[100001],v2[100001];
vec v1,v2;
char s[100004];
char s1[100004];

int edit(char c1)
{  int i;
long long sum;
v1.clear();
v2.clear();
sum=0;
//cout<<s<<endl;
//cout<<s1<<endl;
if(c1=='B')
{for(i=0;i<strlen(s);i++)
if(i%2==0&&s[i]!='B')
    v1.push_back(i);
if(i%2==1&&s[i]!='G')
v2.push_back(i);}

if(c1=='G')
{for(i=0;i<strlen(s);i++)
if(i%2==0&&s[i]!='G')
    v2.push_back(i);
if(i%2==1&&s[i]!='B')
v1.push_back(i);}




for(i=0;i<v1.size();i++)
{  long long p=(v1[i]-v2[i]);
if(p<0)
    p=-p;
    sum=sum+pow(p,type);
}
return sum;

}

int  main()
{int t,i;
int b1;
long long g1,g2;
long long b,g;
char c;

scanf("%d",&t);
while(t--)
{scanf("%d",&type);
s1[0]='\0';
s[0]='\0';
//s.clear();
//s1.clear();
//v1.clear();
//v2.clear();
cin>>s;
//cout<<strlen(s)<<endl;
b1=0;
g1=0;
for(i=0;i<strlen(s);i++)
{if(s[i]=='B')
b1++;
else
g1++;}
//cout<<g1<<b1<<endl;
//cout<<abs(g1-b1)<<endl;
if((g1-b1)>1||(g1-b1)<-1)
    {
        printf("-1");
        continue;

    }
    else if(g1==(b1+1))
    {c='G';
    g=edit(c);

    }
     else if(b1==(g1+1))
        {

                c='B';
            g=edit(c);
        }
     else if(b1==g1)
        {


     int  x=edit((char)'B');
     int  y=edit((char)'G');




           g=min(x,y);

     }

    printf("%lld",g);
}
}Your code here...

~~~~~

  • Vote: I like it
  • -16
  • Vote: I do not like it