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
题目分析
给定两个比分,再给出连续的数进行分配,看最终是否能平分
思路参考这里
第一,观察差值是否与所有数的和相同,
第二,将连续的数分成两堆(1个到n-1个),确定两堆的差值范围
假设有i个,
max(rs-ls)=max(rs*2-(rs+ls))=max(rs*2-sum)
此时rs要最大,则i个数为最大的i个数,即rs = i*right-i*(i-1)/2
所以max:2*(i*right-i*(i-1)/2)-sum
同理min:2*(i*left+i*(i-1)/2)-sum;
注意到从min到max,是以2为公差的等差序列(自己加一,另一堆减一,差值加2)
若一开始的差值在这个序列里就成功了
#include<stdio.h>
#include<math.h>
void process(int A, int B, int lf, int rg)
{
int diff = fabs(A - B);
int sum = 0;
for (int i = lf; i <= rg; i++)
sum += i;
if (diff == sum) {
printf("possible\n");
return;
}
for (int i = 1; i <= rg - lf; i++) {
int maxx = 2 * (i * rg - i * (i - 1) / 2) - sum;
int minn = 2 * (i * lf + i * (i - 1) / 2) - sum;
if (diff <= maxx && diff >= minn && ((diff - minn) % 2 == 0)) {
printf("possible\n");
return ;
}
}
printf("not possible\n");
}
int main()
{
int T;
scanf("%d", &T);
int A, B;
int lf, rg;
for (int i = 1; i <= T; i++) {
scanf("%d%d%d%d", &A, &B, &lf, &rg);
process(A, B, lf, rg);
}
return 0;
}