답안 #471952

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471952 2021-09-11T23:28:39 Z hidden1 Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
2 ms 1724 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 < 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) {
                if(k == bucket[d][rght]) {
                    lft = rght;
                    rght ++;
                } else {
                    if(lft != i - 1) {
                        g[bucket[d][lft]].push_back({k, abs(bucket[d][lft] - k) / d});
                    }
                    if(rght != j + 1) {
                        g[bucket[d][rght]].push_back({k, abs(bucket[d][rght] - k) / d});
                    }
                }
            }
        }
    }
}

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});
            }
        }
    }
    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:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |             while(j < bucket[d].size() && bucket[d][j + 1] % d == bucket[d][i] % d) {
      |                   ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1612 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 2 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1724 KB Output is correct
2 Correct 1 ms 1724 KB Output is correct
3 Incorrect 1 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1612 KB Output is correct
2 Correct 1 ms 1612 KB Output is correct
3 Incorrect 2 ms 1612 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1724 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 -