diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-1.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-1.cpp" new file mode 100644 index 000000000..964f03ef9 --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-1.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +int n, a[1010]; + +int main() { + n = read(); + for (int i = 1; i <= n; ++i) a[i] = read(); + int res = 0; + for (int i = 1; i <= n; ++i) + for (int j = i + 1; j <= n; ++j) + if (abs(a[i] - a[j]) == 1) + ++res; + printf("%d\n", res); + return 0; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-3.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-3.cpp" new file mode 100644 index 000000000..2d66044b3 --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-3.cpp" @@ -0,0 +1,26 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +const int N = 500010; +int n, a[N]; + +int main() { + n = read(); + for (int i = 1; i <= 2 * n; ++i) a[i] = read(); + nth_element(a + 1, a + n, a + 1 + 2 * n); + printf("%d\n", a[n]); + return 0; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-4.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-4.cpp" new file mode 100644 index 000000000..0c700e350 --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-4.cpp" @@ -0,0 +1,39 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +const int N = 500010; +int n, a[N]; + +int main() { + n = read(); + for (int i = 1; i <= n; ++i) a[i] = read(); + int x = read(), l = 1, r = n, mid = 1, cnt = 0; + while (l <= r) { + mid = (l + r) >> 1; + ++cnt; + if (a[mid] == x) + break; + if (a[mid] < x) + l = mid + 1; + else + r = mid - 1; + } + if (a[mid] != x) + printf("-1\n%d\n", cnt); + else + printf("%d\n%d\n", mid - 1, cnt); + return 0; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-5.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-5.cpp" new file mode 100644 index 000000000..e3b371593 --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-5.cpp" @@ -0,0 +1,69 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +typedef long long ll; +const int MAXN = 500010; + +map> f; + +bool Check(string s1, string s2); + +int main() { + int n = read(); + for (int i = 1; i <= n; ++i) { + string a, b; + cin >> a >> b; + if (b[b.size() - 1] == 'f') + f[a] = make_pair(2, ""); + else if (b[b.size() - 1] == 'm') + f[a] = make_pair(1, ""); + else if (b[b.size() - 1] == 'n') + f[a] = make_pair(1, b.substr(0, b.size() - 4)); + else if (b[b.size() - 1] == 'r') + f[a] = make_pair(2, b.substr(0, b.size() - 7)); + } + int m = read(); + while (m--) { + string a, b, c, d; + cin >> a >> b >> c >> d; + if (f.find(a) == f.end() || f.find(c) == f.end()) { + cout << "NA\n"; + continue; + } else if (f[a].first == f[c].first) { + cout << "Whatever\n"; + continue; + } else if (Check(a, c)) + cout << "Yes\n"; + else + cout << "No\n"; + } + return 0; +} + +bool Check(string s1, string s2) { + int fa = 1; + for (string sa = s1; !sa.empty(); sa = f[sa].second, fa++) { + int fb = 1; + for (string sb = s2; !sb.empty(); sb = f[sb].second, fb++) { + if (fa >= 5 && fb >= 5) + return 1; + if (sa == sb && (fa < 5 || fb < 5)) { + return 0; + } + } + } + return 1; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-6.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-6.cpp" new file mode 100644 index 000000000..8d84c25e2 --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-6.cpp" @@ -0,0 +1,40 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +const int N = 500010; +int n, a[N]; + +int main() { + while (cin >> n) { + for (int i = 1; i <= n; ++i) a[i] = read(); + sort(a + 1, a + 1 + n); + for (int i = 1; i <= n; ++i) + cout << a[i] << (i == n ? '\n' : ' '); + int m = read(); + while (m--) { + int x = read(); + int y = lower_bound(a + 1, a + 1 + n, x) - a; + if (a[y] == x) + cout << y; + else + cout << "0"; + if (m) + cout << " "; + } + cout << "\n"; + } + return 0; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/7-7.cpp" "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-7.cpp" new file mode 100644 index 000000000..dac38bf4a --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/7-7.cpp" @@ -0,0 +1,50 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +const int N = 500010; + +int n, m; +var a[N]; + +bool Check(var mid); + +int main() { + n = read(), m = read(); + for (int i = 1; i <= n; ++i) + a[i] = read(); + sort(a + 1, a + 1 + n); + var l = 1, r = 0x7fffffff, ans; + while (l <= r) { + var mid = (l + r) >> 1; + if (Check(mid)) + ans = mid, l = mid + 1; + else + r = mid - 1; + } + cout << ans << "\n"; + return 0; +} + +bool Check(var mid) { + int cnt = 1; + var lst = a[1]; + for (int i = 2; i <= n; ++i) + if (a[i] - lst >= mid) + lst = a[i], ++cnt; + if (cnt >= m) + return 1; + return 0; +} diff --git "a/winter-day5/\347\275\227\346\275\207\351\230\263/solution.md" "b/winter-day5/\347\275\227\346\275\207\351\230\263/solution.md" new file mode 100644 index 000000000..ce875e78e --- /dev/null +++ "b/winter-day5/\347\275\227\346\275\207\351\230\263/solution.md" @@ -0,0 +1,12 @@ +#### 7-2 + +直接调用 sort + +#### 7-3 + +桶排序 + +#### 7-4 + +冒泡排序交换次数等于逆序对数量,直接离散化用树状数组做。 + diff --git "a/winter-day6/\347\275\227\346\275\207\351\230\263/7-2.cpp" "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-2.cpp" new file mode 100644 index 000000000..3d18642e0 --- /dev/null +++ "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-2.cpp" @@ -0,0 +1,38 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +const int N = 110; + +int n; +pair a[N]; + +int main() { + n = read(); + for (int i = 1; i <= n; ++i) { + string s; + cin >> s; + int v = read(); + a[i] = make_pair(v, s); + } + sort(a + 1, a + 1 + n, [=] (const pair a, const pair b) { + if (a.first == b.first) + return a.second < b.second; + return a.first > b.first; + }); + for (int i = 1; i <= n; ++i) + cout << a[i].second << " " << a[i].first << "\n"; + return 0; +} diff --git "a/winter-day6/\347\275\227\346\275\207\351\230\263/7-3.cpp" "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-3.cpp" new file mode 100644 index 000000000..c2e4bbb16 --- /dev/null +++ "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-3.cpp" @@ -0,0 +1,26 @@ +#include +using namespace std; + +typedef long long var; +typedef long double let; +#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++(i)) +#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i)) +#define repl(i, t) for (int i = fi[t]; i; i = ne[i]) + +var read() { + var a = 0, s = 0, c = getchar(); + while (!isdigit(c)) s |= c == '-', c = getchar(); + while (isdigit(c)) a = a * 10 + c - '0', c = getchar(); + return s ? -a : a; +} + +int n, a[55]; + +int main() { + n = read(); + for (int i = 1; i <= n; ++i) + a[read()]++; + for (int i = 0; i <= 50; ++i) + a[i] ? printf("%d:%d\n", i, a[i]) : printf(""); + return 0; +} diff --git "a/winter-day6/\347\275\227\346\275\207\351\230\263/7-4.cpp" "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-4.cpp" new file mode 100644 index 000000000..8de9f7bf1 --- /dev/null +++ "b/winter-day6/\347\275\227\346\275\207\351\230\263/7-4.cpp" @@ -0,0 +1,47 @@ +#include +using namespace std; + +#define lowbit(x) (x & (-x)) +typedef long long ll; +const int MAXN = 500010; + +void in(){} +template +void in(T &t, otr_T & ...otr) +{ + cin >> t; + in(otr...); +} + +int n, m, a[MAXN], b[MAXN]; +ll sum[MAXN]; + +void add(int pos, ll val) +{ + for(; pos <= n; pos += lowbit(pos)) + sum[pos] += val; +} + +ll ask(int pos) +{ + ll val = 0; + for(; pos; pos -= lowbit(pos)) + val += sum[pos]; + return val; +} + +int main() +{ + in(n); ll ans = 0; + for(int i = 1; i <= n; ++i) in(b[i]), a[i] = b[i]; + sort(b + 1, b + 1 + n); + m = unique(b + 1, b + 1 + n) - b - 1; + for(int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b; + for(int i = n; i; --i) + { + ans += ask(a[i] - 1); + add(a[i], 1); + } + cout << ans << "\n"; + return 0; +} \ No newline at end of file diff --git "a/winter-day6/\347\275\227\346\275\207\351\230\263/solution.md" "b/winter-day6/\347\275\227\346\275\207\351\230\263/solution.md" new file mode 100644 index 000000000..ce875e78e --- /dev/null +++ "b/winter-day6/\347\275\227\346\275\207\351\230\263/solution.md" @@ -0,0 +1,12 @@ +#### 7-2 + +直接调用 sort + +#### 7-3 + +桶排序 + +#### 7-4 + +冒泡排序交换次数等于逆序对数量,直接离散化用树状数组做。 +