簡體   English   中英

C++ 超過時間限制

[英]C++ Time Limit Exceeded

我已經有幾個月沒有在 C++ 中編碼了,所以我想在 C 周圍試用 Google Kick Start。

雖然我找不到解決“穩定牆”問題的方法(請參閱此處了解我在說什么),但我在不到 30 分鍾的時間內解決了其他問題,並且它們都使用 google 示例輸入。

這是代碼(我知道它並沒有優化多少,但它可以完成工作):

//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;
    }
}

Problem
Avery has an array of N positive integers. The i-th integer of the array is Ai.

A contiguous subarray is an m-countdown if it is of length m and contains the integers m, m-1, m-2, ..., 2, 1 in that order. For example, [3, 2, 1] is a 3-countdown.

Can you help Avery count the number of K-countdowns in her array?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ K ≤ N.
1 ≤ Ai ≤ 2 × 105, for all i.

Test set 1
2 ≤ N ≤ 1000.

Test set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 1000.

Sample

Input

Output

3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100


Case #1: 2
Case #2: 0
Case #3: 1


In sample case #1, there are two 3-countdowns as highlighted below.
1 2 3 7 9 3 2 1 8 3 2 1
1 2 3 7 9 3 2 1 8 3 2 1

In sample case #2, there are no 2-countdowns.

In sample case #3, there is one 6-countdown as highlighted below.
100 7 6 5 4 3 2 1 100

//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;
    }
}

Problem
Cristobal has an array of N (possibly negative) integers. The i-th integer in his array is Ai. A contiguous non-empty subarray of Cristobal's array is perfect if its total sum is a perfect square. A perfect square is a number that is the product of a non-negative integer with itself. For example, the first five perfect squares are 0, 1, 4, 9 and 16.

How many subarrays are perfect? Two subarrays are different if they start or end at different indices in the array, even if the subarrays contain the same values in the same order.

Input
The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case contains the integer N. The second line contains N integers describing Cristobal's array. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of perfect subarrays.

Limits
Memory limit: 1GB.
1 ≤ T ≤ 100.
-100 ≤ Ai ≤ 100, for all i.

Test set 1
Time limit: 20 seconds.
1 ≤ N ≤ 1000.

Test set 2
Time limit: 30 seconds.
For up to 5 cases, 1 ≤ N ≤ 105.
For the remaining cases, 1 ≤ N ≤ 1000.

Sample

Input

Output

3
3
2 2 6
5
30 30 9 1 30
4
4 0 0 16


Case #1: 1
Case #2: 3
Case #3: 9


In sample case #1, there is one perfect subarray: [2 2] whose sum is 22.

In sample case #2, there are three perfect subarrays:
[9], whose total sum is 32.
[1], whose total sum is 12.
[30 30 9 1 30], whose total sum is 102.

In sample case #3, there are nine perfect subarrays:
[4], whose total sum is 22.
[4 0], whose total sum is 22.
[4 0 0], whose total sum is 22.
[0], whose total sum is 02.
[0 0], whose total sum is 02.
[0 0 16], whose total sum is 42.
[0], whose total sum is 02.
[0 16], whose total sum is 42.
[16], whose total sum is 42.

Note: We do not recommend using interpreted/slower languages for the test set 2 of this problem.

//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;
    }
}

Problem
Carl has an array of N candies. The i-th element of the array (indexed starting from 1) is Ai representing sweetness value of the i-th candy. He would like to perform a series of Q operations. There are two types of operation:
Update the sweetness value of a candy in the array.
Query the sweetness score of a subarray.

The sweetness score of a subarray from index l to r is: Al × 1 - Al+1 × 2 + Al+2 × 3 - Al+3 × 4 + Al+4 × 5 ...

More formally, the sweetness score is the sum of (-1)i-lAi × (i - l + 1), for all i from l to r inclusive.

For example, the sweetness score of:
[3, 1, 6] is 3 × 1 - 1 × 2 + 6 × 3 = 19
[40, 30, 20, 10] is 40 × 1 - 30 × 2 + 20 × 3 - 10 × 4 = 0
[2, 100] is 2 × 1 - 100 × 2 = -198

Carl is interested in finding out the total sum of sweetness scores of all queries. If there is no query operation, the sum is considered to be 0. Can you help Carl find the sum?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing N and Q. The second line contains N integers describing the array. The i-th integer is Ai. The j-th of the following Q lines describe the j-th operation. Each line begins with a single character describing the type of operation (U for update, Q for query).
For an update operation, two integers Xj and Vj follow, indicating that the Xj-th element of the array is changed to Vj.
For a query operation, two integers Lj and Rj follow, querying the sweetness score of the subarray from the Lj-th element to the Rj-th element (inclusive).

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the total sum of sweetness scores of all the queries.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ Ai ≤ 100, for all i.
1 ≤ N ≤ 2 × 105 and 1 ≤ Q ≤ 105 for at most 6 test cases.
For the remaining cases, 1 ≤ N ≤ 300 and 1 ≤ Q ≤ 300.
If the j-th operation is an update operation, 1 ≤ Xj ≤ N and 1 ≤ Vj ≤ 100.
If the j-th operation is a query operation, 1 ≤ Lj ≤ Rj ≤ N.

Test set 1
There will be at most 5 update operations.

Test set 2
There are no special constraints.

Sample

Input

Output

2
5 4
1 3 9 8 2
Q 2 4
Q 5 5
U 2 10
Q 1 2
3 3
4 5 5
U 1 2
U 1 7
Q 1 2


Case #1: -8
Case #2: -3


In sample case #1:
The first query asks for the sweetness score of [3, 9, 8] which is 3 × 1 - 9 × 2 + 8 × 3 = 9.
The second query asks for the sweetness score of [2] which is 2 × 1 = 2.
The third query asks for the sweetness score of [1, 10] which is 1 × 1 - 10 × 2 = -19.
Thus, the final output should be 9 + 2 - 19 = -8.

In sample case #2:
The first and only query asks for the sweetness score of [7, 5] which is 7 × 1 - 5 × 2 = -3.
Thus, the final output should be -3.

問題是我在第一個測試集中對它們中的每一個都有一個TLE (超出時間限制)錯誤,除了“完美子陣列”,我在第二個測試集中得到了TLE

即使每次發生任何變化時都用long long替換int ,所以問題不是 integer 溢出。


關於可能出了什么問題的任何想法?

(比賽結束了,我只是好奇)

我已經有幾個月沒有在 C++ 中編碼了,所以我想在 C 周圍試用 Google Kick Start。

雖然我找不到解決“穩定牆”問題的方法(請參閱此處了解我在說什么),但我在不到 30 分鍾的時間內解決了其他問題,並且它們都使用 google 示例輸入。

這是代碼(我知道它並沒有優化多少,但它可以完成工作):

//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;
    }
}

Problem
Avery has an array of N positive integers. The i-th integer of the array is Ai.

A contiguous subarray is an m-countdown if it is of length m and contains the integers m, m-1, m-2, ..., 2, 1 in that order. For example, [3, 2, 1] is a 3-countdown.

Can you help Avery count the number of K-countdowns in her array?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ K ≤ N.
1 ≤ Ai ≤ 2 × 105, for all i.

Test set 1
2 ≤ N ≤ 1000.

Test set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 1000.

Sample

Input

Output

3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100


Case #1: 2
Case #2: 0
Case #3: 1


In sample case #1, there are two 3-countdowns as highlighted below.
1 2 3 7 9 3 2 1 8 3 2 1
1 2 3 7 9 3 2 1 8 3 2 1

In sample case #2, there are no 2-countdowns.

In sample case #3, there is one 6-countdown as highlighted below.
100 7 6 5 4 3 2 1 100

//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;
    }
}

Problem
Cristobal has an array of N (possibly negative) integers. The i-th integer in his array is Ai. A contiguous non-empty subarray of Cristobal's array is perfect if its total sum is a perfect square. A perfect square is a number that is the product of a non-negative integer with itself. For example, the first five perfect squares are 0, 1, 4, 9 and 16.

How many subarrays are perfect? Two subarrays are different if they start or end at different indices in the array, even if the subarrays contain the same values in the same order.

Input
The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case contains the integer N. The second line contains N integers describing Cristobal's array. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of perfect subarrays.

Limits
Memory limit: 1GB.
1 ≤ T ≤ 100.
-100 ≤ Ai ≤ 100, for all i.

Test set 1
Time limit: 20 seconds.
1 ≤ N ≤ 1000.

