Спасибо за участие! Надеюсь задачи вам понравились.
Идея: FelixArg
Разбор
Tutorial is loading...
Решение (FelixArg)
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int x;
cin >> x;
int ans = 0;
int a1 = 0, a2 = 0, a3 = 0;
while(min({a1, a2, a3}) < x){
if (a1 <= a2 && a1 <= a3){
a1 = min(a2, a3) * 2 + 1;
}
else if (a2 <= a1 && a2 <= a3){
a2 = min(a1, a3) * 2 + 1;
}
else{
a3 = min(a1, a2) * 2 + 1;
}
ans++;
}
cout << ans << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Идея: FelixArg
Разбор
Tutorial is loading...
Решение(FelixArg)
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n, m;
cin >> n >> m;
vector<vector<int>> a(m);
for (int i = 0; i < m; i++){
for (int j = 0; j < 3; j++){
int x;
cin >> x;
a[i].emplace_back(x);
}
sort(a[i].begin(), a[i].end());
}
vector<int> fib(n + 5);
fib[0] = 1;
fib[1] = 2;
for (int i = 2; i < n + 1; i++){
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < m; i++){
if (a[i][0] >= fib[n - 1] && a[i][1] >= fib[n - 1] && a[i][2] >= fib[n]){
cout << '1';
}
else{
cout << '0';
}
}
cout << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение(awoo)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
i = 0
ans = 10**18
while i < n:
j = i
while j < n and a[j] == a[i]:
j += 1
ans = min(ans, (i + n - j) * a[i])
i = j
print(ans)
Идея: FelixArg
Разбор
Tutorial is loading...
Решение(FelixArg)
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n, m;
cin >> n >> m;
vector<int> a(m);
for (int i = 0; i < m; i++){
cin >> a[i];
}
sort(a.begin(), a.end());
vector<vector<int>> ans(n, vector<int> (6));
for (int i = 0; i < n; i += 2){
if (i + 1 == n){
for (int j = 0; j < 6; j++){
if (j % 2 == 0){
ans[i][j] = a[i / 2];
}
else{
ans[i][j] = a[m - i / 2 - 1];
}
}
}
else{
for (int j = 0; j < 6; j++){
if (j % 2 == 0){
ans[i][j] = a[i / 2];
ans[i + 1][j] = a[m - i / 2 - 1];
}
else{
ans[i][j] = a[m - i / 2 - 1];
ans[i + 1][j] = a[i / 2];
}
}
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < 6; j++){
cout << ans[i][j] << ' ';
}
cout << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Идея: FelixArg
Разбор
Tutorial is loading...
Решение(FelixArg)
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<vector<set<int>>> st(3, vector<set<int>> (3));
for (int i = 0; i < q; i++){
char x, y;
cin >> x >> y;
st[x - 'a'][y - 'a'].insert(i);
}
for (int i = 0; i < n; i++){
if (s[i] == 'a'){
continue;
}
if (s[i] == 'b'){
if (!st[1][0].empty()){
st[1][0].erase(st[1][0].begin());
s[i] = 'a';
continue;
}
if (!st[1][2].empty()){
auto ind = *st[1][2].begin();
auto lb = st[2][0].lower_bound(ind);
if (lb != st[2][0].end()){
st[1][2].erase(ind);
st[2][0].erase(lb);
s[i] = 'a';
continue;
}
}
}
if (s[i] == 'c'){
if (!st[2][0].empty()){
st[2][0].erase(st[2][0].begin());
s[i] = 'a';
continue;
}
if (!st[2][1].empty()){
auto ind = *st[2][1].begin();
st[2][1].erase(ind);
s[i] = 'b';
auto lb = st[1][0].lower_bound(ind);
if (lb != st[1][0].end()){
st[1][0].erase(lb);
s[i] = 'a';
}
}
}
}
cout << s << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Идея: FelixArg
Разбор
Tutorial is loading...
Решение(FelixArg)
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int p, s;
cin >> p >> s;
if (p > 4 * s){
cout << "-1\n";
return;
}
if (p > 2 * s){
for (int i = 1; i <= 50000; i++){
if (p * i == (2 + 2 * i) * s){
cout << i << '\n';
for (int j = 0; j < i; j++){
cout << "0 " << j << '\n';
}
return;
}
}
cout << "-1\n";
return;
}
int k = 0;
while(p > 2){
p -= 2;
s -= 1;
k++;
}
if (p == 2){
cout << 4 * s * s + 4 * s * k << '\n';
for (int i = 0; i < 2 * s; i++){
for (int j = 0; j < 2 * s; j++){
cout << i << ' ' << j << '\n';
}
}
for (int i = 0; i < 4 * s * k; i++){
cout << 0 << ' ' << 2 * s + i << '\n';
}
}
else{
cout << 16 * s * s + 16 * s * k << '\n';
for (int i = 0; i < 4 * s; i++){
for (int j = 0; j < 4 * s; j++){
cout << i << ' ' << j << '\n';
}
}
for (int i = 0; i < 16 * s * k; i++){
cout << 0 << ' ' << 4 * s + i << '\n';
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Идея: FelixArg
Разбор
Tutorial is loading...
Решение(FelixArg)
#include<bits/stdc++.h>
using namespace std;
struct PersistentSegmentTree{
#define _null -1
struct Node{
int l, r;
int val;
Node(): l(-1), r(-1), val(_null) {}
};
vector<Node> data;
vector<int> roots;
PersistentSegmentTree(){}
inline void copy(int x, int y){
data[x].l = data[y].l;
data[x].r = data[y].r;
data[x].val = data[y].val;
}
int build(int v, int l, int r, vector<int>& a){
if (data.size() == v){
data.emplace_back();
}
if (l == r){
data[v].val = a[l];
return v;
}
int m = (l + r) / 2;
data[v].l = build(data.size(), l, m, a);
data[v].r = build(data.size(), m + 1, r, a);
data[v].val = max(data[data[v].l].val, data[data[v].r].val);
return v;
}
int update(int v, int l, int r, int pos, int val){
int new_v = data.size();
data.emplace_back();
copy(new_v, v);
if (l == r){
data[new_v].val = val;
return new_v;
}
int m = (l + r) / 2;
if (pos <= m){
data[new_v].l = update(data[v].l, l, m, pos, val);
}
else{
data[new_v].r = update(data[v].r, m + 1, r, pos, val);
}
data[new_v].val = max(data[data[new_v].l].val, data[data[new_v].r].val);
return new_v;
}
int get(int v, int l, int r, int ql, int qr){
if (ql > qr){
return _null;
}
if (l == ql && r == qr){
return data[v].val;
}
int m = (l + r) / 2;
return max(get(data[v].l, l, m, ql, min(qr, m)),
get(data[v].r, m + 1, r, max(ql, m + 1), qr));
}
};
void solve(){
int n;
cin >> n;
vector<int> p(n);
for(int i = 0; i < n; i++){
cin >> p[i];
p[i]--;
}
vector<pair<int,int>> p2(n);
for (int i = 0; i < n; i++){
p2[i] = {p[i], i};
}
sort(p2.begin(), p2.end());
vector<tuple<int,int,int,int>> seg;
set<pair<int,int>> st;
st.insert({-2, -2});
st.insert({n + 1, n + 1});
for (int i = 0; i < n; i++){
auto [val, ind] = p2[i];
auto up = st.lower_bound(pair{ind, -1});
auto down = (--st.lower_bound(pair{ind, -1}));
int l, r;
if (down->second == ind - 1 && up->first == ind + 1){
l = down->first, r = up->second;
st.erase(up);
st.erase(down);
}
else if (down->second == ind - 1){
l = down->first, r = down->second + 1;
st.erase(down);
}
else if (up->first == ind + 1){
l = up->first - 1, r = up->second;
st.erase(up);
}
else{
l = ind, r = ind;
}
st.insert({l, r});
up = st.lower_bound(pair{l + 1, -1});
down = (--st.lower_bound(pair{l, -1}));
if (l > 0){
seg.emplace_back(max(0, down->second + 1), l - 1, r, i * 2);
}
if (r < n - 1){
seg.emplace_back(l, r, min(n - 1, up->first - 1), i * 2 + 1);
}
}
sort(seg.begin(), seg.end());
vector<tuple<int,int,int>> seg2;
for (int i = 0; i < seg.size(); i++){
auto [l1, r1, r2, ind] = seg[i];
seg2.emplace_back(r1 + 1, r2, ind);
}
sort(seg2.begin(), seg2.end());
vector<int> pos(2 * n, -1);
for (int i = 0; i < seg2.size(); i++){
auto [l, r, ind] = seg2[i];
pos[ind] = i;
}
set<pair<int,int>> open_left_segs;
vector<int> cur(2 * n, -1);
PersistentSegmentTree tree;
tree.roots.emplace_back(tree.build(0, 0, 2 * n - 1, cur));
vector<int> T(n, -1);
int u = 0;
for (int i = 0; i < n; i++){
while(u < seg.size() && get<0>(seg[u]) <= i){
auto [l1, r1, r2, ind] = seg[u];
open_left_segs.insert({r1, pos[ind]});
tree.roots.emplace_back(tree.update(tree.roots.back(), 0, 2 * n - 1, pos[ind], r2));
u++;
}
while(!open_left_segs.empty() && open_left_segs.begin()->first < i){
auto [r, ind] = *open_left_segs.begin();
open_left_segs.erase(open_left_segs.begin());
tree.roots.emplace_back(tree.update(tree.roots.back(), 0, 2 * n - 1, ind, -1));
}
T[i] = tree.roots.back();
}
int q;
cin >> q;
for (int i = 0; i < q; i++){
if (i % 10 == 0){
cout.flush();
}
int l, r;
cin >> l >> r;
l--;
r--;
auto lb = lower_bound(seg2.begin(), seg2.end(), tuple{l + 1, -1, -1});
if (lb == seg2.end()){
cout << "NO\n";
continue;
}
auto lb2 = lower_bound(seg2.begin(), seg2.end(), tuple{r + 1, -1, -1});
int val;
if (lb2 == seg2.end()){
val = tree.get(T[l], 0, 2 * n - 1, pos[get<2>(*lb)], 2 * n - 1);
}
else{
val = tree.get(T[l], 0, 2 * n - 1, pos[get<2>(*lb)], pos[get<2>(*lb2)] - 1);
}
cout << (val >= r ? "YES\n" : "NO\n");
}
cout.flush();
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
//cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
return 0;
}
Решение(awoo)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
const int INF = 1e9;
const int LOGN = 18;
struct solver{
int n;
vector<int> p;
solver(const vector<int> &p) : p(p){
n = p.size();
build();
}
vector<int> nxtmn, nxtmx;
vector<vector<int>> up;
vector<vector<int>> mx;
void build(){
nxtmx.resize(n);
vector<int> stmn, stmx;
for (int i = n - 1; i >= 0; --i){
while (!stmx.empty() && p[stmx.back()] < p[i])
stmx.pop_back();
nxtmx[i] = (stmx.empty() ? n : stmx.back());
stmx.push_back(i);
}
vector<vector<int>> qr(n + 1);
forn(i, n) qr[nxtmx[i]].push_back(i);
nxtmn.assign(n, n);
for (int i = n - 1; i >= 0; --i){
while (!stmn.empty() && p[stmn.back()] > p[i])
stmn.pop_back();
stmn.push_back(i);
for (int j : qr[i]){
int l = 0, r = int(stmn.size()) - 1;
while (l <= r){
int m = (l + r) / 2;
if (p[stmn[m]] < p[j]){
nxtmn[j] = stmn[m];
l = m + 1;
}
else{
r = m - 1;
}
}
}
}
up.assign(n + 1, vector<int>(LOGN));
mx.assign(n + 1, vector<int>(LOGN));
forn(i, n){
up[i][0] = nxtmx[i];
mx[i][0] = nxtmn[i];
}
up[n][0] = n;
mx[n][0] = 0;
for (int j = 1; j < LOGN; ++j) forn(i, n + 1){
up[i][j] = up[up[i][j - 1]][j - 1];
mx[i][j] = max(mx[i][j - 1], mx[up[i][j - 1]][j - 1]);
}
}
bool query(int l, int r){
int v = l;
for (int i = LOGN - 1; i >= 0; --i){
if (up[v][i] >= r) continue;
if (mx[v][i] >= r) return true;
v = up[v][i];
}
return false;
}
};
int main(){
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> p(2, vector<int>(n));
forn(i, n){
cin >> p[0][i];
--p[0][i];
}
p[1] = p[0];
reverse(p[1].begin(), p[1].end());
solver p0(p[0]);
solver p1(p[1]);
int m;
cin >> m;
forn(i, m){
if (i % 10 == 0){
cout.flush();
}
int l, r;
cin >> l >> r;
--l; -- r;
bool ans = p0.query(l, r + 1) || p1.query(n - r - 1, n - l);
cout << (ans ? "YES\n" : "NO\n");
}
cout.flush();
}









I have another idea is that we will try all (p,q) , (2*p,2*q) ,(3*p,3*q),... and when we know the perimeter i will create a nearly square with a=(p/2)/2 and b=(p/2)-a so when i reduce one small piece from the top-right the perimeter will not change and the area will reduce 1 so I can check if it can become p/q or not.Also this idea give the smallest value of perimeter and area that can have the ratio is p/q (i can prove this but i can't use English well so I won't prove here)
I have an idea for F — Puzzle is that we first we will simplified fraction p/q and if p is odd we will mul p and q with 2 and we will try all (p,q),(2*p,2*q),... so when we have p we will create a nearly square with a=(p/2)/2 ,b=(p/2)-a(because it has the biggest area with perimeter p) and when we delete 1 small piece from the top-right the perimeter will not change and the area will reduce 1 so we can check if that is possible to have the raito (p,q) if we delete some small piece and also i it can it will be the smallest perimeter and area that we can build.(i can prove that but i can't comment here because my english is bad).
I will look like this
I tried something different on F, I realised that whenever a unit square shares exactly two sides with the existing shape, adding or removing it changes only the area (by +1 and -1 respectively) and leaves the perimeter unchanged. After this, I reduced the p/s fraction to it's basic form, where gcd(p, s) = 1; In this form, s * 2 + 2 is the maximum possible perimeter achievable by s squares, if the needed perimeter ratio exceeds this, then we output -1. Otherwise, we start at (0,0) and alternately attach one square upward or leftward: each step adds area +1 and perimeter +2. As soon as the current perimeter is divisible by p and the target area = s·(perimeter/p) fits inside the m×n “L” we have built (where m,n count how far up/left we have gone), we fill exactly the needed interior squares (each of which shares two edges, so perimeter stays fixed) until area = s·(perimeter/p). This makes a single connected shape with perimeter/area exactly p/s and under 50,000 pieces.
Here is my submission that implements the same, 322889890
An easier way to come up with the correct greedy for E: after observing that you won't use both b -> c -> a and c -> b -> a in the optimal solution, just simulate twice, once for the case where you never do b -> c -> a and once where you never do c -> b -> a, and take the minimum of the two cases. In the first case, for B's you will always try b -> a, for C's you will first try c -> a (so that you don't take away a b -> a for a future B) and then try c -> b -> a, and lastly try c -> b. The second case is similar, for C's you try c -> a then c -> b, for B's you try b -> a then b -> c -> a.
My submission
deleted
in problem B why we are not checking condition f[n] >= x & f[n] >= y & f[n] >= z we checked for largest side of box only.. why..? .Each f[i] is a cube that means each side of box (x , y , z ) must be at least f[n] for f[n] to be fit in box..what i am thinking wrong in this
in the editorial code $$$fib_n$$$ isn't the same as you thought about it
we start at the index 0, which means that $$$fib_n$$$ will be in index $$$fib_{n - 1}$$$, and we have to check that the largest side is greater than or equal to $$$fib_{n - 1}$$$ + $$$fib_{n - 2}$$$ which is the same as $$$fib_n$$$ in the code, and we check that the smallest two sides are greater than or equal to $$$fib_{n - 1}$$$
I hope you got it
Understood.Thanks Mousa_Aboubaker
In my solution for problem E, I too have used a greedy approach with sets, but for some reason it is giving me a TLE on test case 2
https://mirror.codeforces.com/contest/2111/submission/322749796
(My code might not be that readable, but I only want to know why is it exceeding time limit)
you're using set ,that adds to the complexity and unordered gives wa , maybe you are on the right track but think more :)
i case Y=='a',X!='b'i think you need to check that if b is empty or not
Prob B is easy just check if the large cube can contain the 2 largest cube of n cubes
My solution for F: for each multiple of the perimeter, we will check if we can make the corresponding area possible. Lets say we are considering parameter X. X is equal to 2*(a+b), where a and b are the sides of the enclosing rectangle of the shape. so then lets consider the a shape whose sides add up to X/2. the minimum area a shape whose sides add up to X/2 is a+b-1 and the maximum is a*b, where a=(X+1)/2 and b=X/2, so that the area of the corresponding rectangle(or a square if (a+b)%2==0) is maximized. then as long as the corresponding area is within that interval, a solution exists. the construction itself is then trivial
I used the same approach for problem E, I just used variables to count the frequency of each sequence and utilizing the frequency to modify the string.
322753408 Why does it fail for test 4?
you may check 322803496 quite similar approach
My observation on problem E.
To make the string as lexicographically minimal as possible, we can run through the original string and make some changes:
I will use the variables cxy to count the number of times I can change 'x' to 'y' and cxyz to count the number of times I can change 'x' to 'y' then to 'z'. We run through all operations:
Now, we will run through the original string and replace each character to the smaller one if possible, then decrease the counter. E.g., if s[i] == 'b' and cba > 0 then s[i] = 'a', cba --; if s[i] == 'b' and cbca > 0 then s[i] = 'a', cbca --, cca --.
Note: you don't need to make all possible cxy or cxyz, just define enough variable for for implementation.
This is my accepted submission: 323080812
Why is this 322905305 failing on problem E. (Code has been written by little bit different algorithm and has been sent with my second account during contest)****
Thanks for Nice and in-detail, cool tutorials :)
Problem E can be solved in O(N+Q) with regret greedy.
322907253
Since we always want to turn the earliest non-'a' character to 'a', we can take a greedy approach initially. As we go through the queries (which are each a 'find'->'replace' character pair), we store the amount of c->b and b->c that we have seen, since these are not immediately optimal to use. We keep track of the earliest 'b' and 'c' in the string by using pointers, so going through the string is O(N).
Whenever we encounter b->a or c->a, we can use it by itself if the 'find' character is the same as the earliest non-'a' character. If the earliest non-'a' character is not the 'find' character, we can still turn it to 'a' if we have a nonzero amount of c->b or b->c left over (whichever works with our current query) to make c->b->a or b->c->a, respectively. However, if we encounter a c->a or b->a later, respectively, we will want to use that instead. As such, whenever we do a c->b->a or b->c->a, we will store it to query later when we run into a c->a or b->a, swapping them out if it would be optimal. If we do swap them out, we can use the now-available b->a or c->a to modify a later character.
Due to the nature of this problem, we can implement regret greedy by using queues instead of heaps for O(N) overhead instead of O(NlogN) overhead.
After finishing, we use up as many remaining c->b as possible. This can be seen to be optimal.
Overall, this solution is O(N+N+N+Q) = O(N+Q), improving upon the O((N+Q)logQ) solution in the editorial. Please tell me if you have any questions or concerns regarding my code.
I am trying something similar, can you please check why its failing...
wrong answer 970th words differ — expected: 'aabbb', found: 'aabbc'
https://mirror.codeforces.com/contest/2111/submission/322916345
just instead of directly iterating on array, i am using a set for all indices of a, b, c
I think the error may come from how you add to cb at the end. Try handling both cases (b-c-a and c-b-a) separately rather than putting both into CONV and doing both at once.
I thought the same, so i was trying this https://mirror.codeforces.com/contest/2111/submission/322916990
its virtually what you mention in the reply, (with CONV also it was same only logically)
same issue,
idk why the letter 'c' is giving me a headache
You subtract from cba after exhausting bca if ele is either 'b' or 'c' However if ele is 'b' you can keep the c-b since you are only using the b-a in that case. You should handle 'b' case and 'c' case separately once you run out of bca
Damn, missed this, finally got over this one But hit another wall in testcase 6
wrong answer 28th words differ — expected: 'aaaaaaaaaaaaaabbb', found: 'aaaaaaaaaaaaaabbc'
https://mirror.codeforces.com/contest/2111/submission/322917344
I have also done the same using queue instead of set. My solution is similar to the editorial just used queue instead of set. I couldnt understand how the edge cases in the queue solution is resolved in the set solution. Like to iterate once more to perform c->b but in the editorial it is done in the same . Can you explain it.
My solution -322905305
Main difference is that the set solution iterates over the string rather than the queries. This means that they can pick the most optimal choice for each letter, and once they stop being able to turn c to a they just turn c to b. You need to use set for this solution because when you are doing c-b-a you need to find the closest b-a that took place at a later time than c-b (otherwise you might apply c-b-a when b-a appears before c-b). This b-a is not always the one at the front, so we use set for this approach.
My solution iterates over queries, so I do not know for sure if I should use up c-b by itself before I finish reading all queries (after all, there may be a c-a up ahead that would be more optimal). Of course, I could extend regret greedy to also track c-b usage and not have the end loop, but it's easier to just keep that end loop since it doesn't impact the overall complexity.
In summary: set solution knows all queries so it can always be optimal, queue solution doesn't know all queries so it has to make sacrifices. You cannot use queue for the editorial solution because queues have O(n) search instead of O(logn) search and the editorial solution requires searching.
Hope this helps
Can someone explain me in F how is the ratio? i dont understand
In problem F how can I get the ratio? I dont understand
forgot it
I misread problem D by a big margin, I thought that its asked to maximize the minimum no of moves between the floors among all groups and didn't realize until after the contest. I wonder how this version would be solved ?
There is another idea for problem F. It can be solved using Pick's rule for an area of a figure without any holes in it.
So basically Area = inner + border/2 — 1 (inner is a number of integer points strictly inside a figure, border — a number of integer points on the border of a figure). In our case perimeter = border. And also we can observe that in our case 'border' >= 4 and 'border' is also even, which can be quite easily proved.
And with this we can rewrite the formula with the ratio and express 'inner' in terms of 'border'. After this we iterate over 'border', find its 'inner' if it's a positive integer.
And all that's left is to create a construction to build a component (without any holes) with such area and perimeter. I'm sure there are other constructions, but i was able to come up with a not that hard one.
So imagine a 90 degrees angle with sides of width 1 (inner = 0), and we can observe that if we add one piece which will have two adjacent pieces that we already added before, the number of inner points will increase by one, but the number of border points will be the same. And the maximum number of inner points that we can get in such way equals to the (a — 1) * (b — 1), where a, b — sides of the initial angle. And so we want to find such {a, b} with maximum product, and we can find them using formula for 'border', which did not change after the creation of an angle( border = a * 2 + b * 2 ).
Submission
Wow glad that you posted solution 2 in G. That's what I came up with
A with bitmask $$$O(logn)$$$
Actually i found something interesting,the key of problem A can be think alternative which is the most significant bit cannot exceed more than 1,for example $$$3=0011_2$$$ and $$$0=0000_2$$$ so if the most significant bit of two number more than 1,then you can't make the smaller one larger than the bigger number otherwise we can just let the smaller number be bigger number right shift 1 position in binary representation.
problem A — O(1):
ans = ceil(log2(n + 1)) * 2 + 1
Low precision with log function and ceil.Actually log2 function work in O(logn) and your method with an low precision and non-extendable just use some basic bitmask from university is enough
The problems are extremely hard((
What's the offline solution for G?
why in task G, in 8th test, in 9th query, the answer is YES?
When $$$x=3$$$ , $$$\forall i \in [2,3] ,x\ge a_i$$$ and $$$\forall j \in [4,9],x\lt a_j$$$ , which satisfies the first condition.
Why This Approach fails for C??
You can solve G in $$$O(N\log(N)+Q)$$$.
This solution's goal is to construct a binary-jumping structure as follows:
If we have both $$$BL$$$ and $$$BR$$$, the answer to any query is true whenever $$$BL[l][o] \geq r$$$ or $$$BR[r][o] \leq l$$$ where $$$o=\lfloor \log_2(r-l+1) \rfloor$$$. For any $$$(L, M, R)$$$ (defined same as editorial) that is a valid solution for $$$l$$$, $$$r$$$, at least one of the ranges covered by $$$BR$$$ and $$$BL$$$ will fully cover $$$[M-1,M]$$$ and therefore that solution will be included.
To note is that normal construction of binary-jumping structures does not apply to this problem. For the next part I will show only how to construct $$$BL$$$ because $$$BR$$$ is built the same when the original permutation is reversed. Instead, for every $$$(L, M, R)$$$ pair we wish to do the following:
I'm sure there are many ways to do this quickly. This is what I did:
Lmk if anyone needs clarification. Here's my submission for reference: 323347704
I would like to share my approach to F. First we divide both p and s by GCD(p, s). Big thing I noticed is that it must hold 4 * sqrt(S) <= P <= 2 * S + 2 (you can get this by looking at biggest possible P / S, which is straight line and smallest possible, which is a square). From here you can manually solve the cases P = 2 * S + 2 (straight line) and P = 2 * S + 1 (just look at 2 * p / 2 * s, and you get P = 2 * S + 2 again). For P <= 2 * S, no matter which p * k / s * k you take, second part will always hold true. So now we only need to satisfy the first part of the inequality. We need to find smallest k such that 4 * sqrt(k*S) <= 4 * P, so k >= 16 * S / P^2 (note that this k may not be the ideal one because we took assumption that we can take sqrt(S) freely, so we need to check tighter constraint and possibly increase k by a little). Tighter constraint is if S = a^2 + b, b < 2 * a + 1 1) b = 0, O >= 4 * a 2) b < a, O >= 4 * a + 2 3) else, O >= 4 * a + 4 We increase k by 1 until this tighter constraint holds, and possibly by 1 more if P * k is odd (P must be an even number). Now we have p = p * k, s = s * k. After this, we first create a line of P squares, which will have S2 = 2 * P + 2 and start creating a square of size floor(sqrt(P)). Notice that by moving a piece from the line to the square, S stays the same and P2 reduces by 2 when we move the piece to touch 2 parts of the square. So we do this until P2 = P (on the very end, if we create the whole square and it still doesn't hold true, we can start creating square with a side 1 bigger until it holds true)
IDK why Codeforces is so unfair to Python programmers. Maybe they don't even consider python dev a programmer. Getting TLE on following solution for problem B at test-case# 5:
t = int(input())
for _ in range(t):
n, m = map(int, input().split()) a = 1 if n > 1 else 0 b = 2 if n > 1 else 1 for _ in range(3, n + 1): a, b = b, a + b res = "" for _ in range(m): l, w, h = map(int, input().split()) if a + b <= max(l, w, h) and b <= min(l, w, h): res += "1" else: res += "0" print(res)problem A — O(1):
ans = ceil(log2(n + 1)) * 2 + 1
F can be solved with Pick's theorem
(Area) S = i + b / 2 — 1. (Perimeter) P = b. Where i is number of points inside figure and b is number of points on the border. s / p = (i + b / 2 — 1) / b i * p + p * (b / 2) — p = b * s. Lets make t = b / 2 new variable i * p + t * (p — 2 * s) = p. That is out diophantine equation to solve. Lets solve it, make sure i >= 0, t >= 1. How do we construct an answer? It i == 0 then we can just output a line. If i > 0, i suggest doing greedy. Lets build rectangle with sides maximizing the area, but maintaining its perimeter to be 2 * t. Then if i > (side1 — 1) * (side2 — 1) then we won't be able to fit the needed number of points inside. Otherwise we are fine, can construct the answer like two sides of rectangle to get needed perimeter and then fill the inside area to get needed area.
In order to be sure, that answer does not exist in case where we cannot fit it inside we must greedely maximize the sides. That's some ifs with coefficient gained from diophantine equation. This solution requires at most 40100 squares for case p / s == 1/ 50.
324262068
In problem E, For the case where input is 1 3 c a b b a c b Why is the expected answer b, even if I can do the transformation c->b->a using the steps c->b and b->a? I am getting this issue in my submission(325025564), also, plz help if you may.
$$$O((n + q) \log^2{n})$$$ solutions for G can be made to fit within the TL: 326467958
OK
the answer of A is (int)__lg(n)*2+3
In the A problem I'm not able to understand the operation constraint i mean what the problem is trying to say can anyone explain in simple language??
Damn! I can't understand the meaning of question A.