Thursday, December 15, 2016

101 Hack 44 - Picking Numbers

Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is .
Input Format
The first line contains a single integer, , denoting the size of the array. 
The second line contains  space-separated integers describing the respective values of .
Constraints
  • The answer will be .
Output Format
A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is .
Sample Input 0
6
4 6 5 3 3 1
Sample Output 0
3
Explanation 0
We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference  (i.e.,  and ), so we print the number of chosen integers, , as our answer.
Sample Input 1
6
1 2 2 3 1 2
Sample Output 1
5
Explanation 1
We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference  (i.e., , and ), so we print the number of chosen integers, , as our answer.

Solution:
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4. typedef long long LL;
  5. const int S = 100003;
  6. const int MOD = 1e9+7;
  7. const double pi = 2 * acos( 0.0 );
  8.  
  9. inline int _Int(){ int x; scanf( "%d"&); return x; }
  10. inline LL _LLi(){ LL x; scanf( "%lld"&); return x; }
  11. void pruts(){ puts( "-1" ); exit( EXIT_SUCCESS ); }
  12.  
  13. int dirX[] = { 10-101-11-1 };
  14. int dirY[] = { 010-11-1-11 };
  15. int rX[] = { 1122-1-1-2-2 };
  16. int rY[] = { 2-21-12-21-1 };
  17.  
  18. template < class T > T tri_Area( T x1, T y1, T x2, T y2, T x3, T y3 ){ return abs( x1*( y2-y3 ) - y1*( x2-x3 ) + ( x2*y3-x3*y2 ) );};
  19. template < class T > T Distance( T x1, T y1, T x2, T y2 ){ return sqrt( ( x1-x2 ) * ( x1-x2 ) + ( y1-y2 ) * ( y1-y2 ) ); };
  20. template < class T > T bigMod( T n, T p, T m ){ if( p == 0 )return 1; if( p&1 )return ( n*bigMod( n, p-1, m ) )%m; T x = bigMod( n, p/2, m ); return ( x*)%m; };
  21.  
  22. int sset(int N, int pos){return N=N|(1<<pos);}
  23. bool check(int N, int pos){return (bool)(N&(1<<pos));}
  24. int reset(int N, int pos){return N=N&~(1<<pos);}
  25. /*******************###########################################********************
  26. ********************##   MD. YA-SEEN ARAFAT(ThunderStroke)   ##********************
  27. ********************##    CSE, University of Asia Pacific    ##********************
  28. ********************###########################################********************/
  29.  
  30. int frq[ 100+3 ];
  31.  
  32. void Love(){
  33.     int n = _Int();
  34.     for( int i = 0; i < n; i++ )frq[ _Int() ]++;
  35.     int mx = 0;
  36.     for( int i = 1; i < n; i++ )mx = max( mx, frq[ i ]+frq[ i-1 ] );
  37.     cout << mx << endl;
  38. }
  39.  
  40. int main(){
  41.     #ifndef ONLINE_JUDGE
  42. //        freopen("in.txt", "r", stdin);
  43. //        freopen("n.txt", "w", stdout);
  44.     #endif
  45.     Love();
  46.     return 0;
  47. }

No comments:

Post a Comment