답안 #14407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14407 2015-05-14T06:15:04 Z gs14004 Jakarta Skyscrapers (APIO15_skyscraper) C++14
57 / 100
1000 ms 98252 KB
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;

int m, n;

vector<int> vertex;
vector<int> dummy[30005];
vector<int> dx[30005];

bool r_dir[30005][180], l_dir[30005][180];
int px[30005], py[30005];

vector<int> graph[30005];
int *graph0, *graph1;
bool *vis;

queue<int> pq[2];

inline int oldpi(int i, int j){
    return n * j + i;
}

inline int oldpi_first(int p){
    return (p % n + n) % n;
}

inline int oldpi_second(int p){
    return (p - oldpi_first(p)) / n;
}

int dijkstra(int st, int ed){
    pq[0].push(st);
    int dist = 0;
    while (1) {
        if(pq[dist%2].empty()) break;
        while (!pq[dist%2].empty()) {
            int qf = pq[dist%2].front();
            pq[dist%2].pop();
            if(qf == ed){
                return dist;
            }
            if(vis[qf]) continue;
            vis[qf] = 1;
            if(qf < n){
                for (auto &i : graph[qf]){
                    if(!vis[i]){
                        pq[dist%2].push(i);
                    }
                }
            }
            if(qf >= n && graph0[qf] != 0 && !vis[graph0[qf] - 1]){
                pq[dist%2].push(graph0[qf] - 1);
            }
            if(qf >= n && graph1[qf] != 0 && !vis[graph1[qf]]){
                pq[(dist+1)%2].push(graph1[qf]);
            }
        }
        dist++;
    }
    return -1;
}

