-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathCodeForces
216 lines (181 loc) · 6.02 KB
/
CodeForces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
Codeforces Contest - Codeforces Round #824 (Div. 2) Solutions
Problem A - https://codeforces.com/contest/1735/problem/A
Idea -
For maximizing the minimum segments it is obvious to take one segment of length 1 so that its difference with any other becomes maximum. So take a day off at Day 2. Now there are two days off at Day n and Day 2. We need to find another day such that the length of segments between them is maximum. By taking a Day off at 1/3rd the distance from 2, you'll maximize the difference of length of segments.
Code -
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define ll long long int
#define INF 1e9
void solve()
{
int n;
cin >> n;
int points[3] = {2, (n - 2) / 3 + ((n - 2) % 3 > 0) + 2, n};
int segments[3] = {1, points[1] - points[0] - 1, points[2] - points[1] - 1};
cout << min({abs(segments[0] - segments[1]), abs(segments[1] - segments[2]), abs(segments[2] - segments[0])}) << nl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
cin >> T;
for (int t = 1; t <= T; t++)
{
solve();
}
}
Problem B - https://codeforces.com/contest/1735/problem/B
Idea -
It is obvious that we have to consider the smallest element always since it can not become larger to meet the problem's description. Now we need to make rest of all the elements strictly less than twice. We know the largest number that can satisfy our contraints would be "2 * smallest_element - 1". Let's call this number X. So idea was to make all the elements smaller or equal to that number but larger or equal to the smallest_element (This was my thinking, the problem constraints required that for every pair the bigger number is strictly less than twice of the smaller number). Now if I divide the current element by X and adding 1 if there is a remainder it'll give me smallest factor of X larger or equal to current element. The remainder can then be simply distributed among these elements leading to a possible solution.
Code -
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define ll long long int
#define INF 1e9
void solve()
{
int n;
cin >> n;
int a[n];
for (int &x : a)
cin >> x;
ll minVal = 2 * a[0] - 1, ans = 0;
for (int &x : a)
ans += x / minVal + (x % minVal > 0) - 1;
cout << ans << nl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
cin >> T;
for (int t = 1; t <= T; t++)
{
solve();
}
}
Problem C - https://codeforces.com/contest/1735/problem/C
Idea -
It is obvious that we want each character of the string to be preceded by the smallest possible character to give the lexographically smallest string. The problem with this was there if we proceed this way only there may be configuration of characters such that they create a cycle of length less than 26. So to fix this, we'll just check if adding the smallest unassigned character is leading to a cycle or not by simply performing a dfs in reverse direction and returning whether we are reaching the current smallest character or not. If yes, skip to next character otherwise assign it to current character. We'll do this until we have assigned 25 characters and only 1 character is remaining which will complete our cycle of 26 characters.
Code -
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define ll long long int
#define INF 1e9
bool dfs(char curr, const char &target, const vector<int> &v)
{
if (curr == target)
return true;
if (v[curr - 'a'] == -1)
return false;
return dfs(v[curr - 'a'] + 'a', target, v);
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
set<char> st;
for (int i = 0; i < 26; i++)
st.insert('a' + i);
vector<int> v(26, -1);
for (int i = 0; i < n; i++)
{
if (v[s[i] - 'a'] != -1)
continue;
char c;
for (auto &x : st)
if (st.size() == 1 || !dfs(x, s[i], v))
{
c = x;
break;
}
v[s[i] - 'a'] = c - 'a';
st.erase(c);
}
for (int i = 0; i < n; i++)
cout << char(v[s[i] - 'a'] + 'a');
cout << nl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
cin >> T;
for (int t = 1; t <= T; t++)
{
solve();
}
}
Problem D - https://codeforces.com/contest/1735/problem/D
Idea -
The idea was to find for each pair of cards present on the table, the other card that will complete the set. If it is present on the table, just incremenet the count of sets formed by taking that card by 1. Then it is a simple combinatorics problem of selecting 2 out of available sets by simply calculating count_of_sets*(count_of_sets-1)/2.
Code -
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define ll long long int
#define INF 1e9
long long power(long long num, long long pw)
{
ll res = 1;
while (pw)
{
if (pw & 1)
res *= num;
num *= num;
pw /= 2;
}
return res;
}
void solve()
{
int n, k;
cin >> n >> k;
vector<vector<int>> a(n, vector<int>(k));
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
cin >> a[i][j];
map<ll, ll> mp;
for (int i = 0; i < n; i++)
{
ll num = 0;
for (int j = 0; j < k; j++)
num += a[i][j] * power(3, j);
mp[num] = 0;
}
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
{
ll sum = 0;
for (int l = 0; l < k; l++)
if (a[i][l] == a[j][l])
sum += a[i][l] * power(3, l);
else
sum += (3 - a[i][l] - a[j][l]) * power(3, l);
if (mp.count(sum))
mp[sum]++;
}
ll ans = 0;
for (auto &x : mp)
ans += x.second * (x.second - 1) / 2;
cout << ans << nl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
for (int t = 1; t <= T; t++)
{
solve();
}
}