Monday, January 27, 2014

codechef TAVISUAL - "the ball and cups" solution

codechef TAVISUAL - "the ball and cups" : http://www.codechef.com/problems/TAVISUAL
codechef TAVISUAL - "the ball and cups" editorial: http://discuss.codechef.com/questions/7694/tavisual-editorial

codechef TAVISUAL - "the ball and cups" solution: http://ideone.com/rUl3RJ

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
int t, n, c, q, l, r, temp;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &c, &q);
for(int i=0; i<q; i++) {
scanf("%d%d", &l, &r);
if(c>=l && c<=r) {
temp=c-l;
c=r-temp;
}
}
printf("%d\n", c);
}
return 0;
}

note: place where cup stays(c), will change if only it falls between left and right changes(if(c>=l && c<=r)). after change cup's new position will be the the difference it had with left change before the change occurs(temp=c-l; c=r-temp;)

No comments:

Post a Comment