int main(){
    int st = 0, ed = 0;
    scanf("%d %d",&n,&m);
    for (int i=0; i<m; i++) {
        int x,y;
        scanf("%d %d",&x,&y);
        px[i] = x;
        py[i] = y;
        if(i == 0){
            st = x;
        }
        else if(i == 1){
            ed = x;
        }
        if(y < 180){
            r_dir[x][y] = 1;
            l_dir[x][y] = 1;
        }
        else{
            for (int j=0; j * y + x < n; j++) {
                dx[j * y + x].push_back(y);
            }
            for (int j=0; j * y + x >= 0; j--) {
                dx[j * y + x].push_back(-y);
            }
        }
    }
    for (int i=1; i<180; i++) {
        for (int j=i; j<n; j++) {
            r_dir[j][i] |= r_dir[j - i][i];
        }
        for (int j=n-i-1; j>=0; j--) {
            l_dir[j][i] |= l_dir[j + i][i];
        }
        for (int j=0; j<n; j++) {
            if(r_dir[j][i]){
                dx[j].push_back(i);
            }
            if(l_dir[j][i]){
                dx[j].push_back(-i);
            }
        }
    }
    for (int i=0; i<n; i++) {
        sort(dx[i].begin(),dx[i].end());
        dx[i].resize(unique(dx[i].begin(),dx[i].end()) - dx[i].begin());
        for (auto &j : dx[i]){
            vertex.push_back(oldpi(i,j));
        }
    }
    sort(vertex.begin(),vertex.end());
    graph0 = (int*) calloc(n + vertex.size(),sizeof(int));
    graph1 = (int*) calloc(n + vertex.size(),sizeof(int));
    vis = (bool*) calloc(n + vertex.size(),sizeof(bool));
    for(int t=0; t<m; t++){
        int i = px[t];
        int j = py[t];
        int pos = (int)(lower_bound(vertex.begin(),vertex.end(),oldpi(i,j)) - vertex.begin());
        if(pos != vertex.size() && vertex[pos] == oldpi(i,j)){
            graph[i].push_back(pos + n);
        }
        pos = (int)(lower_bound(vertex.begin(),vertex.end(),oldpi(i,-j)) - vertex.begin());
        if(pos != vertex.size() && vertex[pos] == oldpi(i,-j)){
            graph[i].push_back(pos + n);
        }
    }
    for (int i=0; i<vertex.size(); i++){
        int tmp = vertex[i];
        graph0[i+n] = oldpi_first(vertex[i]) + 1;
        if(oldpi_first(tmp) + oldpi_second(tmp) < 0 || oldpi_first(tmp) + oldpi_second(tmp) >= n)
            continue;
        
        tmp = oldpi(oldpi_first(tmp) + oldpi_second(tmp),oldpi_second(tmp));
        int np = (int)(lower_bound(vertex.begin(),vertex.end(),tmp) - vertex.begin());
        if(np == vertex.size() || vertex[np] != tmp) continue;
        graph1[i+n] = np + n;
    }
    printf("%d\n",dijkstra(st,ed));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14476 KB Output is correct
2 Correct 0 ms 14476 KB Output is correct
3 Correct 0 ms 14476 KB Output is correct
4 Correct 0 ms 14476 KB Output is correct
5 Correct 0 ms 14476 KB Output is correct
6 Correct 0 ms 14476 KB Output is correct
7 Correct 0 ms 14476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14476 KB Output is correct
2 Correct 0 ms 14476 KB Output is correct
3 Correct 0 ms 14476 KB Output is correct
4 Correct 0 ms 14476 KB Output is correct
5 Correct 0 ms 14476 KB Output is correct
6 Correct 0 ms 14476 KB Output is correct
7 Correct 0 ms 14476 KB Output is correct
8 Correct 0 ms 14476 KB Output is correct
9 Correct 0 ms 14476 KB Output is correct
10 Correct 3 ms 14476 KB Output is correct
11 Correct 0 ms 14616 KB Output is correct
12 Correct 0 ms 14476 KB Output is correct
13 Correct 0 ms 14476 KB Output is correct
14 Correct 4 ms 14624 KB Output is correct
15 Correct 2 ms 14624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14476 KB Output is correct
2 Correct 0 ms 14476 KB Output is correct
3 Correct 0 ms 14476 KB Output is correct
4 Correct 0 ms 14476 KB Output is correct
5 Correct 0 ms 14476 KB Output is correct
6 Correct 0 ms 14476 KB Output is correct
7 Correct 2 ms 14476 KB Output is correct
8 Correct 0 ms 14476 KB Output is correct
9 Correct 0 ms 14476 KB Output is correct
10 Correct 0 ms 14476 KB Output is correct
11 Correct 3 ms 14616 KB Output is correct
12 Correct 3 ms 14476 KB Output is correct
13 Correct 0 ms 14476 KB Output is correct
14 Correct 0 ms 14624 KB Output is correct
15 Correct 4 ms 14624 KB Output is correct
16 Correct 0 ms 14612 KB Output is correct
17 Correct 8 ms 14884 KB Output is correct
18 Correct 6 ms 14624 KB Output is correct
19 Correct 6 ms 14608 KB Output is correct
20 Correct 3 ms 14620 KB Output is correct
21 Correct 3 ms 14476 KB Output is correct
22 Correct 8 ms 14616 KB Output is correct
23 Correct 6 ms 14620 KB Output is correct
24 Correct 7 ms 14796 KB Output is correct
25 Correct 7 ms 14772 KB Output is correct
26 Correct 10 ms 14764 KB Output is correct
27 Correct 4 ms 14624 KB Output is correct
28 Correct 10 ms 15052 KB Output is correct
29 Correct 15 ms 15672 KB Output is correct
30 Correct 3 ms 15004 KB Output is correct
31 Correct 11 ms 15336 KB Output is correct
32 Correct 12 ms 15028 KB Output is correct
33 Correct 32 ms 16760 KB Output is correct
34 Correct 36 ms 16784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14476 KB Output is correct
2 Correct 0 ms 14476 KB Output is correct
3 Correct 2 ms 14476 KB Output is correct
4 Correct 0 ms 14476 KB Output is correct
5 Correct 0 ms 14476 KB Output is correct
6 Correct 0 ms 14476 KB Output is correct
7 Correct 0 ms 14476 KB Output is correct
8 Correct 0 ms 14476 KB Output is correct
9 Correct 0 ms 14476 KB Output is correct
10 Correct 0 ms 14476 KB Output is correct
11 Correct 2 ms 14616 KB Output is correct
12 Correct 2 ms 14476 KB Output is correct
13 Correct 0 ms 14476 KB Output is correct
14 Correct 0 ms 14624 KB Output is correct
15 Correct 0 ms 14624 KB Output is correct
16 Correct 3 ms 14612 KB Output is correct
17 Correct 8 ms 14884 KB Output is correct
18 Correct 6 ms 14624 KB Output is correct
19 Correct 5 ms 14608 KB Output is correct
20 Correct 6 ms 14620 KB Output is correct
21 Correct 3 ms 14476 KB Output is correct
22 Correct 4 ms 14616 KB Output is correct
23 Correct 4 ms 14620 KB Output is correct
24 Correct 11 ms 14796 KB Output is correct
25 Correct 11 ms 14772 KB Output is correct
26 Correct 4 ms 14764 KB Output is correct
27 Correct 8 ms 14624 KB Output is correct
28 Correct 11 ms 15052 KB Output is correct
29 Correct 17 ms 15672 KB Output is correct
30 Correct 7 ms 15004 KB Output is correct
31 Correct 15 ms 15336 KB Output is correct
32 Correct 12 ms 15028 KB Output is correct
33 Correct 32 ms 16760 KB Output is correct
34 Correct 36 ms 16784 KB Output is correct
35 Correct 49 ms 17936 KB Output is correct
36 Correct 8 ms 15016 KB Output is correct
37 Correct 66 ms 18788 KB Output is correct
38 Correct 71 ms 18876 KB Output is correct
39 Correct 70 ms 18768 KB Output is correct
40 Correct 71 ms 18756 KB Output is correct
41 Correct 70 ms 18740 KB Output is correct
42 Correct 19 ms 15152 KB Output is correct
43 Correct 9 ms 15060 KB Output is correct
44 Correct 12 ms 15012 KB Output is correct
45 Correct 152 ms 23852 KB Output is correct
46 Correct 133 ms 23852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14476 KB Output is correct
2 Correct 2 ms 14476 KB Output is correct
3 Correct 0 ms 14476 KB Output is correct
4 Correct 0 ms 14476 KB Output is correct
5 Correct 0 ms 14476 KB Output is correct
6 Correct 2 ms 14476 KB Output is correct
7 Correct 0 ms 14476 KB Output is correct
8 Correct 0 ms 14476 KB Output is correct
9 Correct 0 ms 14476 KB Output is correct
10 Correct 3 ms 14476 KB Output is correct
11 Correct 0 ms 14616 KB Output is correct
12 Correct 0 ms 14476 KB Output is correct
13 Correct 0 ms 14476 KB Output is correct
14 Correct 0 ms 14624 KB Output is correct
15 Correct 4 ms 14624 KB Output is correct
16 Correct 3 ms 14612 KB Output is correct
17 Correct 5 ms 14884 KB Output is correct
18 Correct 9 ms 14624 KB Output is correct
19 Correct 4 ms 14608 KB Output is correct
20 Correct 7 ms 14620 KB Output is correct
21 Correct 3 ms 14476 KB Output is correct
22 Correct 4 ms 14616 KB Output is correct
23 Correct 6 ms 14620 KB Output is correct
24 Correct 11 ms 14796 KB Output is correct
25 Correct 7 ms 14772 KB Output is correct
26 Correct 9 ms 14764 KB Output is correct
27 Correct 7 ms 14624 KB Output is correct
28 Correct 14 ms 15052 KB Output is correct
29 Correct 21 ms 15672 KB Output is correct
30 Correct 10 ms 15004 KB Output is correct
31 Correct 11 ms 15336 KB Output is correct
32 Correct 12 ms 15028 KB Output is correct
33 Correct 36 ms 16760 KB Output is correct
34 Correct 35 ms 16784 KB Output is correct
35 Correct 49 ms 17936 KB Output is correct
36 Correct 8 ms 15016 KB Output is correct
37 Correct 67 ms 18788 KB Output is correct
38 Correct 67 ms 18876 KB Output is correct
39 Correct 74 ms 18768 KB Output is correct
40 Correct 59 ms 18756 KB Output is correct
41 Correct 70 ms 18740 KB Output is correct
42 Correct 16 ms 15152 KB Output is correct
43 Correct 12 ms 15060 KB Output is correct
44 Correct 15 ms 15012 KB Output is correct
45 Correct 146 ms 23852 KB Output is correct
46 Correct 148 ms 23852 KB Output is correct
47 Correct 257 ms 31712 KB Output is correct
48 Correct 150 ms 20264 KB Output is correct
49 Correct 128 ms 18920 KB Output is correct
50 Correct 129 ms 18752 KB Output is correct
51 Correct 200 ms 23108 KB Output is correct
52 Correct 213 ms 23672 KB Output is correct
53 Correct 145 ms 18932 KB Output is correct
54 Correct 86 ms 16016 KB Output is correct
55 Correct 104 ms 16836 KB Output is correct
56 Correct 116 ms 17304 KB Output is correct
57 Correct 265 ms 29804 KB Output is correct
58 Correct 111 ms 17792 KB Output is correct
59 Correct 127 ms 18144 KB Output is correct
60 Correct 136 ms 19644 KB Output is correct
61 Correct 119 ms 18020 KB Output is correct
62 Correct 263 ms 25768 KB Output is correct
63 Correct 827 ms 68952 KB Output is correct
64 Correct 979 ms 73848 KB Output is correct
65 Execution timed out 1000 ms 98252 KB Program timed out
66 Halted 0 ms 0 KB -