Advertisement
STANAANDREY

bacT16 SIII 3

May 21st, 2021
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream fin("bac.txt");
  5. #define NMAX (int)1e6 + 3
  6. int n, m, arr1[NMAX], arr2[NMAX];
  7. int ans[NMAX * 2] = {-1}, top;
  8.  
  9. void readArr(int len, int arr[]) {
  10.     for (int i = 1; i <= len; i++) {
  11.         fin >> arr[i];
  12.     }
  13. }
  14.  
  15. int getMini(int a, int b) {
  16.     return a > b ? b : a;
  17. }
  18.  
  19. void addRest(int it, int len, int arr[]) {
  20.     while (it <= len) {
  21.         if (ans[top] % 2 != arr[it] % 2) {
  22.             ans[++top] = arr[it];
  23.         }
  24.         it++;
  25.     }
  26. }
  27.  
  28. void intercl() {
  29.     int it1 = 1, it2 = 1;
  30.     while (it1 <= m && it2 <= n) {
  31.         int mini = getMini(arr1[it1], arr2[it2]);
  32.         if (ans[top] == -1 || (ans[top] % 2 != mini % 2)) {
  33.             ans[++top] = mini;
  34.         }
  35.         if (arr1[it1] == mini && arr2[it2] == mini) {
  36.             it1++, it2++;
  37.         } else if (arr2[it2] != mini && arr1[it1] == mini) {
  38.             it1++;
  39.         } else {
  40.             it2++;
  41.         }
  42.     }
  43.     addRest(it1, m, arr1);
  44.     addRest(it2, n, arr2);
  45. }
  46.  
  47. void write() {
  48.     for (int i = 1; i <= top; i++) {
  49.         cout << ans[i] << ' ';
  50.     }
  51. }
  52.  
  53. int main() {
  54.     fin >> m >> n;
  55.     readArr(m, arr1);
  56.     readArr(n, arr2);
  57.     intercl();
  58.     write();
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement