UVA: 10038 :Jolly Jumpers
Background- This problem is very simple, even the most obvious solutions also having very less run time. For more on the problem, please visit UVA: 10038 :Jolly Jumpers Below implementation has 0.008 of run time on UVA test data. I still wonder how can this be done in 0.000 timing.
C++ Implementation -
#include <iostream> #include <sstream> #include <bitset> #include <cstdlib> #include <stdio.h> using namespace std; int main() { long number = 0; while(scanf("%li",&number) != EOF) { bool ret = false; //cout << "num: " << number << endl; if(number == 0) { scanf("%*[^\n]"); getc(stdin); continue; } else if(number == 1) { ret = true; scanf("%*[^\n]"); getc(stdin); } else { long counter = number; long prev, cur; bitset<3001> hash; bitset<3001> value; scanf("%li",&cur); while(counter > 1) { prev = cur; scanf("%li",&cur); long index= abs(prev - cur)-1; if(index < 0 || index > (number-2) || hash.test(index) == true) { //cout << " Counter: " << counter <<" index: " << index << endl; ret = false; scanf("%*[^\n]"); getc(stdin); goto DONE; } hash.set(index); value.set(--counter); } value.set(0); value.set(number-1, 0); ret = (hash == value); } DONE: ret ? (cout << "Jolly" << endl) : (cout << "Not jolly" << endl); } return 0; }
Afterthoughts -
- I was struggling with c++ cin input stream to figure out a way where I can quickly skip the current line when decision has been made that this sequence is not JOLLY. Unfortunately I have to go back to c scanf to do that.
- People out there who can solve this problem in just no time 0.000
Give a try and do share -
No comments:
Post a Comment