#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 100005; int n, k, i, j, tab[2][N], occ[2][N], l, m, res[12], act, x, y, pozX[2 * N], pozY[2 * N], xA, yA, xB, yB, xC, yC, zap[2 * N], bat[10], d1, d; int main() { scanf("%d %d", &n, &k); for (i=0;i<n;i++) { scanf("%d", &tab[0][i]); pozX[tab[0][i]] = i; pozY[tab[0][i]] = 0; } for (i=0;i<n;i++) { scanf("%d", &tab[1][i]); pozX[tab[1][i]] = i; pozY[tab[1][i]] = 1; } for (i=2*n;i>=1;i--) { act = 0; x = pozX[i]; y = pozY[i]; xA = (x - 1 + n) % n; xB = x; xC = (x + 1) % n; yA = y; yB = 1 - y; yC = y; d = 0; if (tab[yA][xA] > i) bat[d++] = tab[yA][xA]; if (tab[yB][xB] > i) bat[d++] = tab[yB][xB]; if (tab[yC][xC] > i) bat[d++] = tab[yC][xC]; if (tab[yB][xC] > i) bat[d++] = tab[yB][xC]; if (tab[yB][xA] > i) bat[d++] = tab[yB][xA]; bat[d++] = 2 * n + 1; sort(bat, bat + d); d1 = 0; zap[i] = 1; //for (j=0;j<d;j++) printf(" %d", bat[j]); printf("\n"); for (j=i;j<=2*n;j++) { x = pozX[j]; y = pozY[j]; if (bat[d1] == j) { while (bat[d1] == j) d1++; xA = (x - 1 + n) % n; xB = x; xC = (x + 1) % n; yA = y; yB = 1 - y; yC = y; zap[j] = 0; if (!occ[yA][xA] && !occ[yB][xB] && !occ[yC][xC]) { zap[j] = 1; } else if (!occ[yA][xA] && !occ[yB][xB]) { } else if (!occ[yC][xC] && !occ[yB][xB]) { } else if (!occ[yA][xA] && !occ[yC][xC]) { } else if (!occ[yA][xA]) { if (!occ[yB][xC]) zap[j] = -1; } else if (!occ[yC][xC]) { if (!occ[yB][xA]) zap[j] = -1; } else if (!occ[yB][xB]) { zap[j] = -1; } else { if (!occ[yB][xA] && !occ[yB][xC]) zap[j] = -2; else if (!occ[yB][xA] || !occ[yB][xC]) zap[j] = -1; } } //printf("%d\n", zap[j]); act += zap[j]; occ[y][x] = 1; if (act < 1) act = 1; if (act <= k) res[act]++; //printf("%d %d %d\n", i, j, act); } for (j=i;j<=2*n;j++) occ[pozY[j]][pozX[j]] = 0; } for (i=1;i<=k;i++) printf("%d ", res[i]); printf("\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 100005; int n, k, i, j, tab[2][N], occ[2][N], l, m, res[12], act, x, y, pozX[2 * N], pozY[2 * N], xA, yA, xB, yB, xC, yC, zap[2 * N], bat[10], d1, d; int main() { scanf("%d %d", &n, &k); for (i=0;i<n;i++) { scanf("%d", &tab[0][i]); pozX[tab[0][i]] = i; pozY[tab[0][i]] = 0; } for (i=0;i<n;i++) { scanf("%d", &tab[1][i]); pozX[tab[1][i]] = i; pozY[tab[1][i]] = 1; } for (i=2*n;i>=1;i--) { act = 0; x = pozX[i]; y = pozY[i]; xA = (x - 1 + n) % n; xB = x; xC = (x + 1) % n; yA = y; yB = 1 - y; yC = y; d = 0; if (tab[yA][xA] > i) bat[d++] = tab[yA][xA]; if (tab[yB][xB] > i) bat[d++] = tab[yB][xB]; if (tab[yC][xC] > i) bat[d++] = tab[yC][xC]; if (tab[yB][xC] > i) bat[d++] = tab[yB][xC]; if (tab[yB][xA] > i) bat[d++] = tab[yB][xA]; bat[d++] = 2 * n + 1; sort(bat, bat + d); d1 = 0; zap[i] = 1; //for (j=0;j<d;j++) printf(" %d", bat[j]); printf("\n"); for (j=i;j<=2*n;j++) { x = pozX[j]; y = pozY[j]; if (bat[d1] == j) { while (bat[d1] == j) d1++; xA = (x - 1 + n) % n; xB = x; xC = (x + 1) % n; yA = y; yB = 1 - y; yC = y; zap[j] = 0; if (!occ[yA][xA] && !occ[yB][xB] && !occ[yC][xC]) { zap[j] = 1; } else if (!occ[yA][xA] && !occ[yB][xB]) { } else if (!occ[yC][xC] && !occ[yB][xB]) { } else if (!occ[yA][xA] && !occ[yC][xC]) { } else if (!occ[yA][xA]) { if (!occ[yB][xC]) zap[j] = -1; } else if (!occ[yC][xC]) { if (!occ[yB][xA]) zap[j] = -1; } else if (!occ[yB][xB]) { zap[j] = -1; } else { if (!occ[yB][xA] && !occ[yB][xC]) zap[j] = -2; else if (!occ[yB][xA] || !occ[yB][xC]) zap[j] = -1; } } //printf("%d\n", zap[j]); act += zap[j]; occ[y][x] = 1; if (act < 1) act = 1; if (act <= k) res[act]++; //printf("%d %d %d\n", i, j, act); } for (j=i;j<=2*n;j++) occ[pozY[j]][pozX[j]] = 0; } for (i=1;i<=k;i++) printf("%d ", res[i]); printf("\n"); return 0; } |