data structures greedy implementation *800

Please click on ads to support us..

Python Code:

import os
import sys
sys.setrecursionlimit(10 ** 5)
LOCAL = True
try:
    open(f"{os.path.realpath(os.path.dirname(__file__))}/../generator.py")
except:
    LOCAL = False

MOD = 10 ** 9 + 7

fib_mem = [0, 1]
def fib(n):
    while len(fib_mem) < n + 1:
        fib_mem.append((fib_mem[-1] + fib_mem[-2]) % MOD)
    return fib_mem[n]
fac_mem = [1]
def fac(n):
    while len(fac_mem) < n + 1:
        fac_mem.append(fac_mem[-1] * len(fac_mem) % MOD)
    return fac_mem[n]
def perm(n, k):
    return fac(n) * pow(fac(k), MOD - 2, MOD) % MOD
def comb(n, k):
    return fac(n) * pow(fac(k) * fac(n - k) % MOD, MOD - 2, MOD) % MOD
def count(l):
    r = {}
    for i in l:
        r[i] = r.get(i, 0) + 1
    return r
def group(l):
    r = []
    p = None
    c = 0
    for i in l:
        if i == p:
            c += 1
        else:
            r.append((p, c))
            p = i
            c = 1
    if c:
        r.append((p, c))
    return r
class Tree:
    def __init__(self, id=0):
        self.id = id
        self.parent = None
        self.children = []
    def __repr__(self):
        if self.children:
            return f"{self.id} -> [" + ", ".join(str(c) for c in self.children) + "]"
        else:
            return f"{self.id}"
def bin_search(l, a, b, g):
    if a == b:
        return a
    mid = (a + b) // 2
    if g < l[mid]:
        return bin_search(l, mid + 1, b, g)
    else:
        return bin_search(l, a, mid, g)
def ints():
    return [int(j) for j in input().split()]
def strings():
    return input().split()
def deb(*l, **kwargs):
    if LOCAL:
        print("\033[2m", end="")
        print(*l, **kwargs)
        print("\033[0m", end="")
def ans(*l, **kwargs):
    if LOCAL:
        print("\033[32m", end="")
    if l and type(l[0]) == bool:
        print(["NO", "YES"][l[0]], **kwargs)
    else:
        print(*l, **kwargs)
    if LOCAL:
        print("\033[0m", end="")


for case in range(int(input())):
    n, = ints()
    s = ints()
    f = ints()

    d = [0] * n
    t = 0

    for i in range(n):
        t = max(s[i], t)
        d[i] = f[i] - t
        t = f[i]
    
    ans(*d)

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define pp pop_back
#define sz size()
#define N 200005
#define ff first
#define ss second
#define pb push_back
using namespace std;

long long t, n, s[N], f[N];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> t;

	while( t-- ){
		cin >> n;
		for(int i = 1; i <= n; i++){
			cin >> s[i];
		}

		for(int i = 1; i <= n; i++){
			cin >> f[i];
		}

		for(int i = 1; i <= n; i++){
			if(s[i] < f[i-1]){
				cout << abs(f[i-1]-f[i]) << ' ';
			}

			else {
				cout << abs(s[i]-f[i]) << ' ';
			}
		}
		cout << '\n';
	}
}


Comments

Submit
0 Comments
More Questions

1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring