From eaf3fc044cb281cb3c5aab755492682d11769e0b Mon Sep 17 00:00:00 2001 From: TooYoungTooSimp <6648049+TooYoungTooSimp@users.noreply.github.com> Date: Thu, 22 Dec 2016 08:07:05 +0800 Subject: [PATCH] =?UTF-8?q?bzoj=201179=201202=201207=201257=201293=201295?= =?UTF-8?q?=201303=201912=202257=203097=204198=20=E5=92=8C=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E8=B0=83=E4=BA=86=E4=B8=A4=E4=B8=AA=E5=B0=8F=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E5=82=BB=E9=80=BC=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Graph/NetworkFlow/ISAP_no_pointers.cpp | 72 ++++++ .../{ISAP.cpp => ISAP_with_pointers.cpp} | 210 +++++++++--------- OnlineJudges/lydsy/1179.cpp | 81 +++++++ OnlineJudges/lydsy/1202.cpp | 37 +++ OnlineJudges/lydsy/1207.cpp | 15 ++ OnlineJudges/lydsy/1257.cpp | 14 ++ OnlineJudges/lydsy/1293.cpp | 27 +++ OnlineJudges/lydsy/1295.cpp | 61 +++++ OnlineJudges/lydsy/1303.cpp | 22 ++ OnlineJudges/lydsy/1912.cpp | 59 +++++ OnlineJudges/lydsy/2257.cpp | 22 ++ OnlineJudges/lydsy/3097.cpp | 20 ++ OnlineJudges/lydsy/4198.cpp | 44 ++++ 13 files changed, 579 insertions(+), 105 deletions(-) create mode 100644 Algorithms/Graph/NetworkFlow/ISAP_no_pointers.cpp rename Algorithms/Graph/NetworkFlow/{ISAP.cpp => ISAP_with_pointers.cpp} (91%) create mode 100644 OnlineJudges/lydsy/1179.cpp create mode 100644 OnlineJudges/lydsy/1202.cpp create mode 100644 OnlineJudges/lydsy/1207.cpp create mode 100644 OnlineJudges/lydsy/1257.cpp create mode 100644 OnlineJudges/lydsy/1293.cpp create mode 100644 OnlineJudges/lydsy/1295.cpp create mode 100644 OnlineJudges/lydsy/1303.cpp create mode 100644 OnlineJudges/lydsy/1912.cpp create mode 100644 OnlineJudges/lydsy/2257.cpp create mode 100644 OnlineJudges/lydsy/3097.cpp create mode 100644 OnlineJudges/lydsy/4198.cpp diff --git a/Algorithms/Graph/NetworkFlow/ISAP_no_pointers.cpp b/Algorithms/Graph/NetworkFlow/ISAP_no_pointers.cpp new file mode 100644 index 0000000..0b67a4e --- /dev/null +++ b/Algorithms/Graph/NetworkFlow/ISAP_no_pointers.cpp @@ -0,0 +1,72 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +const int maxn = 210; +int head[maxn], from[maxn << 1], to[maxn << 1], cap[maxn << 1], next[maxn << 1], ecnt, m, n; +int d[maxn], que[maxn], cur[maxn], num[maxn], fa[maxn]; +inline void addEdge(int f, int t, int c) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + from[ecnt] = f; + to[ecnt] = t; + cap[ecnt] = c; + ecnt++; +} +int ISAP(int s, int e) +{ + int flow = 0; + int h = 0, t = 0, x; + for (int i = 1; i <= n; i++) d[i] = n; + d[que[t++] = e] = 0; + while (h != t) for (int i = head[x = que[h++]]; i; i = next[i]) + if (cap[i ^ 1] > 0 && d[to[i]] > d[x] + 1) + d[que[t++] = to[i]] = d[x] + 1; + memset(num, 0, sizeof(num)); + for (int i = 1; i <= n; i++) num[d[i]]++, cur[i] = head[i]; + x = s; + while (d[s] < n) + { + if (x == e) + { + int curFlow = 0x3f3f3f3f; + for (x = e; x != s; x = from[fa[x]]) + curFlow = min(curFlow, cap[fa[x]]); + for (x = e; x != s; x = from[fa[x]]) + cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow; + flow += curFlow; + x = s; + } + bool needRetreat = true; + for (int i = cur[x]; needRetreat && ~i; i = next[i]) + if (cap[i] && d[to[i]] == d[x] - 1) + needRetreat = false, cur[x] = i, fa[x = to[i]] = i; + if (needRetreat) + { + int mn = n - 1; + for (int i = head[x]; ~i; i = next[i]) + if (cap[i]) mn = min(mn, d[to[i]]); + if (--num[d[x]] == 0) break; + num[d[x] = mn + 1]++; + cur[x] = head[x]; + if (x != s) x = from[fa[x]]; + } + } + return flow; +} +int main() +{ + while (~scanf("%d%d", &m, &n)) + { + memset(head, -1, sizeof(head)); + ecnt = 0; + for (int i = 0, x, y, z; i < m; i++) + { + scanf("%d%d%d", &x, &y, &z); + addEdge(x, y, z); + addEdge(y, x, 0); + } + printf("%d\n", ISAP(1, n)); + } + return 0; +} diff --git a/Algorithms/Graph/NetworkFlow/ISAP.cpp b/Algorithms/Graph/NetworkFlow/ISAP_with_pointers.cpp similarity index 91% rename from Algorithms/Graph/NetworkFlow/ISAP.cpp rename to Algorithms/Graph/NetworkFlow/ISAP_with_pointers.cpp index e7f7faf..15c997b 100644 --- a/Algorithms/Graph/NetworkFlow/ISAP.cpp +++ b/Algorithms/Graph/NetworkFlow/ISAP_with_pointers.cpp @@ -1,105 +1,105 @@ -#include -#include -struct Edge -{ - int from, to, cap; - Edge *next, *rev; - Edge(int f, int t, int c) :from(f), to(t), cap(c), next(0), rev(0) {} -}; -typedef Edge* lpEdge; -inline int min(int a, int b) { return (a < b ? a : b); } -lpEdge addEdge(lpEdge* G, int from, int to, int cap) -{ - lpEdge newEdge = new Edge(from, to, cap); - newEdge->next = G[from]; - G[from] = newEdge; - return newEdge; -} -const int maxn = 201, inf = 0x3f3f3f3f; -int m, n; -int num[maxn], d[maxn]; -lpEdge G[maxn], cur[maxn], fa[maxn]; -int ISAP(int s, int e, int cnt) -{ - int flow = 0, x; - int* que = new int[maxn]; - int head, tail; - head = tail = 0; - for (int i = 0; i < cnt; i++) d[i] = cnt; - d[e] = 0; - que[tail++] = e; - while (head < tail) - { - x = que[head++]; - for (lpEdge i = G[x]; i; i = i->next) - if (i->rev->cap > 0 && d[i->to] >= cnt) - que[tail++] = i->to, - d[i->to] = d[x] + 1; - } - delete[] que; - memset(cur, 0, sizeof(cur)); - memset(num, 0, sizeof(num)); - for (int i = 0; i < cnt; i++) num[d[i]]++; - x = s; - while (d[s] < cnt) - { - if (x == e) - { - int curFlow = inf; - lpEdge curEdge; - while (x != s) - { - curEdge = fa[x]; - curFlow = min(curFlow, curEdge->cap); - x = curEdge->from; - } - x = e; - while (x != s) - { - curEdge = fa[x]; - curEdge->cap -= curFlow; - curEdge->rev->cap += curFlow; - x = curEdge->from; - } - flow += curFlow; - x = s; - } - bool needRetreat = true; - for (lpEdge& i = (cur[x] ? cur[x] : cur[x] = G[x]); i; i = i->next) - if (i->cap > 0 && d[x] == d[i->to] + 1) - { - fa[i->to] = i; - x = i->to; - needRetreat = false; break; - } - if (needRetreat) - { - int dis = cnt - 1; - for (lpEdge i = G[x]; i; i = i->next) - if (i->cap) - dis = min(dis, d[i->to]); - if (--num[d[x]] == 0) break; - num[d[x] = dis + 1]++; - if (x != s) x = fa[x]->from; - } - } - return flow; -} -int main() -{ - lpEdge edge1, edge2; - while (~scanf("%d%d", &m, &n)) - { - memset(G, 0, sizeof(G)); - for (int i = 0, s, e, c; i < m; i++) - { - scanf("%d%d%d", &s, &e, &c); - edge1 = addEdge(G, s, e, c); - edge2 = addEdge(G, e, s, 0); - edge1->rev = edge2; - edge2->rev = edge1; - } - printf("%d\n", ISAP(1, n, n + 1)); - } - return 0; -} +#include +#include +struct Edge +{ + int from, to, cap; + Edge *next, *rev; + Edge(int f, int t, int c) :from(f), to(t), cap(c), next(0), rev(0) {} +}; +typedef Edge* lpEdge; +inline int min(int a, int b) { return (a < b ? a : b); } +lpEdge addEdge(lpEdge* G, int from, int to, int cap) +{ + lpEdge newEdge = new Edge(from, to, cap); + newEdge->next = G[from]; + G[from] = newEdge; + return newEdge; +} +const int maxn = 201, inf = 0x3f3f3f3f; +int m, n; +int num[maxn], d[maxn]; +lpEdge G[maxn], cur[maxn], fa[maxn]; +int ISAP(int s, int e, const int cnt) +{ + int flow = 0, x; + int* que = new int[maxn]; + int head, tail; + head = tail = 0; + for (int i = 0; i < cnt; i++) d[i] = cnt; + d[e] = 0; + que[tail++] = e; + while (head < tail) + { + x = que[head++]; + for (lpEdge i = G[x]; i; i = i->next) + if (i->rev->cap > 0 && d[i->to] >= cnt) + que[tail++] = i->to, + d[i->to] = d[x] + 1; + } + delete[] que; + memset(cur, 0, sizeof(cur)); + memset(num, 0, sizeof(num)); + for (int i = 0; i < cnt; i++) num[d[i]]++; + x = s; + while (d[s] < cnt) + { + if (x == e) + { + int curFlow = inf; + lpEdge curEdge; + while (x != s) + { + curEdge = fa[x]; + curFlow = min(curFlow, curEdge->cap); + x = curEdge->from; + } + x = e; + while (x != s) + { + curEdge = fa[x]; + curEdge->cap -= curFlow; + curEdge->rev->cap += curFlow; + x = curEdge->from; + } + flow += curFlow; + x = s; + } + bool needRetreat = true; + for (lpEdge& i = (cur[x] ? cur[x] : cur[x] = G[x]); i; i = i->next) + if (i->cap > 0 && d[x] == d[i->to] + 1) + { + fa[x = i->to] = i; + needRetreat = false; + break; + } + if (needRetreat) + { + int dis = cnt - 1; + for (lpEdge i = G[x]; i; i = i->next) + if (i->cap) + dis = min(dis, d[i->to]); + if (--num[d[x]] == 0) break; + num[d[x] = dis + 1]++; + if (x != s) x = fa[x]->from; + } + } + return flow; +} +int main() +{ + lpEdge edge1, edge2; + while (~scanf("%d%d", &m, &n)) + { + memset(G, 0, sizeof(G)); + for (int i = 0, s, e, c; i < m; i++) + { + scanf("%d%d%d", &s, &e, &c); + edge1 = addEdge(G, s, e, c); + edge2 = addEdge(G, e, s, 0); + edge1->rev = edge2; + edge2->rev = edge1; + } + printf("%d\n", ISAP(1, n, n + 1)); + } + return 0; +} diff --git a/OnlineJudges/lydsy/1179.cpp b/OnlineJudges/lydsy/1179.cpp new file mode 100644 index 0000000..1511962 --- /dev/null +++ b/OnlineJudges/lydsy/1179.cpp @@ -0,0 +1,81 @@ +#include +inline int min(int a, int b) { return a < b ? a : b; } +inline int max(int a, int b) { return a > b ? a : b; } +const int maxn = 500010; +int n, m, s, p; +struct Graph +{ + int head[maxn], next[maxn], to[maxn], rnk[maxn], ecnt; + bool ispub[maxn]; + inline void addEdge(int f, int t) + { + ecnt++; + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + } +}g1, g2; +int low[maxn], dfn[maxn], scc[maxn], scccnt, idx, st[maxn], top, dis[maxn], que[maxn << 2]; +bool inq[maxn]; +void tarjan(int u) +{ + low[u] = dfn[u] = ++idx; + st[top++] = u; + for (int i = g1.head[u]; i; i = g1.next[i]) + if (!dfn[g1.to[i]]) tarjan(g1.to[i]), low[u] = min(low[u], low[g1.to[i]]); + else if (!scc[g1.to[i]]) + low[u] = min(low[u], dfn[g1.to[i]]); + if (dfn[u] == low[u]) + { + scccnt++; + do scc[st[--top]] = scccnt; + while (st[top] != u); + } +} +void spfa() +{ + int h, t; + h = t = 0; + que[t++] = s; + dis[s] = g2.rnk[s]; + inq[s] = true; + for (int x = 0; h ^ t; inq[que[h++]] = false) + for (int i = g2.head[x = que[h]]; i; i = g2.next[i]) + if (dis[g2.to[i]] < g2.rnk[g2.to[i]] + dis[x]) + { + dis[g2.to[i]] = g2.rnk[g2.to[i]] + dis[x]; + if (!inq[g2.to[i]]) inq[que[t++] = g2.to[i]] = true; + } +} +int main() +{ + scanf("%d%d", &n, &m); + for (int i = 1, x, y; i <= m; i++) + { + scanf("%d%d", &x, &y); + g1.addEdge(x, y); + } + for (int i = 1; i <= n; i++) scanf("%d", &g1.rnk[i]); + scanf("%d%d", &s, &p); + for (int i = 0, x; i < p; i++) + scanf("%d", &x), g1.ispub[x] = true; + for (int i = 1; i <= n; i++) + if (!dfn[i]) tarjan(i); + for (int i = 1; i <= n; i++) + { + if (g1.ispub[i]) g2.ispub[scc[i]] = true; + g2.rnk[scc[i]] += g1.rnk[i]; + } + for (int i = 1; i <= n; i++) + for (int j = g1.head[i]; j; j = g1.next[j]) + if (scc[i] != scc[g1.to[j]]) + g2.addEdge(scc[i], scc[g1.to[j]]); + s = scc[s]; + spfa(); + int ans = 0; + for (int i = 1; i <= scccnt; i++) + if (g2.ispub[i]) + ans = max(ans, dis[i]); + printf("%d", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1202.cpp b/OnlineJudges/lydsy/1202.cpp new file mode 100644 index 0000000..ec378a4 --- /dev/null +++ b/OnlineJudges/lydsy/1202.cpp @@ -0,0 +1,37 @@ +#include +int fa[105], s[105]; +int Find(int x) +{ + if (fa[x] != x) + { + int fx = fa[x]; + fa[x] = Find(fa[x]); + s[x] += s[fx]; + } + return fa[x]; +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + bool flag = true; + int n, m; + scanf("%d%d", &n, &m); + for (int i = 0; i <= n; i++) fa[i] = i, s[i] = 0; + for (int i = 0, x, y, z; i < m && flag; i++) + { + scanf("%d%d%d", &x, &y, &z); x--; + int fx = Find(x), fy = Find(y); + if (fx != fy) + { + fa[fx] = fy; + s[fx] = s[y] - s[x] + z; + } + else if (s[x] - s[y] != z) flag = false; + } + puts(flag ? "true" : "false"); + } + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1207.cpp b/OnlineJudges/lydsy/1207.cpp new file mode 100644 index 0000000..afbdb14 --- /dev/null +++ b/OnlineJudges/lydsy/1207.cpp @@ -0,0 +1,15 @@ +#include +inline int abs(int x) { return x >= 0 ? x : -x; } +int t[100010], x[100010], y[100010], f[100010], n, m, ans; +int main() +{ + scanf("%d%d", &n, &m); + for (int i = 0; i < m; i++) f[i] = 1, scanf("%d%d%d", t + i, x + i, y + i); + for (int i = 0; i < m; i++) + for (int j = 0; j < i; j++) + if (abs(x[i] - x[j]) + abs(y[i] - y[j]) <= abs(t[i] - t[j]) && f[i] < f[j] + 1) + f[i] = f[j] + 1; + for (int i = 0; i < m; i++) if (f[i] > ans) ans = f[i]; + printf("%d", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1257.cpp b/OnlineJudges/lydsy/1257.cpp new file mode 100644 index 0000000..0db1c65 --- /dev/null +++ b/OnlineJudges/lydsy/1257.cpp @@ -0,0 +1,14 @@ +#include +long long n, k, ans; +int main() +{ + scanf("%lld%lld", &n, &k); + long long sz = n < k ? n : k; + for (long long i = 1, last, t; i <= sz; i = last + 1) + { + t = k / i, last = k / t; + if (last >= n) last = n; + ans -= ((last - i + 1) * (i + last) >> 1) * t; + } + printf("%lld", ans + n * k); +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1293.cpp b/OnlineJudges/lydsy/1293.cpp new file mode 100644 index 0000000..d614816 --- /dev/null +++ b/OnlineJudges/lydsy/1293.cpp @@ -0,0 +1,27 @@ +#include +#include +inline int min(int a, int b) { return a < b ? a : b; } +struct node +{ + int pos, col; + bool operator<(const node & R) const { return pos < R.pos || (pos == R.pos&&col < R.col); } +}a[1000010]; +int cnt[65]; +int main() +{ + int n, k, ncnt = 0; + scanf("%d%d", &n, &k); + for (int i = 0, ti; i < k; i++) + for (scanf("%d", &ti); ti; ti--, ncnt++) + a[ncnt].col = i, scanf("%d", &a[ncnt].pos); + std::sort(a, a + ncnt); + int ans = 0x7fffffff, ccnt = 0; + for (int i = 0, j = 0; i < n && j < n;) + { + while (i < n && ccnt < k) ccnt += cnt[a[i++].col]++ == 0; + while (j < n && ccnt == k) ccnt -= --cnt[a[j++].col] == 0; + ans = min(ans, a[i - 1].pos - a[j - 1].pos); + } + printf("%d", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1295.cpp b/OnlineJudges/lydsy/1295.cpp new file mode 100644 index 0000000..2fb26cf --- /dev/null +++ b/OnlineJudges/lydsy/1295.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +struct point +{ + int x, y; +}que[1 << 16 | 1]; +const int dx[] = { 0, 0, 1, -1 }; +const int dy[] = { 1, -1, 0, 0 }; +int m, n, T, a[33][33], f[33][33]; +char buf[35]; +double ans; +bool inq[33][33]; +inline double dis(const point &l, const point &r) +{ + return sqrt((l.x - r.x) * (l.x - r.x) + (l.y - r.y) * (l.y - r.y)); +} +void spfa(const point& s) +{ + int h = 0, t = 0; + memset(f, 0x3f, sizeof(f)); + memset(inq, 0, sizeof(inq)); + que[t++] = s; + f[s.x][s.y] = a[s.x][s.y]; + inq[s.x][s.y] = true; + for (point cur, tmp; h != t; h++) + { + cur = que[h]; + inq[cur.x][cur.y] = false; + for (int i = 0; i < 4; i++) + { + tmp.x = cur.x + dx[i], tmp.y = cur.y + dy[i]; + if (tmp.x >= 0 && tmp.x < m && tmp.y >= 0 && tmp.y < n) + if (f[tmp.x][tmp.y] > f[cur.x][cur.y] + a[tmp.x][tmp.y] && + f[cur.x][cur.y] + a[tmp.x][tmp.y] <= T) + { + f[tmp.x][tmp.y] = f[cur.x][cur.y] + a[tmp.x][tmp.y]; + if (!inq[tmp.x][tmp.y]) que[t++] = tmp, inq[tmp.x][tmp.y] = true; + } + } + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (f[i][j] <= T) + ans = fmax(ans, dis(s, { i,j })); +} +int main() +{ + scanf("%d%d%d\n", &m, &n, &T); + for (int i = 0; i < n; i++) + { + scanf("%s", buf); + for (int j = 0; j < m; j++) + a[i][j] = buf[j] - '0'; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + spfa({ i, j }); + printf("%.6lf", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/1303.cpp b/OnlineJudges/lydsy/1303.cpp new file mode 100644 index 0000000..3377e1b --- /dev/null +++ b/OnlineJudges/lydsy/1303.cpp @@ -0,0 +1,22 @@ +#include +const int maxn = 100010; +int n, k, p, a[maxn], l[maxn << 1], r[maxn << 1]; +int main() +{ + scanf("%d%d", &n, &k); + for (int i = 1, x; i <= n; i++) + { + scanf("%d", &x); + if (x < k) a[i] = -1; + if (x > k) a[i] = 1; + if (x == k) p = i; + } + for (int i = 1; i <= n; i++) a[i] += a[i - 1]; + for (int i = 0; i < p; i++) l[a[i] + n]++; + for (int i = p; i <= n; i++) r[a[i] + n]++; + int ans = 0; + for (int i = n << 1; i >= 0; i--) + ans += l[i] * r[i]; + printf("%d", ans); + return 0; +} diff --git a/OnlineJudges/lydsy/1912.cpp b/OnlineJudges/lydsy/1912.cpp new file mode 100644 index 0000000..fde912b --- /dev/null +++ b/OnlineJudges/lydsy/1912.cpp @@ -0,0 +1,59 @@ +#include +#include +int n, k, head[100010], next[100010 << 1], from[100010 << 1], to[100010 << 1], len[100010 << 1], +maxlen, ecnt, fa[100010], mx1[100010], mx2[100010], en; +void addEdge(int f, int t) +{ + next[ecnt] = head[f]; + head[f] = ecnt; + to[ecnt] = t; + from[ecnt] = f; + len[ecnt] = 1; + ecnt++; +} +int dfs(int u, int fa) +{ + int max1 = 0, max2 = 0; + for (int i = head[u]; ~i; i = next[i]) + if (to[i] != fa) + { + int tmp = len[i] + dfs(to[i], u); + if (tmp > max1) + { + max2 = max1; + max1 = tmp; + mx2[u] = mx1[u]; + mx1[u] = i; + } + else if (tmp > max2) + { + max2 = tmp; + mx2[u] = i; + } + } + if (maxlen < max1 + max2) maxlen = max1 + max2, en = u; + return max1; +} +int main() +{ + memset(head, -1, sizeof(head)); + scanf("%d%d", &n, &k); + for (int i = 1, x, y; i < n; i++) + { + scanf("%d%d", &x, &y); + addEdge(x, y); + addEdge(y, x); + } + dfs(1, 0); + int ans = ((n - 1) << 1) - (maxlen - 1); + if (k == 2) + { + for (int i = mx1[en]; i; i = mx1[to[i]]) len[i] = len[i ^ 1] = -1; + for (int i = mx2[en]; i; i = mx1[to[i]]) len[i] = len[i ^ 1] = -1; + maxlen = 0; + dfs(1, 0); + ans -= maxlen - 1; + } + printf("%d", ans); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/2257.cpp b/OnlineJudges/lydsy/2257.cpp new file mode 100644 index 0000000..cf5720b --- /dev/null +++ b/OnlineJudges/lydsy/2257.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; +map M; +int main() +{ + int n, k; + cin >> n >> k; + for (int i = 0, j, x; i < n; i++) + { + cin >> x; + for (j = 1; j * j < x; j++) if (x % j == 0) M[j]++, M[x / j]++; + if (j * j == x) M[j]++; + } + for (map::reverse_iterator rite = M.rbegin(); rite != M.rend(); rite++) + if (rite->second >= k) + { + cout << rite->first; + break; + } + return 0; +} diff --git a/OnlineJudges/lydsy/3097.cpp b/OnlineJudges/lydsy/3097.cpp new file mode 100644 index 0000000..ee86140 --- /dev/null +++ b/OnlineJudges/lydsy/3097.cpp @@ -0,0 +1,20 @@ +#include +int n = 1, l; +char s[100001]; +int main() +{ + s[0] = 'a'; + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < n; j++) + s[j + n] = s[j] == 'a' ? 'b' : 'a'; + n <<= 1; + } + l = n >> 1; + s[n++] = 'a'; + for (int i = 1; i < l; i++) s[n++] = 'a'; + s[n++] = 'b'; + for (int i = 1; i < l; i++) s[n++] = 'a'; + printf("8192 2048\n%s", s); + return 0; +} \ No newline at end of file diff --git a/OnlineJudges/lydsy/4198.cpp b/OnlineJudges/lydsy/4198.cpp new file mode 100644 index 0000000..1c9854f --- /dev/null +++ b/OnlineJudges/lydsy/4198.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +typedef long long int64; +inline int64 _max(int64 a, int64 b) { return a > b ? a : b; } +struct word +{ + int64 w, h; + bool operator<(const word& rhs) const + { + return w > rhs.w || (w == rhs.w && h > rhs.h); + } +}; +priority_queue heap; +int main() +{ + int n, k; + scanf("%d%d", &n, &k); + for (int i = 0; i < n; i++) + { + int64 x; + scanf("%lld", &x); + heap.push({ x, 0 }); + } + int top = (k - 1 - (n - 1) % (k - 1)) % (k - 1); + for (int i = 0; i < top; i++) heap.push({ 0, 0 }); + top += n; + int64 ans = 0; + for (; top != 1; top -= k - 1) + { + int64 w, h; + w = h = 0; + for (int i = 0; i < k; i++) + { + w += heap.top().w; + h = _max(h, heap.top().h); + heap.pop(); + } + ans += w; + heap.push({ w, h + 1 }); + } + printf("%lld\n%lld", ans, heap.top().h); + return 0; +} \ No newline at end of file