zoukankan      html  css  js  c++  java
  • soj1047.Super Snooker(转换思路+二路求和)

    Description
    On one of my many interplanetary travels I landed on a beautiful little planet called Crucible. It was inhabited by an extremely peace-loving people who called themselves Snooks. They received me very gracefully and told me I had arrived at precisely the right time, as the biggest event of the year was just then taking place: the Super Snooker Championship. Of course I couldn’t decline an invitation to go and watch. 
    That year the Super Snooker Championship was contested by two experienced Snooks universally ac-claimed as the best players on the planet: Stephanie McHendry and Joanna McHiggins. The game involved an immense rectangular table covered with green cloth and lined by edges two inches high, except in the four corners and in the middle of the longer sides where there were holes. On it were put a number of balls (from 6 up to as many as 25), each representing a value or certain number of points (anywhere from 2 to 1000, but numbered consecutively). Each player in turn tried to nudge the lowest valued ball left on the table into one of the holes on the edges of the table using a strange limb called a “kew”. If one succeeded, she was said to have “podded” the ball and the value of the podded ball was added to her score. 
    But here is the strange thing: the object of the game was not to finish with more points than the opponent. No, being a people who loved peace above all else, the object for both players was to end up with an equal number of points. This presented a bit of a problem. It was very important to them to know if it was possible to finish equal given the score of both players and the values of the balls left on the table. For instance, with a score-line of 56–34 and three balls left with values 13, 14 and 15, it is impossible to reach equal end-scores. If there are five balls left with values 20–24, it is possible: 56 + 20 + 24 = 34 + 21 + 22 + 23 = 100. You are asked to write a program that helps the Snooks by calculating whether it is possible for two Super Snooker players to win their game by finishing equal, given a score-line and the range of values of the range of the remaining balls. 
    Input
    The input consists of a line containing the number of configurations N (0 ≤ N ≤ 10000) to be calculated. It is followed by N lines each containing two scores and the lowest and highest values of the remaining balls.
    Output
    The output consists of one line for each configuration with the string ‘possible’ or the string ‘not possible’, depending on whether it is possible or not to finish equal from the given configuration.
    Sample Input
     Copy sample input to clipboard
    5
    56 34 13 15
    56 34 20 24
    0 0 500 519
    0 0 500 520
    0 0 500 521
    Sample Output
    not possible
    possible
    possible
    not possible
    not possible
     
    思路:转换一下想法,存在a+x=S-x,其中S为由c到d之间数之和,因此我们只需要判断x的只是否在c到S这个范围里面就可以,比较聪明的做法是使用2路求和,
    m=low+..+low+len-1 n=high-len+1+...+high
    因为是连续数,所以 len 个在[low,high]内的数的和 必然是在[m,n]内,故只要 x 落在该范围内,则组合成功
    代码如下:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main()
    {
    	int N;
    	cin >> N;
    	while(N--)
    	{
    		int a,b,c,d;
    		cin >> a >> b >> c >> d;
    		int len = d - c + 1;
    		int sum = (c + d)*len/2;
    		if(a > b)
    			swap(a,b);
    		if((b - a + sum) % 2 == 1 || (a + b + sum) % 2 == 1 || b > a + sum)
    			cout << "not possible" << endl;
    		else 
    		{
    			bool flag = false;
    			int i;
    			int Min,Max;
    			int x = (b - a + sum) / 2;
    			for(i = 1; i <= len ;i++)
    			{
    				Min = (c + c + i - 1)* i / 2;
    				Max = (d - i + 1 + d)* i / 2;
    				if(x >= Min && x <= Max)
    				{
    					flag = true;
    					break;
    				}
    			}
    			if(flag == true)
    				cout << "possible" << endl;
    			else
    				cout << "not possible" << endl;
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Android开发自学笔记(Android Studio)—4.1布局组件
    交换机远程管理
    ThinkPHP开发博客系统笔记之二
    PHP编码规范
    Kali Linux学习笔记
    CSS编码规范
    ThinkPHP开发博客系统笔记之一
    2 Powershell与Cmd以及Unix/Linux Shell
    不同vlan间通信的三种配置方式
    配置超级用户口令(Cisco IOS系统)
  • 原文地址:https://www.cnblogs.com/sysu-blackbear/p/3254255.html
Copyright © 2011-2022 走看看