Solving Wormholes (ZCO 2012)

-2

Description:

The year is 2102 and today is the day of ZCO. This year there are N contests and the starting and ending times of each contest is known to you. You have to participate in exactly one of these contests. Different contests may overlap. The duration of different contests might be different.

There is only one examination centre. There is a wormhole V that transports you from your house to the examination centre and another wormhole W that transports you from the examination centre back to your house. Obviously, transportation through a wormhole does not take any time; it is instantaneous. But the wormholes can be used at only certain fixed times, and these are known to you.

So, you use a V wormhole to reach the exam centre, possibly wait for some time before the next contest begins, take part in the contest, possibly wait for some more time and then use a W wormhole to return back home. If you leave through a V wormhole at time t1 and come back through a W wormhole at time t2, then the total time you have spent is (t2 - t1 + 1). Your aim is to spend as little time as possible overall while ensuring that you take part in one of the contests.

You can reach the centre exactly at the starting time of the contest, if possible. And you can leave the examination centre the very second the contest ends, if possible. You can assume that you will always be able to attend at least one contest–that is, there will always be a contest such that there is a V wormhole before it and a W wormhole after it.

For instance, suppose there are 3 contests with (start,end) times (15,21), (5,10), and (7,25), respectively. Suppose the V wormhole is available at times 4, 14, 25, 2 and the W wormhole is available at times 13 and 21. In this case, you can leave by the V wormhole at time 14, take part in the contest from time 15 to 21, and then use the W wormhole at time 21 to get back home. Therefore the time you have spent is (21 - 14 + 1) = 8. You can check that you cannot do better than this.

Input:

The first line contains 3 space separated integers N, X, and Y, where N is the number of contests, X is the number of time instances when wormhole V can be used and Y is the number of time instances when wormhole W can be used. The next N lines describe each contest. Each of these N lines contains two space separated integers S and E, where S is the starting time of the particular contest and E is the ending time of that contest, with S < E. The next line contains X space separated integers which are the time instances when the wormhole V can be used. The next line contains Y space separated integers which are the time instances when the wormhole W can be used.

Output:

Print a single line that contains a single integer, the minimum time needed to be spent to take part in a contest.

My code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
#define loop(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define ll long long
#define inf 9223372036854775807
#define printgrid(arr,m,n){cout<<"[";for(int i=0;i<m;++i){if(i!=0)cout<<" ";cout<<"[";for(int j=0;j<n;++j){cout<<arr[i][j];if(j!=n-1)cout<<" ";}cout<<"]";if(i!=m-1)cout<<endl;}cout<<"]"<<endl;}
using namespace std;


int searchl(int *arr,int s,int v)
{
    int l=0;
    int r=s-1;
    while(l<=r)
    {
        int m = (l+r)/2;
        if (arr[m] == v) return v;
        if(arr[m]<v)
        {
            l = m + 1;
        } else {
            r = m - 1;
        }
    }
    if (r<0) r=0;
    return arr[r];
}

int searchh(int *arr,int s,int v)
{
    int l=0;
    int r=s-1;
    while(l<=r)
    {
        int m = (l+r)/2;
        if (arr[m] == v) return v;
        if(arr[m]<v)
        {
            l = m + 1;
        } else {
            r = m - 1;
        }
    }
    if(l>s-1) l=s-1;
    return arr[l];
}


int main() {
    int n,x,y;
    cin>>n>>x>>y;
    vector<pair<int,int>> v(n);
    int a[x];
    int b[y];
    loop(i,n) cin >> v[i].first >> v[i].second;
    loop(i,x) cin >> a[i];
    loop(i,y) cin >> b[i];
    sort(a,a+x);
    sort(b,b+y);

    int mn = 0x7fffffff;

    int l;
    int u;

    loop(i,n)
    {
        l = searchl(a,x,v[i].first);
        u = searchh(b,y,v[i].second);
        mn = min(mn,(u-l+1));
    }
    cout << mn << endl;
}

What I tried is I use binary search for the lower range and upper range. and return the closest integer in the list. This should result in the lowest difference. This code AC's in 90% of the testcases however fails two testcases with a wrong answer.

c++
algorithm
sorting
binary-search
asked on Stack Overflow Nov 27, 2019 by 3xpl017 • edited Nov 27, 2019 by Nico Schertler

1 Answer

0

Correct code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
#define loop(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define ll long long
#define inf 9223372036854775807
#define printgrid(arr,m,n){cout<<"[";for(int i=0;i<m;++i){if(i!=0)cout<<" ";cout<<"[";for(int j=0;j<n;++j){cout<<arr[i][j];if(j!=n-1)cout<<" ";}cout<<"]";if(i!=m-1)cout<<endl;}cout<<"]"<<endl;}
using namespace std;


int searchl(int *arr,int s,int v)
{
    int l=0;
    int r=s-1;
    while(l<=r)
    {
        int m = (l+r)/2;
        if (arr[m] == v) return v;
        if(arr[m]<v)
        {
            l = m + 1;
        } else {
            r = m - 1;
        }
    }
    if (r<0) return -1;
    return arr[r];
}

int searchh(int *arr,int s,int v)
{
    int l=0;
    int r=s-1;
    while(l<=r)
    {
        int m = (l+r)/2;
        if (arr[m] == v) return v;
        if(arr[m]<v)
        {
            l = m + 1;
        } else {
            r = m - 1;
        }
    }
    if(l>s-1) return -1;
    return arr[l];
}


int main() {
    cin.sync_with_stdio(false);
    cin.tie(NULL);
    int n,x,y;
    cin>>n>>x>>y;
    vector<pair<int,int>> v(n);
    int a[x];
    int b[y];
    loop(i,n) cin >> v[i].first >> v[i].second;
    loop(i,x) cin >> a[i];
    loop(i,y) cin >> b[i];
    sort(a,a+x);
    sort(b,b+y);

    int mn = 0x7fffffff;

    int l;
    int u;

    loop(i,n)
    {
        l = searchl(a,x,v[i].first);
        u = searchh(b,y,v[i].second);
        if(u==-1 or l==-1) continue;
        mn = min(mn,(u-l+1));
    }
    cout << mn << endl;
}
answered on Stack Overflow Nov 28, 2019 by 3xpl017

User contributions licensed under CC BY-SA 3.0