Test set 2
Time limit: 30 seconds.
For up to 5 cases, 1 ≤ N ≤ 105.
For the remaining cases, 1 ≤ N ≤ 1000.

Sample

Input

Output

3
3
2 2 6
5
30 30 9 1 30
4
4 0 0 16


Case #1: 1
Case #2: 3
Case #3: 9


In sample case #1, there is one perfect subarray: [2 2] whose sum is 22.

In sample case #2, there are three perfect subarrays:
[9], whose total sum is 32.
[1], whose total sum is 12.
[30 30 9 1 30], whose total sum is 102.

In sample case #3, there are nine perfect subarrays:
[4], whose total sum is 22.
[4 0], whose total sum is 22.
[4 0 0], whose total sum is 22.
[0], whose total sum is 02.
[0 0], whose total sum is 02.
[0 0 16], whose total sum is 42.
[0], whose total sum is 02.
[0 16], whose total sum is 42.
[16], whose total sum is 42.

Note: We do not recommend using interpreted/slower languages for the test set 2 of this problem.

//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;
    }
}

Problem
Carl has an array of N candies. The i-th element of the array (indexed starting from 1) is Ai representing sweetness value of the i-th candy. He would like to perform a series of Q operations. There are two types of operation:
Update the sweetness value of a candy in the array.
Query the sweetness score of a subarray.

The sweetness score of a subarray from index l to r is: Al × 1 - Al+1 × 2 + Al+2 × 3 - Al+3 × 4 + Al+4 × 5 ...

More formally, the sweetness score is the sum of (-1)i-lAi × (i - l + 1), for all i from l to r inclusive.

For example, the sweetness score of:
[3, 1, 6] is 3 × 1 - 1 × 2 + 6 × 3 = 19
[40, 30, 20, 10] is 40 × 1 - 30 × 2 + 20 × 3 - 10 × 4 = 0
[2, 100] is 2 × 1 - 100 × 2 = -198

Carl is interested in finding out the total sum of sweetness scores of all queries. If there is no query operation, the sum is considered to be 0. Can you help Carl find the sum?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing N and Q. The second line contains N integers describing the array. The i-th integer is Ai. The j-th of the following Q lines describe the j-th operation. Each line begins with a single character describing the type of operation (U for update, Q for query).
For an update operation, two integers Xj and Vj follow, indicating that the Xj-th element of the array is changed to Vj.
For a query operation, two integers Lj and Rj follow, querying the sweetness score of the subarray from the Lj-th element to the Rj-th element (inclusive).

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the total sum of sweetness scores of all the queries.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ Ai ≤ 100, for all i.
1 ≤ N ≤ 2 × 105 and 1 ≤ Q ≤ 105 for at most 6 test cases.
For the remaining cases, 1 ≤ N ≤ 300 and 1 ≤ Q ≤ 300.
If the j-th operation is an update operation, 1 ≤ Xj ≤ N and 1 ≤ Vj ≤ 100.
If the j-th operation is a query operation, 1 ≤ Lj ≤ Rj ≤ N.

Test set 1
There will be at most 5 update operations.

Test set 2
There are no special constraints.

Sample

Input

Output

2
5 4
1 3 9 8 2
Q 2 4
Q 5 5
U 2 10
Q 1 2
3 3
4 5 5
U 1 2
U 1 7
Q 1 2


Case #1: -8
Case #2: -3


In sample case #1:
The first query asks for the sweetness score of [3, 9, 8] which is 3 × 1 - 9 × 2 + 8 × 3 = 9.
The second query asks for the sweetness score of [2] which is 2 × 1 = 2.
The third query asks for the sweetness score of [1, 10] which is 1 × 1 - 10 × 2 = -19.
Thus, the final output should be 9 + 2 - 19 = -8.

In sample case #2:
The first and only query asks for the sweetness score of [7, 5] which is 7 × 1 - 5 × 2 = -3.
Thus, the final output should be -3.

問題是我在第一個測試集中對它們中的每一個都有一個TLE (超出時間限制)錯誤,除了“完美子陣列”,我在第二個測試集中得到了TLE

即使每次發生任何變化時都用long long替換int ,所以問題不是 integer 溢出。


關於可能出了什么問題的任何想法?

(比賽結束了,我只是好奇)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM