Блог пользователя Singinginginging

Автор Singinginginging, история, 23 месяца назад, По-английски

I started coding after I saw Singinginginging was coding

When I was unrated, Singinginginging was already a specialist

When I was a specialist, Singinginginging was already an expert

Thank you, Singinginginging, without you, I would not be coding now

I can still recall that 10 years ago, when I first started coding, you gave me two quotes--

"The next statement is wrong."

"The previous statement is correct."

After 10 years, the quotes are still my life motto, and I will treasure them for life.

Now I am an candidate master, but your rating will not change anymore

Thank you Singinginginging, you are not only my teacher of coding, but also my teacher of life.

Rest In Peace

In memory of Singinginginging

Полный текст и комментарии »

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

Автор Singinginginging, история, 3 года назад, По-английски

Dear fellow Codefeorcers, In hope of educating the vast public, I have written the following notes which hopefully can help readers to have skyrocketed ratings soon. Enjoy! the following is a poggers string function: string s, t; getline(cin, s); stringstream ss(s); while (ss >> t) cout << t << '\n'; As a side note, some coders like to use "goto die;" when they are frustrated enough with a task Global variables are initialized as 0, so you don't need to initialize them (unless you want to initialize to something else) remember to write an init function for multisets Today's fact: MATH one of the laws of log Log (a’)=,loga -> this can help prevent use of binary exponentiation to prevent an extra log factor in time complexity If you want to determine whether the i-th bit (from right, 0-based) is set (equal to 1 in binary representation) in integer x, write: if(x & (1LL << i)) (1LL << i) means "2 to the power of i" For example if(x & (1LL << 3)) { cout << "orz\n"; } x = 8, 9, 10, 11: output orz x = 4, 5, 6, 7: output nothing You can use reverse indexing. int a[2]; cin>>0[a]>>1[a]; // this works! cout<<false[a]+true[a]<<"\n"; Because some keyboards do not have [ and ], you can also write <: and :> . cout<<false<:a:>+true[a:><<"\n"; // this works! c is a character isalpha(c): returns if c is alphabet isdigit(c): returns if c is digit (0-9) isalnum(c): returns if c is alphanumeric, i.e. alphabet or digit isupper(c): returns if c is uppercase alphabet islower(c): returns if c is lowercase alphabet *From memory only other mentors please correct me if anything is wrong Q# is a bad language When coding difference array, always use an array of size N + 2. Reason: When dealing with range [X, Y] with Y = N we need to access the (N+1)-th element in general it is a good habit to open arrays with [n+5] just in case C++ STL: priority_queue Priority queue automatically sorts the elements in the queue according to some specific order. The default order is descending. Declaration: priority_queue name;, for example priority_queue pq;, priority_queue<pair<int, string> > orz;. Usage (assume we are using a priority queue named pq of int data types: pq.push(x); Push an element into the priority queue with value x pq.top(); Gets the top element of the priority queue, since a priority queue sorts elements according to descending order (by default), this is equivalent to finding the maximum value in the priority queue. You can do cout << pq.top(); to output this value. pq.pop(); Pop the top element of the priority queue, i.e. the largest value pq.size(); Returns the number of elements in the priority queue, you can also do cout << pq.size(); to output this value. Tips: 1) pq.size() is an unsigned integer, so if you try to do pq.size() — 1 with pq.size() = 0 integer overflow might happen (probably, depending on the compiler) 2) You can change the sorting order to ascending or even custom order. - Ascending order for ints: priority_queue<int, vector, greater > pq; - Custom order: requires custom sort so I'll not include it here yet Tasks that use priority queue: typical usage of custom order in pq: class cmp{ public: bool operator()(obj a, obj b){ // do some sorting here } }; int main(){ // some code here priority_queue<obj,vector,cmp> pq; } Ceiling function, floor function, integer division and precision errors: 1) In C++, the ceil() and floor() function return the ceiling and floor of a real number respectively. for example, ceil(3.5) = 4, floor(-1.3) = -2, floor(7.34) = 7, ceil(-π) = -3. Formal definition: ceil(x) is the smallest integer that is at least x, floor(x) is the biggest integer that is at most x. 2) a/b means dividing a by b in C++. However, if a and b are both integers, the result a/b is also integer. So, for example if you want to find the ceiling of a/b, do not write ceil(a/b). Instead, write ceil(a * 1.0 / b) so the thing inside the brackets is a double instead of an int. 3) Precision errors (very annoying) might occur if a and b are big enough. So, if you want to calculate the ceiling of integer division (and a and b are big enough, say > 10^8), don't write ceil(a * 1.0 / b), instead write (a + b — 1) / b (and store the result as an integer). int a, b; cin >> a >> b; cout << (a + b — 1) / b << "\n"; When using map, no need to initialize values to 0, because everything is default 0. including bool strings are initiated as empty strings --> "" Compiler Explorer

define int long long: all int in your code becomes long long.

Place it right after using namespace std; (on a new line). Drawbacks: - More memory used - Instead of int main you need signed main, main or int32_t main. Do we needa give a lecture on how macros r despicable features , as suggested by the great this function processes a string as its space-less substrings name Why would I bother to read to prove (?) no string s, t; getline(cin, s); stringstream ss(s); while (getline(ss,t,',')) cout << t << '\n'; Similarly, this processes a string and gets all of its string separated by a comma, e.g. ABC,DEF,AB will output the following: ABC DEF AB You can use rand() % M to produce an integer randomly from 0 to M-1. A better way: mt19937 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int main() {int M = 1226;int x = uniform_int_distribution(0, M — 1)(rng);} if you dont wanna init then declare all your variables locally "break" command terminates a loop. For example, for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (condition) break;}} Here, when condition holds, the code moves to the next i. However, if our code is instead: for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){ if (condition) continue;}} The code moves to the next j. Be careful with which you use. The "goto" command can jump from anywhere to anywhere in a code!!! (it has to be within the same function tho, e.g. inside the main function) How to use: It consists of a "start" and "end" points to instruct the computer to jump from one line of code to another. For the "start" point: goto here; For the "end" point: here:; (You can name "here" as any other names you like) Here's an example of how to use it: Let's say you are writing an exhaustion with 4 loops, and when a condition is satisfied you will need to break all of the loops at once. But since break can only terminate the current loop, you may have to do the following (which is quite tedious): int done=0; for (int i=1; i<=a; i++){ for (int j=1; j<=b; j++){for (int k=1; k<=c; k++){ for (int g=1; g<=d; g++){ if (condition){done=1;break;}}if (done==1) break;} if (done==1) break;}if (done==1) break;} With "goto", this becomes much happier: for (int i=1; i<=a; i++){ for (int j=1; j<=b; j++){ for (int k=1; k<=c; k++){ for (int g=1; g<=d; g++){ if (condition) goto here;}}}}} here:; Like custom sort, you can also have a custom priority queue The code is as below: class cmp { public: bool operator() (int a, int b) { // return true if a is AFTER b, // and return false if b is BEFORE a // THIS IS OPPOSITE FROM CUSTOM SORT } }; int main() { priority_queue<int, vector, cmp> pq; } For simple if else statements, you may opt to shorten it with ternary operators. Syntax: (condition) ? condition_true : condition_false; As an example: if (a>=6) b=arr[1]; else b=arr[2]; can be shortened into b = (a>=6) ? arr[1] : arr[2]; You can use the inbuilt log function in c++ STL (standard template library): int count=-1; while(c>0){ c/=2; count++; } and log(c)/log(2); These two codes will give the same output. I hope the above personal findings I shared can shed some light on ways of improvement. I hope to see everyone in purple soon! Yours sincerely, Singinginginging

Полный текст и комментарии »

  • Проголосовать: нравится
  • -34
  • Проголосовать: не нравится

Автор Singinginginging, история, 3 года назад, По-английски

In light of the great success of my previous blog, I decide to post more wirting and donwgred mye ingalish and psot on Codfrose.

Write about what you will miss most about being a student in Class 6C.

I will miss a lot of thing. take my class desk. class miss and every any school library as an exmple.

but I miss most is my mathematic teacher, He most good thing is He have a lot of can not support you can not doing. He thing any people have potentail, included beg bag bed bad student. and He teach coach student ability is good, he coach me any thing, apart from mathematic more. For exmple, inter problem solve skill. so He is my favourite teacher. after I can not offer see his, but I will ofter come back be back, my second secondary school visit his.

Sorrie for my prefict ingalish i am indain.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -33
  • Проголосовать: не нравится

Автор Singinginginging, история, 3 года назад, По-английски

Since there are so many blogs which are totally unrelated to competitive programming but gaining tons of upvote, I think that posting my writing when I am grade 10 should also gain at least 10 upvote.

Dear Editor,

I am writing to express my opinion on the phenomenon that some parents in Asia countries are installing apps on their children’s mobile phones that can monitor their activities to ensure that they use these devices responsibly. Nowadays, more and more children have their own mobile phones. Hence some parents want to control their son’s or daughter's activities on the Internet. However, I do not agree with these actions.

Firstly, this kind of action makes children feel untrusted. Children and adolescents are at a stage of mental development. They require parents’ love and trust, to develop their minds in a healthy way. In contrast, installing apps on their phone will lead to them thinking that their parents do not trust that they can use their phone responsibly. There will be mistrust between children and their parents. As a result, their mental health will not be promoted, but instead worsened. Then, children cannot have a correct mindset on trust in others, so they will face difficulty when socializing. They will find it hard to make friends , and eventually, they will suffer from depression and they may even commit suicide, which is a situation that we should do our best to avoid.

Secondly, this kind of action will hinder the development of the discipline of children . Children are not adults, who will discipline themselves. Children do not know how to self discipline. If parents install apps to monitor activities, children will use mobile phones responsibly only due to the app and the punishment from parents. This will cause a deterring effect, but children still do not get to learn how to self-discipline. On the other hand, if children can use their phone freely, they may overuse the phone at the beginning. However, children can develop self-discipline skills and restrict tier screen time if they are under the correct guidance of parents. They can learn to allocate their time effectively, and they will have an edge over those who use phones responsibly due to punishment in terms of time allocation.

Thirdly, this kind of act violates the law of privacy. How children use their phone is a matter of personal information, which should not be known by anyone. In fact, no one would like to have their personal information exposed to everyone, including searching pre-recorded and on-screen activities. Moreover, children also have secrets, which they want nobody to know. If parents install apps to violate their privacy, this will increase conflict between parents and children, as children want their secret to be kept a secret, but parents simply put their secret under the spotlight. Parents should put themselves in their children’s shoes and understand their feelings before installing.

We are in an era of technology, and parents can have more ways to monitor their children’s actions. However, due to the above reasons, I disagree with the parents monitoring their children's actions by installing apps on their phone. It is hoped that parents can stop extending their overprotective hand ,to restrict the development of their children.

Yours faithfully

/* I am not going to reveal my name*/

//yeah i am not good at english

Полный текст и комментарии »

  • Проголосовать: нравится
  • -62
  • Проголосовать: не нравится

Автор Singinginginging, история, 3 года назад, По-английски

Hi I always have some problem on coding binary search

        int l=1,r=n-1;
        while(l<r){
            int mid=(l+r)/2;
            if(/*condition*/)l=mid;
            else r=mid-1;
        }

This may seems ok, but this is wrong, as there may be a infinite loop.

        int l=1,r=n-1;
        while(l<r){
            int mid=(l+r+1)/2;
            if(/*condition*/)l=mid;
            else r=mid-1;
        }

with the mid rounded up, this will not give a infinite loop.

Is there a way to memorize this? I don't want to debug my binary search in contests, which takes a long time.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -47
  • Проголосовать: не нравится