1047. Super Snooker

1047. Super Snooker

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

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

题意:

.给定4个数a,b,c,d。
.实质上是给出了a,b,c,c+1,c+2,…,c+(d-c-1),c+(d-c).
.问是否能恰当分配c到d这d-c+1个数给两个人,第一个人已拥有a分数,第二个人已拥有b分数,分配完后两人所拥有分数相等。
.例如a=0,b=0,c=1,d=3
.则一个分配方案为0+1+2=0+3.

思路
◦设c到d的和为Sum,分配给第一个人最终共有x,则分配给第二个人的有Sum-x。
◦故一个显然的做法是求解一个背包问题,也就是能否用c到d的数组成x或者Sum-x.
◦但题目有非常多询问,必然超时
◦实质上等价于a+x = b+(Sum-x),也就是b-a=2x-Sum(即x=((b-a)+Sum)/2;)


sum=((c+d)*(d-c+1))/2,求出c到d的d-c+1个数的和;

不可能情况:

 (1)如果a+b+sum为奇数,不可能平均分开

 (2)如果(b-a)+Sum为奇数,则(b-a)+Sum)不能整除2,即X不是整数,不符合要求

 (3)b>a+sum或者a>b+sum,一方的数字太大,即使加上sum也不能平衡

 (4)求出符合b-a=2x-Sum的X,但是,x不能由c到d里面的数字组成,也是不符合要求的

可能的情况:

    在c+1,c+2,…,c+(d-c-1),d,两头各取出len个数,例如len=2,则取前两个数为一组求和,后两个数为一组求和,如果sum1<=x<=sum2,则可以保证X存在且由c到d中的数字组成。(将中间的数字通过与前后两段交换获得)

eg:输入56,34,20,24

            a=56,b=34,c=20,d=24   (if (a>b),swap(a,b))

            sum=110     b-a=22,       x=66

            c到d的数字有:20,21,22,23,24

            枚举len,取len=3,此时,sum1=20+21+22=63,sum2=22+23+24=69   x=66正好在63跟69之间。所以符合要求。因为,要组成x=66,将len1中的20跟len2中的23交换即可。

             即:只要x在两段序列和的中间,由于数字序列的连续性,可通过数字交换获得x。


代码如下:



#include<iostream>
using namespace std;
int a,b,c,d;
int sum;
bool p;
int main()
{
    int time;
    cin>>time;
    while(time--)
    {
    cin>>a>>b>>c>>d;
    p=0;

    sum=((c+d)*(d-c+1))/2;//实质上等价于a+x = b+(S-x),也就是b-a=2x-S 可以证明2x-S取遍-S,-S+2,-S+4,….S-2,S.
    if(a>b) {
            int temp=b;
            b=a;
            a=temp;
            }
    if((a+b+sum)%2==1||b>a+sum||(b-a+sum)%2==1)  cout<<"not possible"<<endl;
    else{
    int x=(b-a+sum)/2;
    int sum1=0,sum2=0;
    bool y=0;
    for(int i=1;i<=d-c+1;i++)
    {
     sum1=0;
     sum2=0;
      for(int j=0;j<i;j++)
      {
      sum1+=c+j;
      sum2+=d-j;
      }
      if(x>=sum1&&x<=sum2) {cout<<"possible"<<endl;  y=1;break;}
    }
    if(y==0) cout<<"not possible"<<endl;
}
}
    //system("pause");
    return 0;
}                                 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值