Skip to content

Commit 3ae329a

Browse files
Add files via upload
1 parent ce83d37 commit 3ae329a

5 files changed

+391
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
for _ in range(int(input())):
2+
n, a, b = map(int, input().split())
3+
moves = input()
4+
5+
dx = {"N": 0, "S": 0, "E": 1, "W": -1}
6+
dy = {"N": 1, "S": -1, "E": 0, "W": 0}
7+
x, y = 0, 0
8+
9+
found = False
10+
for _ in range(30):
11+
for move in moves:
12+
x += dx[move]
13+
y += dy[move]
14+
15+
if x == a and y == b:
16+
found = True
17+
break
18+
19+
if found:
20+
break
21+
22+
if found:
23+
print("YES")
24+
else:
25+
print("NO")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
for _ in range(int(input())):
2+
n, b, c = map(int, input().split())
3+
if n == 1:
4+
if c == 0:
5+
print(0)
6+
else:
7+
print(1)
8+
continue
9+
10+
if b == 0:
11+
if n > c + 2:
12+
print(-1)
13+
else:
14+
if n == c + 1 or n == c + 2:
15+
print(n - 1)
16+
else:
17+
print(n)
18+
continue
19+
20+
maxI = (n - 1 - c) // b
21+
maxI = max(maxI, -1)
22+
23+
numSkipped = maxI + 1
24+
numLeft = n - numSkipped
25+
26+
print(numLeft)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
template <typename T = ll>
14+
struct StaticSum {
15+
const int n;
16+
vector<T> pfs; // arr[i] contains the range sum from 0 to i inclusively
17+
18+
T rsq(int l, int r) const { // range sum from l to r inclusively
19+
if (r < l) assert(false); // invalid input
20+
if (l < 0 || r >= n) assert(false); // boundary condition check
21+
if (l == 0) return pfs[r];
22+
return (pfs[r] - pfs[l - 1]);
23+
}
24+
25+
StaticSum(const vector<T>& arr) : n(arr.size()), pfs(arr) {
26+
for (int i = 1; i < n; i++) {
27+
pfs[i] += pfs[i - 1];
28+
}
29+
}
30+
};
31+
32+
int main() {
33+
fast_cin();
34+
int tc;
35+
cin >> tc;
36+
while (tc--) {
37+
ll n, m, v;
38+
cin >> n >> m >> v;
39+
vector<ll> arr(n + 1);
40+
41+
for (int i = 0; i < n; i++) {
42+
cin >> arr[i];
43+
}
44+
45+
vector<ll> brr(n + 1, 0); // brr[i] is the maximum number of animals that can be fed from arr[i] onward inclusive
46+
int curFood = 0;
47+
int curAnimals = 0;
48+
for (int i = n - 1; i >= 0; i--) {
49+
curFood += arr[i];
50+
if (curFood >= v) {
51+
curAnimals++;
52+
curFood = 0;
53+
}
54+
brr[i] = curAnimals;
55+
}
56+
57+
vector<ll> crr(n + 1); // crr[i] is the maximum number of animals that can be fed from arr[0] to arr[i] inclusive
58+
curFood = 0;
59+
curAnimals = 0;
60+
for (int i = 0; i < n; i++) {
61+
curFood += arr[i];
62+
if (curFood >= v) {
63+
curAnimals++;
64+
curFood = 0;
65+
}
66+
crr[i] = curAnimals;
67+
}
68+
69+
if (crr[n - 1] < m) { // not enough to feed m animals
70+
cout << -1 << endl;
71+
continue;
72+
}
73+
74+
ll ans = 0;
75+
StaticSum<ll> ss(arr);
76+
77+
int right = 0;
78+
int animalsFed = brr[right];
79+
while (animalsFed >= m) {
80+
right++;
81+
animalsFed = brr[right];
82+
}
83+
right--;
84+
animalsFed = brr[right];
85+
86+
if (right - 1 >= 0) {
87+
ans = max(ans, ss.rsq(0, right - 1)); // all animals on the right of what we eat
88+
}
89+
// cout << "right: " << right << " ans: " << ans << endl;
90+
91+
int left = 0;
92+
for (int i = 1; i < m; i++) {
93+
// i animals on the left, m-i animals on the right
94+
while (crr[left] < i) {
95+
left++;
96+
}
97+
98+
while (brr[right] >= m - i) {
99+
right++;
100+
}
101+
right--;
102+
103+
// cout << "i = " << i << " left = " << left << " right = " << right << endl;
104+
if (left + 1 <= right - 1) ans = max(ans, ss.rsq(left + 1, right - 1));
105+
}
106+
107+
while (crr[left] < m) {
108+
left++;
109+
}
110+
111+
// cout << "left: " << left << " ans: " << ans << endl;
112+
113+
if (left + 1 <= n - 1) ans = max(ans, ss.rsq(left + 1, n - 1)); // all animals on the left
114+
115+
cout << ans << endl;
116+
}
117+
118+
return 0;
119+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
int main() {
14+
fast_cin();
15+
int tc;
16+
cin >> tc;
17+
while (tc--) {
18+
int n;
19+
cin >> n;
20+
vector<pair<int, int>> arr(n);
21+
for (int i = 0; i < n; i++) {
22+
cin >> arr[i].first;
23+
arr[i].first -= 1;
24+
arr[i].second = i;
25+
}
26+
27+
vector<pair<int, int>> brr(n);
28+
for (int i = 0; i < n; i++) {
29+
cin >> brr[i].first;
30+
brr[i].first -= 1;
31+
brr[i].second = i;
32+
}
33+
34+
vector<pair<int, int>> crr(n);
35+
for (int i = 0; i < n; i++) {
36+
cin >> crr[i].first;
37+
crr[i].first -= 1;
38+
crr[i].second = i;
39+
}
40+
41+
vector<tuple<int, int, int>> cards;
42+
for (int i = 0; i < n; i++) {
43+
cards.push_back({arr[i].first, brr[i].first, crr[i].first});
44+
}
45+
46+
auto [a, b, c] = cards[0];
47+
int maxa = 0;
48+
int maxb = 0;
49+
int maxc = 0;
50+
51+
sort(arr.begin(), arr.end());
52+
sort(brr.begin(), brr.end());
53+
sort(crr.begin(), crr.end());
54+
55+
vector<bool> unlocked(n, false);
56+
vector<pair<int, char>> unlocker(n, {-1, -1});
57+
unlocked[0] = true;
58+
for (int i = 0; i < n; i++) {
59+
if (!unlocked[i]) continue;
60+
61+
auto [newmaxA, newmaxB, newmaxC] = cards[i];
62+
63+
for (int a = maxa; a <= newmaxA; a++) {
64+
if (unlocked[arr[a].second]) continue;
65+
unlocked[arr[a].second] = true;
66+
unlocker[arr[a].second] = {i, 'q'};
67+
}
68+
for (int b = maxb; b <= newmaxB; b++) {
69+
if (unlocked[brr[b].second]) continue;
70+
unlocked[brr[b].second] = true;
71+
unlocker[brr[b].second] = {i, 'k'};
72+
}
73+
for (int c = maxc; c <= newmaxC; c++) {
74+
if (unlocked[crr[c].second]) continue;
75+
unlocked[crr[c].second] = true;
76+
unlocker[crr[c].second] = {i, 'j'};
77+
}
78+
79+
maxa = max(maxa, newmaxA);
80+
maxb = max(maxb, newmaxB);
81+
maxc = max(maxc, newmaxC);
82+
}
83+
84+
if (unlocked[n - 1]) {
85+
cout << "YES" << endl;
86+
vector<pair<char, int>> ans;
87+
int cur = n - 1;
88+
while (cur != 0) {
89+
ans.push_back({unlocker[cur].second, cur});
90+
cur = unlocker[cur].first;
91+
}
92+
93+
reverse(ans.begin(), ans.end());
94+
cout << ans.size() << endl;
95+
for (auto [c, i] : ans) {
96+
cout << c << " " << i + 1 << endl;
97+
}
98+
} else {
99+
cout << "NO" << endl;
100+
}
101+
}
102+
103+
return 0;
104+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
constexpr ll MOD = 998244353;
14+
int n;
15+
vector<vector<int>> adjlist;
16+
vector<int> visited;
17+
vector<int> parent;
18+
vector<ll> depth;
19+
20+
constexpr ll mod(ll a, ll n) { return (a % n + n) % n; }
21+
constexpr ll extEuclid(ll a, ll b, ll &x, ll &y) { // pass x and y by ref
22+
ll xx = y = 0;
23+
ll yy = x = 1;
24+
while (b) { // repeats until b == 0
25+
ll q = a / b;
26+
tie(a, b) = tuple(b, a % b);
27+
tie(x, xx) = tuple(xx, x - q * xx);
28+
tie(y, yy) = tuple(yy, y - q * yy);
29+
}
30+
return a; // returns gcd(a, b)
31+
// ax + by = gcd(a, b)
32+
}
33+
constexpr ll modInverse(ll a, ll n) { // returns modular inverse of a mod n
34+
ll x, y;
35+
extEuclid(a, n, x, y);
36+
return mod(x, n);
37+
}
38+
39+
void labelDfs(int u) {
40+
visited[u] = 1;
41+
for (int v : adjlist[u]) {
42+
if (visited[v] == 0) {
43+
parent[v] = u;
44+
depth[v] = depth[u] + 1;
45+
labelDfs(v);
46+
}
47+
}
48+
}
49+
50+
int main() {
51+
fast_cin();
52+
int tc;
53+
cin >> tc;
54+
while (tc--) {
55+
cin >> n;
56+
adjlist.assign(n, vector<int>());
57+
58+
visited.assign(n, 0);
59+
parent.assign(n, -1);
60+
depth.assign(n, 0);
61+
62+
for (int i = 0; i < n - 1; i++) {
63+
int u, v;
64+
cin >> u >> v;
65+
u--;
66+
v--;
67+
adjlist[u].push_back(v);
68+
adjlist[v].push_back(u);
69+
}
70+
71+
// root at 0
72+
depth[0] = 0;
73+
labelDfs(0);
74+
75+
vector<pair<int, int>> leaves; // (depth, node)
76+
for (int i = 1; i < n; i++) {
77+
if (adjlist[i].size() == 1) {
78+
leaves.push_back({depth[i], i});
79+
}
80+
}
81+
82+
sort(leaves.begin(), leaves.end()); // shallowest leaves first
83+
84+
vector<ll> num(n, -1); // numerator of the probablity
85+
vector<ll> den(n, 0); // denominator of the probability
86+
87+
num[0] = 1;
88+
den[0] = 1;
89+
90+
for (auto [d, leaf] : leaves) {
91+
int u = leaf;
92+
int chainLength = 0; // counts leaf until first finished node (not including finished node)
93+
while (num[u] == -1) {
94+
u = parent[u];
95+
chainLength++;
96+
}
97+
ll numFactor = num[u];
98+
ll denFactor = den[u];
99+
100+
u = leaf;
101+
int idx = 0;
102+
while (num[u] == -1) {
103+
num[u] = (numFactor * idx) % MOD;
104+
den[u] = (denFactor * chainLength) % MOD;
105+
u = parent[u];
106+
idx++;
107+
}
108+
}
109+
110+
for (int i = 0; i < n; i++) {
111+
cout << (num[i] * modInverse(den[i], MOD)) % MOD << " ";
112+
}
113+
cout << endl;
114+
}
115+
116+
return 0;
117+
}

0 commit comments

Comments
 (0)