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

soj1047.Super Snooker(转换思路+二路求和)的更多相关文章

  1. 看JQ时代过来的前端,如何转换思路用Vue打造选项卡组件

    前言 在Vue还未流行的时候,我们都是用JQuery来封装一个选项卡插件,如今Vue当道,让我们一起来看看从JQ时代过来的前端是如何转换思路,用数据驱动DOM的思想打造一个Vue选项卡组件. 接下来, ...

  2. java从命令行接受多个数字求和输出

    一·设计思路 1.定义一个整型变量sum,用于接收和 2.利用循环将命令行数字求和 3.输出参数个数以及参数之和 二·流程图 三·程序源代码 public class JavaAppArguments ...

  3. 一个简单的XML与数组之间的转换

    xml是网络使用最多的数据交换格式,所以,不掌握怎么操作它,又有蛋疼的了. php中可以操作xml的类/函数很多,个人认为最简单的是SimpleXMLElement这个类,它的使用就跟其名字一样:简单 ...

  4. [POJ1220]NUMBER BASE CONVERSION (高精,进制转换)

    题意 任意进制之间的高进的转换 思路 相模倒排,高精处理 代码 我太弱了,下面附一个讨论里发的maigo思路的代码 ],A[]; ],d[]; main(){ for(scanf("%d&q ...

  5. 文件在线预览doc,docx转换pdf(一)

    文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...

  6. Spark2.0 特征提取、转换、选择之一:数据规范化,String-Index、离散-连续特征相互转换

    数据规范化(标准化) 在数据预处理时,这两个术语可以互换使用.(不考虑标准化在统计学中有特定的含义). 下面所有的规范化操作都是针对一个特征向量(dataFrame中的一个colum)来操作的. 首先 ...

  7. 将HTML元素转换成图片供用户下载(html2canvas + canvas2Image)

    这是项目中遇到的一个问题,起初觉得把一个html元素生成图片,提供给用户下载的需求挺容易实现的,毕竟看过一些截图的插件,但是在真正操作中遇到了各种各样的问题,比如移动端上截图显示不清晰,html元素中 ...

  8. 九度OJ 1208:10进制 VS 2进制 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2040 解决:612 题目描述: 对于一个十进制数A,将A转换为二进制数,然后按位逆序排列,再转换为十进制数B,我们乘B为A的二进制逆序数. ...

  9. HTML连载18-id选择器与class区别&class选择器使用思路&后代选择器

    一.id选择器和classable选择器的区别 选择器 CSS中的开头 HTML标签可以绑定几个 是否可重复 用途 id选择器 # 仅能一个 不可以重复(一个标签里仅有一个) 一般情况下是给JS用的, ...

随机推荐

  1. iOS完整学习路线图-对知识的回顾/整理

    第一阶段:语言基础 Mac系统使用.常用UNIX指令.C语言.Objective-C语言.Foundation框架. 第二阶段:iOS基础 AppDelegate & UIApplicatio ...

  2. KEIL C51程序中如何嵌入汇编

    模块内接口:使用如下标志符:#pragma asm汇编语句#pragma endasm注意:如果在c51程序中使用了汇编语言,注意在Keil编译器中需要激活Properties中的“Generate ...

  3. Few-Shot/One-Shot Learning

    Few-Shot/One-Shot Learning指的是小样本学习,目的是克服机器学习中训练模型需要海量数据的问题,期望通过少量数据即可获得足够的知识. Matching Networks for ...

  4. 1使用 vue-cli 搭建项目(cp)

    http://www.cnblogs.com/wisewrong/p/6255817.html(copy:web) https://zhuanlan.zhihu.com/p/26183652(也很好) ...

  5. Java多线程之ThreadLocal总结

    原贴地址:http://www.cnblogs.com/zhengbin/p/5674638.html 阅读目录 官方对ThreadLocal的描述: <Thinking in Java> ...

  6. Python 变量(下)

    列表 列表是可修改的序列类型.所以列表不可以作为字典的键. >>> a = [1] >>> hash(a) Traceback (most recent call ...

  7. springboot整合spring @Cache和Redis

    转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10779450.html spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: ...

  8. java 自动装箱

    Java 编译器把原始类型自动转换为封装类的过程称为自动装箱(autoboxing),相当于调用包装类的valueof方法.举例说明: 源码: 编译之后的代码:

  9. 【DP】【CF1097D】 Makoto and a Blackboard

    更好的阅读体验 Description 给定一个数 \(n\),对它进行 \(k\) 次操作,每次将当前的数改为自己的因数,包括 \(1\) 和自己.写出变成所有因数的概率是相等的.求 \(k\) 次 ...

  10. 【数据结构】【CF1073D】 Berland Fair

    Description 给定 \(n\) 个商店,他们围成一个圆圈,按照顺时针从 \(1\) 到 \(n\) 编号.你有 \(T\) 元钱,从 \(1\) 号点开始按照顺时针方向走,每到一个商店,只要 ...