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 t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
string a, b;
for (int i = 0; i < 2 * n; ++i) {
a += "()"[i & 1];
b += ")("[i < n];
}
if (a.find(s) == string::npos) {
cout << "YES\n" << a << '\n';
} else if (b.find(s) == string::npos) {
cout << "YES\n" << b << '\n';
} else {
cout << "NO\n";
}
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution 1 (BledDest)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
int m, k, a1, ak;
cin >> m >> k >> a1 >> ak;
int taken_k = m / k;
int taken_1 = m % k;
int taken_fancy_1 = max(0, taken_1 - a1);
int left_regular_1 = max(0, a1 - taken_1);
int taken_fancy_k = max(0, taken_k - ak);
int to_replace = min(left_regular_1 / k, taken_fancy_k);
int ans = taken_fancy_1 + taken_fancy_k - to_replace;
cout << ans << endl;
}
}
Solution 2 (BledDest)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
int m, k, a1, ak;
cin >> m >> k >> a1 >> ak;
// function which calculates the number of fancy coins taken
// if we take exactly x coins of value k
auto f = [m, k, a1, ak](int x)
{
int taken_1 = m - k * x;
return max(0, taken_1 - a1) + max(0, x - ak);
};
int lf = 0;
int rg = m / k;
while(rg - lf > 2)
{
int mid = (lf + rg) / 2;
if(f(mid) < f(mid + 1))
rg = mid + 1;
else
lf = mid;
}
int ans = 1e9;
for(int i = lf; i <= rg; i++) ans = min(ans, f(i));
cout << ans << endl;
}
}
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 t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
int mn = n + 1, mnWin = n + 1;
while (n--) {
int x;
cin >> x;
if (mn < x && x < mnWin) {
ans += 1;
mnWin = x;
}
mn = min(mn, x);
}
cout << ans << '\n';
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
using li = long long;
const int N = 111;
int n;
string s;
int dp[2][N][N * N];
int main() {
cin >> s;
n = s.size();
dp[0][0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i + 1; ++j) {
for (int cnt = 0; cnt <= j * (i + 1 - j); ++cnt) {
dp[1][j][cnt] = n;
}
}
for (int j = 0; j <= i; ++j) {
for (int cnt = 0; cnt <= j * (i - j); ++cnt) {
dp[1][j + 1][cnt] = min(dp[1][j + 1][cnt], dp[0][j][cnt] + (s[i] != '0'));
dp[1][j][cnt + j] = min(dp[1][j][cnt + j], dp[0][j][cnt] + (s[i] != '1'));
}
}
swap(dp[0], dp[1]);
}
int cnt0 = count(s.begin(), s.end(), '0');
cout << dp[0][cnt0][cnt0 * (n - cnt0) / 2] / 2 << '\n';
}
1860E - Fast Travel Text Editor
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;
const int INF = 1e9;
const int AL = 26;
struct query{
int f, t, ans;
};
struct edge{
int u, w;
};
int main() {
cin.tie(0);
iostream::sync_with_stdio(false);
string s;
cin >> s;
int n = s.size();
int m;
cin >> m;
vector<query> a(m);
forn(i, m){
cin >> a[i].f >> a[i].t;
--a[i].f, --a[i].t;
a[i].ans = abs(a[i].f - a[i].t);
}
int k = n - 1 + AL * AL;
vector<vector<edge>> g(k);
forn(i, n - 1){
int j = n - 1 + (s[i] - 'a') * AL + (s[i + 1] - 'a');
g[i].push_back({j, 0});
g[j].push_back({i, 1});
if (i > 0){
g[i].push_back({i - 1, 1});
g[i - 1].push_back({i, 1});
}
}
for (int st = n - 1; st < k; ++st){
vector<int> d(k, INF);
d[st] = 0;
deque<int> q;
q.push_back(st);
while (!q.empty()){
int v = q.front();
q.pop_front();
for (auto it : g[v]){
if (d[it.u] <= d[v] + it.w) continue;
d[it.u] = d[v] + it.w;
if (it.w == 0) q.push_front(it.u);
else q.push_back(it.u);
}
}
forn(i, m){
a[i].ans = min(a[i].ans, d[a[i].f] + d[a[i].t] - 1);
}
}
forn(i, m) cout << a[i].ans << '\n';
return 0;
}
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;
const int INF = 1e9;
struct bracket{
int a, b, c;
};
struct point{
int x, y;
};
long long cross(const point &a, const point &b){
return a.x * 1ll * b.y - a.y * 1ll * b.x;
}
long long dot(const point &a, const bracket &b){
return a.x * 1ll * b.a + a.y * 1ll * b.b;
}
bool operator <(const point &a, const point &b){
return cross(a, b) > 0;
}
int main() {
int t;
cin >> t;
while (t--){
int n;
cin >> n;
vector<bracket> val;
forn(i, 2 * n){
int a, b;
string c;
cin >> a >> b >> c;
val.push_back({a, b, c[0] == '(' ? 1 : -1});
}
map<point, vector<int>> opts;
forn(i, 2 * n) forn(j, 2 * n){
int dx = val[i].b - val[j].b;
int dy = val[j].a - val[i].a;
if (dx <= 0 || dy <= 0) continue;
opts[{dx, dy}].push_back(i);
opts[{dx, dy}].push_back(j);
}
opts[{1, INF}];
vector<int> ord(2 * n), rord(2 * n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j){
long long di = dot({INF, 1}, val[i]);
long long dj = dot({INF, 1}, val[j]);
if (di != dj) return di < dj;
return val[i].c > val[j].c;
});
forn(i, 2 * n) rord[ord[i]] = i;
int neg = 0, cur = 0;
vector<int> bal(1, 0);
for (int i : ord){
cur += val[i].c;
bal.push_back(cur);
neg += cur < 0;
}
bool ans = neg == 0;
vector<int> prv;
for (auto it : opts){
vector<int> tot = prv;
for (int x : it.second) tot.push_back(x);
sort(tot.begin(), tot.end(), [&](int i, int j){
return rord[i] < rord[j];
});
tot.resize(unique(tot.begin(), tot.end()) - tot.begin());
for (int x : tot) neg -= bal[rord[x] + 1] < 0;
vector<int> tmp = tot;
sort(tot.begin(), tot.end(), [&](int i, int j){
long long di = dot(it.first, val[i]);
long long dj = dot(it.first, val[j]);
if (di != dj) return di < dj;
return val[i].c > val[j].c;
});
vector<int> nrord(tot.size());
forn(i, tot.size()) nrord[i] = rord[tmp[i]];
forn(i, tot.size()) rord[tot[i]] = nrord[i];
for (int x : tot){
bal[rord[x] + 1] = bal[rord[x]] + val[x].c;
neg += bal[rord[x] + 1] < 0;
}
if (neg == 0){
ans = true;
break;
}
prv = it.second;
}
puts(ans ? "YES" : "NO");
}
return 0;
}
Pardon me if this is a stupid question, In problem D, after dp is used, the editorial says the answer is in the dp[n][cnt0][need], why can't "need" also be equal to just cnt0*cnt1 instead of (cnt0*cnt1/2) because in 00011 its balanced and the number of 01 subsequences is cnt0*cnt1 so should'nt we also check dp[n][cnt0][cnt0*cnt1].Thanks
00011 is not balanced there are 6 subsequences of 01 but 0 subsequence of 10
ah fudge thanks a lot for answering
00011 is not balanced unless you mean one of the resulting string after swap(s) is balanced. For example: 01010. In that case, the count of 01 & 10 is 3 which is equal to 3*2/2.
In problem C, I used the approach of maximum increasing subsequence. If Alice puts the chip on ith number, BOB, in the next turn, will put it to the second smallest number in maximum increasing subsequence, and then Alice has to move to the smallest number in the next turn which makes BOB the winner.
so Alice can only win if the size of the maximum increasing subsequence is 1 till the ith number. so that bob has to move the chip to the smallest number and Alice wins.
I got the wrong answer on test case 2. Please help me to know why this approach did not work. Thanks in advance.
Alice will not win if the length is 1, She'll only put the coin in first move on the i th element. Now Bob won't have anywhere to move that coin to, So Bob wins. Alice only wins if the length of the maximum increasing subsequence ending at ith element has the length 2 : she puts the coin on ith element, then Bob has to move it to the remaining 1 element, then Alice won't have any moves to make further.
Your text to link here... I also used the approach of maximum increasing subsequence. And I use stack and array q to store the length of maximum increasing subsequence.I think when the length is 2,Alice will win the game.But I got the wrong answer on test case 2. Please help me to know why this approach did not work. Thanks in advance.
Lets take an example:
According to your solution position with value 2 and 4 will be lucky.
But if you see for the value 4, Bob will make a move to 2, Then Alice is forced to make a move to 1, and Bob can't make a move now. So Bob wins.
It is now obviuos that this round had a problem with tests for task D. They were weak, tons of wrong greedy solutions got accepted and then hacked. Only an idiot would consider tests of such a quality normal. This is not okay, and I think the least authors owe to contestants is an apology for their sloppy work. Things like this should not be allowed by the community.
P.S.: Dear BledDest, I'm asking, I'm begging on my knees: please, don't make other posts about how wrong being toxic is. Because, as we've seen yesterday, sometimes if the author spends too much time fighting with toxicity in the community, he may not have enough time left to develop good tests for his problems.
P.P.S.: Don't mean to offend anybody. Make love, not crappy tests.
“Greedy makes man blind and foolish, and makes him an easy prey for death.”
-Rumi
If the test were strong, you can just spamming greedy solution without proving it.
P/S: FST==Skill issue
P/s: BledDest don't listen to the idiot who can't prove his solution because of his skill issue and then blamming you for FST. Your contest is awesome.
Maybe then we should remove all the tests? To assure nobody will send a solution without proving? Moron, submitting without a strong prove is a common thing in competitive programming, exactly because there are tests to tell if solution is right or wrong! And btw remind me, what harm is in someone spamming wrong greedy solution, getting a WA verdict and receiving extra penalty?
This man speaks facts.
What is the point of tests if they accpet ideologically wrong solutions? More than 20% of solutions were hacked right after the contest. Need to pretend that everything is okay and not pay attention to it?
OMG Lucy in cyberpunk O.o
There is also a $$$O(n^4/\omega)$$$ approach for D:
Let's say the imbalance score of a string is the difference between the number of 01 and 10 sequences. Then the desired string should have a imbalance score of 0. There are two observations:
So you can calculate the imbalance score of the initial string and then do a backpack with bitset. In detail, let $$$S_0$$$ denote the set of position where there are 0s initially, and $$$S_1$$$ the set of position where there are 1s initially, and $$$d$$$ to be imbalance score of the initial string.
By observation 2, the task is now transformed into this: find the minimal $$$k$$$ such that you can select exactly $$$k$$$ numbers from each of $$$S_0$$$ and $$$S_1$$$, so that the sum of the $$$k$$$ numbers selected from $$$S_0$$$ is exactly $$$d/2$$$ greater than the sum of the $$$k$$$ numbers selected from $$$S_1$$$.
https://mirror.codeforces.com/contest/1860/submission/219317671
but, why is it always mimimum cnt of exchanges that 0 and 1 in order if (cnt of 01) > (cnt of 10)?
I think it's $$$O(n^4/\omega)$$$ though much faster than std
You are correct, I've just updated the comment.
Hey, can you elaborate more about "backpack with bitset"? Is it some technique ? Any useful link will be appreciated !!
1854B - Earn or Unlock uses this trick.
it is stored in dp(n,cnt0,need) , where cnt0 is equal to the number of characters 0 in the string s , and need=cnt0⋅cnt12 (because the number of subsequences 01 should be equal to the number of subsequences 10). But our dynamic programming stores the number of changes in the string, and the problems asks for the minimum number of swaps. However, we can easily get it from the dp value. Since the amounts of zeroes and ones are fixed in the string, then the number of changes from 0 to 1 equals to the number of changes from 1 to 0 and we can pair them up. So, the answer to the problem is the half of the dp value.
Can anyone explaine me how need is equal to c0*c1 also how storing string changes helps in calculating answer?Please
In problem C, How is it solved using Binary Indexed Tree or Segment tree? What is the logic?
Segment tree or BIT was used to calculate the "Longest Increasing Subsequence" size from 1 to index i for all i from 1 to n.
https://mirror.codeforces.com/contest/1860/submission/219290618
Is this code calculating "Longest Increasing Subsequence"?
The code is checking if the size of the "LIS" is 2 from index 1 to index i if we must take the i'th element. But it is not calculating the actual size.
What can be the best complexity in problem C, if we would allow repetitions in the array?
But the solution will be the same
In problem D, Is there any way to solve it with recursive dp?
Yes: 219363424
There is alternative solution with recursive dp. In my opinion, it is harder (but possible) to proof that it is not TL (Time Limit) exceeding solution. 219620926
The complexity of recursive DP is O(n^4). But there won't be more than 100*100*5000(c0*c1*d) states. Actually it is even smaller than that, because c0+c1<=100. Plus the time limit was 2 seconds and there was no multiple test cases.
The rotating sweep line intuition in F is cumbersome and not so easy to think about. I find the following way to be more easy and intuitive (Well, they are equivalent, but I don't like rotating sweeplines).
Sorting pairs $$$(a, b)$$$ by $$$ax + by$$$ is the same as sorting pairs by $$$a + b\cdot \frac{y}{x}$$$. Now you can replace $$$\frac{y}{x}$$$ with any positive real $$$t > 0$$$, and you sort pairs by $$$a + b\cdot t$$$. From now it's obvious, that initially you arrange the pairs lexicographically ($$$t = +\varepsilon$$$), and then gradually increase $$$t$$$, the pairs that are swapped are of kind $$$(a_1, b_1)$$$ and $$$(a_2, b_2)$$$, where $$$a_1 < a_2, b_1 > b_2$$$ (and the swap is performed for $$$t = \frac{a_2 - a_1}{b_1 - b_2}$$$).
Finally new Color(CM).
Video Editorial for Problem D,E:-
https://youtu.be/khVG1JPdR1o
Video Editorial for Problem A,B,C:-
https://youtu.be/wZF5qfvBhuM
Explanation for those who are confused in Problem E why the author has not constructed a transposed graph to find out the distance from f -> s :
Let say we are connecting an edge of weight 1 when we are going from dummy node to an index node and an edge of weight 0 from index node to dummy node. When we are finding shortest distance from dummy node to index node then bfs on the normal graph will work. It is obvious.
But when we are finding shortest distance from index node to dummy node then we should apply bfs from the index node. But we can't do that as it will result in O(n^2) complexity. Alternative is that we can apply bfs from dummy node in the transposed graph and find the shortest path for each index node. So the complexity is now reduced.
But it is not necessary to construct the transposed graph. Edges between index nodes are already bidirectional. In transposed graph, from dummy to index we will have an edge weight of 0 now . In the original graph, we are getting an extra edge weight as from dummy to index we are traversing via edge weight 1 and rest all edges in path have the same effect.
So we can use the original distance and reduce it by 1.
Hope it helps !!
BledDest Sorry for necroposting. For Problem F, I would like make a clarification to the problem statement by asking what the expected output is given the following test case:
According to the second paragraph of your problem statement, we may choose $$$(x, y) = (1, 1)$$$ so all the $$$a_i \cdot x + b_i \cdot y$$$ are $$$5$$$ and place them in
()()
since it's a tie. However, the sample solution written by awoo above outputsNO
instead.Thanks a lot!
This test case is given in incorrect format. There are $$$2n$$$ points in the problem, not $$$n$$$, so the number in the second line should be $$$2$$$.
If you change it to $$$2$$$, then the model solution says YES.
Thanks for your replying and sorry for making such a stupid mistake...
1860C - Game on Permutation
If a[i] < a[j] j < i will a[i] be lucky ?
If there exist j < i and a[j] is lucky as well a[j] < a[i] will a[i] be lucky ?
a[i] < a[j]
givenj < i
willa[i]
be unlucky. Maintain two variableslucky
andunlucky
.lucky
store min lucky number till indexi
.unlucky
store min unlucky number till indexi
Now fori
checkIf
lucky < a[i]
thena[i]
is unlucky. Updateunlucky=min(unlucky,a[i])
.Otherwise if
unlucky < a[i]
thena[i]
is lucky. Updatelucky=min(lucky,a[i])
Submission : 237228234
Double ke jalwe har jagah dikh rahe h OoO
C was just an application of the classic LIS problem.