Skip to content

Commit 6c89249

Browse files
committed
[#7] Add contest codes
1 parent ee864f7 commit 6c89249

12 files changed

+502
-0
lines changed

ABC_181_A.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
int main() {
6+
int n;
7+
cin >> n;
8+
if (n%2==1)
9+
{
10+
cout << "Black" << endl;
11+
}
12+
else
13+
{
14+
cout << "White" << endl;
15+
}
16+
}

ABC_181_B.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
int main() {
6+
long long n, a, b;
7+
long long sum = 0;
8+
9+
cin >> n;
10+
11+
while (n--)
12+
{
13+
cin >> a >> b;
14+
15+
a--;
16+
sum += (b * (b + 1) / 2) - (a * (a + 1) / 2);
17+
}
18+
19+
cout << sum << endl;
20+
}

ABC_181_C.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
using namespace std;
4+
5+
int x[100];
6+
int y[100];
7+
8+
int main() {
9+
int n;
10+
11+
cin >> n;
12+
13+
for (int i = 0; i < n; i++)
14+
{
15+
int a, b;
16+
17+
cin >> a >> b;
18+
19+
x[i] = a;
20+
y[i] = b;
21+
}
22+
23+
for (int i = 0; i < n; i++)
24+
{
25+
for (int j = 0; j < n; j++)
26+
{
27+
for (int k = 0; k < n; k++)
28+
{
29+
if (i == j || j == k || i == k)
30+
{
31+
continue;
32+
}
33+
34+
if ((y[j] - y[i]) == 0 && (y[k] - y[j]) == 0)
35+
{
36+
cout << "Yes" << endl;
37+
return 0;
38+
}
39+
else if ((y[j] - y[i]) == 0 || (y[k] - y[j]) == 0)
40+
{
41+
continue;
42+
}
43+
44+
45+
if ((double)((double)x[j] - x[i]) / ((double)y[j] - y[i]) == (double)((double)x[k] - x[j]) / ((double)y[k] - y[j]))
46+
{
47+
cout << "Yes" << endl;
48+
return 0;
49+
}
50+
}
51+
}
52+
}
53+
54+
cout << "No" << endl;
55+
}

ABC_181_D.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <map>
4+
using namespace std;
5+
6+
int digits[10];
7+
8+
bool isAvailable(int n) {
9+
int temp[10];
10+
for (int i = 0; i < 10; i++)
11+
{
12+
temp[i] = digits[i];
13+
}
14+
15+
int num = n;
16+
while (num != 0)
17+
{
18+
int d = num % 10;
19+
20+
if (temp[d] > 0)
21+
{
22+
temp[d]--;
23+
}
24+
else
25+
{
26+
return false;
27+
}
28+
29+
num /= 10;
30+
}
31+
32+
if (n < 100)
33+
{
34+
for (int i = 0; i < 10; i++)
35+
{
36+
if (temp[i] != 0)
37+
{
38+
return false;
39+
}
40+
}
41+
}
42+
43+
return true;
44+
}
45+
46+
int main() {
47+
string s;
48+
cin >> s;
49+
50+
for (int i = 0; i < s.length(); i++)
51+
{
52+
digits[s[i] - '0']++;
53+
}
54+
55+
int num = 0;
56+
for (int i = 8; i < 1000; i += 8)
57+
{
58+
if (isAvailable(i))
59+
{
60+
cout << "Yes" << endl;
61+
return 0;
62+
}
63+
}
64+
cout << "No" << endl;
65+
}

ABC_184_A.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int a, b, c, d;
6+
cin >> a >> b >> c >> d;
7+
cout << a * d - b * c << endl;
8+
}

ABC_184_B.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
int main() {
6+
int n, x;
7+
string str;
8+
9+
cin >> n >> x >> str;
10+
11+
for (int i = 0; i < str.size(); i++)
12+
{
13+
if (str[i] == 'o')
14+
{
15+
x++;
16+
}
17+
else if (x > 0)
18+
{
19+
x--;
20+
}
21+
}
22+
23+
cout << x << endl;
24+
}

ABC_184_E.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <utility>
4+
#include <string>
5+
#include <cstring>
6+
using namespace std;
7+
8+
string maps[2000];
9+
int visited[2000][2000];
10+
bool teleported[26];
11+
12+
vector<pair<int, int> > teleport[26];
13+
pair<int, int> start, goal;
14+
15+
int dir[][2] = { -1, 0, 0, -1, 0, 1,1,0 };
16+
17+
int main() {
18+
int h, w;
19+
20+
cin >> h >> w;
21+
22+
for (int i = 0; i < h; i++)
23+
{
24+
cin >> maps[i];
25+
26+
for (int j = 0; j < maps[i].size(); j++)
27+
{
28+
if (maps[i][j] >= 'a' && maps[i][j] <= 'z')
29+
{
30+
teleport[maps[i][j] - 'a'].push_back(make_pair(i, j));
31+
}
32+
else if (maps[i][j] == 'S')
33+
{
34+
start = make_pair(i, j);
35+
}
36+
else if (maps[i][j] == 'G')
37+
{
38+
goal = make_pair(i, j);
39+
}
40+
}
41+
}
42+
43+
memset(visited, -1, sizeof(visited));
44+
45+
int result = -1;
46+
47+
queue< pair<int, int> > que;
48+
que.push(start);
49+
visited[start.first][start.second] = 0;
50+
51+
while (!que.empty())
52+
{
53+
pair<int, int> next = que.front();
54+
que.pop();
55+
56+
if (next.first == goal.first && next.second == goal.second)
57+
{
58+
result = visited[next.first][next.second];
59+
break;
60+
}
61+
62+
for (int d = 0; d < 4; d++)
63+
{
64+
int x = next.first + dir[d][0];
65+
int y = next.second + dir[d][1];
66+
67+
if (x >= h || x < 0 || y >= w || y < 0 || visited[x][y] != -1 || maps[x][y] == '#')
68+
{
69+
continue;
70+
}
71+
72+
visited[x][y] = visited[next.first][next.second] + 1;
73+
que.push(make_pair(x, y));
74+
}
75+
76+
// teleport
77+
if (maps[next.first][next.second] >= 'a' && maps[next.first][next.second] <= 'z' && teleported[maps[next.first][next.second] - 'a'] == false)
78+
{
79+
teleported[maps[next.first][next.second] - 'a'] = true;
80+
81+
for (int i = 0; i < teleport[maps[next.first][next.second] - 'a'].size(); i++)
82+
{
83+
int x = teleport[maps[next.first][next.second] - 'a'][i].first;
84+
int y = teleport[maps[next.first][next.second] - 'a'][i].second;
85+
86+
if (x >= h || x < 0 || y >= w || y < 0 || visited[x][y] != -1 || maps[x][y] == '#')
87+
{
88+
continue;
89+
}
90+
91+
visited[x][y] = visited[next.first][next.second] + 1;
92+
que.push(make_pair(x, y));
93+
}
94+
}
95+
}
96+
97+
cout << result << endl;
98+
}

ABC_184_F.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// https://www.geeksforgeeks.org/meet-in-the-middle/
2+
3+
// C++ program to demonstrate working of Meet in the
4+
// Middle algorithm for maximum subset sum problem.
5+
#include <iostream>
6+
#include <algorithm>
7+
using namespace std;
8+
typedef long long int ll;
9+
ll X[1 << 21], Y[1 << 21];
10+
11+
// Find all possible sum of elements of a[] and store
12+
// in x[]
13+
void calcsubarray(ll a[], ll x[], int n, int c)
14+
{
15+
for (int i = 0; i < (1 << n); i++)
16+
{
17+
ll s = 0;
18+
for (int j = 0; j < n; j++)
19+
if (i & (1 << j))
20+
s += a[j + c];
21+
x[i] = s;
22+
}
23+
}
24+
25+
// Returns the maximum possible sum less or equal to S
26+
ll solveSubsetSum(ll a[], int n, ll S)
27+
{
28+
// Compute all subset sums of first and second
29+
// halves
30+
calcsubarray(a, X, n / 2, 0);
31+
calcsubarray(a, Y, n - n / 2, n / 2);
32+
33+
int size_X = 1 << (n / 2);
34+
int size_Y = 1 << (n - n / 2);
35+
36+
// Sort Y (we need to do doing binary search in it)
37+
sort(Y, Y + size_Y);
38+
39+
// To keep track of the maximum sum of a subset
40+
// such that the maximum sum is less than S
41+
ll max = 0;
42+
43+
// Traverse all elements of X and do Binary Search
44+
// for a pair in Y with maximum sum less than S.
45+
for (int i = 0; i < size_X; i++)
46+
{
47+
if (X[i] <= S)
48+
{
49+
// lower_bound() returns the first address
50+
// which has value greater than or equal to
51+
// S-X[i].
52+
int p = lower_bound(Y, Y + size_Y, S - X[i]) - Y;
53+
54+
// If S-X[i] was not in array Y then decrease
55+
// p by 1
56+
if (p == size_Y || Y[p] != (S - X[i]))
57+
p--;
58+
59+
if ((Y[p] + X[i]) > max)
60+
max = Y[p] + X[i];
61+
}
62+
}
63+
return max;
64+
}
65+
66+
// Driver code
67+
int main()
68+
{
69+
int n, t;
70+
ll a[40];
71+
72+
cin >> n >> t;
73+
for (int i = 0; i < n; i++)
74+
{
75+
cin >> a[i];
76+
}
77+
78+
cout << solveSubsetSum(a, n, t) << endl;
79+
}

ABC_187_A.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
int main() {
7+
string a, b;
8+
9+
cin >> a >> b;
10+
11+
cout << max((a[0] + a[1] + a[2] - '0' * 3), b[0] + b[1] + b[2] - '0' * 3) << endl;
12+
}

0 commit comments

Comments
 (0)