greedy

Please click on ads to support us..

Python Code:

from collections import Counter
def main():
  t = int(input())
  for t in range(1, t + 1):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    dmin = [0] * n
    dmax = [0] * n
    j = 0
    for i in range(n):
      while b[j] < a[i]:
        j += 1
      dmin[i] = b[j] - a[i]

    j = 0
    for i in range(n):
      j = max(i, j)
      while j+1 < n and a[j+1] <= b[j]:
        j += 1
      dmax[i] = b[j] - a[i]

    print(" ".join(list(map(str, dmin))))
    print(" ".join(list(map(str, dmax))))



if __name__ == "__main__":
  main()

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
bool visited[100009];
bool on_stack[100009];
unordered_map<int,vector<int>> d;
vector<int> ans;
 

// Fiding the d_min: Find the first thing larger than or equal to the current index - maybe two pointer?
// Finding the d_largest: The issue is that you can't always go for the max, because you could have a greater number i
 
int main() {
    int t;
    cin>>t;
    for(int i = 0; i<t; i++){
        int n;
        cin>>n;
        vector<int> a;
        vector<int> b;
        for(int j = 0; j<n; j++){
            int g;
            cin>>g;
            a.push_back(g);
        }
        for(int j = 0; j<n; j++){
            int g;
            cin>>g;
            b.push_back(g);
        }
        vector<int> mi;
        vector<int> ma;
        int right = n-1;
        int ptr1 = right;
        int ptr2 = right;
        while(right>=0){
            while(ptr1>=0 and b[ptr1]>=a[right]){
                // cout<<ptr1<<endl;
                ptr1--;
            }
            // cout<<"hi"<<endl;
            mi.push_back(b[ptr1+1]-a[right]);
            ma.push_back(b[ptr2]-a[right]);
            if(ptr2-ptr1==n-right){
                ptr2 = ptr1;
                n = right;
                //cout<<ptr2<<endl;
            }
            right--;
        }
        reverse(mi.begin(),mi.end());

        for(auto s:mi){
            cout<<s<<" ";
        }
        reverse(ma.begin(),ma.end());
        cout<<endl;
        for(auto s: ma){
            cout<<s<<" ";
        }
        cout<<endl;
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation