Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day5&day6 #892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions winter-day5/罗潇阳/7-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
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;
}
26 changes: 26 additions & 0 deletions winter-day5/罗潇阳/7-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
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;
}
39 changes: 39 additions & 0 deletions winter-day5/罗潇阳/7-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
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;
}
69 changes: 69 additions & 0 deletions winter-day5/罗潇阳/7-5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <bits/stdc++.h>
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<string, pair<int, string>> 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;
}
40 changes: 40 additions & 0 deletions winter-day5/罗潇阳/7-6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
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;
}
50 changes: 50 additions & 0 deletions winter-day5/罗潇阳/7-7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
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;
}
12 changes: 12 additions & 0 deletions winter-day5/罗潇阳/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#### 7-2

直接调用 sort

#### 7-3

桶排序

#### 7-4

冒泡排序交换次数等于逆序对数量,直接离散化用树状数组做。

38 changes: 38 additions & 0 deletions winter-day6/罗潇阳/7-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
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<int, string> 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<int, string> a, const pair<int, string> 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;
}
26 changes: 26 additions & 0 deletions winter-day6/罗潇阳/7-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
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;
}
47 changes: 47 additions & 0 deletions winter-day6/罗潇阳/7-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>
using namespace std;

#define lowbit(x) (x & (-x))
typedef long long ll;
const int MAXN = 500010;

void in(){}
template<typename T, typename ...otr_T>
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;
}
12 changes: 12 additions & 0 deletions winter-day6/罗潇阳/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#### 7-2

直接调用 sort

#### 7-3

桶排序

#### 7-4

冒泡排序交换次数等于逆序对数量,直接离散化用树状数组做。