Skip to content

Commit 4436536

Browse files
Add files via upload
1 parent 4990708 commit 4436536

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

Other_Tasks/CF2062_A_String.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
for _ in range(int(input())):
3+
print(input().count('1'))

Other_Tasks/CF2062_B_Clockwork.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
for _ in range(int(input())):
2+
3+
n = int(input())
4+
5+
arr = [int(x) for x in input().split()]
6+
7+
for i in range(n):
8+
if arr[i] <= 2*i:
9+
print("NO")
10+
break
11+
if arr[i] <= 2*(n-i-1):
12+
print("NO")
13+
break
14+
else:
15+
print("YES")
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
def diff(arr):
3+
ans = [arr[i+1] - arr[i] for i in range(len(arr)-1)]
4+
return ans
5+
6+
for _ in range(int(input())):
7+
n = int(input())
8+
arr = [int(x) for x in input().split()]
9+
10+
best = sum(arr)
11+
while len(arr) > 1:
12+
arr = diff(arr)
13+
best = max(best, sum(arr))
14+
best = max(best, -sum(arr))
15+
16+
print(best)
17+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
vector<vector<int>> adjlist;
11+
vector<int> visited;
12+
vector<int> parent;
13+
vector<ll> r, l;
14+
vector<pair<ll, ll>> memo; // value, totalIncrease
15+
16+
void rootTree(int u){
17+
visited[u] = 1;
18+
for (auto v: adjlist[u]){
19+
if (!visited[v]){
20+
parent[v] = u;
21+
rootTree(v);
22+
}
23+
}
24+
}
25+
26+
pair<ll, ll> dp(int u){
27+
if (memo[u].first != -1) return memo[u];
28+
29+
ll maxChildValue = 0;
30+
ll totalIncrease = 0;
31+
for (auto v: adjlist[u]){
32+
if (v == parent[u]) continue;
33+
pair<ll, ll> p = dp(v);
34+
maxChildValue = max(maxChildValue, p.first);
35+
totalIncrease += p.second;
36+
}
37+
ll thisValue = max(l[u], min(maxChildValue, r[u]));
38+
39+
for (auto v: adjlist[u]){
40+
if (v == parent[u]) continue;
41+
pair<ll, ll> p = dp(v);
42+
totalIncrease += max(0LL, p.first - thisValue);
43+
}
44+
45+
return memo[u] = {thisValue, totalIncrease};
46+
}
47+
48+
int main(){
49+
int tc;
50+
cin >> tc;
51+
while (tc--){
52+
int n;
53+
cin >> n;
54+
adjlist.assign(n, vector<int>());
55+
l.assign(n, 0);
56+
r.assign(n, 0);
57+
for (int i = 0; i < n; i++){
58+
cin >> l[i] >> r[i];
59+
}
60+
for (int i=0; i<n-1; i++){
61+
int u, v;
62+
cin >> u >> v;
63+
u--; v--;
64+
adjlist[u].push_back(v);
65+
adjlist[v].push_back(u);
66+
}
67+
68+
visited.assign(n, 0);
69+
parent.assign(n, -1);
70+
rootTree(0);
71+
72+
memo.assign(n, {-1, -1});
73+
pair<ll, ll> p = dp(0);
74+
cout << p.second + p.first << endl;
75+
}
76+
77+
return 0;
78+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
8+
int n;
9+
int maxi;
10+
vector<int> numMaxInSubtree;
11+
vector<vector<int>> adjlist;
12+
vector<int> value, parent;
13+
void countMaxInSubtree(int u) {
14+
numMaxInSubtree[u] = (value[u] == maxi);
15+
for (auto v : adjlist[u]) {
16+
if (v == parent[u]) continue;
17+
parent[v] = u;
18+
countMaxInSubtree(v);
19+
numMaxInSubtree[u] += numMaxInSubtree[v];
20+
}
21+
}
22+
23+
int main() {
24+
int tc;
25+
cin >> tc;
26+
while (tc--) {
27+
cin >> n;
28+
adjlist.assign(n, vector<int>());
29+
value.assign(n, 0);
30+
numMaxInSubtree.assign(n, 0);
31+
maxi = 0;
32+
33+
set<int> svs;
34+
for (int i = 0; i < n; i++) {
35+
cin >> value[i];
36+
maxi = max(maxi, value[i]);
37+
svs.insert(value[i]);
38+
}
39+
vector<int> uniqueVals = vector<int>(svs.begin(), svs.end());
40+
reverse(uniqueVals.begin(), uniqueVals.end()); // reverse to get the values in decreasing order
41+
42+
for (int i = 0; i < n - 1; i++) {
43+
int u, v;
44+
cin >> u >> v;
45+
u--;
46+
v--;
47+
adjlist[u].push_back(v);
48+
adjlist[v].push_back(u);
49+
}
50+
51+
parent.assign(n, -1);
52+
parent[0] = 0;
53+
countMaxInSubtree(0);
54+
55+
int totalMax = numMaxInSubtree[0];
56+
if (svs.size() == 1) {
57+
cout << 0 << endl;
58+
continue;
59+
}
60+
61+
int secondMax = *(--(--svs.end()));
62+
63+
vector<int> criticalPath;
64+
int u = 0;
65+
int prevVal = value[u];
66+
while (true) {
67+
criticalPath.push_back(u);
68+
69+
int newu = -1;
70+
for (auto v : adjlist[u]) {
71+
if (v == parent[u]) {
72+
continue;
73+
}
74+
if (numMaxInSubtree[v] == totalMax) {
75+
newu = v;
76+
}
77+
}
78+
79+
if (newu == -1) {
80+
break;
81+
}
82+
u = newu;
83+
}
84+
85+
set<int> critpathSet(criticalPath.begin(), criticalPath.end());
86+
vector<int> critNodeToIdx(n, -1);
87+
for (int i = 0; i < criticalPath.size(); i++) {
88+
critNodeToIdx[criticalPath[i]] = i;
89+
}
90+
91+
map<int, vector<int>> m;
92+
for (int i = 0; i < n; i++) {
93+
m[value[i]].push_back(i);
94+
}
95+
96+
97+
int curEnd = criticalPath.size() - 1;
98+
int ans = 0;
99+
for (int i = 1; i < uniqueVals.size() && !ans; i++) { // iterate over unique values in decreasing order
100+
int val = uniqueVals[i];
101+
int newEnd = curEnd;
102+
for (auto u : m[val]) {
103+
if (critpathSet.find(u) == critpathSet.end()) { // if we find this value not on a critical path, we can pick it
104+
ans = u + 1;
105+
break;
106+
}
107+
108+
assert(critNodeToIdx[u] != -1);
109+
if (critNodeToIdx[u] > curEnd) { // if we find a node of this value deeper than a node of a bigger value, we can pick it
110+
ans = u + 1;
111+
break;
112+
}
113+
114+
newEnd = min(newEnd, critNodeToIdx[u]);
115+
}
116+
117+
curEnd = newEnd;
118+
}
119+
120+
cout << ans << endl;
121+
}
122+
123+
return 0;
124+
}

0 commit comments

Comments
 (0)