Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=2578
Dating with girls (1) Description
Everyone in the HDU knows, the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ' IQ, the girls make a problem, and the boys who can solve the problem
Correctly and cost less time can date with them.
The problem is that:give your n positive integers and an integer k. Need to calculate how many different solutions the equation x + y = k have. X and y must be among the given n integers. Solutions is different if x0! = x1 or y0! = y1.
Now smart acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!
Input
The first line contain an integer T. Then T cases followed. Each case begins with the integers n (2 <= n <= 100000), K (0 <= K < 2^31). And then the next line contain n integers.
Output
For each cases,output the numbers of solutions to the equation.
Sample Input
2
5 4
1 2 3) 4 5
8 8
1 4 5 7 8 9 2 6
Sample Output
3
5
First sort, then two points, there may be duplicate elements, go to the weight can ...
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <map>8#include <Set>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingstd::p air; the usingstd::vector; - usingStd::unique; - usingStd::lower_bound; - #definePB (E) push_back (e) + #defineSZ (c) (int) (c). Size () - #defineMP (A, b) Make_pair (A, B) + #defineAll (c) (c). Begin (), (c). End () A #defineITER (c) Decltype ((c). Begin ()) at #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intMax_n =100010; -typedef unsignedLong Longull; in intN, M; -vector<int>Vec; to voidsolve () { + intAns =0; - sort (All (VEC)); the vec.erase (All (VEC)), Vec.end ()); * Rep (i, Sz (VEC)) { $vector<int>::iterator ite = Lower_bound (All (VEC), M-vec[i]);Panax Notoginseng if(Ite! = Vec.end () && *ite + vec[i] = = m) | | Vec[i] <<1= = m) ans++; - } the vec.clear (); +printf"%d\n", ans); A } the intMain () { + #ifdef LOCAL -Freopen ("In.txt","R", stdin); $Freopen ("OUT.txt","w+", stdout); $ #endif - intT, v; -scanf"%d", &t); the while(t--) { -scanf"%d%d", &n, &m);WuyiRep (i, N) scanf ("%d", &v), VEC.PB (v); the solve (); - } Wu return 0; -}
View Code
Hdu 2578 Dating with girls (1)