Sort & Merge in reverse order: New Snooker

Source: Internet
Author: User

Title Description
Description

Snooker, also known as the English billiards, is a popular billiards sport. On the ball table, the four Corners and the center of the two long sides have a hole, the use of the ball is 1 white ball, 15 red ball and 6 balls (yellow, green, brown, blue, pink, black) a total of 22 balls.

The batting order is a red ball, a colored ball until the red balls all fall into the bag, and then in yellow, green, brown, blue, pink, black in the order of each shot, and finally to score high wins. Snooker's charm is also the ability to play defensive ball, can create some obstacle ball so that the other side can not hit the target ball and be deducted points. It is because of this that snooker is a sport full of magic.

Now consider a new snooker, set the mother Ball (the ball is the white ball, used to hit the other ball) the marking of M, table has n red ball in a row, each red ball has a label, their marking represents their score.

Now hit the red ball with the mother Ball, one stroke, if the mother ball in contact with the red ball, it is called "K to red ball." We assume that you can hit any number of contiguous red balls at a time, or you can hit only one ball. And the red ball will neither fall into the bag nor collide with each other, but just stay in place. Each time you hit, you want to "K to red ball", at least to hit a red ball, if you want to hit more than one red ball, then hit the red ball must be sequentially in sequence. If a "K to red ball" all the red ball of the number of the sum of the average is greater than the female ball of the number m, a "combo".

Now, please calculate how many "combo" scenarios you can have in total.

Note: If there are three red balls labeled 1, 2, 3, the female ball is 0, there are 6 ways to get the "combo" scheme: (1), (2), (3), (), (2,3), (three-way)

Enter a description Input Description

There are two lines.

The first line is N,m (n<=100000,m<=10000), n indicates a total of n red balls on the table, and M denotes the number of the mother ball.

The second line is n positive integers, which in turn represent the number of n red balls on the table, and all the labels are no more than 10000.

Output description Output Description

There is only one number, which is the total number of "combo" scenarios.

Sample input Sample Input

4 3

3 7 2 4

Sample output
Sample Output
7

It is assumed that the interval of i+1 to J is established;
(Sum[j]-sum[i])/(j-i) >m
Sum[j]-sum[i]> (j-i) *m
Sum[j]-sum[i]>m*j-m*i
M*I-SUM[I]>M*J-SUM[J]

Set A[i]=m*i-sum[i];
A[I]>A[J], the combo is set up.

******
I=0, a[i]=0;
*****
The variables are int64;

Code
var init,r,sum:array[0..10000]of longint;
I,j,k:longint;
N,m,ans:longint;
Procedure Msort (S,t:longint);
var m,i,j,k:longint;
Begin if S=t then exit;
m:= (s+t) Div 2;
Msort (S,M);
Msort (m+1,t);
I:=s; j:=m+1; K:=s;
while (i<=m) and (j<=t) do
Begin IF INIT[I]>INIT[J]
Then begin r[k]:=init[i];
 ans:=ans+m-i+1; 
Inc (k);
Inc (I);
End
Else begin R[K]:=INIT[J];
Inc (k);
Inc (J);
End;
End;
while i<=m do
begin r[k]:=init[i];
Inc (k);
Inc (I);
End;
while j<=t do
begin R[K]:=INIT[J];
Inc (k);
Inc (J);
End;
for I:=s-T do
Init[i]:=r[i];
End;
Begin READLN (N,M);
Fillchar (init,sizeof (init), 0);
Fillchar (sum,sizeof (sum), 0);
Fillchar (r,sizeof (R), 0);
init[0]:=0;
For I:=1 to N do
Begin read (init[i]);
Sum[i]:=sum[i-1]+init[i];
End
For i:=0 to N do
Init[i]:=m*i-sum[i];
ans:=0;
init[0]:=0;
{
For i:=0 to N do
Write (Init[i], ");
Writeln;
}
Msort (0,n);
Writeln (ANS);
End.

Sort & Merge in reverse order: New Snooker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.