1547C - Pair Programming - CodeForces Solution


greedy two pointers *1100

Please click on ads to support us..

Python Code:

def solve(s, a, b):
    k = s[0]
    indexA = 0
    indexB = 0
    l = []
    for i in range(s[1] + s[2]):
        if indexA == s[1] and indexB < s[2]:
            while indexB < s[2]:
                if b[indexB] == 0:
                    k += 1
                    l.append(0)
                    indexB += 1
                elif b[indexB] <= k:
                    l.append(b[indexB])
                    indexB += 1
                else:
                    return -1

        elif indexB == s[2] and indexA < s[1]:
            while indexA < s[1]:
                if a[indexA] == 0:
                    k += 1
                    l.append(0)
                    indexA += 1
                elif a[indexA] <= k:
                    l.append(a[indexA])
                    indexA += 1
                else:
                    return -1

        if indexA != s[1] and indexB != s[2]:
            if a[indexA] < b[indexB]:
                if a[indexA] == 0:
                    k += 1
                    l.append(0)
                    indexA += 1
                elif a[indexA] <= k:
                    l.append(a[indexA])
                    indexA += 1
                else:
                    return -1

            elif a[indexA] >= b[indexB]:
                if b[indexB] == 0:
                    k += 1
                    l.append(0)
                    indexB += 1
                elif b[indexB] <= k:
                    l.append(b[indexB])
                    indexB += 1
                else:
                    return -1

    return l


t = int(input())

for i in range(t):
    input()
    s = [int(j) for j in input().split()]
    a = [int(j) for j in input().split()]
    b = [int(j) for j in input().split()]
    l = solve(s, a, b)
    if l != -1:
        for i in l:
            print(i, end=" ")
        print()
    else:
        print(-1)

C++ Code:

#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int>PII;

const int N=200010;

int a[N],b[N];
int H,d,n,k,w,m;

signed main()
{
    int T;
    cin>>T;
    while(T--)
    {
        bool f=1;
        cin>>k>>n>>m;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)scanf("%d",&b[i]);
        vector<int>v;
        int i=0,j=0;
        while(i<=n||j<=m)
        {
            i++,j++;
            
            while(a[i]==0&&i<=n)i++,k++,v.push_back(0);
            while(b[j]==0&&j<=m)j++,k++,v.push_back(0);
            
            if(i<=n&&j<=m)
            {
                if(a[i]<=b[j])
                {
                    if(a[i]>k)f=0;
                    v.push_back(a[i]);
                    j--;
                }
                else
                {
                    if(b[j]>k)f=0;
                    v.push_back(b[j]);
                    i--;
                }
            }
            else if(i<=n&&j>m)
            {
                if(a[i]>k)f=0;
                v.push_back(a[i]);
            }
            else if(j<=m&&i>n)
            {
                if(b[j]>k)f=0;
                v.push_back(b[j]);
            }
        }
        
        if(f)
        {
            for(auto it:v)cout<<it<<' ';
            cout<<endl;
        }
        else puts("-1");
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary