Getting wrong answer in codechef : A Balanced Contest - c++

The link to the problem is "https://www.codechef.com/OCT17/problems/PERFCONT"
I have worked out a solution to this problem but I am getting wrong answer.
My solution:
#include<iostream>
using namespace std;
int main(){
long long int hard,cakewalk,t,temp;
long long int n,p;
cin>>t;
while(t--){
hard = cakewalk = 0;
cin>>n;
cin>>p;
while(n--){
cin>>temp;
if(temp<=(p/10))
hard++;
if(temp>=(p/2))
cakewalk++;
if(hard>2 || cakewalk>1){
break;
}
}
if(hard==2 && cakewalk ==1){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
}
}
return 0;
}
As I have gathered we have to calculate the number of Hard and Cakewalk type problems and if there exactly 2 and 1 respectively, it's a balanced contest.
Kindly help me in solving this.

You are getting WA because you are breaking when hard > 2 || cakewalk > 1 without reading the whole input line. So, after it breaks and gives the correct input for the given test case, but it will fail because the number that is next inputted is technically not the input for the next test case. It is the remaining part of first test case.
For input:
2
4 100
1 1 1 1
2 100
1 50
For the first test case, you skip taking input after reading 1 1 1 from line 3. So, the next input you take is 1 (considering it to be n) whereas n should be 2 for next test case.
Removing this should work:
if(hard>2 || cakewalk>1){
break;
}

Related

How to find greater than a number that should be a Fascinating number ,

Fascinating Number:-
Hey find any four digit number fascinating that has all the 4 digits unique. For example 1234 is a fascinating number. His friend Rahul gave him N numbers and asks him to find the minimum number which is strictly larger than the given one and has only distinct digits.
Input format
The first line of the input contains integer N, denoting the count of numbers provided by Rahul.
Each of the next N lines contains one integer.
Output format
Print the next fascinating number.
Constraints
1<=N<=10
1000<=number<=9000
Time Limit
1 second
Example
Input
2
1234
2010
Output
1235
2013
Sample test case explanation, 1235 is the minimum number that is larger than 1234 with all 4 digits 1,2,3,5 distinct.
Please can anyone help, my approach is to take a number from user and find out all the digits and check whether they are equal or not, if they aren't equal then add +1 in the digit.
#include <iostream>
using namespace std;
int main() {
int N,num;
cin>>num;
int first_digit,second_digit,third_digit,fourth_digit;
fourth_digit=num%10;
num=num/10;
third_digit=num%10;
num=num/10;
second_digit=num%10;
num=num/10;
first_digit=num%10;
while (first_digit==second_digit || first_digit==third_digit || first_digit==fourth_digit || second_digit==first_digit||second_digit==third_digit||second_digit==fourth_digit || third_digit==first_digit|| third_digit==second_digit|| third_digit==fourth_digit || fourth_digit==first_digit || fourth_digit==second_digit || fourth_digit==third_digit)
{
if (first_digit == second_digit)
{
second_digit=second_digit+1;
}
else if(first_digit == third_digit)
{
third_digit=third_digit+1;
}
else if(first_digit == fourth_digit)
{
fourth_digit=fourth_digit+1;
}
if (second_digit == first_digit)
{
second_digit=second_digit+1;
}
else if(second_digit == third_digit)
{
third_digit=third_digit+1;
}
else if(second_digit == fourth_digit)
{
fourth_digit=fourth_digit+1;
}
if (third_digit == first_digit)
{
third_digit=third_digit+1;
}
else if(third_digit == second_digit)
{
third_digit=third_digit+1;
}
else if(third_digit == fourth_digit)
{
fourth_digit=fourth_digit+1;
}
if (fourth_digit == first_digit)
{
fourth_digit=fourth_digit+1;
}
else if(fourth_digit == second_digit)
{
fourth_digit=fourth_digit+1;
}
else if(fourth_digit == third_digit)
{
fourth_digit=fourth_digit+1;
}
}
cout<<first_digit<<second_digit<<third_digit<<fourth_digit;
return 0;
}
Their are many mistakes in the code let me point them so you check and try it yourself
Mistakes which i noticed
1.What are u exactly checking in the while condition (your intention is to check weather the number digits are unique or not but when u give a unique input then it will never go inside the while loop)
When their is a digit like 9 when u add 1 to it it becomes 10 and 10 will appended please try other approach when its 9 (hint increase the previous digit by 1 and move that 9 to zero) [example if number is 2039 you code will make it 20310 which is wrong if u try increased the before number which means 2049 and replacing 9 with 0 and again as 0 is duplicate repeat until u get unique numbers]
Hope this explanation helps!
(Remember these things(Few suggestions))
If this question is from a competition of codding post the questions after competition
If you just want direct code from here this is not the correct place!

Doing a scenario problem in C++ and am unsure of how to proceed with boolean and if-loops

**This is a translation, very hastily written. If you need any clarification, just comment.
The question given is:
We live in a world with too much garbage. We have found a way to compress the garbage, but it can only be done in a specific way, or else the garbage will explode. The garbage has to be laid out in a line, and it can only be compressed with its neighbor, and only if its neighbor has the same value as it.
The first input is int N, and it represents the amount of garbage in the row. The second input is t, and it must have an input of as many characters as the value in N. If the whole thing is able to be compressed until there's only 1 garbage (t) left, then the output will be "YES".
We've figured out that as long as either N == 1, or all inputs in t (all the characters) are the same, the output will be YES.
Example inputs/outputs:
Input:
2
1 1
Output:
YES
Or
Input:
3
1 2 1
Output:
NO
Or
Input:
1
5
Output:
YES
Here's what we've got so far:
#include <iostream>
#include <string>
using namespace std;
int N;
string t;
bool allCharactersSame(string s)
{
int n = s.length();
for (int i = 1; i < n; i++) {
if (s[i] != s[0])
return false;
}
return true;
}
int main()
{
cin>>N;
cin >> t;
if (N == 1)
{
cout << "YES";
}
else if (allCharactersSame(t))
{
cout <<"YES";
}
else
{
cout<<"NO";
}
}
The problem with this is that it outputs YES no matter what, and we think it's because it takes the whitespace of the input into consideration. If we don't include spaces, it works fine. BUT the question dictates that we Have To have spaces separating our inputs. So, we're stumped. Any suggestions?
(I can't comment, therefore I write this as an answer.)
There is some other problem than you think, because the code in the question works as it should. When I gave it input "5 11111" it said "YES" when I gave it "5 12345" it said "NO".
Kai's first comment is slightly weird, when determining whether all characters in a string are the same it is sufficient to compare each of them to the first one, just as you do it in your allCharactersSame() method.
I'd suggest you add some checks on the provided input; the program should probably notice if given N doesn't match given strings' length and it should probably notice when the given string doesn't consist of numbers. As it is now, e.g. input "3 a" says "YES".

SPOJ LASTDIG, getting TLE

Below is my code for the problem. I am getting TLE. Can anyone tell me how to fix it.
Here is the problem statement:
Nestor was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives two numbers a and b. The problem consist in find the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000), a and b both are not 0. You have to find the last digit of a^b.
Input
The first line of input contains an integer t, the number of test cases (t <= 30). t test cases follow. For each test case will appear a and b separated by space.
Output
For each test case output an integer per line representing the result.
Example
Input:
2
3 10
6 2
Output:
9
6
Here is my code
#include <iostream>
using namespace std;
int main() {
int t;
scanf("%d",&t);
int num;
unsigned int pow;
while(t--)
{
scanf("%d",&num);
scanf("%d",&pow);
int z=1;
if(num==0&&pow==0)
printf("1");
else
{
while(pow!=0)
{
z=z*num;
z=z%10;
pow--;
}
printf("%d\n",z);
}
}
return 0;
}
The value for b is too high, a simple approach such as yours is bound to give a TLE. You can solve this problem using Modular Exponentiation.
http://en.wikipedia.org/wiki/Modular_exponentiation

Unexpected output for test case

