Idea: vovuh
Tutorial
Tutorial is loading...
Solution (vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", H"r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int tc;
cin >> tc;
while (tc--) {
int n, m;
cin >> n >> m;
int sum = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
}
cout << max(0, sum - m) << endl;
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, q;
cin >> n >> q;
vector<long long> p(n), s(n + 1);
for (auto& x : p) cin >> x;
sort(p.begin(), p.end());
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + p[i];
while (q--) {
int x, y;
cin >> x >> y;
cout << s[n - x + y] - s[n - x] << '\n';
}
}
1697C - awoo's Favorite Problem
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
for _ in range(int(input())):
n = int(input())
s = input()
t = input()
if s.count('b') != t.count('b'):
print("NO")
continue
j = 0
for i in range(n):
if s[i] == 'b':
continue
while t[j] == 'b':
j += 1
if s[i] != t[j] or (s[i] == 'a' and i > j) or (s[i] == 'c' and i < j):
print("NO")
break
j += 1
else:
print("YES")
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
char ask_character(int i)
{
cout << "? 1 " << i << endl;
cout.flush();
string s;
cin >> s;
return s[0];
}
int ask_cnt(int l, int r)
{
cout << "? 2 " << l << " " << r << endl;
cout.flush();
int x;
cin >> x;
return x;
}
int main()
{
int n;
cin >> n;
string s = "";
vector<vector<int>> cnt(n + 1);
for(int i = 0; i < n; i++)
{
if(i == 0)
{
s.push_back(ask_character(1));
cnt[0] = {1};
}
else
{
int cur = ask_cnt(1, i + 1);
if(cur > cnt[i - 1][0])
s.push_back(ask_character(i + 1));
else
{
map<char, int> last;
for(int j = 0; j < s.size(); j++)
last[s[j]] = j;
vector<int> lasts;
for(auto x : last) lasts.push_back(x.second);
sort(lasts.begin(), lasts.end());
int l = 0;
int r = lasts.size();
// there is always an occurrence in [lasts[l], i)
// there is no occurrence in [lasts[r], i)
while(r - l > 1)
{
int m = (l + r) / 2;
int c = ask_cnt(lasts[m] + 1, i + 1);
if (c == cnt[i - 1][lasts[m]])
l = m;
else
r = m;
}
s.push_back(s[lasts[l]]);
}
cnt[i].resize(i + 1);
set<char> q;
for(int j = i; j >= 0; j--)
{
q.insert(s[j]);
cnt[i][j] = q.size();
}
}
}
cout << "! " << s << endl;
cout.flush();
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int N = 143;
const int K = 5;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int binpow(int x, int y)
{
int z = 1;
while(y > 0)
{
if(y % 2 == 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int fact[N];
int rfact[N];
int A(int n, int k)
{
return mul(fact[n], rfact[n - k]);
}
int n;
vector<int> g[N];
int x[N];
int y[N];
int dist[N][N];
int color[N];
int dp[N][N];
int cc = 0;
set<int> pts;
vector<int> compsize;
void dfs1(int i)
{
//cerr << i << endl;
if(pts.count(i) == 1) return;
pts.insert(i);
for(auto v : g[i])
{
dfs1(v);
}
}
void dfs2(int i, int c)
{
if(color[i] == c) return;
color[i] = c;
for(auto v : g[i])
dfs2(v, c);
}
int main()
{
fact[0] = 1;
for(int i = 1; i < N; i++)
fact[i] = mul(i, fact[i - 1]);
for(int i = 0; i < N; i++)
rfact[i] = binpow(fact[i], MOD - 2);
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d %d", &x[i], &y[i]);
}
for(int i = 0; i < n; i++)
{
dist[i][i] = int(1e9);
for(int j = 0; j < n; j++)
if(i != j)
dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
for(int i = 0; i < n; i++)
{
int d = *min_element(dist[i], dist[i] + n);
for(int j = 0; j < n; j++)
if(dist[i][j] == d) g[i].push_back(j);
}
for(int i = 0; i < n; i++)
{
if(color[i] != 0) continue;
cc++;
pts.clear();
dfs1(i);
//cerr << "!" << endl;
int d = *min_element(dist[i], dist[i] + n);
set<int> pts2 = pts;
bool bad = false;
for(auto x : pts)
for(auto y : pts2)
if(x != y && dist[x][y] != d)
bad = true;
if(bad)
{
color[i] = cc;
compsize.push_back(1);
}
else
{
dfs2(i, cc);
compsize.push_back(pts.size());
}
}
dp[0][0] = 1;
int m = compsize.size();
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
{
if(dp[i][j] == 0) continue;
dp[i + 1][j + 1] = add(dp[i + 1][j + 1], dp[i][j]);
if(compsize[i] != 1)
{
dp[i + 1][j + compsize[i]] = add(dp[i + 1][j + compsize[i]], dp[i][j]);
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
ans = add(ans, mul(dp[m][i], A(n, i)));
cout << ans << endl;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct constraint{
int tp, i, j, x;
};
vector<vector<int>> g, tg;
vector<char> used;
vector<int> clr, ord;
void ts(int v){
used[v] = true;
for (int u : g[v]) if (!used[u])
ts(u);
ord.push_back(v);
}
void dfs(int v, int k){
clr[v] = k;
for (int u : tg[v]) if (clr[u] == -1)
dfs(u, k);
}
void either(int x, int y){
g[x ^ 1].push_back(y);
g[y ^ 1].push_back(x);
tg[y].push_back(x ^ 1);
tg[x].push_back(y ^ 1);
}
void implies(int x, int y){
either(x ^ 1, y);
}
void must(int x){
either(x, x);
}
int main() {
int t;
scanf("%d", &t);
forn(_, t){
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
vector<constraint> c(m);
forn(i, m){
scanf("%d%d", &c[i].tp, &c[i].i);
if (c[i].tp != 1) scanf("%d", &c[i].j);
scanf("%d", &c[i].x);
--c[i].i, --c[i].j;
}
int vts = n * (k + 2);
g.assign(2 * vts, vector<int>());
tg.assign(2 * vts, vector<int>());
forn(i, n){
forn(j, (k + 2) - 1)
implies(2 * (i * (k + 2) + j + 1) + 1, 2 * (i * (k + 2) + j) + 1);
must(2 * (i * (k + 2) + 1) + 1);
must(2 * (i * (k + 2) + k + 1));
}
forn(i, n - 1) forn(j, k + 2){
implies(2 * (i * (k + 2) + j) + 1, 2 * ((i + 1) * (k + 2) + j) + 1);
}
forn(i, m){
if (c[i].tp == 1)
either(2 * (c[i].i * (k + 2) + c[i].x + 1) + 1, 2 * (c[i].i * (k + 2) + c[i].x));
else if (c[i].tp == 2){
for (int a = max(1, c[i].x - k); a <= min(k, c[i].x - 1); ++a){
implies(2 * (c[i].i * (k + 2) + a) + 1, 2 * (c[i].j * (k + 2) + (c[i].x - a) + 1));
implies(2 * (c[i].j * (k + 2) + a) + 1, 2 * (c[i].i * (k + 2) + (c[i].x - a) + 1));
}
}
else{
for (int a = max(1, c[i].x - k); a <= min(k, c[i].x - 1); ++a){
implies(2 * (c[i].i * (k + 2) + a + 1), 2 * (c[i].j * (k + 2) + (c[i].x - a)) + 1);
implies(2 * (c[i].j * (k + 2) + a + 1), 2 * (c[i].i * (k + 2) + (c[i].x - a)) + 1);
}
}
}
used.assign(2 * vts, 0);
ord.clear();
forn(i, used.size()) if (!used[i]) ts(i);
reverse(ord.begin(), ord.end());
clr.assign(2 * vts, -1);
int cc = 0;
for (int v : ord) if (clr[v] == -1){
dfs(v, cc);
++cc;
}
vector<int> vals(vts);
bool ans = true;
forn(i, vts){
if (clr[2 * i] == clr[2 * i + 1]){
ans = false;
break;
}
vals[i] = (clr[2 * i] < clr[2 * i + 1]);
}
if (!ans){
puts("-1");
continue;
}
forn(i, n){
int lst = 0;
forn(j, k + 2) if (vals[i * (k + 2) + j])
lst = j;
printf("%d ", lst);
}
puts("");
}
return 0;
}
BledDest has made the task "awoo's favorite problem" :| kinda weired.
I prepared it and made the title a reference to this banger comment.
It shows tutorial is not available excluding problem A.
Could any tell me why my approach to solving C is giving WA? https://codeforces.me/contest/1697/submission/160346935
My approach:
=> 1. Because we don't have ac or ca operations.
=> 2. Make s[i]=='b' and t[i]=='c' to s[i]='c' and t[i]='c' using bc operation via two pointer approach
=> 3. Finally you can remove characters which are equal i.e; s[i]==t[i] At this moment, we have all c's matched in both s and t Now we can match a's and b's using ab operation
First observations is OK, sorry, I forgot statements
Is this code correct? Gives AC but just because of a single else break; it got hacked during contest. My rank went down from 2000 to 8k+. Had solved C in 14 mins. This shit hurts! Just missed a single else statement. 160439872
Soon you will learn to gather yourself and lift yourself up from these situations. Wish you lots of good luck.
What doesn't kill you makes you stronger.
can anyone please explain problem C ?
Video Solution for Problem C and Problem D.
Your solution is great! Could you helo me in resolving this: https://codeforces.me/blog/entry/103835?#comment-922172
It fails on the following case.
1 7 acbabbb bcbabba
F's idea was brilliant. It's not hard to recognize it as a 2-sat problem, but I got stuck with "is $$$a_i$$$ equal to $$$x$$$?" and can't come up with the idea presented in the solution.
Great problems overall. Thanks sir.
In the ask_character function, can we not directly input a char instead of inputting a string and then returning its first charachter? Asking cause I seen a string being inputted in someone else's submission too
Reading a char can be awkward as you have to be careful about spaces / EOL. Reading a string makes it more reliable and simpler (and probably has close to no impact on performance due to small string optimisations).
Oh okay, understood. Thanks for the reply :)
Can someone tell me how test #3 in problem B is generated? All other tests are fine, but test #3 alone seems to be quite an adversary to the dual-pivot quicksort which is used for the built-in primitive array sorting in java. What's more curious is that the built-in sorting algorithm switches to heap sort if it is too slow. I also tried to make a bad array for a bit via local machine and custom invocation, but it wasn't successful at all.
for problem E Im using this fact that size of each component is at most 4 and use this to solve problem with another way
One can prove or disprove this؟؟
Yes, I thought the same as well. It would be the four midpoints of the edges of a square.
I had just figured this out. It is entirely dependent on the 1st condition. Consider fixing the distance, and imagining the diamond of points around a given point equal to that distance. You can see that for two given points, if they are not on the same diagonal (same x + y or x — y), there can be at most two intersections of such diamonds and therefore at most two points that are equidistant to both of them, thus the max size is at most 4 if two are not on same diagonal. Obviously we cannot get more than 2 points all equidistant to each other that are on same diagonal. Lastly, you can find construction showing size 4 set is possible, therefore max size is 4.
nice
I got this. It's not a proof, but a visualization for some cases. Let's see:
The "big squares" are the sets of points having the same Manhattan distance to point A (or B).
thanks :)
Hi! In the 1st diagram you shared, are you referring that the points A and B (which are not on the same diagonal) are equi-distant from point C (which is the intersection of 2 squares) ?
Yes! You can relate them to two intersecting circles, like in triangle inequality proof or something.
P/S: by the way, you can just PM me instead.
This idea was used in recent contest div2 E: https://codeforces.me/contest/1979/problem/E
C is a very strange problem. Let me describe my method.
We can replace "ab" with "ba", and replace "bc" with "cb". Firstly, the number of 'a', 'b' and 'c' must be equal in string $$$s$$$ and $$$t$$$. And if we remove 'b' from $$$s$$$ and $$$t$$$, the remaining string $$$s'$$$ and $$$t'$$$ should also be the same. This is because we can't change the relative positions of 'a' and 'c'.
So lets extract all 'b' in $$$s$$$ and $$$t$$$. They must match one by one like this:
___b_b______b_
_b______b___b_
For a matched pair of 'b', where $$$s_i=t_j=$$$'b'.
The check can be done using prefix sum.
You don't need prefix sum even, you can just iterate over the middle letters (since atmost you look at each letter once)
I don't put that much time into doing Codeforces contests — I am not professionally involved with programming, it is more like a hobby for me. But anyhow, I had been initially very pleased (since I am only a Newbie) during the contest when I had my solutions to A, B and C accepted. But then, during the hacking period, my solution to B was hacked. So, I was curious about what mistake I made. And now that I see the "official" solution to B described as merely "sorting the array and doing prefix sums" — which is what I did — my guess (which seems consistent with other comments) is that apparently by using one of the standard python built-in sort functions ("sorted"), it led to my solution exceeding the time limit. If that is indeed truly what happened (and maybe it wasn't?), that seems to suggest a significant problem on the Codeforces end in setting the time limits.
Input/output time seems to be critical in your case. Refer to my submissions or https://codeforces.me/blog/entry/83441
Thanks! I very much appreciate learning the true explanation. As an outsider to these issues, I do find it interesting that on Leetcode, whose contests I regularly participate in, this — i.e., the slowness of Python in terms of input/output — doesn't appear to be much of an issue. Yes, there are times when Python's slowness does mean that a Python solution will not pass, in comparison to, e.g., a C++ solution. But I don't recall those differences being attributed to input/output issues (and thus they were not correctable by changing how participants using Python coded their input/output).
Anyhow, thanks again for teaching me how to try to avoid this issue in future Codeforces contests. It is certainly interesting and surprising, at least to me, to learn about this being an issue.
Leetcode doesn't ask you to use standard input and output, it provides function arguments and checks return values, which has its advantages and disadvantages
But yes in general, python is slow. In some platform, python and other slow languages are given some additional time. I've heard PyPy is sometimes a bit faster, or a bit slower, depending on problems.
I think my solution for E is simpler and more intuitive ( I think it might be because I observed that the maximum number of points colored the same is 4 ) I'll copy my comment from the other post (sorry if you read it there too ^_^):
"""
Just did problem E. Awesome problem!
First I noticed that the sizes of equal distance groups can only be 4 at most.
Then I noticed that you just need to keep a list of all points of minimum distance from you.
Then I noticed that the only way to color a few points the same color, is if they are all minimum distance from each other.
Then I noticed that a group (of min dist from each other) has to either be colored the same color, or all different, and this type of coloring is completely independent from the other choices for other groups.
From there it's straight forward, a dynamic programming depending on how many colors remain, and if you choose to color the group in the same color or all different colors (no other options). i.e. dp[number_of_group][colors_remaining][color_same_or_all_different]. dp size is n*n*2.
Don't yet know its rating, but might be the highest rated problem I solved!
"""
The way I found the groups is easy. After adding point X itself to the list of points with minimum distance from X (for convenience), to be part of a group, all the neighbors (with minimum distance) must have the exact same set of neighbors! if and only if one of them isn't — you are not part of a group. this was a simply O(n) for loop.
And this is not dfs as I'm only looking at neighbors!
The proof that there is at most 4 points with the same color in problem E. Suppose there are 5 points satisfy the conditions. We call it (x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5). WLOG suppose x1>=x2>=x3>=x4>=x5. Suppose their distant is N. The famous lemma from pigeonhole principle: In a mn+1 number sequence, then there exist an increasing subsequence with length m+1, or a decreasing subsequence with length n+1. By the lemma, WLOG suppose that y1<=y2<=y3. So the three equations hold. (x1-x2)+(y2-y1)=N (x2-x3)+(y3-y2)=N (x1-x3)+(y3-y1)=N But sum up the first two equation we can get: (x1-x3)+(y3-y1)=2N Which is a contradiction since N!=0. So there is at most 4 points with the same color.
Thank you sigma problemsetters for pretests in E! I really love submitting, getting AC on the last 7 minuites, FSTing, fixing it with one if statement in 2 minutes 33 seconds! It is obvious to me that if a problem includes case analysis the pretests should exclude specifically one case so you feel like an idiot! ORZ ORZ ORZ problemsetters.
I liked the problem, it is just painful to perform like a 2520, getting +154, FSTing and realizing that if that was in the testcases, you had time to correct it.
Can anyone explain why I get
TLE
using Java in question B? https://codeforces.me/contest/1697/submission/160471991Array.sort() worst complexity is O(n^2) instead use collection.sort()
I try to shuffle the array and get
AC
, why does this happen? https://codeforces.me/contest/1697/submission/160477796Could Problem F be solved if k is 1e5?
why?????
Can anyone explain C i still don't get it
go through each character one by one.
else, string can be changed only if (s[i] == 'a'and t[i] == 'b') or (s[i] == 'b' and t[i] == 'c'). for any other combination you can't arrive to t.
now let's take eg: s[i] == 'a' and t[i] == 'b', now I found nearest 'b' that is mispalced in s, let it be at index j.
key point to note is there shouldn't be any 'c' between index i and j, because if there is 'c' in between you can't swap 'a' and 'b'. If there is no 'c' then you can simply write s[i] = 'b' and s[j] = 'a'
I checked if there is any c in between by prefix sum method and I maintained a que for a,b,c seperately which has misplaced indexes of a,b,c respectively and took out first indexes of a and b ques, if i could swap them both.
thank u
You actually did with O(N^2) complexity. Try doing it with O(N).
Here's an approach to problem C with O(n^2) (because the limit is 2 seconds). We will iterate i in the string from 0 to n — 1, if s[i] != t[i] then: *i = n — 1 or s[i] == 'c' or t[i] != s[i] + 1 (since to have 'ba', we need 'ab' and the same with 'cb) => NO *Next, we have j = i + 1, iterating to n — 1 and since we want to swap s[i] and s[j], j is the smallest that s[i] != s[j] *If j = n or s[j] != s[i] + 1 => NO *s[j] = s[i]
This is the code to know more what I'm saying: https://codeforces.me/contest/1697/submission/160477577
Cleaner implementation for problem d 160750718
For D if we binary search on
[1, i - 1]
(which is what I did during the contest) the number of type 2 queries has a loose upper bound of1000*log(1000)
which is roughly 10000. But the actual number is more likelog_2(999)+log_2(998)+...+log_2(1)
so I thought that might be under 6000, which was unfortunately wrong.It's possible to get a very readable implementation for problem F with operator overloading.
For instance, we can make it possible to write
ts.either(A[i] < x, A[i] > x)
for queries of the 1st type.A full solution (in Python) is available here: https://codeforces.me/contest/1697/submission/160806370
The trick also works in C++, as it supports operator overloading too.
for D Can we do like this we will ask 2nd query for every substring of length 2, so if we at 0th index and answer for this is 2 then we can say this s[0] and s[1] are different and we will ask first query for index 0 and 1, and if answer is 1 then we will ask first query for 0th index and s[1] will also be s[0] and if we are at ith index i > 0, and answer is 2 for 2nd query , then we will ask first query only for (i+1)th index as we have already know the char at ith index and if answer is 1, then simply s[i+1] = s[i] this way it will ask n-1 query of 2nd type and max 26 query for 1st type
and if this logic is right ,can anyone tell me where i am doing wrong in my implementation. if logic is not right , please tell me where i am thinking wrong in this logic.
My implementation
Hello!
The same character can appear many times and make many sub-strings whose answer for the 2nd query is 2. So your number of 1st query may be very large.
Thanks, got it
In problem E, one observation I could make is that ...
The clique size in a planar graph with equal distance (manhattan) can be atmost 4.
Only <= 4 vertices can be of the same color.
So maybe $$$O(n^4)$$$ .. or like exactly $$$O((n^4)/24)$$$ can pass.