codechef The Ball And Cups题解

The Ball And Cups

At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game but not how to win the game
like his assistants do.

The game requires players to find the only ball under one of the N cups after their positions are changed in a special way. At the beginning of the game, The Chef places N cups in a row and put a ball under the C-th
cup from the left (the cups are numbered from 1 to N). All players can see the initial position of the ball. Then Chef performs Q flip operations. Each flip operation is defined by two integers L and R such
that 1 ≤ L ≤ R ≤ N and consists in reversing the segment [L, R] of cups. Namely, Chef swaps L-th and R-th cups, (L+1)-th and (R?1)-th cups, and so on. After
performing all the operations Chef asks his assistants to choose a cup that they think the ball is under it. Who can guess the position of the ball will win the game, and of course, the others will have to clean the kitchen.

The Chef doesn‘t want to check all the N cups at the end of the game. He notes down the value of C and the pairs (L, R) and asked you, the mastered programmer, to determine the cup that contains the ball.

Input

The first line of the input contains a single integer T, denoting the number of test cases. The description of Ttest cases follows. The first line of each test case contains three space-separated integers NC and Q,
denoting the total number of cups, the initial position of the ball and the number of flip operations Chef will perform. Each of the following Q lines contains two space-separated integers L and R, denoting
the ends of the segment of the current flip operation.

Output

For each test case output on a separate line the final position of the ball.

Constraints

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 100000 (105)
  • 1 ≤ C ≤ N
  • 1 ≤ Q ≤ 10000 (104)
  • 1 ≤ L ≤ R ≤ N

Example

Input:
1
5 2 3
1 4
3 5
1 5

Output:
1

也是个构造数学公式的样例。

这里是过万个输入。故此最优点理一下输入,使得程序能够0ms过。

注意:

1 陷阱 - C会不在[L, R]范围内

2 fread处理输入,记得推断最后输入结束的条件 - fread返回长度为零,否则。尽管能够AC。可是程序是有bug的。

我都使用类当做函数使用了,能够非常好降低变量名的冲突。

#pragma once
#include <stdio.h>

class TheBallAndCups
{
	int st, len;
	static const int BU_MAX = 5120;
	char buffer[BU_MAX];

	char getFromBuffer()
	{
		if (st >= len)
		{
			len = fread(buffer, 1, BU_MAX, stdin);
			st = 0;
		}
		return buffer[st++];
	}

	int scanInt()
	{
		char c = getFromBuffer();
		while (c < ‘0‘ || ‘9‘ < c)
		{
			c = getFromBuffer();
		}
		int num = 0;
		while (‘0‘ <= c && c <= ‘9‘ && 0 != len)//必需要加0 != len推断输入结束
		{
			num = (num<<3) + (num<<1) + (c - ‘0‘);
			c = getFromBuffer();
		}
		return num;
	}

public:
	TheBallAndCups() : st(0), len(0)
	{
		int T = 0, N = 0, C = 0, L = 0, R = 0, Q = 0;
		T = scanInt();
		while (T--)
		{
			N = scanInt();
			C = scanInt();
			Q = scanInt();
			while (Q--)
			{
				L = scanInt();
				R = scanInt();
				if (C < L || R < C) continue;
				int M = L + ((R-L)>>1);
				if (C <= M)
				{
					int diff = C - L;
					C = R - diff;
				}
				else
				{
					int diff = R - C;
					C = L + diff;
				}
			}
			printf("%d\n", C);
		}
	}
};

int theBallAndCups()
{
	TheBallAndCups();
	return 0;
}
时间: 2024-08-24 21:04:21

codechef The Ball And Cups题解的相关文章

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

codechef Correctness of Knight Move题解

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first

codechef Sums in a Triangle题解

Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths st

codechef Holes in the text 题解

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For exampl

codechef Little Elephant and Permutations题解

The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ...,N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversio

codechef Johnny and the Beanstalk 题解

One evening Johnny found some funny looking beens in his grandfather's garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count it

CodeChef Chef and Digit Jumps 题解

原题链接:Chef and Digit Jumps 题意:原题中有链接. 题解:一道很明显的bfs题,就是跳就可以了,当然,跳的时候可以加一些优化,具体看代码 #include <queue> #include <cstdio> #include <cstring> using namespace std; #define Maxn 100000 char s[Maxn+5]; int n; int a[Maxn+5]; queue<int> q; vect

HDU 1556 Color the Ball 线段树 题解

本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点卡了我下.看来我的线段树还是不够熟,须要多多练习. 线段树是二分法的高级应用,可是却不是简单应用,要锻炼好并应用好线段树思维还是比二分法难非常多的. static const int SIZE = 100005; static const int TREESIZE = SIZE<<2; int a