Submission #31249

#TimeUsernameProblemLanguageResultExecution timeMemory
31249nibnalinJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
273 ms7292 KiB
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

const int inf = int(1e9)+5, maxn = int(3e4)+5;

int B[maxn], P[maxn], D[maxn][2];
vector<pair<int, int>> doge[maxn];

// 0 => doge
// 1 => skyscraper

bool go(int x, int y)
{
	if(B[x] == y) return 0;
	auto it = lower_bound(doge[y].begin(), doge[y].end(), make_pair(P[x], -1));
	return (it != doge[y].end() && it->first == P[x]);
}

int main(void)
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 0;i < m;i++)
    {
        scanf("%d%d", &B[i], &P[i]), D[i][0] = inf;
        doge[B[i]].push_back({P[i], i});
    }
    for(int i = 0;i < n;i++) sort(doge[i].begin(), doge[i].end()), D[i][1] = inf;

    set<pair<int, pair<int, int>>> Q;
    D[0][0] = 0;
    Q.insert({0, {0, 0}});

    while(!Q.empty())
    {
        pair<int, pair<int, int>> top = *Q.begin();
        Q.erase(Q.begin());
        if(top.second == make_pair(1, 0))
        {
        	printf("%d\n", top.first);
        	return 0;
        }
        //cout << top.first << " " << top.second.first << " " << top.second.second << "\n";

        if(top.second.second)
        {
            for(auto it: doge[top.second.first])
            {
            	int node = it.second;
                if(top.first < D[node][0])
                {
                    if(D[node][0] != inf) Q.erase({D[node][0], {node, 0}});
                    D[node][0] = top.first;
                    Q.insert({D[node][0], {node, 0}});
                }
            }
        }
        else
        {
        	auto it = lower_bound(doge[B[top.second.first]].begin(), doge[B[top.second.first]].end(), make_pair(P[top.second.first], -1));
  			if(it->second != top.second.first) continue;
            for(int i = B[top.second.first];i < n;i += P[top.second.first])
            {
                int c = top.first+(abs(i-B[top.second.first])/P[top.second.first]);
                if(c < D[i][1])
                {
                    if(D[i][1] != inf) Q.erase({D[i][1], {i, 1}});
                    D[i][1] = c;
                    Q.insert({D[i][1], {i, 1}});
                }

                if(go(top.second.first, i)) break;
            }

            for(int i = B[top.second.first];i >= 0;i -= P[top.second.first])
            {
                int c = top.first+(abs(i-B[top.second.first])/P[top.second.first]);
                if(c < D[i][1])
                {
                    if(D[i][1] != inf) Q.erase({D[i][1], {i, 1}});
                    D[i][1] = c;
                    Q.insert({D[i][1], {i, 1}});
                }

                if(go(top.second.first, i)) break;
            }
        }
    }

    printf("%d\n", -1);
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
                          ^
skyscraper.cpp:30:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &B[i], &P[i]), D[i][0] = inf;
                                                   ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...