📜  竞争性编程中最严重的错误和技巧

📅  最后修改于: 2022-05-13 01:55:31.687000             🧑  作者: Mango

竞争性编程中最严重的错误和技巧

初学者听到这个词的那一刻,脑海中就会浮现出一个画面,学生们坐在一个满是电脑的房间里,正在编写一些来自世界各地的东西,老实说,这并不难掌握,所以我们会提出帮助程序员更快升级的技巧。因此,让我们从每个主题中最被低估的点开始,即使是高级竞争程序员也会滑倒。

竞争性编程中最关键的错误提示

所以溢出的概念可以在竞争性编码中看到很多,最糟糕的问题是不理解它,编写一个完全完美的代码但仍然得到错误的答案,这是因为在更高的输入范围内,编译器会剥离结果根据数据大小,因此记住数据类型的基本范围很重要。

Int => [-109  To 109]
Long Int / long => [-1012,1012]
Long Long Int / long long => [-1018,10^18]

例子:

C++
// C++ Program, to Illustrate General Tips In Competitive
// Programming
 
// Importing all C++ libraries
#include 
 
using namespace std;
 
// Main method
int main()
{
 
    // Case: Stack Overflow
    int a = 100000, b = 100000;
    // Expected answer ? 10^10 but
    // no the answer you get is 1410065408, error in
    // precision.
    int c = a * b;
    long long int d = a * b;
 
    // Note: Still error will be generated bacause
    // calculation was done on two ints which were later
    // converted into long long ie they already
    // lost their data before converting
 
    // Print and display on console
    cout << c << " " << d << endl;
 
    // Now if we store a value more than its capacity then
    // what happens is the number line of range of value
    // bacomes a number
    // Example: Circle, If min val is 1 and max is 9, so if
    // we add 1 to 9 it will result in 1, it looped back to
    // starting, this is overflow
 
    int p = INT_MAX;
 
    // An example of overflow
    cout << "An example of overflow " << p + 1 << endl;
 
    // Long long is way better than double,
    // double although can store more than long long but
    // in exchange it will cost you your precision.
    // We can simply use the below command as follows:
    // cout<> t;
    while (t--) {
        cin >> n;
 
        // Here, before i used int and got wrong answer,
        // then made it long long
        long long pdt = 1, temp;
 
        for (int i = 0; i < n; i++) {
            cin >> temp;
            pdt *= temp;
        }
 
        if (pdt % 10 == 2 || pdt % 10 == 3 || pdt % 10 == 5)
            cout << "YES" << endl;
 
        else
            cout << "NO" << endl;
    }
}


C++
// C++ Program to Illustrate STL Algorithms Tips
 
// Importing all C++ libraries
// Standard command
#include 
 
using namespace std;
 
// Main method
// From here the code starts executing
int main()
{
 
    // Creating vector and initializing objects
    // by passing custom integer numbers
    vector v
        = { 1, 4, 2, 5, 6, 3, 9, 0, 10, 3, 15, 17, 3 };
 
    // Operation 1
    // Finding the minimum element in the array,
 
    // Note: It returns the pointer/iterator
    auto min = min_element(v.begin(), v.end());
 
    // Print and display elements on console
    cout << "Min element: " << *min << endl;
 
    // Operation 2
    // // Finding the maximum  element in the array
    auto max = max_element(v.begin(), v.end());
    cout << "Max Element: " << *max << endl;
 
    // Operation 3
    // Sum of all elements, the third parameter tells us
    // initial sum ki value kya hai.
    auto sum = accumulate(v.begin(), v.end(), 0);
    cout << "The sum of all elements: " << sum << endl;
 
    // Operation 4
    // Count the frequency of an element in the array/vector
    auto cnt = count(v.begin(), v.end(), 3);
    cout << "Frequency Of 3 is: " << cnt << endl;
 
    // Operation 5
    // Finds an element and returns its pointer
    auto elem = find(v.begin(), v.end(), 3);
 
    if (elem != v.end())
        cout << "the element is at posn: " << *elem << endl;
    else
        cout << "Element not found" << endl;
 
    // Operation 6
    // Reversing a string or array/vector
    // using reverse method
    reverse(v.begin(), v.end());
 
    cout << "Reversed Vector: ";
 
    for (auto val : v) {
        cout << val << " ";
    }
 
    // New line for better readability
    cout << endl;
 
    // Operation 7
    // Sort array/vector using sort() method
    sort(v.begin(), v.end());
    cout << "Sorted Vector: ";
    for (auto val : v) {
        cout << val << " ";
    }
}


