Skip to content

Commit 1e9973e

Browse files
more usaco
1 parent 5945946 commit 1e9973e

20 files changed

+846
-93
lines changed

usaco/.DS_Store

0 Bytes
Binary file not shown.

usaco/silver/abc

108 KB
Binary file not shown.

usaco/silver/abc.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
6+
using vi = vector<int>;
7+
#define pb push_back
8+
#define all(x) begin(x), end(x)
9+
#define sz(x) (int) (x).size()
10+
11+
using pi = pair<int,int>;
12+
#define f first
13+
#define s second
14+
#define mp make_pair
15+
16+
void __print(int x) {cerr << x;}
17+
void __print(long x) {cerr << x;}
18+
void __print(long long x) {cerr << x;}
19+
void __print(unsigned x) {cerr << x;}
20+
void __print(unsigned long x) {cerr << x;}
21+
void __print(unsigned long long x) {cerr << x;}
22+
void __print(float x) {cerr << x;}
23+
void __print(double x) {cerr << x;}
24+
void __print(long double x) {cerr << x;}
25+
void __print(char x) {cerr << '\'' << x << '\'';}
26+
void __print(const char *x) {cerr << '"' << x << '"';}
27+
void __print(const string &x) {cerr << '"' << x << '"';}
28+
void __print(bool x) {cerr << (x ? "true" : "false");}
29+
30+
template<typename T, typename V>
31+
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
32+
template<typename T>
33+
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
34+
void _print() {cerr << "]\n";}
35+
template <typename T, typename... V>
36+
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
37+
#ifndef ONLINE_JUDGE
38+
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
39+
#else
40+
#define debug(x...)
41+
#endif
42+
43+
void setIO(string s = "") {
44+
ios_base::sync_with_stdio(0);
45+
cin.tie(0);
46+
if ((int)s.size()) {
47+
freopen((s + ".in").c_str(), "r", stdin);
48+
freopen((s + ".out").c_str(), "w", stdout);
49+
}
50+
}
51+
52+
void solve() {
53+
int n;
54+
cin >> n;
55+
vi nums(n);
56+
for (int i = 0; i < n; i++) {
57+
cin >> nums[i];
58+
}
59+
set<int> s;
60+
for (int i : nums) {
61+
s.insert(i);
62+
for (int j : nums) {
63+
if (i < j) s.insert(j - i);
64+
}
65+
}
66+
int triplets = 0;
67+
for (int a : s) {
68+
for (int b : s) {
69+
for (int c : s) {
70+
if (a <= b && b <= c) {
71+
bool works = true;
72+
set<int> p{a, b, c, a + b, a + c, b + c, a + b + c};
73+
for (int x : nums) {
74+
if (!p.count(x)) works = false;
75+
}
76+
if (works) triplets++;
77+
}
78+
}
79+
}
80+
}
81+
cout << triplets << endl;
82+
}
83+
84+
int main() {
85+
setIO();
86+
int t;
87+
cin >> t;
88+
while (t--) solve();
89+
return 0;
90+
}

usaco/silver/cowoperations

67.3 KB
Binary file not shown.

usaco/silver/cowoperations.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using vi = vector<int>;
6+
#define pb push_back
7+
#define all(x) begin(x), end(x)
8+
#define sz(x) (int) (x).size()
9+
10+
using pi = pair<int,int>;
11+
#define f first
12+
#define s second
13+
#define mp make_pair
14+
15+
void __print(int x) {cerr << x;}
16+
void __print(long x) {cerr << x;}
17+
void __print(long long x) {cerr << x;}
18+
void __print(unsigned x) {cerr << x;}
19+
void __print(unsigned long x) {cerr << x;}
20+
void __print(unsigned long long x) {cerr << x;}
21+
void __print(float x) {cerr << x;}
22+
void __print(double x) {cerr << x;}
23+
void __print(long double x) {cerr << x;}
24+
void __print(char x) {cerr << '\'' << x << '\'';}
25+
void __print(const char *x) {cerr << '"' << x << '"';}
26+
void __print(const string &x) {cerr << '"' << x << '"';}
27+
void __print(bool x) {cerr << (x ? "true" : "false");}
28+
29+
template<typename T, typename V>
30+
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
31+
template<typename T>
32+
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
33+
void _print() {cerr << "]\n";}
34+
template <typename T, typename... V>
35+
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
36+
#ifndef ONLINE_JUDGE
37+
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
38+
#else
39+
#define debug(x...)
40+
#endif
41+
42+
void setIO(string s = "") {
43+
ios_base::sync_with_stdio(0);
44+
cin.tie(0);
45+
if ((int)s.size()) {
46+
freopen((s + ".in").c_str(), "r", stdin);
47+
freopen((s + ".out").c_str(), "w", stdout);
48+
}
49+
}
50+
51+
/*
52+
OW -> WCCO -> WO (you can swap neighborring letters)
53+
you can rearrange the word however you want
54+
55+
we can test based on parity because order doens't matter
56+
*/
57+
58+
int main() {
59+
setIO();
60+
string s;
61+
int q;
62+
cin >> s >> q;
63+
int n = sz(s);
64+
vi c(n + 1, 0), o(n + 1, 0), w(n + 1, 0);
65+
for (int i = 0; i < n; i++) {
66+
c[i + 1] = c[i] + (s[i] == 'C');
67+
o[i + 1] = o[i] + (s[i] == 'O');
68+
w[i + 1] = w[i] + (s[i] == 'W');
69+
}
70+
while (q--) {
71+
int l, r;
72+
cin >> l >> r;
73+
int C = (c[r] - c[l - 1]) % 2;
74+
int O = (o[r] - o[l - 1]) % 2;
75+
int W = (w[r] - w[l - 1]) % 2;
76+
77+
// (C O W)
78+
// (0 0 0) -> 0
79+
// (1 0 0) -> 1
80+
// (0 1 0) -> 0
81+
// (0 0 1) -> 0
82+
// (1 1 0) -> 0
83+
// (1 0 1) -> 0
84+
// (0 1 1) -> 1
85+
// (1 1 1) -> 0
86+
87+
if ((C == 1 && O == 0 && W == 0) || (C == 0 && O == 1 && W == 1)) {
88+
cout << "Y";
89+
} else {
90+
cout << "N";
91+
}
92+
}
93+
cout << "\n";
94+
return 0;
95+
}

usaco/silver/directions

54.9 KB
Binary file not shown.

usaco/silver/directions.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
6+
using vi = vector<int>;
7+
#define pb push_back
8+
#define all(x) begin(x), end(x)
9+
#define sz(x) (int) (x).size()
10+
11+
using pi = pair<int,int>;
12+
#define f first
13+
#define s second
14+
#define mp make_pair
15+
16+
struct DSU {
17+
vector<int> e;
18+
void init(int N) { e = vector<int>(N, -1); }
19+
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
20+
bool sameSet(int a, int b) { return get(a) == get(b); }
21+
vector<int> sameSetElements(int x) {
22+
vector<int> res;
23+
for (int i = 0; i < e.size(); i++) {
24+
if (sameSet(x, i)) res.push_back(i);
25+
}
26+
return res;
27+
}
28+
int size(int x) { return -e[get(x)]; }
29+
bool unite(int x, int y) {
30+
x = get(x), y = get(y);
31+
if (x == y) return 0;
32+
if (e[x] > e[y]) swap(x, y);
33+
e[x] += e[y]; e[y] = x;
34+
return 1;
35+
}
36+
};
37+
38+
int n;
39+
DSU dsu;
40+
const int N = 1505;
41+
int grid[N + 1][N + 1];
42+
int comp[N + 1][N + 1];
43+
bool vis[N + 1][N + 1] = {false};
44+
45+
void floodfill(int r, int c) {
46+
if (r < 0 || r >= n || c < 0 || c >= n || vis[r][c]) return;
47+
vis[r][c] = true;
48+
if ((char)grid[r][c] == 'R') {
49+
dsu.unite(r * (n + 1) + c, r * (n + 1) + c + 1);
50+
floodfill(r, c + 1);
51+
}
52+
if ((char)grid[r][c] == 'D') {
53+
dsu.unite(r * (n + 1) + c, (r + 1) * (n + 1) + c);
54+
floodfill(r + 1, c);
55+
}
56+
}
57+
58+
void solve() {
59+
ll cost = 0;
60+
for (int i = 0; i < n; i++) {
61+
cost += (dsu.size(n * (n + 1) + i) - 1) * grid[n][i];
62+
cost += (dsu.size(i * (n + 1) + n) - 1) * grid[i][n];
63+
}
64+
cout << cost << endl;
65+
}
66+
67+
int main() {
68+
cin >> n;
69+
dsu.init((n + 1) * (n + 1));
70+
for (int i = 0; i < n; i++) {
71+
string s; int c;
72+
cin >> s >> c;
73+
for (int j = 0; j < s.size(); j++) {
74+
grid[i][j] = s[j];
75+
}
76+
grid[i][n] = c;
77+
}
78+
for (int i = 0; i < n; i++) {
79+
cin >> grid[n][i];
80+
}
81+
for (int r = 0; r < n; r++) {
82+
for (int c = 0; c < n; c++) {
83+
if (!vis[r][c]) {
84+
floodfill(r, c);
85+
}
86+
}
87+
}
88+
solve();
89+
int q;
90+
cin >> q;
91+
while (q--) {
92+
int i, j;
93+
cin >> i >> j;
94+
i--; j--;
95+
if (grid[i][j] == 'R') {
96+
grid[i][j] = 'D';
97+
} else {
98+
grid[i][j] = 'R';
99+
}
100+
auto elems = dsu.sameSetElements(i * (n + 1) + j);
101+
for (auto x : elems) {
102+
vis[x / (n + 1)][x % (n + 1)] = false;
103+
dsu.e[x] = -1;
104+
}
105+
for (auto x : elems) {
106+
if (!vis[x / (n + 1)][x % (n + 1)]) {
107+
floodfill(x / (n + 1), x % (n + 1));
108+
}
109+
}
110+
solve();
111+
}
112+
return 0;
113+
}

usaco/silver/findandreplace.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
6+
using vi = vector<int>;
7+
#define pb push_back
8+
#define all(x) begin(x), end(x)
9+
#define sz(x) (int) (x).size()
10+
11+
using pi = pair<int,int>;
12+
#define f first
13+
#define s second
14+
#define mp make_pair
15+
16+
void __print(int x) {cerr << x;}
17+
void __print(long x) {cerr << x;}
18+
void __print(long long x) {cerr << x;}
19+
void __print(unsigned x) {cerr << x;}
20+
void __print(unsigned long x) {cerr << x;}
21+
void __print(unsigned long long x) {cerr << x;}
22+
void __print(float x) {cerr << x;}
23+
void __print(double x) {cerr << x;}
24+
void __print(long double x) {cerr << x;}
25+
void __print(char x) {cerr << '\'' << x << '\'';}
26+
void __print(const char *x) {cerr << '"' << x << '"';}
27+
void __print(const string &x) {cerr << '"' << x << '"';}
28+
void __print(bool x) {cerr << (x ? "true" : "false");}
29+
30+
template<typename T, typename V>
31+
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
32+
template<typename T>
33+
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
34+
void _print() {cerr << "]\n";}
35+
template <typename T, typename... V>
36+
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
37+
#ifndef ONLINE_JUDGE
38+
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
39+
#else
40+
#define debug(x...)
41+
#endif
42+
43+
void setIO(string s = "") {
44+
ios_base::sync_with_stdio(0);
45+
cin.tie(0);
46+
if ((int)s.size()) {
47+
freopen((s + ".in").c_str(), "r", stdin);
48+
freopen((s + ".out").c_str(), "w", stdout);
49+
}
50+
}
51+
52+
int main() {
53+
return 0;
54+
}

usaco/silver/loan

53.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)