Codeforces problem 158B-http://codeforces.com/problemset/problem/158/B
I am getting an unexpected output for test case 5.I think I should get 1 as output but I am it as 2.Please guide me.Judge's log:
Test: #5, time: 30 ms., memory: 380 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
2
2 1
Output
2
Answer
1
Checker Log
wrong answer expected 1, found 2
My solution:
#include<iostream>
using namespace std;
int n,a[100000],i,b,c,d,e,f,g,h;
int main()
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
b=0;
c=0;
d=0;
e=0;
for(i=0;i<n;i++)
{
if(a[i]==1) //To check for number of 1,2,3 and 4 membered groups.
b=b+1;
if(a[i]==2)
c=c+1;
if(a[i]==3)
d=d+1;
if(a[i]==4)
e=e+1;
}
f=e;
if(d>b) //More 3 member groups than 1.
{
f=f+d; //f=f+b+(g-b) 3 and 1 member groups combine.Remaining 3 i.e. (g-b) member groups will can't combine with 2 member groups.Hence,they take separate taxies.
g=-1;
}
if(b>=d) //More 1 member groups than 3.
{
f=f+d;
g=b-d; //g=remaining 1 member groups.
}
h=(2*c)%4; //Empty seats in last taxi.Possible values can be 0,1,2,3.
if(h==0)
f=f+(c/2);
else
f=f+((c+1)/2);
if(g!=-1)
{
g=g-h; //Remaining 1 member groups after combining with remaining seats in last 2 member taxi.
if((g%4)==0)
f=f+(g/4);
else
f=f+(g/4)+1;
}
cout<<f;
}
If your input is 2 2 1, then b and c will both be 1, making f 0 and g 1 in the first set of conditionals. h will be (2 * 1) % 4 or 2, making an update to f (0 + 1 = 1). Since g is 1, g-h is -1, which will lead to you executing f=f+(g/4)+1 which is f=1 + (-1/4)+1 which is 1 + 0 + 1 = 2 in integer math.
I think you wanted to check if g-h>0 instead of g!=-1, but there are a ton of places you could simplify your code. Note that using a debugger and stepping through this would have shown you where your problems are much faster, and be much more helpful to increasing your skills, than asking SO.
Just for anyone else looking at this question, this is a fairly simple answer to the problem.
Do it by hand and see if you get the same answer. If you get the same answer by hand as the computation, your algorithm is wrong. If you don't, your code is wrong.
Print variables from intermediate computations to see if they are what you think they should be.
Make sure, if it might matter, that you reinitialize your variables (including arrays) before each use.
Other tips:
Use a switch statement instead of multiple if-equal statements.
Name your variables. It helps you keep track when looking at your code.
When you have multiple variables with similar use, consider using an array instead. b, c, d, and e all seem similar.

UVa 10035 Primary Arithmetic

Can anybody tell me why my program keeps getting wrong answer? It must count the number of carry operations in a sum.
I tried every testcase came to my mind. I didn't get wrong output.
Problem Description:
Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
Input
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.
Output
For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
Here's my current code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
unsigned long a, b, carry;
vector <int> v1, v2;
int index_a, index_b;
void digit(unsigned long x, unsigned long y)
{
for(int i=x; i>0; i/=10)
v1.push_back(i%10);
for(int i=y; i>0; i/=10)
v2.push_back(i%10);
}
int main()
{
while(cin>>a>>b && a!=0 && b!=0)
{
v1.clear();
v2.clear();
int carry_counter=0;
digit(a, b);
for(int j=0; j<v1.size() && j<v2.size(); j++)
{
carry=(v1[j]+v2[j]+carry)/10;
if(carry)
carry_counter++;
index_a=index_b=j;
}
for(int i=index_a; i+1<v1.size(); i++)
{
carry=(v1[i]+carry)/10;
if(carry)
carry_counter++;
}
for(int i=index_b; i+1<v2.size(); i++)
{
carry=(v2[i]+carry)/10;
if(carry)
carry_counter++;
}
if(carry_counter==1)
cout<<"1 carry operation."<<endl;
else if(carry_counter>1)
cout<<carry_counter<<" carry operations."<<endl;
else
cout<<"No carry operation."<<endl;
}
return 0;
}
Your code at least has two bugs.
Bug #1
This is the test case you are failing
989 1
0 0
Your code answers that there are 2 carry operations, while there is only one. And the problem (actually, one of the problems) is in these lines:
for(int i=index_a; i+1<v1.size(); i++)
for(int i=index_b; i+1<v2.size(); i++)
These should start from index_a + 1 and index_b + 1 and end with i < v1.size() and i < v2.size().
Bug #2
And you are not reading all the input! Your main loop condition should be:
while (cin>>a>>b && (a!=0 || b!=0))
Test Case, Expected Output and Received Output
To be more clear, here's a test case:
989 1
1 989
11 0
0 11
2 3
2234 766
0 0
And here's the expected (correct) output:
1 carry operation.
1 carry operation.
No carry operation.
No carry operation.
No carry operation.
3 carry operations.
But your code gives this output (which is obviously wrong):
2 carry operations.
2 carry operations.
What about this test-case:
5 5
11 0
99 999
0 0
?