Submission #471958

# Submission time Handle Problem Language Result Execution time Memory
471958 2021-09-11T23:50:43 Z hidden1 Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
2 ms 1716 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 3e4 + 10;
vector<pair<int, int> > g[MAX_N];
vector<int> bucket[MAX_N];
int currentSorter = 1;
int n, m, q;

bool cmp(const int a, const int b) {
    if(a % currentSorter == b % currentSorter) {
        return a < b;
    }
    return (a % currentSorter) < (b % currentSorter);
}

void genGraph() {
    for(int d = 0; d < MAX_N; d ++) {
        if(bucket[d].size() == 0) {continue;}
        currentSorter = d;
        sort(bucket[d].begin(), bucket[d].end(), cmp);
        bucket[d].resize(unique(bucket[d].begin(), bucket[d].end()) - bucket[d].begin());
        for(int i = 0; i < bucket[d].size(); i ++) {
            int j = i;
            while(j + 1 < bucket[d].size() && bucket[d][j + 1] % d == bucket[d][i] % d) {
                j ++;
            }
            int lft = i - 1;
            int rght = i;
            for(int k = bucket[d][i] % d; k < n; k += d) {
                while(rght != j + 1 && k == bucket[d][rght]) {
                    lft = rght;
                    rght ++;
                }
                if(lft != i - 1) {
                    g[bucket[d][lft]].push_back({k, abs(bucket[d][lft] - k) / d});
                    //cout << "From " << bucket[d][lft] << " " << g[bucket[d][lft]].back() << endl;
                }
                if(rght != j + 1) {
                    g[bucket[d][rght]].push_back({k, abs(bucket[d][rght] - k) / d});
                    //cout << "From " << bucket[d][rght] << " " << g[bucket[d][rght]].back() << endl;
                }
            }
            i = j;
        }
    }
}

int dist[MAX_N];

int dijkstra() {
    for(int i = 0; i < n; i ++) {
        dist[i] = mod;
    }
    priority_queue<pair<int, int> > pq;
    pq.push({0, 0});
    while(!pq.empty()) {
        auto curr = pq.top(); pq.pop();
        curr.first *= -1;
        for(auto it : g[curr.second]) {
            if(dist[it.first] > curr.first + it.second) {
                dist[it.first] = curr.first + it.second;
                pq.push({-dist[it.first], it.first});
            }
        }
    }
    if(dist[1] == mod) {
        return -1;
    } else {
        return dist[1];
    }
}

signed main() {
    //ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m;
    for(int i = 0; i < m; i ++) {
        int a, b;
        cin >> a >> b;
        bucket[b].push_back(a);
    }
    genGraph();
    cout << dijkstra() << endl;
    return 0;
}


Compilation message

skyscraper.cpp: In function 'void genGraph()':
skyscraper.cpp:33:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         for(int i = 0; i < bucket[d].size(); i ++) {
      |                        ~~^~~~~~~~~~~~~~~~~~
skyscraper.cpp:35:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |             while(j + 1 < bucket[d].size() && bucket[d][j + 1] % d == bucket[d][i] % d) {
      |                   ~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1704 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 1 ms 1716 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1612 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 1 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1612 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 1 ms 1708 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1612 KB Output is correct
2 Correct 2 ms 1612 KB Output is correct
3 Incorrect 2 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1612 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 1 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -