Editorials should be made simple

Wtf! :joy: :joy::joy::joy::joy::joy:

:joy::joy::joy::joy::joy:
OK so, what can i do to improve this? I am essentially using the same approach as the editorial, i replaced the array ct[] with a map but that increased the time instead.
Even though using map the complexity is Q√n (compared to Qn using ct[] array), so i guess that’s because of weak test cases.
Even though 1-3 subtask takes only 1.4sec or so last subtask times out.
Any help is appreciated.

Can anybody suggest me some good set of problems and solutions of Graphs/trees along with DP ? Video links would be of great help. :confused:

Try this out

6 Likes

If you have the time, then I have one more query :sweat_smile:
I noticed that when you declared some variables outside the loop in my program the execution time decreased quite a bit, I have read about this before and almost every answer on stackoverflow states that it’s best to keep variables as close as possible to their scope and this does not affect the execution time. But in this case this doesn’t seems to hold. Of course if we are using same value in every iteration it would be best to declare it outside the loop. But in this case that’s basically the same thing, we are calling memeset anyway.
Are there any cases where i shall actually prefer declaring them outside ? (to bypass a tight TLE)

I think allocation of memory in a loop is bad.
As it will have to redeclare it again and again in runtime.
Hence I took it outside.
I keep it outside.
I am not sure if keeping it in loop optimizes code.
One optimization is declare it as static
It reduces running time.

I read that modern compilers automatically prevents re-deceleration, but i guess that might not be true for all cases. Thanks Anyway!
Btw do you have any tip to optimize my code? complexity would be fine if i use a map
( N √n , even though it takes more time :rofl: ) .

EDIT: finally AC, all i had to do was assign ct[]'s used places back to 0 while calculating the answer, this makes it q*√n again. XD this was so simple yet took so much time :joy:.
Thanks for your help mate!

Wtf.
Few extra words.

1 Like

Glad that I could help !!

Yup that is what I do generally
:slight_smile:

XD
completing 20 chars

Lol😂
Codeforces editorials are more confusing, they are not even editorials, they are just hints with no reading-friendly code. See comment section of that blog and you will come to.
Codechef editorials explain from scratch…

What is that? I’ve seen this earlier too.

@ darkcodernavv If you’re writing a comment, it must contain atleast 20 characters, thats the wired char limit discuss has.

1 Like

Mate i am talking about things like this .
Editorialis’t solution of proxy question .
//teja349
#include <bits/stdc++.h>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill - cout << setfill (‘x’) << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;

#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i–)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (100010001000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector
#define vll vector
#define viii vector
#define mod (100010001000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define flush fflush(stdout)
#define primeDEN 727999983
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// find_by_order() // order_of_key
typedef tree<
int,
null_type,
less,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int main(){
std::ios::sync_with_stdio(false); cin.tie(NULL);
int t;
cin>>t;
while(t–){
int d;
cin>>d;
string s;
cin>>s;
int i,cnt=0,gg=0;
int sz=s.length();
rep(i,sz){
if(s[i]==‘P’)
cnt++;
}
int j,val,good=0;
val=(75*sz+99)/100;
f(i,2,sz-2){
if(cnt>=val)
break;
if(s[i]==‘P’)
continue;
good=0;
f(j,-2,0){
if(s[i+j]==‘P’){

                good=1;
                break;
            }
        }
        if(good==0)
            continue;
        good=0;
        rep(j,3){
            if(s[i+j]=='P'){

                good=1;
                break;
            }
        }
       // cout<<i<<endl;
        if(good){
            cnt++;
            gg++;
        }
        
    }
    if(cnt<val)
        gg=-1;
    cout<<gg<<endl;
}
return 0;   

}

is things like things necessary for cp.

1 Like

Bro it is not necessary but everyone have their own style of writing code.
But these little things speed up during contest and u don’t need to write same thing again and again.
As @aryanc403 said read some basics about c++ templates.

After all
In good programming contest all that matters is logic and typing speed…that’s why tourist never uses c++ code template but still secures first position…

2 Likes

A Great Set of problems! But what to do when I get stuck on a problem… It has no solutions anywhere.

I read this discussion top to down and it seems by editorial you guys mean just the setter and tester solution? Is that correct?

1 Like

I guess so,
like they mean the template and seem intimidating to new coders.

The explanation could be made more simple like in a less mathematical way for some problems. :blush:

Mathematical way is the unambiguous way. I would really suggest everybody not to shy away from mathematics. Also, the editorialists usually provide intuition along with mathematics.

2 Likes