[Offer收割]编程练习赛32
Posted PoorLitt1eThin9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Offer收割]编程练习赛32相关的知识,希望对你有一定的参考价值。
气泡图
两两判断关系,dfs。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> double x[1005], y[1005], r[1005]; int f[1005][1005]; const double eps = 1e-10; #include<vector> using namespace std; vector<int> G[1005]; int p[1005], father[1005]; void dfs(int x, int fa) { father[x] = fa; for (int i = 0; i < G[x].size(); i++) { p[G[x][i]]--; } for (int i = 0; i < G[x].size(); i++) { int v = G[x][i]; if (p[v] == 0 && father[v]==0) { dfs(v, x); } } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lf%lf%lf", &x[i], &y[i], &r[i]); } memset(f, 0, sizeof(f)); memset(p, 0, sizeof(p)); memset(father, 0, sizeof(father)); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (i != j) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); if (r[i] - r[j] >= d) { f[i][j] = 1; G[i].push_back(j); f[j][i] = -1; p[j]++; } if (r[j] - r[i] >= d) { f[i][j] = -1; f[j][i] = 1; G[j].push_back(i); p[i]++; } } } } for (int i = 1; i <= n; i++) { if (p[i] == 0) { dfs(i, 0); break; } } for (int i = 1; i <= n; i++) { printf("%d\n", father[i]); } return 0; }
候选人追踪
维护S集合中的最小值和其余候选人中的最大值
#include<stdio.h> #include<string.h> #include<stdlib.h> template <class T> class SegmentTree { public: T dat, lazy; int leftBorder, rightBorder, mid; SegmentTree * leftSon, * rightSon; T(* lazyfunc)(T, T); T(* mergefunc)(T, T); SegmentTree() { leftBorder = rightBorder = -1; leftSon = rightSon = NULL; } void pushdown(); void pushup(); void Build(T *, int, int, T(*)(T, T), T(*)(T, T)); void Modify(int, int, T); T Query(int, int); void Free(); }; template<class T> void SegmentTree<T>::pushdown() { if (lazy && leftBorder != rightBorder) { leftSon->dat = lazyfunc(leftSon->dat, lazy); rightSon->dat = lazyfunc(rightSon->dat, lazy); leftSon->lazy = lazyfunc(leftSon->lazy, lazy); rightSon->lazy = lazyfunc(rightSon->lazy, lazy); } lazy = (T)0; } template<class T> void SegmentTree<T>::pushup() { dat = mergefunc(leftSon->dat, rightSon->dat); } template<class T> void SegmentTree<T>::Build(T * S, int l, int r, T(* lfunc)(T, T), T(* mfunc)(T, T)) { if (l > r) { return; } lazy = (T)0; leftBorder = l; rightBorder = r; mid = (leftBorder + rightBorder) >> 1; lazyfunc = lfunc; mergefunc = mfunc; if (l == r) { dat = S[l]; return; } leftSon = new SegmentTree; leftSon->Build(S, l, mid, lfunc, mfunc); rightSon = new SegmentTree; rightSon->Build(S, mid + 1, r, lfunc, mfunc); pushup(); } template<class T> void SegmentTree<T>::Modify(int l, int r, T NewDat) { if (l > r || l < leftBorder || rightBorder < r) { return; } if (leftBorder == l && rightBorder == r) { dat = lazyfunc(dat, NewDat); lazy = lazyfunc(lazy, NewDat); return; } pushdown(); if (r <= mid) { leftSon->Modify(l, r, NewDat); } else if (mid < l) { rightSon->Modify(l, r, NewDat); } else { leftSon->Modify(l, mid, NewDat); rightSon->Modify(mid + 1, r, NewDat); } pushup(); } template<class T> T SegmentTree<T>::Query(int l, int r) { if (l > r || l < leftBorder || rightBorder < r) { return dat; } pushdown(); if (l == leftBorder && r == rightBorder) { return dat; } if (r <= mid) { return leftSon->Query(l, r); } else if (mid < l) { return rightSon->Query(l, r); } else { return mergefunc(leftSon->Query(l, mid), rightSon->Query(mid + 1, r)); } } template<class T> void SegmentTree<T>::Free() { if (leftSon != NULL) { leftSon->Free(); } if (rightSon != NULL) { rightSon->Free(); } delete leftSon; delete rightSon; } int lazyfunc(int a, int b) { return a + b; } int mergefunc(int a, int b) { return a > b ? b : a; } int n, k; bool f[320000]; class tick { public: int t, c; }; int cmp(const void *x, const void *y) { tick * tx = (tick *)x; tick * ty = (tick *)y; return tx->t > ty->t ? 1 : -1; } tick p[320000]; int index_____[320000], a[320000]; SegmentTree<int> st; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif memset(a, 0, sizeof(a)); scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d%d", &p[i].t, &p[i].c); } memset(f, false, sizeof(f)); for (int i = 0; i < k; i++) { int s; scanf("%d", &s); f[s] = true; } int cnt = 0; for (int i = 0; i < 315159; i++) { if (f[i]) { index_____[i] = cnt++; } } qsort(p, n, sizeof(tick), cmp); int ptr = 0, ans = 0, max = 0, time = p[0].t, s = -1; st.Build(a, 0, k - 1, lazyfunc, mergefunc); while (ptr < n) { do { if (f[p[ptr].c]) { st.Modify(index_____[p[ptr].c], index_____[p[ptr].c], 1); } else { a[p[ptr].c]++; if (a[p[ptr].c] > max) { max = a[p[ptr].c]; } } ptr++; } while (p[ptr].t == p[ptr - 1].t && ptr < n); int q = st.Query(0, k - 1); if (s == 1) { ans += p[ptr - 1].t - time; } time = p[ptr - 1].t; if (q > max) { s = 1; } else { s = -1; } } printf("%d\n", ans); return 0; }
以上是关于[Offer收割]编程练习赛32的主要内容,如果未能解决你的问题,请参考以下文章