Idea: Alan_dong
Preparation: Register
If one team got a score of $$$x$$$ in the first half, what is the maximum score the other team could get?
We can consider the first half and the second half separately. The dream might come true if and only if the scores in both halves are possible. In the second half, the RiOI team scored $$$(c - a)$$$ goals, while the KDOI team scored $$$(d - b)$$$ goals.
Suppose the RiOI team scored $$$x$$$ goals in some half. Denoting the score of the KDOI team as $$$y$$$, one can see that the maximal value of $$$y$$$ is $$$2\cdot x + 2$$$, under the goal order $$$\tt{\color{red}K\color{red}K\color{black}RK\color{red}K\color{black}R...RK\color{red}K\color{black}RK\color{red}K}$$$. When $$$y \geq x$$$, all scores in the range $$$[x, 2\cdot x+2]$$$ can be achieved by deleting several $$$\tt K$$$-s colored red.
Therefore, the scores $$$x: y$$$ in a single half are possible, if and only if $$$\max(x, y) \leq 2\cdot \min(x, y) + 2$$$.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T, a, b, c, d;
int main() {
for (scanf("%d", &T); T--; ) {
scanf("%d%d%d%d", &a, &b, &c, &d), c -= a, d -= b;
if (a > b) swap(a, b); if (c > d) swap(c, d);
puts((a + 1 << 1) >= b && (c + 1 << 1) >= d ? "YES" : "NO");
}
}
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
a,b,c,d=map(int,input().split())
if(max(a,b)>2*min(a,b)+2):
print("NO")
elif(max(c-a,d-b)>2*min(c-a,d-b)+2):
print("NO")
else:
print("YES")
Idea & Preparation: _istil
When shall we output $$$\tt{NO}$$$?
We can put larger numbers on indices with $$$s_i = \tt{0}$$$, and smaller numbers on indices with $$$s_i = \tt{1}$$$.
Note that if there is an interval $$$p_l, \ldots, p_r$$$ such that $$$r - l + 1 \geq k$$$, and $$$s_l = s_{l+1} = \ldots = s_r = \tt{1}$$$, then it's impossible to satisfy all the requirements, since by iterating $$$i$$$ over $$$l$$$ to $$$r$$$, there must be some $$$i$$$ such that $$$p_i = \max(p_l, \ldots, p_r)$$$.
Otherwise, denote $$$c$$$ as the number of occurrences of $$$\tt{1}$$$ in the string $$$s$$$. We can always construct the permutation by filling the indices $$$i$$$ of $$$s_i = \tt{1}$$$ with $$$1$$$ to $$$c$$$ respectively, and indices of $$$s_i = \tt{0}$$$ with $$$(c + 1)$$$ to $$$n$$$ respectively. It works because for every interval $$$[l, r]$$$ such that $$$r - l + 1 \geq k$$$, there is at least one index $$$l \leq i \leq r$$$ such that $$$s_i = \tt{0}$$$, and it will be greater than all the indices of $$$s_i = \tt{1}$$$.
Time complexity: $$$\mathcal O(n)$$$ per test case.
#include <bits/stdc++.h>
void solve() {
int n, k; std::cin >> n >> k;
std::string s; std::cin >> s, s = " " + s;
for (int i = 1, j = 1; i <= n; i = ++j) if (s[i] == '1') {
while (j < n && s[j + 1] == '1') j++;
if (j - i + 1 >= k) return (void)puts("NO");
}
puts("YES");
int c1 = 0, c2 = std::count_if(s.begin(), s.end(), [&](char ch) -> bool { return ch == '1'; });
for (int i = 1; i <= n; ++i)
std::cout << (s[i] == '1' ? ++c1 : ++c2) << " \n"[i == n];
}
int main() { int t; std::cin >> t; while (t--) solve(); return 0; }
import sys
input=lambda:sys.stdin.readline().rstrip()
for _ in range(int(input())):
n,k=map(int,input().split())
s=input()
if "1"*k in s: # YES YOU WILL NOT BELIEVE THAT THIS IS O(N)
print("No")
else:
print("Yes")
a=sorted(range(0,n),key=lambda i:s[i],reverse=True)
ans=[0]*n
for i in range(n):
ans[a[i]]=i+1
print(*ans)
2135A - Against the Difference
Idea: Error_Yuan
Preparation: __baozii__
DP is needed for this problem.
If you force some index to be in the subsequence, there is only one optimal transition.
Let's go for DP. Denote $$$dp(i)$$$ as the length of the longest neat subsequence of prefix $$$[a_1, a_2, \ldots, a_i]$$$. It's obvious that $$$dp(0) \leq dp(1) \leq \ldots \leq dp(n)$$$.
Suppose we have calculated $$$dp(0), \ldots, dp(i-1)$$$. Let's try to calculate $$$dp(i)$$$:
- If $$$a_i$$$ is not selected in the neat subsequence, we simply use $$$dp(i-1)$$$ to update $$$dp(i)$$$;
- If $$$a_i$$$ is selected in the neat subsequence, the subsequence must end with $$$a_i$$$ elements equal to $$$a_i$$$. Since the $$$dp$$$ array is monotone, we should greedily find the $$$a_i$$$-th largest index $$$x$$$ in $$$[a_1, \ldots, a_i]$$$ where $$$a_x = a_i$$$, and use $$$dp(x-1) + a_i$$$ to update $$$dp(i)$$$.
The resulting $$$dp(i)$$$ is $$$\max(dp(i-1), dp(x-1) + a_i)$$$. Finding $$$x$$$ can be done efficiently by storing the occurrences of each different value.
Time complexity: $$$\mathcal O(n)$$$ per test case.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
int T, n, a[MAXN], dp[MAXN]; deque<int> q[MAXN];
int main() {
for (scanf("%d", &T); T--; ) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) q[i].clear();
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1], q[a[i]].emplace_back(i);
if (q[a[i]].size() > a[i]) q[a[i]].pop_front();
if (q[a[i]].size() == a[i]) dp[i] = max(dp[i], dp[q[a[i]].front() - 1] + a[i]);
}
printf("%d\n", dp[n]);
}
}
Idea & Preparation: Error_Yuan
You may not know the given Manhattan distance is between you and which anchor point. Can you make some moves so that you can determine it?
The absolute value in $$$\lvert x_i-c \rvert + \lvert y_i-d \rvert$$$ is somehow annoying, because you don't know whether it's $$$(x_i - c)$$$ or $$$(c - x_i)$$$. Find a way to fix the sign of $$$(x_i - c)$$$ and $$$(y_i - d)$$$.
Try to move the robot to the top-left corner.
Read the hints.
Denote $$$V$$$ as $$$10^9$$$. Let's first use four operations $$$(\texttt{L}, V)$$$, $$$(\texttt{L}, V)$$$, $$$(\texttt{U}, V)$$$, and $$$(\texttt{U}, V)$$$. Since initially $$$-V \leq X, Y \leq V$$$, after the four operations, it's guaranteed that $$$X', Y' \leq -V$$$.
Thus, one can note that the current value received from the jury is exactly $$$\min\limits_{1 \leq i \leq n}(\lvert x_i - X'\rvert + \lvert y_i - Y'\rvert) = \min\limits_{1 \leq i \leq n}(X' - x_i + y_i - Y')$$$, which equals $$$\min\limits_{1 \leq i \leq n}(x_i - y_i) - X + Y + 4V$$$. This gives us the value of $$$X + Y$$$.
Similarly, if we move to the top-right corner (or the bottom-left one), we can know the value of $$$X - Y$$$. It would take $$$4$$$ extra steps of moving down or moving right (instead of $$$4 + 4$$$). By knowing $$$X + Y$$$ and $$$X - Y$$$, $$$X$$$ and $$$Y$$$ can be solved out.
Hence, we use only $$$8$$$ operations for each test case, which is sufficient to pass.
#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int _N = 1e5 + 5;
int T;
void solve() {
int n; cin >> n;
ll mn1 = LLONG_MAX, mx2 = -LLONG_MAX;
for (int i = 1; i <= n; i++) {
ll x, y; cin >> x >> y;
mn1 = min(mn1, y - x);
mx2 = max(mx2, x + y);
}
ll res;
cout << "? R 1000000000" << endl;
cin >> res;
cout << "? R 1000000000" << endl;
cin >> res;
cout << "? D 1000000000" << endl;
cin >> res;
cout << "? D 1000000000" << endl;
cin >> res;
ll res1 = mn1 - res + 4000000000ll;
cout << "? U 1000000000" << endl;
cin >> res;
cout << "? U 1000000000" << endl;
cin >> res;
cout << "? U 1000000000" << endl;
cin >> res;
cout << "? U 1000000000" << endl;
cin >> res;
ll res2 = mx2 + res - 4000000000ll;
cout << "! " << (res2 - res1) / 2 << ' ' << (res2 + res1) / 2 << endl;
return;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while (T--) {
solve();
}
}
Idea & Preparation: Register
Consider the value of nodes on a cycle.
Consider the parity of the cycle's length.
The following are equivalent statements:
- "There is a cycle containing nodes $$$u, v$$$";
- "Nodes $$$u, v$$$ are in the same two-edge-connected component".
Two two-edge-connected components don't affect each other.
Read the hints.
First, focus on a simple cycle only. Denote its length as $$$l$$$. We can choose two arbitrary nodes $$$u$$$ and $$$v$$$ on the cycle, obtaining two different simple paths. The two paths have equal values for all pairs of $$$(u, v)$$$. This implies that, for each set of size $$$l-2$$$ containing only nodes on the cycle, the bitwise XOR of the weight of the nodes in the set should equal $$$0$$$.
Thus, all nodes on the cycle have equal weight, since when sets $$$S_0 \cup {v}$$$ and $$$S_0 \cup {u}$$$ have equal bitwise XOR of node weights, this leads to nodes $$$u$$$ and $$$v$$$ having equal weights. Additionally, if $$$l$$$ is odd, one can verify that the weight of nodes on the cycle must be equal to $$$0$$$ (otherwise, it can be an arbitrary integer in $$$[0, V)$$$).
By decomposing the graph into two-edge-connected components, one can easily determine whether two nodes share a cycle —— by checking whether they belong to the same component. The answer is independent in each component, so we can simply multiply them together.
How to calculate the answer for a two-edge-connected component? First of all, the weight of all nodes must be equal. Then, if there exists an odd cycle (which can be detected with 2-coloring), it should equal $$$0$$$. There are only three different possible answers: $$$0$$$, $$$1$$$, and $$$V$$$.
Thus, one can calculate the answer to the original problem efficiently.
Time Complexity: $$$\mathcal O(n + m)$$$ per test case.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
const int mod = 998244353;
vector<int> g[MAXN], dcc[MAXN];
int dfn[MAXN], low[MAXN], id;
int s[MAXN], tp, p[MAXN], cnt;
void tarjan(int u, int f = 0) {
dfn[u] = low[u] = ++id, s[++tp] = u;
for (int v : g[u]) {
if (!dfn[v]) tarjan(v, u), low[u] = min(low[u], low[v]);
else if (v != f) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
cnt++;
for (int x = 0; x != u; ) {
x = s[tp--], p[x] = cnt;
dcc[cnt].emplace_back(x);
}
}
}
int col[MAXN];
bool check(int u, int f = 0) {
if (~col[u]) return col[u] == f; col[u] = f;
for (int v : g[u]) if (p[u] == p[v] && !check(v, f ^ 1)) return 0;
return 1;
}
int T, n, m, V, a[MAXN], ans;
int main() {
for (scanf("%d", &T); T--; ) {
scanf("%d%d%d", &n, &m, &V), id = cnt = 0, ans = 1;
for (int i = 1; i <= n; i++) dfn[i] = low[i] = p[i] = 0, col[i] = -1;
for (int i = 1; i <= n; i++) g[i].clear(), dcc[i].clear();
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
g[u].emplace_back(v), g[v].emplace_back(u);
}
for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = 1; i <= cnt; i++) {
int x = -1;
for (int u : dcc[i]) {
if (a[u] < 0) continue;
if (x < 0) x = a[u];
else if (x != a[u]) { ans = 0; break; }
}
if (!ans) break;
bool f = check(dcc[i][0]);
if (f) { if (x < 0) ans = (ll)ans * V % mod; }
else if (x > 0) ans = 0;
if (!ans) break;
}
printf("%d\n", ans);
}
}
2135D1 - From the Unknown (Easy Version)
2135D2 - From the Unknown (Hard Version)
Idea & Preparation: Error_Yuan
Try to input $$$10^5$$$ copies of $$$1$$$ in the first query. What can you get then?
After the first query, you can get $$$W\in [L, R]$$$. And $$$2\cdot L\ge R$$$ always holds. Now, can you construct the second query?
In the first query, we will input $$$10^5$$$ copies of $$$1$$$. And the result will be equal to $$$r_1 = \left\lceil\frac{10^5}{W}\right\rceil$$$. By solving the equation $$$\left\lceil\frac{10^5}{x}\right\rceil = r_1$$$, we can get
For example, if:
- $$$r_1 = 1$$$, then $$$W=100\,000$$$;
- $$$r_1 = 2$$$, then $$$W\in [50\,000, 99\,999]$$$;
- $$$r_1 = 3$$$, then $$$W\in [33\,334, 49\,999]$$$;
- ...
For each interval $$$[L, R]$$$ above, $$$2\cdot L \gt R$$$ always holds, which is because $$$\lfloor \frac{n}{2L} \rfloor = \lfloor \frac{\lfloor \frac{n}{L} \rfloor}{2} \rfloor \neq \lfloor \frac{n}{L} \rfloor$$$.
Thus, we can input the following article in the second query:
For each of the underlined group $$$(L, x)$$$:
- If $$$L + x \le W$$$, the two words in this group will be displayed in the same line;
- Otherwise, they will be displayed in different lines.
Note that $$$L + x + L \gt 2\cdot L \gt R \ge W$$$, so for each $$$1\le x \le W - L$$$, the group $$$(L, x)$$$ takes a single line, and for each $$$W - L \lt x \le R - L$$$, the group $$$(L, x)$$$ always take two lines. So the result of the query is $$$r_2 = (W - L) + 2\cdot (R - W)$$$. Hence, we get $$$W = 2\cdot R - L - r_2$$$. And we need to use $$$n = 2\cdot (R - L)\le 10^5$$$ words in this query.
Thus, we solved the problem in at most $$$2$$$ queries, which fits the constraints of the Easy Version.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(v) v.begin(), v.end()
#define logg(x) (31 - __builtin_clz(x))
#define llogg(x) (63 - __builtin_clzll(x))
#define mini(v) min_element(v.begin(), v.end())
#define maxi(v) max_element(v.begin(), v.end())
#define TIME cerr << double(clock() - st) / (double)CLOCKS_PER_SEC
#define sq(a) ((a)*(a))
#ifdef hocln
#include "deb.h"
#else
#define imie(...) ""
#define debug() cerr
#endif
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
typedef tuple<ll, ll, ll> triple;
typedef tuple<ll, ll, ll, ll, ll> five;
typedef unsigned long long ull;
const long long INF = 4e18;
const int inf = 2e9;
const int MN = 3e5 + 15;
const int MX = 2e6 + 15;
//const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long double PI = 3.141592653589793238462643383279502884197;
template<typename T, typename T2> bool chmax(T& a, const T2& b) { return a < b ? a = b, 1 : 0; }
template<typename T, typename T2> bool chmin(T& a, const T2& b) { return a > b ? a = b, 1 : 0; }
template<typename T> using vector2 = vector<vector<T>>;
const int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
const int dy[] = { 1, -1, 0, 0 , 1, -1, 1, -1};
std::random_device rd;
std::mt19937 gen(rd());
ll random(ll low, ll high) { uniform_int_distribution<> dist(low, high); return dist(gen); }
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first;
return is >> p.second;
}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto &i: v) os << i << ' ';
return os;
}
int tc = 0;
inline void get(int l, int r) {
int len = r-l+1;
cout << "? " << len * 2 << ' ';
for(int i = 1;i <= len;i++) {
cout << l << ' ' << i << ' ';
}
cout << endl;
int x;
cin >> x;
cout << "! " << len * 2 - x + l << endl;
}
inline void solve_test() {
int n=1e5;
cout << "? ";
cout << n << ' ';
for(int i = 1;i <= n;i++) cout << "1 ";
cout << endl;
int x;
cin >> x;
if(x == 1) {
cout << "! " << n << endl;
return;
}
int l = -1, r = -1;
for(int i = 1;i <= n;i++) {
if((n + i - 1) / i == x && l == -1) l = i;
if((n + i - 1) / i == x) r = i;
}
get(l,r);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int tt = 1;
cin >> tt;
while(tt--) {
++tc;
solve_test();
}
}
Can you do something better than $$$10^5~1~1~\ldots~1$$$ in the first query, so that the interval length will be reduced for the second query?
Change the first query to $$$N$$$ copies of $$$B$$$, where $$$N$$$ and $$$B$$$ are both undetermined constants.
Brute force to find the optimal value of $$$N$$$ and $$$B$$$.
We want to cut down the total length of articles. So here comes a new strategy for the first query: $$$N$$$ copies of $$$B$$$, where $$$N$$$ and $$$B$$$ are both undetermined constants.
Now, the result for the first query is $$$r_1 = \displaystyle\left\lceil \frac{N}{\left \lfloor\frac{W}{B}\right\rfloor} \right\rceil$$$.
If $$$W\in [1, B - 1]$$$, the editor will be unable to display the article, and unfortunately our original strategy for the second query will not work in this case. But we can still use the strategy in the first query! Now we can query $$$B^2$$$ copies of $$$1$$$, and it's easy to show that the results for each $$$W=1,2,\ldots,B - 1$$$ will be distinct.
Otherwise, $$$W\in [B, 10^5]$$$, similar to the easy version, we will get an interval $$$[L, R]$$$, where $$$2\cdot L\ge R$$$ always holds. Then, we can use the same strategy as the second query in the easy version and get a solution with $$$2\cdot(R - L)$$$ queries. To calculate $$$L$$$ and $$$R$$$, you can simply use binary search, or do some math work to get
There is also a fact that the rightmost interval will be cut off by the upper bound of $$$10^5$$$.
We can use brute force to find suitable values for $$$N$$$ and $$$B$$$. In the main correct solution, we choose $$$N = 11\,343$$$ and $$$B = 116$$$, and the maximal interval length is $$$6\,263$$$. Thus, the total length of articles won't exceed $$$11\,343 + \max(2\cdot 6\,263, 116^2) = 24\,799\le 25\,000$$$.
By some further optimization we can reduce $$$116^2$$$ a little to $$$\le 12\, 526$$$, so the total length of articles can be reduced to $$$23\,869$$$, but that's not needed.
#include <bits/stdc++.h>
using namespace std;
const int MAXW = 100'000;
const int MAXSUM = 25'000;
const int B = 116;
const int K = 11343;
int t;
inline int ask(vector<int> v) {
cout << "? " << v.size();
for (int i : v) {
cout << ' ' << i;
}
cout << endl;
int x;
cin >> x;
if (x == -1) {
exit(0);
}
return x;
}
inline void answer(int x) { cout << "! " << x << endl; }
int main() {
cin >> t;
while (t--) {
vector<int> v_qry1(K, B);
int res_qry1 = ask(v_qry1);
if (res_qry1 == 0) {
// W < B
vector<int> v_qry2(B * B, 1);
int res_qry2 = ask(v_qry2);
int w = (B * B - 1) / (res_qry2 - 1);
answer(w);
} else {
// W >= B
int min_w = ((K - 1) / res_qry1 + 1) * B,
max_w = ((K - 1) / (res_qry1 - 1) + 1) * B - 1;
max_w = min(max_w, MAXW);
for (int w = B; w <= MAXW; w++) {
int h = (K - 1) / (w / B) + 1;
if (h == res_qry1) {
assert(min_w <= w);
assert(w <= max_w);
} else {
assert(w < min_w || w > max_w);
}
}
if (min_w == max_w) {
answer(min_w);
} else {
vector<int> v_qry2;
for (int i = min_w + 2; i <= max_w; i++) {
v_qry2.push_back(min_w + 1);
v_qry2.push_back(i - min_w - 1);
}
if (v_qry2.empty()) {
v_qry2.push_back(min_w + 1);
}
int res_qry2 = ask(v_qry2);
if (res_qry2 == 0) {
answer(min_w);
} else {
answer(min_w + 1 + ((int)v_qry2.size() - res_qry2));
}
}
}
}
}
2135E1 - Beyond the Palindrome (Easy Version)
2135E2 - Beyond the Palindrome (Hard Version)
Idea & Preparation: Register
Full Solution: STUDENT0
Replace $$$\tt 0$$$ with $$$1$$$ and $$$\tt 1$$$ with $$$-1$$$. Try to find a sufficient and necessary condition of $$$s$$$ almost-palindrome.
Denote the $$$\pm 1$$$-s you get in Hint 1 as array $$$a$$$, and $$$b_i = \sum_{j=1}^n a_j$$$. ($$$s$$$ is almost-palindrome) $$$\Longleftrightarrow$$$ ($$$\min b_i + \max b_i = b_n$$$),
You will need to solve the following subproblem:
You are on an infinite 2D plane, initially at $$$(0, 0)$$$. You can only move up/right by $$$1$$$ unit in one step. Also, you cannot touch the line $$$y=x + l$$$ or $$$y = x+ r$$$ during your move. Count the number of ways to travel to $$$(n, m)$$$.
We can solve the subproblem by a technique called "reflection inclusion-exclusion" in $$$\mathcal{O}\left(\frac{n}{|l - r|}\right)$$$ time complexity. You can view more details in problem E1 part of this blog: https://mirror.codeforces.com/blog/entry/129027
Or here is a detailed explanation in Chinese: https://www.cnblogs.com/Hanghang007/p/18159154
Try to compress sums by processing all $$$\max b_i - \min b_i = k$$$ together.
Read the hints first.
Recall the answer for the subproblem in Hint 3 as $$$f(n, m, l, r)$$$, we have
Suppose $$$\min b_i = l$$$ and $$$\max b_i = r$$$, the number of $$$b$$$-s is the number of ways to travel from $$$(0, 0)$$$ to $$$(n, m)$$$, "exactly" touching $$$y=x+l$$$ and $$$y=x+r$$$, that is,
Now we can compress the sums by processing all $$$\max b_i - \min b_i = k$$$ together. Let's enumerate on $$$k$$$, and note that the upper index of binomials is always $$$\frac{n-(l+r)}{2}+\frac{n+(l+r)}{2}=n$$$, and the lower index moves contiguously with the same $$$k$$$.
Thus, we have solved the problem in $$$\mathcal{O}(n\log n)$$$.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
const int mod = 998244353;
inline int add(int x, int y) { return x += y, x < mod ? x : x - mod; }
inline int sub(int x, int y) { return x -= y, x < 0 ? x + mod : x; }
inline void cadd(int &x, int y) { x += y, x < mod || (x -= mod); }
inline void csub(int &x, int y) { x -= y, x < 0 && (x += mod); }
int inv[MAXN], a[MAXN], s[MAXN];
inline
void init(int n) {
inv[1] = 1;
for (int i = 2; i <= n; i++) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
}
int n;
inline
int ask(int l, int r) {
l = max(l, 0), r = min(r, n);
return l ? sub(s[r], s[l - 1]) : s[r];
}
inline
int calc(int k, int t) {
int sum = 0;
for (int p = 0; (n + k >> 1) - p * (k + 2 - t) >= 0; p++) {
cadd(sum, ask((n - k >> 1) - p * (k + 2 - t), (n + k >> 1) - t - p * (k + 2 - t)));
}
for (int p = 0; (n - k >> 1) - 1 - p * (k + 2 - t) >= 0; p++) {
csub(sum, (ll)(k + 1 - t) * a[(n - k >> 1) - 1 - p * (k + 2 - t)] % mod);
}
for (int p = 1; (n - k >> 1) + p * (k + 2 - t) <= n; p++) {
cadd(sum, ask((n - k >> 1) + p * (k + 2 - t), (n + k >> 1) - t + p * (k + 2 - t)));
}
for (int p = 0; (n + k >> 1) + 1 - t + p * (k + 2 - t) <= n; p++) {
csub(sum, (ll)(k + 1 - t) * a[(n + k >> 1) + 1 - t + p * (k + 2 - t)] % mod);
}
return sum;
}
int T, ans;
int main() {
for (scanf("%d", &T), init(1e6 + 1); T--; ) {
scanf("%d", &n), *a = *s = 1, ans = 0;
for (int i = 1, x = 1; i <= n; i++) {
a[i] = (ll)a[i - 1] * inv[i] % mod * (n - i + 1) % mod;
s[i] = add(s[i - 1], a[i]);
}
for (int i = 1, x = 0, y = 0; i <= n; i++) {
if (n + i & 1) continue;
cadd(ans, x), cadd(ans, x = calc(i, 0));
y = calc(i, 1), csub(ans, add(y, y));
}
printf("%d\n", ans);
}
}
List out the coefficients of all $$${n\choose m}$$$.
Try linear sieve.
Read the hints and E1 editorial first. We'll try to speed up in a different way.
Let's consider $$$f\left(\frac{n-(l+r)}{2},\frac{n+(l+r)}{2},l,r\right)$$$ first. Let $$$k=r-l$$$. For each $$$k=1,2,\ldots,n$$$, we have
$$$\displaystyle\sum_{-k\le l\le 0}f\left(\frac{n-(l+r)}{2},\frac{n+(l+r)}{2},l,r\right)$$$
$$$=\displaystyle\sum_{-k\le l\le 0}\sum_{z\in\mathbb Z} {n\choose (n-(2z+1)\cdot k) / 2-l}-{n\choose (n-(2z-1)\cdot k) / 2}$$$
$$$=\displaystyle\sum_{-k+1\le l\le 0}\sum_{z\in\mathbb Z} {\color{red}{n\choose (n-(2z+1)\cdot k) / 2-l}}-{\color{blue}{n\choose (n-(2z-1)\cdot k) / 2}}$$$
$$$={\color{red}2^{\color{red}n}}-\displaystyle k\cdot { \sum_{z\in\mathbb Z}\color{blue}{n\choose (n-(2z-1)\cdot k) / 2}}$$$
We will consider the coefficient of $$${n\choose m}$$$ in the blue part. Note that $$$(n-(2z-1)\cdot k) / 2 = m\Longleftrightarrow k=(n-2m)/(2z-1)$$$. Define $$$g(n)=\sum_{d\mid n,2\nmid(n/d)} d$$$, then the coefficient of $$${n\choose m}$$$ is $$$g(|n-2m|)$$$.
The other 3 parts are similar.
Summing all, we got the final answer is
And we can use linear sieve to precalculate $$$g(n)$$$. Thus, the whole problem is solved in $$$\mathcal{O}(n)$$$.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e7 + 10;
const int mod = 998244353;
inline int add(int x, int y) { return x += y, x < mod ? x : x - mod; }
inline int sub(int x, int y) { return x -= y, x < 0 ? x + mod : x; }
inline void cadd(int &x, int y) { x += y, x < mod || (x -= mod); }
inline void csub(int &x, int y) { x -= y, x < 0 && (x += mod); }
inline
int qpow(int b, int p) {
int res = 1;
for (; p; b = (ll)b * b % mod, p >>= 1) if (p & 1) res = (ll)res * b % mod;
return res;
}
int f[MAXN], g[MAXN], p[MAXN], tot;
int fac[MAXN], ifac[MAXN];
inline
void init(int n) {
f[1] = 1;
for (int i = 2; i <= n; i++) {
if (!f[i]) p[++tot] = i, f[i] = g[i] = i + (i > 2);
for (int j = 1; j <= tot; j++) {
if (i * p[j] > n) break;
if (i % p[j] == 0) {
g[i * p[j]] = g[i] * p[j] + (j > 1);
f[i * p[j]] = f[i] / g[i] * g[i * p[j]];
break;
}
f[i * p[j]] = f[i] * (p[j] + (j > 1));
g[i * p[j]] = p[j] + (j > 1);
}
}
*fac = 1;
for (int i = 1; i <= n; i++) fac[i] = (ll)fac[i - 1] * i % mod;
ifac[n] = qpow(fac[n], mod - 2);
for (int i = n; i; i--) ifac[i - 1] = (ll)ifac[i] * i % mod;
}
int T, n, ans;
int main() {
for (scanf("%d", &T), init(2e7 + 1); T--; ) {
scanf("%d", &n), ans = 0;
for (int i = 0; i <= n; i++) {
ans = (ans + (ll)ifac[i] * ifac[n - i] % mod * sub(f[abs(n - i * 2 - 1)], f[abs(n - i * 2)])) % mod;
}
ans = (ll)ans * fac[n] % mod;
cadd(ans, ans), (n & 1 ? cadd : csub)(ans, qpow(2, n));
printf("%d\n", ans);
}
}
Idea: STUDENT0
Preparation: Register
How to compare two $$$f$$$ values? Since this function grows extremely fast, try to compare $$$\ln f$$$, $$$\ln \ln f$$$ and so on.
You only need to compare lexicographical order.
How to quickly compare lexicographical order? Try using hashing.
Use a data structure to maintain the hash value for each point.
Consider performing a topological sort on the tree from the bottom up. Then, when a node needs to be inserted into another tree, its ranking is already determined. A segment tree can be used instead of a balanced tree.
First, define the concept of a $$$\textbf{power tower}$$$. $$$X$$$ is a power tower if and only if one of the following holds:
- $$$X = x$$$.
- $$$X = x^{X_1X_2\cdots X_m}$$$, where $$$m$$$ is a constant independent of $$$x$$$, and $$$X_1, X_2, \cdots, X_m$$$ are all power towers.
Clearly, for power towers $$$X$$$ and $$$Y$$$, $$$X^Y$$$ is also a power tower. Therefore, in this problem, any $$$f_u(x)$$$ is a power tower.
First, consider how to compare two power towers $$$X$$$ and $$$Y$$$. The steps are as follows:
- If $$$X = x$$$ or $$$Y = x$$$, directly return their magnitudes.
- Otherwise, let $$$X = x^{X_1X_2\cdots X_p}$$$, $$$Y = x^{Y_1Y_2\cdots Y_q}$$$, where the quantities in the exponents are power towers.
- Sort $$$X_1, X_2, \cdots, X_p$$$ and $$$Y_1, Y_2, \cdots, Y_q$$$ in descending order.
- Compare their lexicographical order. If the lexicographical orders are equal, return that $$$X$$$ and $$$Y$$$ are equal; otherwise, return that the power tower with the larger lexicographical order is greater than the one with the smaller lexicographical order.
This gives a solution that can determine the relative sizes of two power towers in polynomial complexity. However, this is far from sufficient.
To optimize the complexity, when comparing $$$X$$$ and $$$Y$$$, we at least need to know the sorted order of $$$X_1, X_2, \cdots, X_p$$$ and $$$Y_1, Y_2, \cdots, Y_q$$$. This is equivalent to knowing the size relationships among them.
A key property of the tree can be easily observed: Let $$$fa_u$$$ be the father of $$$u$$$ in the tree. Then, it must hold that $$$f_u(x) \prec f_{fa_u}(x)$$$. Consider performing a topological sort on the original tree and using a heap to maintain the current queue. Each time, the smallest node is taken out to update the others. For any node $$$u$$$ in the queue, the corresponding power tower $$$X = f_u(x)$$$ already has known rankings for $$$X_1, X_2, \cdots, X_p$$$. A persistent segment tree can then be used to maintain prefix-sum hashes. When comparing two power towers, binary search can be performed on the segment tree to quickly find the first position where their exponent power towers differ, achieving a fast comparison in $$$\mathcal{O}(\log n)$$$ time per operation.
Time complexity: $$$\mathcal{O}(n\log^2 n)$$$ per test case.
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int MAXN = 4e5 + 10;
ull A = mt19937_64(time(0))();
inline ull shift(ull x) {
return x ^= A, x ^= x << 13, x ^= x >> 7, x ^= x << 11, x ^= A;
}
struct node {
int l, r, num; ull val;
} t[MAXN * 20]; int cnt, rt[MAXN];
void add(int &p, int pre, int l, int r, int k, ull x) {
t[p = ++cnt] = t[pre], t[p].val += x, t[p].num++;
if (l == r) return ; int mid = l + r >> 1;
if (k <= mid) add(t[p].l, t[pre].l, l, mid, k, x);
else add(t[p].r, t[pre].r, mid + 1, r, k, x);
}
bool find(int p, int pre, int l, int r) {
if (l == r) return t[p].num > t[pre].num; int mid = l + r >> 1;
if (t[t[p].r].val == t[t[pre].r].val) return find(t[p].l, t[pre].l, l, mid);
else return find(t[p].r, t[pre].r, mid + 1, r);
}
int n, l[MAXN], r[MAXN], fa[MAXN], d[MAXN];
int rk[MAXN], tot, lst; ull h[MAXN];
struct cmp {
bool operator () (int x, int y) {
return h[x] == h[y] ? x > y : find(rt[x], rt[y], 1, n);
}
}; priority_queue<int, vector<int>, cmp> q;
inline
void upd(int u) {
h[u] = h[l[u]] + shift(h[r[u]]);
add(rt[u], rt[l[u]], 1, n, rk[r[u]], shift(h[r[u]]));
}
int T;
int main() {
for (scanf("%d", &T); T--; ) {
scanf("%d", &n), cnt = lst = tot = 0;
for (int i = 1; i <= n; i++) rt[i] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
d[i] = (l[i] && r[i] ? 2 : 0), fa[l[i]] = fa[r[i]] = i;
}
for (int i = 1; i <= n; i++) if (!d[i]) h[i] = 1, q.emplace(i);
for (int u; !q.empty(); ) {
printf("%d ", u = q.top()), q.pop();
if (h[u] != h[lst]) tot++; lst = u, rk[u] = tot;
if (!--d[fa[u]]) upd(fa[u]), q.push(fa[u]);
}
puts("");
}
}








Thank you for the problems, loved D and E, F seems cool too (hate A)
What's your problem with A? I'm curious.
The problem is fine, I just took too long on it and then decided to read the other problems before finally submitting it
thanks for nice problems and quick editorial.
In problem-A, I thought the ratios were given
a:bandc:dbut they are just scores. That completely messed up my contest.Thank you for the contest! C was a really very fun problem (I still don't get how 8000 people solved it but.. anyways)
CHATGPT
may be
Just think about dp
I was so getting a feeling to use dp in C, I wish I had gone forward with it and solved it
How come a problem like Div2C has more than 8000 solves? Is it because it is a very standard problem?
It does seem very suspicious:(
apparently 8k people are good with DP?
I felt it was pretty easy . The implementation was very simple since values were <10^5.
Have you like done topics?
Yeah, I felt that too. It seems like a typical ~1500 DP problem, so the 8k rating isn’t surprising.
why does my logic fail for test case 5 in the div2 D ?? I think we can solve it by moving to extreme positions of the board (top right and bottom left) and then get 2 equations 2 variables to solve for the positions. Am I missing something here?
intcan only hold till2e9approx. you might be getting overflowsI use this all the time
oh I am sorry
can you try to print this variable
int max_diff_xy = -4e9 - 7;how will it help exactly? like this is for finding the corners yk, and I feel I might have messed up here
oh I am guessing this value is overflowing
yk like I am only taking 4 steps and the current answer has given 8 steps, so it seems sus in the logic
ok sorry I am not on my coding laptop so can't help more .. I guess you can see the test case once system test finish.
thanks for trying , I am working hard to reach a higher rating so that I can switch from software to quant. lowkey gonna grind fr fr and everyday
best wishes !!
Please always use the spoiler feature when writing code in comments. That way your comment won't take so much space in the comment section. To do that, click the small CF icon present on the editor, then click "Spoiler".
you have k=1e9 and you are using 2*k it is 2e9 and it is mentioned that you can't use k more than 1e9 in one ope
Your start position can be $$$(-10^9,10^9)$$$, so a single
URwon't guarantee you to be at the top-right.lowkey fr fr, I just observed that, I gave my code to gpt and it solved it in 2 mins, I am 100% sure that gpt cannot take my job as no-one can write just bad code
K while querying should be <= 1e9. You are querying with 2*K which is 2e9
You have to go 1. ? R 1e9 2. ? R 1e9 3. ? U 1e9 4. ? U 1e9 Otherwise you're not guaranteed to reach the top right corner. You can start from (-1e9;-1e9) for example. I ran into the same problem and discovered it in the last 30 minutes.
yeah , I understood my stuff now, it is sad that I did not see that I willl have to move 4 times at least
You should move up/right twice, after a single 1e9 move you might still be in the middle of the field
Thank you for the contest! I enjoyed Div2F, I thought the idea was cool. I think there is a range of B values that work, mine worked with B = 120.
why you suddenly decided to switch from
long longtollstarting from problem D?lol I got lazy to have to type it in the pair every time
Thanks for amazing problems and amazing round!
https://mirror.codeforces.com/contest/2136/submission/336022643 WHAT'S WRONG IN THIS CODE
In a map, you are grouping n elements by taking their modulo, for example (3 3 3), (3 3 3). However, you can also group them using any previous values. If you use a queue, after having the n value in the query, it would be much easier to do this by simply popping one element.
I just don't get how to solve C... not even with the explanation. I knew I had to use DP. Sort of used it, but got time limit exceeded.
it is essentially a (weighted) task scheduling problem, if you see it from that angle the dp is easier to see.
What an amazing contest! Enjoyed A-D problems :)
why does my logic fail in D
336052875
dude , you did the same blunder like me, (-10^9, 10^9) fails for you too
how to fix
you gotta do 2 ups, 2 rights, and 4 downs and then your code will work fine
Woow, so it was written 10 not 8 queries, for faking purposes then? haha I assumed that too, but I said there is a high chance they will not do that so maybe I am wrong
In problem D's editorial.
Let's first use four operations (L,V), (L,V), (U,V), and (U,V). Since initially −V≤X,Y≤V, after the four operations, it's guaranteed that X′,Y′ ≤ −V.
How? isn't up mean add V to y? if my actual position is (0, 0) after this 4 operation my position will be (-20^9, +20^9). Then how it's gurantedd that X', Y' <= -V? .
V = 1e9and X' in your case turns out to be-2e9which is less than-V = -1e9Yes, I already wrote it. But I said how Y' <= -V?
ah ok, my bad
I think they meant going down 2 times by 1e9.
Maybe. Otherwise, it will not become less than -V. They should cross-check the editorial before posting it.
I was struggling with that as well, I guess may be it is just a bug in solution, as we have Cartesian coordinate system we need to do (L,V), (L,V), (D,V), (D,V) and it will work
Why in Div2D/Div1B, moving the robot to top-left corner implies that $$$y_i - Y^\prime \ge 0$$$ if it suppose to $$$Y^\prime$$$ be the maximum possible. This isn't $$$Y^\prime - y_1 \ge 0$$$?
$$${Y'}$$$ is the minimum possible.
If we are moving up, why would be the minimum? There is something that I do not understand?
Oh, I see it now. They messed up the moves. You can't calculate $$${X+Y}$$$ by going top-left. You need to go to the bottom-left or top-right to calculate $$${X+Y}$$$. They are actually trying to go to bottom-left but wrote the wrong moves/confused the coordinates.
Thank you! That was driving me crazy.
I need some assistance regarding Problem D:
When I run the code on my device, it outputs correctly, but when I submit, I get surprised by the judge saying it was a wrong answer, only to find the response the judge received different than mine.
I tried debugging, searching for uninitialized variables, anything compiler dependent, but to no avail (it was a painful last 30 minutes during contest), could anyone help me find the issue?
submission link
Your X-axis is flipped (x decreases when going left, your code assumes it increases). I'm assuming that when testing locally, you also answered with this flipped X-axis in mind, which is why it worked locally.
My approach was slightly different than the editorial's, I went UpLeft and DownLeft, which meant taking the highest $$$y_i + x_i$$$ value (marked by $$$dec$$$ for decreasing to mark highest negative slope line) for the top left anchor, and lowest $$$y_i - x_i$$$ value (marked by $$$inc$$$ for increasing to mark lowest positive slope line) for the bottom left anchor
Explanation :
\left( (X + 2 \times 10^9) - x_{dec} \right) + \left( (Y + 2 \times 10^9) - y_{dec} \right) = UL \newline
X + Y = UL + x_{dec} + y_{dec} - 4 \times 10^9 = UL + dec - 4 \times 10^9 = rhs1 \newline
\left( (X + 2 \times 10^9) - x_{inc} \right) + \left( y_{inc} - (Y + 2 \times 10^9) \right) = DL \newline
X - Y = DL + x_{inc} - y_{inc} - 4 \times 10^9 = DL - inc - 4 \times 10^9 = rhs2 \newline
X = \frac{rhs1 + rhs2}{2} \newline
Y = \frac{rhs1 - rhs2}{2} \newline
$$$
which is my solution
Going left means X-2e9
You are absolutely right, I was calculating for right while labeling it as left (I don't know how I messed this up), but I don't think this labeling switch changes the equations, does it?
Update: This was the issue all along, the query I was sending says left
Lwhile I meant to say rightR30 minutes only to have mixed left with right... outstanding performance, me
Thanks for both of your helps, I really appreciate it
My D1 is failing cause of not flushing final output. It was passing with incorrect interactor during contest. I was given option to drop from rating calculation
KAN can you look into this, I have replied to your message.
Learning from Today's contest,I came up with correct solution of
Div2D(For the champion problem)and was usingmapto store y coordinates with same x and then sorting it using below code:Here
iis a copy of each key-value pair in the map.i.secondonly changes the copy,the original map is untouched,in turn not sorting the real map, giving meWA. So instead usereferencenext time:Wrong Submission:336043993Accepted Submission:336058516for the subproblem of d1E if we want to "exactly" touch x+r and x+l shouldn't we do PIE with l+1 and r-1 instead of l-1 and r+1?
We are sorry about that...
its okay! I was just trying to understand the solution properly since I am not familiar with reflection principle :)
The definition of the subproblem was wrong. It is corrected now ("cross" -> "touch"). Sorry for the inconvenience.
I kind of have a different solution for 2135C - By the Assignment
We can figure out that any odd cycle should have 0s and even cycles should have the same value, and this is necessary and sufficient for the graph to be balanced.
With an understanding of cycle bases, and DFS Tree, it's sufficient to only consider cycles created by back edges in the DFS tree.
So, what remains is to make a DSU and try to unite all those in odd cycles, and check if there is any violation (max value on component > 0). Otherwise, set all odd-cycled components to 0.
Then, unite all those in even cycles, and check if there is any violation (two different non -1 values).
After that, count components with max value = -1.
Consider the classical compression DSU:
Use that, along with another DSU that tells you the first ancestor who is not in the same component as you:
With those two DSU, you can easily unite a path from a node $$$v$$$ to its ancestor
Here is an accepted submission
Baskot
Yep, Same Solution :)
I use Tarjan to get EDCC so I can unite cycles
D2 :skull
The contest has really good problems. I am very weak, so I believe that the contest is not very friendly to people that are not good at ad-hoc and interaction problems (like me)
Same
It feels like E<D
Hi,is there anyone who can explain the state transition equation in the problem C to me?
imagine the arr is like this
2,2,3,2,4,3,2,1,3now if you are at last
3.. and want to calculate ansENDINGexactly at this indexif there are
< threecounts of value3then we can't have any answer ending there. but we do have3 counts of 3... so if an answer is ending here, where might last block start .. it will have to contain exactly3 counts of 3, so we need to findindex of 3which is2 positions behind current index.. and this index is denoted byxin the editorial (and in our example value ofxis2)so you start block for3at index2. now you have to add maximum possible subsequence ending before thatxnow in the editorial
dp[i] means answer ending at 'i' or before, ie for prefix upto index isodptransition just looks at previous indexi-1andprevious index where valid block might have startedSorry,I still don't understand the purpose of x.Can you explain it more,please
do you know DP ?
if yes you should try to create a solution which is bad in time complexity first and then you can optimize it to reach here
otherwise you can ask me which part was not clear, I can explain that , but explaining DP will be difficult over comments.
Maybe I only understand the surface of DP.But can x take any value from 1 to i-1?
no .. if we are looking at
i-thelement thenx = a[i]ie value at i-th index.So we are at
i-thindex and we want to check answer ending here, so we look at value atiposition onlyThank you very much.You are so good at coding and English.
no problem,
also I struggle with understanding editorials so I paste the whole editorial and ask chatgpt to explain it to me
but at the end I add the prompt
assume I know basic DP and basic programming so don't explain those thingsand if I don't understand something I keep asking it again and again
it can help with both coding and english.. you can ask it to explain in simple terms and like step by step
like I am literally doing this right now to upsolve div2E
Yep,I usually use other AI tool to help with my coding skills.But I found they would sometimes make stupid problems,which drove me crazy.
yeah , that is something we have to deal with right now .. hope these tools get more awesome with time.
fingers crossed
Can anyone help me debug my code for problem C? Also many along with me are getting Jury's verdict as 102nd numbers differ for the 2nd test. Can anyone get the exact test case? That will also help..
Anyways, speaking of my solution, I believed to have followed what the editorial says. Still I am unable to crack it. Either my implementation is not matching the logic (for which I am unable to find a test case where the mismatch occurs) or there is a slight misunderstanding in the logic I used to solve.
My submission
Thanks
Found the test case — 2 1 2 2 So my logic was at flaw, got it.
didn't look in detail but I think this is similar to one mistake I made
in both the
if and elsecase you HAVE to dodp[i+] = max(dp[i+1], dp[i]).. I think you are not doing it in the 'if' conditionNo, what you have mentioned is only partially right, as the main issue was that when I encountered 2 1 2, here I was emptying my queue which stored the locations of 2, before going ahead. My solution was not pure dp, as I realized I was using a greedy queue, so since a better solution was found at 2, the previously lying answer [1] which interfered with the subsequence [2, 2] was no longer in memory. And with the next 2, my queue was storing the starting index, which will be 3, eventually leading to a non-optimal answer. Thus when the queue is fixed to continuously keep track of all positions, and following the editorial's logic, then as you rightly mentioned, the if-else cases dissolve.
Corrected Solution for Reference
Anyway thanks for looking into it
ok I am sorry for not looking at deeply, glad you were able to conquer the problem.
Aah its ok, nvm. All is well that ends well
Nice round, enjoyed the problems. Div. 2 D is truly amazing!
Came up with the idea of E quickly but didn't know how to find these two-edge-connected components lmao
Same
So strong of you that can come up with the solution of E quickly! Orz
I can't even imagine the solution at first. I was thinking stuff like linear basis.
But it was truly amazing :p
And then you solved it! Congrats for gaining +212 rating XD
Thanks for your encouragement! I hope I can do better.
Thank you for the wonderful problems and the detailed editorial! It is disheartening to see so many people cheat on problem C. Or maybe I am just pessimistic and assume everyone is a cheater. Yet 8k seems like a suspiciously high number.
theres no way they didnt weed out cheaters, 8k submissions on C problem is insane, how did i get negative delta after solving 3 problems as pupil wtf
wow negative delta after solving 3 problems FOR A PUPIL ... lol .. this is just mind blowing..
I am doing CP before AI stuff, I am quite sure I rarely solved 4 questions before reaching expert
yeah, we are cooked
I wonder why this code gets Time limit exceeded on test 2.Is it because I used getchar() and puts() to interact ?
May I ask for better samples in the next contests please? I guess one of the reasons was that in this contest samples were much less informative than in others and that let me to many small bugs, which took a lot of time to find
In B I added the number of minimal diagonal instead of subtracting it, and ALL of the points in both testcases were on 0th diagonal, like, why?
In C I had written 2 similar solutions and both had small bugs, maybe there aren't any small samples which catch those.
I know that samples don't have to cover all special cases and all, but usually they do. In AtCoder contests there's also some big testcase in the end, that's usually used to check your answer because it takes a long time to observe something on the testcase. I'd appreciate if you did so next time
I solved div2 C using dp + data structures + binary search.. i know it is more redundant, but it comes down to what i'm most comfortable with during the contest :) https://mirror.codeforces.com/contest/2136/submission/336004436
Why D2 gives the constraint so tight? I just check for B=142 and N*B=719660 and it gives 25012 operations , it takes me crazy Not saying D is a bad problem,just for some complaints of the constraints.
Could someone suggest problems similar to Div. 2 C: 2136C - Against the Difference?
The solution code for this problem is incorrect (2135C — By the Assignment). It submitted the same code as for this problem (2135D2 — From the Unknown (Hard Version)), and I hope there is correct code.: 2135D2 — From the Unknown (Hard Version)
sorry for that, I'll fix it soon.
I was really confused about why we use N=11343,B=116.In other words,how can I get them?
You can use brute force to get them, the running time in local is acceptable. The author said he get them by dp, but I don't clearly know how to dp hmmmmm.
336124323 in the first test case W = 20, I am outputting 160s in the first query and the interactor responds with 2, isn't it supposed to respond back with 0?
any help will be appreciated!
Error_Yuan please check it out.
The interactor did respond you with $$$0$$$. I guess you misread the output.
2 1 7863means: you answered $$$2$$$, used $$$1$$$ query, and total length is $$$7863$$$.336155639 you can see here that it didn't respond with 0. I only moved the ! line into the else body.
You forgot to read the number of test cases...
oh, my bad! sorry for the inconvenience.
why my code in problem C is wrong, pls check everyone :(((
1
6
3 3 1 3 3 3
What should be the output of this test case? 4 but your code gives 3. Now think about it.
oh thanks, i'll check it again :)
I think I have some ideas that can further reduce the $$$\sum n$$$ in Div. 1 D2.
The main observation is that when $$$W$$$ is small, instead of only using copies of $$$1$$$, we can actually insert $$$2$$$ in our second query (If $$$W=1$$$ we'd just get a $$$0$$$).
Assuming we'd be using a lot of $$$2$$$, the function of $$$1$$$ mostly becomes “distinguish odd and even $$$W$$$”, which would imply $$$1$$$ is very sparse (so that two $$$1$$$s wouldn't "neutralize" each other). From there on it's basically trial and error. For example, when $$$B=150$$$, you can generate an article that can distinguish all $$$W\in[1,B]$$$ by putting $$$1$$$ copy of $$$1$$$, $$$127$$$ copies of $$$2$$$, and repeat this for $$$75$$$ times. The rest are pretty much the same.
By choosing $$$N=10800$$$ and $$$B=150$$$, we can yield a $$$\sum n\leq21598$$$ solution. There's most likely room to reduce it even more by picking different $$$N,B$$$ and the article that deals with small $$$W$$$ .
(In fact when I was first solving this I mistakenly used a $$$n=3(R-L)$$$ query in the big $$$W$$$ case, but was somehow still able to pass with this optimization...)
I managed to squeeze into 20127 336201560. The maximum is at the $$$W \lt B$$$ case.
BTW,I didn't find the $$$2(R-L)$$$ solution and used $$$3(R-L)$$$ at first. It also passed.
UPD: 19991 336207439
Can someone help me understand what is wrong with my solution for Div2C 336218122
My logic was:
ans[i][action] denotes number of neat subsequences in the subarray indexed (0 indexed) [i, n — 1] such that the i'th element was taken according to the 'action'
Action is 0 if this element is the last of its block. Action is 1 if this element is somewhere in the middle (or beginning) of a block that ends at j, st. j >= i.
I increment ans only when a block is STARTED (since I am going backwards).
Recurrence:
If we want to end a block at i, then just take the maximum number of subsequences that have occured at any index after i (+1 if nums[i] is 1)
When considering i, let nxt[num] be the index of num at any index after i. And cum[i] be the number of numbers equal to nums[i] at indices j >= i.
Then if we want i to be a non-ending part of a block, then find the index of next number in this block, which is nxt[nums[i]], and use its cum to update cum[i].
If cum[i] after update becomes a multiple of nums[i], increment ans[i][?] by nums[i].
In problem E how For odd length cycle the nodes value must be zero ? Is there a proof ? I am Quite Confused ?
If you can see that, elements must be same in a cycle where i-th node is equal to the node i + 2, i + 4 and so on........ that way you can prove it to be zero for ODD cases.
how is div2 C rated less than 1200 on clist??????
Div 1 B (Div 2 D) was such a beautiful problem with fairly easy solution. Loved solving this contest.
In the editorial for problem C, it says that we have to find the two-edge-connected-components. But the code provided for it uses strongly-connected-components. I tried to solve it using TECC but it gets WA. Here is a bit of the code:
Am I missing something? Can someone kindly help? I think I misunderstood something.
Why do I feel like the solution to problem 2135B might be incorrect? $$$\min\limits_{1 \leq i \leq n}(\lvert x_i - X'\rvert + \lvert y_i - Y'\rvert) = \min\limits_{1 \leq i \leq n}(X' - x_i + y_i - Y')$$$
I think it should
$$$\min\limits_{1 \leq i \leq n}(\lvert x_i - X'\rvert + \lvert y_i - Y'\rvert) = \min\limits_{1 \leq i \leq n}(x_i - X' + y_i - Y')$$$
Problem F:
Test case:
(n) 11, (1) 2 7, (2) 3 4, (3) 0 0, (4) 5 6, (5) 0 0, (6) 0 0, (7) 8 11, (8) 9 10, (9) 0 0, (10) 0 0, (11) 0 0What should be the output? If I run it with the provided solution I get
3 5 6 9 10 11 4 8 7 2 1
But according to my understanding, here 2 and 7 has same value for f(x), so 2 should come first since 2 < 7. What am I missing here? img
im curious aswell, having similar issue
Can anyone explain the Hint 2 of 1E
Deleted
Deleted
Can someone tell me how can i optimise my code: https://mirror.codeforces.com/contest/2136/submission/337101177
In the 6th sample input in problem C shouldn't the blocks be [1][1][1][2,2][3,3,3] and the answer be 8?
In 2135C — By the Assignment, my submission has
low[u]==low[v]inis_bipartiteand get WA. Why is it wrong? I think that nodes in the same two-edge connected components have the same low values. If I setscc_idfor each node and comparescc_id[u]==scc_id[v]then get AC.Thank you for providing such amazing problems especially D & E!
nice problem a)) before contest i should read football rules to understant that score after 1 half don't reset??
I want to ask div1 C. My solution is as follows.
Make a DFS-tree from node 1. (preprocess parent, depth for each node)
If the back edge length is odd, which is calculated by depth, allocate all node to zero. If there is any value a[i] > 0, return 0.
If the back edge length is even, allocate all node to some value V, if all a[i] = -1. If there are two more value in a[i], return 0.
I just use DSU for merging nodes in cycle.
Does this solution is different from two edge connected component solution? Even I heard this concept in this editiorial first.. Thank you in advance!
Could you give the proof for the sufficient and necessary condition for problem div1E?
Problems are amazing, it makes me feel very satisfied after solving them. Thank you, authors
C is Good.