输出
1410065408 1410065408
An example of overflow -2147483648

数据结构提示

提示 1:在竞争性编程中,当您需要一个函数和 main函数时,始终全局声明数组,全局声明的数组的大小可以为 10^7,因为它们存储在Data Segments中。

提示 2:每当您声明一个需要返回多个项目的函数时,或者甚至在一般情况下您的目标是在经过一些修改后返回一个数组时,请始终使用Pass By Reference(例如 void swap(int &a, int &b ) ,它以两种方式帮助您,一种是通过减少制作数据副本然后对其进行处理的时间,其次,当您直接处理主要数据时,您可以根据需要修改任意数量的值,而无需担心返回值并处理它们。

  • 在 main 方法中声明的数组的大小限制为 10^5,因为它存储在Stack Memory中,内存限制约为 8MB,如果大小超过此值,则会导致Stack Overflow
  • 当您在 cin 输入之后将多个字符串作为输入时,有时会发生一个非常特殊的错误。它不采用所需的输入,而是自动将空格作为输入,然后采用剩余的两个字符串来解决此问题,然后再使用getline()字符串作为输入。
  • 在从另一个字符串形成一个新字符串时,而不是使用像str = str + str2 [I]这样的语法,即添加字符来获得一个字符串,这个方法的时间复杂度是O(size(字符串)) 因此列出了将字符添加到字符串中,我们使用时间复杂度为O(n)push_back(字符 )。

STL 技巧

到目前为止,我们已经知道 STL 代表 C++ 中的标准模板库 这对有竞争力的程序员来说是一个福音,它包含内置的特殊数据结构和算法,可以像 Crazy 一样减少和优化你的代码。请记住以下列出的 STL 数据结构提示,如下所示:

提示 1:每当我们需要对问题中的数据进行排序并删除重复项时,最佳做法是将其存储在一个Set 中,它会自动对数据进行排序,并且根据Set的属性,它只存储唯一值。

提示 2:在我们需要找到所有值的频率的问题中,最好的做法是使用Map,Map的属性使其自动按字典顺序排列数据,只需一个小技巧,你就会有最有效的解决方案。

例子

C++

// C++ Program to Illustrate STL Algorithms Tips
 
// Importing all C++ libraries
// Standard command
#include 
 
using namespace std;
 
// Main method
// From here the code starts executing
int main()
{
 
    // Creating vector and initializing objects
    // by passing custom integer numbers
    vector v
        = { 1, 4, 2, 5, 6, 3, 9, 0, 10, 3, 15, 17, 3 };
 
    // Operation 1
    // Finding the minimum element in the array,
 
    // Note: It returns the pointer/iterator
    auto min = min_element(v.begin(), v.end());
 
    // Print and display elements on console
    cout << "Min element: " << *min << endl;
 
    // Operation 2
    // // Finding the maximum  element in the array
    auto max = max_element(v.begin(), v.end());
    cout << "Max Element: " << *max << endl;
 
    // Operation 3
    // Sum of all elements, the third parameter tells us
    // initial sum ki value kya hai.
    auto sum = accumulate(v.begin(), v.end(), 0);
    cout << "The sum of all elements: " << sum << endl;
 
    // Operation 4
    // Count the frequency of an element in the array/vector
    auto cnt = count(v.begin(), v.end(), 3);
    cout << "Frequency Of 3 is: " << cnt << endl;
 
    // Operation 5
    // Finds an element and returns its pointer
    auto elem = find(v.begin(), v.end(), 3);
 
    if (elem != v.end())
        cout << "the element is at posn: " << *elem << endl;
    else
        cout << "Element not found" << endl;
 
    // Operation 6
    // Reversing a string or array/vector
    // using reverse method
    reverse(v.begin(), v.end());
 
    cout << "Reversed Vector: ";
 
    for (auto val : v) {
        cout << val << " ";
    }
 
    // New line for better readability
    cout << endl;
 
    // Operation 7
    // Sort array/vector using sort() method
    sort(v.begin(), v.end());
    cout << "Sorted Vector: ";
    for (auto val : v) {
        cout << val << " ";
    }
}
输出
Min element: 0
Max Element: 17
The sum of all elements: 78
Frequency Of 3 is: 3
the element is at posn: 3
Reversed Vector: 3 17 15 3 10 0 9 3 6 5 2 4 1 
Sorted Vector: 0 1 2 3 3 3 4 5 6 9 10 15 17