Saturday, 9 May 2015

 Dress'em in Vests!

The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y ≥ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x ≤ bj ≤ ai + y.
Input
The first input line contains four integers nmx and y (1 ≤ n, m ≤ 1050 ≤ x, y ≤ 109) — the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) in non-decreasing order, separated by single spaces — the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 ≤ bj ≤ 109) in non-decreasing order, separated by single spaces — the sizes of the available vests.
Output
In the first line print a single integer k — the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (uivi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Sample test(s)
input
5 3 0 0
1 2 3 3 4
1 3 5
output
2
1 1
3 2
input
3 3 2 2
1 5 9
3 5 7
output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped.
---------------   ---------------------------------------------------------EDITORIAL----------------------------------
consider a singel vest can be  provide to any 1 solder only  and vest size and solder requirement is already sorted so .. so start iterating from 1st vest check whether 1 sth vest can fit 1st solder or not if it is valid for 1st solder than increase pointer for both solder and vest ans also increase count of  covered .
else -- if size of vest is small than arr[i]-x than this vest canot be fit to any solder since  i+i th solder demand more bigger vest than ith solder ,, so increase pointer of vest ..
else if size of vest is more than arr[i]+y than ith person cant gat any vest since no vest is available with size less than arr[i]+y so increase pointer of solder ......
------------------------------------------------------------CODE-----------------------------------------------------
#include<iostream>
using namespace std;
typedef long long int lli;
#include<bits/stdc++.h>
int main()
 {
  lli n;
  cin>>n;
  lli m;
   cin>>m;
   lli x,y;
    cin>>x>>y;
   lli arr[n+10];
   lli brr[m+10];
   vector<pair<lli,lli> >v;
    for(int i=0;i<n;i++) cin>>arr[i];
     for(int j=0;j<m;j++) cin>>brr[j];
     lli s=0;
     lli c=0;
     lli cov=0;
      while(s<n && c<m)
       {
        if(arr[s]-x>brr[c]) c++;
        else if(arr[s]+y<brr[c]) s++;
        else
        {
        v.push_back(make_pair(s+1,c+1));
        s++;
        c++;
        cov++;
        }
       }
        cout<<cov<<endl;
        for(int i=0;i<cov;i++)
         {
          cout<<v[i].first<<" "<<v[i].second<<endl;
         }
      return 0;
  }
  

No comments:

Post a Comment