intcalc(){ int zero = 0, one = 0, seg = 0, con = 0, i = 0; while (s[i] != '\0'){ switch (s[i]){ case'0': if (!con){seg++; con = 1;} zero++; break; case'1': con = 0; one++; break; } i++; } if (zero == 0) return0; if (one == 0) return1; return seg < 2 ? seg : 2; }
intmain(){ int T; scanf("%d", &T); while (T--){ scanf("%s", &s); printf("%d\n", calc()); } return0; }
voidreverse(int p, int q){ if (q - p <= 1) return; info tmp; int i, r; for (i = p; i < (p + q) / 2; i++){ r = p + q - i - 1; tmp = b[i]; b[i] = b[r]; b[r] = tmp; } }
voidadjust(int n, int m){ int r, t = 0, pos, i, j; for (i = 0; i < n; i++){ for (j = 0; j < m; j++, t++){ if (j == 0){r = b[t].a; pos = t; continue;} if (b[t].a != r){ r = b[t].a; reverse(pos, t); pos = t; } } reverse(pos, t); } }
intcalc(int n, int m){ int sum = 0, i, j, k, t; for (i = 0; i < n; i++){ for (j = m - 1; j >= 0; j--){ t = b[i * m + j].i; for (k = j - 1; k >= 0; k--){ if (b[i * m + k].i < t) sum++; } } } return sum; }
intmain(){ int T; int n, m, i, t; scanf("%d", &T); while (T--){ scanf("%d %d", &n, &m); t = n * m; for (i = 0; i < t; i++){ scanf("%d", &b[i].a); b[i].i = i; } qsort(b, t, sizeof (info), cmp); adjust(n, m); printf("%d\n", calc(n, m)); } return0; }
// Referred to the code on https://codeforces.com/contest/1566/submission/128689795
#include<stdio.h> #include<vector>
usingnamespace std;
vector<int> tree[200001]; int ans;
intdfs(int node, int pre){ int n = 0, i; for (i = 0; i < tree[node].size(); i++){ int next = tree[node][i]; if (next != pre) n += dfs(next, node); } if (n == 0) return1; ans += n - 1; return0; }
intmain(){ int T, n, a, b, i; scanf("%d", &T); while (T--){ scanf("%d", &n); for (i = 1; i <= n; i++) tree[i].clear(); for (i = 1; i < n; i++){ scanf("%d %d", &a, &b); tree[a].push_back(b), tree[b].push_back(a); } ans = 1; dfs(1, 0); printf("%d\n", ans); } return0; }