fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <climits>
  5. #include <vector>
  6. #include <set>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include <map>
  10.  
  11. using namespace std;
  12. const int MOD = 1000000007;
  13.  
  14.  
  15. int main() {
  16. int n, x;cin >> n >> x;
  17. vector<int>v;
  18. for (int i = 1;i <= n;i++) {
  19. int value;cin >> value;
  20. v.push_back(value);
  21. }
  22.  
  23. // capture list
  24. // []: Không lấy giá trị nào bên ngoài
  25. // [=]: lấy tất cả giá trị bên ngoài ở dạng pass by value, giá trị
  26. // [&]: lấy tất cả giá trị bên ngoài ở dạng pass by reference, tham chiếu
  27. // [x]: lấy x dạng pass by value
  28. // [&x]: lấy x dạng pass by ref
  29. sort(v.begin(), v.end(), [x](const int& a, const int& b) {
  30. int da = abs(a - x);
  31. int db = abs(b - x);
  32. if (da != db) {
  33. return da < db;
  34. }
  35. return a < b;
  36. });
  37.  
  38. for (auto value : v) {
  39. cout << value << " ";
  40. }
  41. cout << endl;
  42.  
  43. sort(v.begin(), v.end(), [](const int& a, const int& b) {
  44. if (a % 2 == 0 && b % 2 == 0) {
  45. return a < b;
  46. }
  47. if (a % 2 == 0 && b % 2 != 0) {
  48. return true;
  49. }
  50. if (a % 2 != 0 && b % 2 != 0) {
  51. return a > b;
  52. }
  53. if (a % 2 != 0 && b % 2 == 0) {
  54. return false;
  55. }
  56. });
  57.  
  58. for (auto value : v) {
  59. cout << value << " ";
  60. }
  61. }
  62.  
  63.  
  64.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout