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. Sprint2-2.0

    1.开始一个新的冲刺: 起止:2016.6.1~2016.6.14 按照以下过程进行 ProductBacklog:继续向下细化 Sprint 计划会议:确定此次冲刺要完成的目标 Sprint Bac ...

  2. linux安全配置学习

    参考摘自https://www.cnblogs.com/hiccup/p/4300963.html 1.关闭icmp请求 #vm虚拟机是130地址,通过echo 1 > /proc/sys/ne ...

  3. 第十周(11.18-11.24)----个人项目----学习java总结2

    一.获取随机数 方法1  (数据类型)(最小值+Math.random()*(最大值-最小值+1)) ,注意这里的每一个括号最好都不要省略掉. 例: public static void main(S ...

  4. GraphQL & REST API

    GraphQL & REST API GraphQL https://mp.weixin.qq.com/s/X-jm7jLXWmMmLBVgHfkRiQ https://webapplog.c ...

  5. Kafka日志存储原理

    引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互独立的.每个topic又可以分成几个不同的partition(每个topic有几个partition是在创建 ...

  6. sqlserver查询数据库中包含某个字段的所有表和所有存储过程

    1.查询包含某字段的所有表 select object_name(id) objName,Name as colName from syscolumns where (name like'%你要查询的 ...

  7. linux下怎么修改grub.cfg

    一.grub2的启动配置文件grub.cfggrub2的启动配置文件grub.cfg是/boot/grub/grub.cfg,而不是以前的memu.lst.如果你是多系统,有Ubuntu和window ...

  8. [洛谷P3175][HAOI2015]按位或

    题目大意:刚开始有一个数$x=0$,每秒钟有一个数$y\in[0,2^n)(n\leqslant20)$按一定概率随机出现,数$i$的概率为$p_i$,保证$\sum\limits_{i=0}^{2^ ...

  9. What?

    What? 本文主要讲解一下kubernetes周边的概念,可以说是一小部分的生态圈,逐渐了解一下,走进kubernetes的世界.请读者在读的时候,带着批判的态度去读. 一张概览图: 云计算: 原文 ...

  10. 【hdu4057】 恨7不成妻

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 (题目链接) 题意 求区间${[a,b]}$中的某些数的平方和,这些数要满足1.不是7的倍数,2.不含有7 ...