超过C ++时限

我已经几个月没有使用C ++进行编码了,所以我想尝试C的Google Kick Start。

While I couldn't find a way to solve the "Stable Wall" problem (see here to know what I'm talking about), I solved the other ones in less than 30 minutes and they all worked with google sample inputs.

这是代码(我知道它并没有进行太多优化,但是可以完成工作):

//Countdown:
#include <iostream>
#include <string>

using namespace std;

int getter(string temp, int much) {
    string result = "";
    int count = 1;

    for (auto it : temp) {
        if (count == much)
            result += it;

        if (it == ' ') {
            if (count++ == much)
                break;
        }
    }

    return stoi(result);
}

int main() {
    string get, temp;
    getline(cin, get);

    int cases = stoi(get);

    for (int n = 1; n <= cases; n++)
    {
        getline(cin, get);
        int num =  getter(get, 1);
        int k = getter(get, 2);

        getline(cin, get);
        int equal = k;
        int count = 0;

        for (int m = 1; m <= num; m++)
        {
            int sub = getter(get, m);
            if (sub == equal)
                equal--;
            else
                equal = k;

            if (equal == 0)
            {
                count++;
                equal = k;
            }
        }

        cout << "Case #" << n << ": " << count << endl;
    }
//Perfect subarray:
#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int getter(string temp, int much) {
    string result = "";
    int count = 1;

    for (auto it : temp) {
        if (count == much)
            result += it;

        if (it == ' ') {
            if (count++ == much)
                break;
        }
    }

    return stoi(result);
}

int main() {
    string get, temp;
    getline(cin, get);

    int cases = stoi(get);

    for (int n = 1; n <= cases; n++)
    {
        getline(cin, get);
        int num = getter(get, 1);

        getline(cin, get);
        vector<int> all;

        for (int m = 1; m <= num; m++)
            all.push_back(getter(get, m));

        int count = 0;

        for (int m = 0; m < all.size(); m++)
        {
            int sum = 0;

            for (int o = m; o < all.size(); o++)
            {
                sum += all[o];

                double sqr = sqrt(sum);

                if (sum >= 0 && sqr == (int)sqr)
                    count++;
            } 
        }

        cout << "Case #" << n << ": " << count << endl;
    }
}
//Candies:
#include <iostream>
#include <string>

using namespace std;

int getter(string temp, int much) {
    string result = "";
    int count = 1;

    for (auto it : temp) {
        if (count == much)
            result += it;

        if (it == ' ') {
            if (count++ == much)
                break;
        }
    }

    return stoi(result);
}

int main() {
    string get, temp;
    getline(cin, get);

    int cases = stoi(get);

    for (int n = 1; n <= cases; n++)
    {
        getline(cin, get);
        int num =  getter(get, 1);
        int k = getter(get, 2);

        getline(cin, get);
        int equal = k;
        int count = 0;

        for (int m = 1; m <= num; m++)
        {
            int sub = getter(get, m);
            if (sub == equal)
                equal--;
            else
                equal = k;

            if (equal == 0)
            {
                count++;
                equal = k;
            }
        }

        cout << "Case #" << n << ": " << count << endl;
    }
}

问题是,除了“ Perfect Subarray”(在第二个测试集上有TLE)外,第一个测试集上的每个集都有TLE(超过时间限制)错误。

Even replacing int with long long every time anything changed, so the problem wasn't an integer overflow.

关于可能出错的任何想法?

(比赛结束了,我很好奇)

评论