程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> codechef The Ball And Cups題解

codechef The Ball And Cups題解

編輯:C++入門知識

 

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 N, C 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

  • 1T10
  • 1N100000 (105)
  • 1CN
  • 1Q10000 (104)
  • 1LRN

    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 
    
    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;
    }


     

     

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved