Codeforces 161A Dress'em in Vests!
なんかどっちを固定するかで一度、ミスって大変だった。
O(max(n,m))かなぁ。直感的には。わからん。
#include <iostream> #include <iomanip> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <bitset> #include <numeric> #include <algorithm> #include <functional> #include <cctype> #include <complex> #include <string> #include <sstream> #define pb push_back #define all(c) c.begin(),c.end() #define rall(c) c.rbegin(),c.rend() #define rep(i,n) for(int i=0;i<(n);i++) #define tr(container, it) \ for(typeof(container.begin()) it = container.begin(); it != container.end(); ++it) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) (find(all(container),element) != container.end()) #define sz(a) ((int)a.size()) typedef long long ll; const int dx[] = {1,0,-1,0}; const int dy[] = {0,-1,0,1}; const double EPS = 1e-9; using namespace std; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); int n,m,x,y; cin >> n >> m >> x >> y; vector<int> A(n); vector<pair<int,int> > H(n); vector<int> B(m); rep(i,n) cin >> A[i]; rep(i,m) cin >> B[i]; sort(all(A)); sort(all(B)); rep(i,n){ H[i] = make_pair(A[i]-x,A[i]+y); } vector<pair<int,int> > out; int ret = 0; int ind = 0; rep(i,n){ for(int j=ind;j<m;j++){ if(H[i].first <= B[j] && B[j] <= H[i].second){ out.push_back(make_pair(i+1,j+1)); ind = j+1; ret++; break; }else if(H[i].second < B[j]){ break; } ind = j+1; } } cout << out.size() << endl; tr(out,it){ cout << (*it).first << " " << (*it).second << endl; } return 0; }