"Solving" A-b

Source: Internet
Author: User

"Problem description"
It's a painful thing to have a quiz!
Topic see more also have aesthetic fatigue, so I abandoned everyone familiar a+b problem, switch to a-B ha
Ha!
Well, here's the question: give a bunch of numbers and a number C, which requires that all a-b=c be counted.
Number. (number pairs of numbers in different positions are counted in different pairs)
Input
The first line consists of 2 non-negative integers N and C, separated by a space in the middle.
The second line has N integers, separated by a space, as the number of strings required to be processed.
Output
Outputs a row that represents the number of all the pairs of a-b=c that are included in the number of strings.
"Input and Output sample"

Dec.in Dec.out
4 1 3
1 1 2 3
First, such as 2,3,4,6,7, to make the difference of 1.
You find that 3-2 equals 1, then 4-2 must be greater than 1, so to reduce to 1, only 2 behind that number is possible.
So if 4-3 equals 1, then 6 must start from the 3 behind that number, so the problem is very simple.
Of course, if there is repetition, you still need to record it.

1#include <algorithm>2#include <iostream>3#include <fstream>4#include <cstdlib>5 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/6 using namespacestd;7 8Ifstream Fin ("dec.in");9Ofstream Fout ("Dec.out");Ten  One intcnt_shu=0; A Long LongCha=0; - Long Longshu[200005]={0}; - intcnt_cs[200005]={0}; the Long Longsze_jianshu=0; - Long Longcs=0; -  - BOOLQqsort (Long LongALong Longb); + voidXunintSze_jian); -  + BOOLQqsort (Long LongALong Longb) { A  if(A&LT;B)return 1; at  return 0;  - } -  -  - voidXunintSze_jian) { -  if(Sze_jianshu==sze_jian) sze_jianshu++;  in   for(intx=sze_jianshu;x<=cnt_shu;x++){ -   if(shu[x]-shu[sze_jian]==cha) { tocs+=cnt_cs[sze_jian]*Cnt_cs[x]; +sze_jianshu=x;  -   }       the   if(Shu[x]-shu[sze_jian]<cha) sze_jianshu=x; *   Else  Break; $  }    Panax Notoginseng  return; - }  the  + intMainintargcChar*argv[]) { AFin>>cnt_shu>>cha; the  if(cha<0) cha=0-cha; +   for(intx=1; x<=cnt_shu;x++) fin>>Shu[x]; -Sort (shu+1, shu+1+cnt_shu,qqsort); $   $  intDa=1, gs=1, num=shu[1]; -   for(intx=2; x<=cnt_shu+1; x + +){ -   if(shu[x]==num&&x!=cnt_shu+1){ thegs++; -    Continue;Wuyi   }      theshu[da]=num;  -num=Shu[x]; Wucnt_cs[da++]=GS; -gs=1; About  } $   -  if(cha==0){ -   Long Longcs1=0; -    for(intx=1; x<da;x++){ Acs1+= (cnt_cs[x]* (cnt_cs[x]-1))/2; +   }     thefout<<CS1; -   return 0;  $  } the   thesze_jianshu=1;  the   for(intx=1; x<da;x++){ the Xun (x);  -  } infout<<CS; the  return 0; the}

"Solving" A-B

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.