Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- freopen("/home/daminator/Desktop/Programming/YandexAlgorithms/1.0/5. Префиксные суммы и два указателя/test.cpp", "r", stdin);
- freopen("/home/daminator/Desktop/Programming/YandexAlgorithms/1.0/5. Префиксные суммы и два указателя/test.cpp", "w", stdout);
- ios::sync_with_stdio();
- cin.tie(0);
- int N, M;
- cin>>N;
- vector<int> shirts;
- vector<int> shorts;
- for(int i = 0; i < N; i++)
- {
- int x;
- cin >> x;
- shirts.push_back(x);
- }
- cin >> M;
- for(int i = 0; i < M; i++)
- {
- int x;
- cin >> x;
- shorts.push_back(x);
- }
- int mn = abs(shirts[0]-shorts[0]);
- int mnN = shirts[0], mnM = shorts[0];
- int J = 0;
- for(int i = 0; i < N; i++)
- {
- int n = shirts[i];
- for(int j = J; j < M; j++)
- {
- int m = shorts[j];
- if(n==m){cout << n << " " << m; return 0;}
- if(m<n)
- {
- if(abs(n-m)<mn){mn = abs(n-m); mnN = n, mnM = m; J = j;}
- else{J = j;}
- }
- if(m>n)
- {
- if(abs(n-m)<mn){mn = abs(n-m); mnN = n, mnM = m; J = j;}
- else{J = j; break;}
- }
- }
- }
- cout << mnN << " " << mnM;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement