Menu
Positive Newspaper
  • Home
  • About Us
  • Culture
  • Health
  • Life
  • Science
  • Technology
  • Contact Us
Positive Newspaper
Shortcuts in Competitive Programming

Shortcuts in Competitive Programming

Posted on April 6, 2017

Competitive programming is nothing but coding in different languages such as C/C++ and sometimes in Java. While coding in C/C++, macros and typdefs are the only shortcuts that geeks are familiar with. Here are some shortcuts that you can use while coding.

Shortcuts in Competitive Programming

Shortcuts for “common” data types in contests

typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

Infinite

const int INF = 0x3f3f3f3f;

To clear array of integers

memset(arr, 0, sizeof arr);

initialize DP memoization table with -1

memset(memo, -1, sizeof memo);

to simplify: if (a) ans = b; else ans = c;

ans = a ? b : c;

Use a vector to create a dynamic array and to initialize it in one statement. Creates a vector of size N and values as 0.

vector<int> v(N, 0); // OR vi v(N, 0);

to simplify: ans = ans + val; and its variants

ans += val;

index–; if (index < 0) index = n – 1;

index = (index + n – 1) % n;

Read Also:  Tricks about Web Browser

index++; if (index >= n) index = 0;

index = (index + 1) % n;

 

min/max shortcut to update max/min

ans = min(ans, new_computation);

for rounding to nearest integer

int ans = (int)((double)d + 0.5);

To return true if a value matches a given

number, else return false

if (val == given_no) return true;

else return false;

return (val == given_no)

Well, these were some of the basic shortcuts that can be used while coding. The main motive of coding is to make things easy. One rule that codes follow is to reduce the length of the code. The quality of your code is determined the length of the code. An ideal coder gives out the same result in less lines of code .

So to all the coders out there, enjoy the tips!!!

Archives

  • July 2022 (1)
  • June 2022 (3)
  • March 2022 (3)
  • February 2022 (13)
  • January 2022 (10)
  • October 2019 (2)
  • November 2017 (14)
  • October 2017 (12)
  • September 2017 (33)
  • August 2017 (19)
  • July 2017 (4)
  • June 2017 (8)
  • May 2017 (11)
  • April 2017 (12)
  • March 2017 (36)

Follow Us

follow us

Categories

  • Auto (2)
  • Business (10)
  • Business Services & Administration (253)
  • Culture (3)
  • Global (1)
  • Global News (21)
  • Health (17)
  • Life (6)
  • Machinery and Tools (1)
  • Market News (1)
  • Materials & Chemicals (470)
  • Medical (1)
  • Research Paper (1)
  • Sci-Tech (1)
  • Science (8)
  • Tchnology (4)
  • Technology (128)
©2023 Positive Newspaper | Powered by SuperbThemes & WordPress