Skip to content

Commit 25eb808

Browse files
committed
🚀 01-Jul-2021
1 parent 6403abe commit 25eb808

30 files changed

+3265
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.
3+
4+
In the city in which Alice and Bob live, the first metro line is being built. This metro line contains n stations numbered from 1 to n. Bob lives near the station with number 1, while Alice lives near the station with number s. The metro line has two tracks. Trains on the first track go from the station 1 to the station n and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.
5+
6+
Some stations are not yet open at all and some are only partially open — for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.
7+
8+
When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.
9+
10+
Input
11+
The first line contains two integers n and s (2=s=n=1000) — the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station 1.
12+
13+
Next lines describe information about closed and open stations.
14+
15+
The second line contains n integers a1,a2,…,an (ai=0 or ai=1). If ai=1, then the i-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track.
16+
17+
The third line contains n integers b1,b2,…,bn (bi=0 or bi=1). If bi=1, then the i-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track.
18+
19+
Output
20+
Print "YES" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and "NO" (quotes for clarity) otherwise.
21+
22+
You can print each letter in any case (upper or lower).
23+
24+
Examples
25+
inputCopy
26+
5 3
27+
1 1 1 1 1
28+
1 1 1 1 1
29+
outputCopy
30+
YES
31+
inputCopy
32+
5 4
33+
1 0 0 0 1
34+
0 1 1 1 1
35+
outputCopy
36+
YES
37+
inputCopy
38+
5 2
39+
0 1 1 1 1
40+
1 1 1 1 1
41+
outputCopy
42+
NO
43+
Note
44+
In the first example, all stations are opened, so Bob can simply travel to the station with number 3.
45+
46+
In the second example, Bob should travel to the station 5 first, switch to the second track and travel to the station 4 then.
47+
48+
In the third example, Bob simply can't enter the train going in the direction of Alice's home.
49+
*/
50+
51+
52+
53+
54+
55+
56+
57+
58+
#include<bits/stdc++.h>
59+
using namespace std;
60+
61+
void solve() {
62+
int n, s;
63+
cin >> n >> s;
64+
65+
vector <int> a(n), b(n), vis(n);
66+
67+
for (auto &x: a) cin >> x;
68+
for (auto &x: b) cin >> x;
69+
70+
71+
if (a[0] == 1) {
72+
for (int i = 0; i < n; i++) {
73+
if (a[i] == 1) {
74+
vis[i] = 1;
75+
if (b[i] == 1) {
76+
for (int j = i - 1; j >= 0; j--) {
77+
if (b[j] == 1) vis[j] = 1;
78+
}
79+
}
80+
}
81+
}
82+
}
83+
84+
cout << (vis[0] == 1 && vis[s - 1] == 1 ? "YES" : "NO");
85+
}
86+
87+
int main(){
88+
ios_base::sync_with_stdio(false);
89+
cin.tie(NULL);
90+
91+
int t = 1;
92+
//cin >> t;
93+
94+
while (t--) solve();
95+
96+
return 0;
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Monocarp had a sequence a consisting of n+m integers a1,a2,…,an+m. He painted the elements into two colors, red and blue; n elements were painted red, all other m elements were painted blue.
3+
4+
After painting the elements, he has written two sequences r1,r2,…,rn and b1,b2,…,bm. The sequence r consisted of all red elements of a in the order they appeared in a; similarly, the sequence b consisted of all blue elements of a in the order they appeared in a as well.
5+
6+
Unfortunately, the original sequence was lost, and Monocarp only has the sequences r and b. He wants to restore the original sequence. In case there are multiple ways to restore it, he wants to choose a way to restore that maximizes the value of
7+
8+
f(a)=max(0,a1,(a1+a2),(a1+a2+a3),…,(a1+a2+a3+?+an+m))
9+
Help Monocarp to calculate the maximum possible value of f(a).
10+
11+
Input
12+
The first line contains one integer t (1=t=1000) — the number of test cases. Then the test cases follow. Each test case consists of four lines.
13+
14+
The first line of each test case contains one integer n (1=n=100).
15+
16+
The second line contains n integers r1,r2,…,rn (-100=ri=100).
17+
18+
The third line contains one integer m (1=m=100).
19+
20+
The fourth line contains m integers b1,b2,…,bm (-100=bi=100).
21+
22+
Output
23+
For each test case, print one integer — the maximum possible value of f(a).
24+
25+
Example
26+
inputCopy
27+
4
28+
4
29+
6 -5 7 -3
30+
3
31+
2 3 -4
32+
2
33+
1 1
34+
4
35+
10 -3 2 2
36+
5
37+
-1 -2 -3 -4 -5
38+
5
39+
-1 -2 -3 -4 -5
40+
1
41+
0
42+
1
43+
0
44+
outputCopy
45+
13
46+
13
47+
0
48+
0
49+
Note
50+
In the explanations for the sample test cases, red elements are marked as bold.
51+
52+
In the first test case, one of the possible sequences a is [6,2,-5,3,7,-3,-4].
53+
54+
In the second test case, one of the possible sequences a is [10,1,-3,1,2,2].
55+
56+
In the third test case, one of the possible sequences a is [-1,-1,-2,-3,-2,-4,-5,-3,-4,-5].
57+
58+
In the fourth test case, one of the possible sequences a is [0,0].
59+
*/
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
/*
70+
#include<bits/stdc++.h>
71+
using namespace std;
72+
73+
void solve() {
74+
int n;
75+
cin >> n;
76+
77+
vector <int> r(n);
78+
79+
for (auto &x: r) cin >> x;
80+
81+
int m;
82+
cin >> m;
83+
84+
vector <int> b(m);
85+
86+
for (auto &x: b) cin >> x;
87+
88+
vector <int> rpsum(n), bpsum(m); // r prefix sum, b prefix sum
89+
rpsum[0] = r[0];
90+
bpsum[0] = b[0];
91+
92+
for (int i = 1; i <n; i++) rpsum[i] = rpsum[i - 1] + r[i];
93+
for (int i = 1; i < m; i++) bpsum[i] = bpsum[i - 1] + b[i];
94+
95+
int maxr = *max_element(rpsum.begin(), rpsum.end());
96+
int maxb = *max_element(bpsum.begin(), bpsum.end());
97+
98+
cout << max(max(0, maxr + maxb), max(maxr, maxb)) << "\n";
99+
}
100+
101+
int main(){
102+
ios_base::sync_with_stdio(false);
103+
cin.tie(NULL);
104+
105+
int t = 1;
106+
cin >> t;
107+
108+
while (t--) solve();
109+
110+
return 0;
111+
}
112+
*/
113+
114+
115+
116+
// DP
117+
118+
#include<bits/stdc++.h>
119+
using namespace std;
120+
121+
void solve() {
122+
int n;
123+
cin >> n;
124+
125+
vector <int> r(n);
126+
127+
for (auto &x: r) cin >> x;
128+
129+
int m;
130+
cin >> m;
131+
132+
vector <int> b(m);
133+
134+
for (auto &x: b) cin >> x;
135+
136+
vector <vector<int>> dp(n + 1, vector <int> (m + 1, INT_MIN));
137+
138+
dp[0][0] = 0;
139+
140+
int res = 0;
141+
142+
for (int i = 0; i <= n; i++) {
143+
for (int j = 0; j <= m; j++) {
144+
if (i < n) {
145+
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + r[i]);
146+
}
147+
if (j < m) {
148+
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + b[j]);
149+
}
150+
res = max(res, dp[i][j]);
151+
}
152+
}
153+
154+
cout << res << "\n";
155+
}
156+
157+
int main(){
158+
ios_base::sync_with_stdio(false);
159+
cin.tie(NULL);
160+
161+
int t = 1;
162+
cin >> t;
163+
164+
while (t--) solve();
165+
166+
return 0;
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
You are given a string s, consisting only of characters '0' or '1'. Let |s| be the length of s.
3+
4+
You are asked to choose some integer k (k>0) and find a sequence a of length k such that:
5+
6+
1=a1<a2<?<ak=|s|;
7+
ai-1+1<ai for all i from 2 to k.
8+
The characters at positions a1,a2,…,ak are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence a should not be adjacent.
9+
10+
Let the resulting string be s'. s' is called sorted if for all i from 2 to |s'| s'i-1=s'i.
11+
12+
Does there exist such a sequence a that the resulting string s' is sorted?
13+
14+
Input
15+
The first line contains a single integer t (1=t=1000) — the number of testcases.
16+
17+
Then the descriptions of t testcases follow.
18+
19+
The only line of each testcase contains a string s (2=|s|=100). Each character is either '0' or '1'.
20+
21+
Output
22+
For each testcase print "YES" if there exists a sequence a such that removing the characters at positions a1,a2,…,ak and concatenating the parts without changing the order produces a sorted string.
23+
24+
Otherwise, print "NO".
25+
26+
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
27+
28+
Example
29+
inputCopy
30+
5
31+
10101011011
32+
0000
33+
11111
34+
110
35+
1100
36+
outputCopy
37+
YES
38+
YES
39+
YES
40+
YES
41+
NO
42+
Note
43+
In the first testcase you can choose a sequence a=[1,3,6,9]. Removing the underlined letters from "10101011011" will produce a string "0011111", which is sorted.
44+
45+
In the second and the third testcases the sequences are already sorted.
46+
47+
In the fourth testcase you can choose a sequence a=[3]. s'= "11", which is sorted.
48+
49+
In the fifth testcase there is no way to choose a sequence a such that s' is sorted.
50+
*/
51+
52+
53+
54+
55+
56+
57+
#include<bits/stdc++.h>
58+
using namespace std;
59+
60+
void solve() {
61+
string s;
62+
cin >> s;
63+
64+
int ones = s.find("11");
65+
int zeros = s.rfind("00");
66+
67+
if (ones != -1 && zeros != -1 && ones < zeros) cout << "NO\n";
68+
else cout << "YES\n";
69+
}
70+
71+
int main(){
72+
ios_base::sync_with_stdio(false);
73+
cin.tie(NULL);
74+
75+
int t = 1;
76+
cin >> t;
77+
78+
while (t--) solve();
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)