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