【问题标题】:Google Kickstart 2020 Problem Record Breaker Wrong AnswerGoogle Kickstart 2020 问题记录破坏者错误答案
【发布时间】:2020-11-21 05:29:50
【问题描述】:

我正在练习上一轮 Google Kick Start 2020 中提出的这个问题。这个问题被称为 Record Breaker,如下:

Isyana 得到了她在 N 上当地主题公园的游客人数 连续多日。第 i 天的访客数为 Vi。一天 如果满足以下两个条件,则创纪录: 当天参观人数严格大于人数 前几天的访客。要么是最后一天, 或当天的访客人数严格大于 次日来访人数。请注意,第一天 可能是破纪录的一天!

请帮助 Isyana 找出打破记录的天数。

Input 输入的第一行给出了测试用例的数量,T.T 测试用例如下。每个测试用例都以包含 整数 N。第二行包含 N 个整数。第 i 个整数是 六.

输出 对于每个测试用例,输出一行包含 Case #x: y, 其中 x 是测试用例编号(从 1 开始),y 是编号 破纪录的天数。

限制时间限制:每个测试集 20 秒。内存限制:1GB。 1≤T≤ 100. 0 ≤ Vi ≤ 2 × 105.

测试集 1 1 ≤ N ≤ 1000。

测试集 2 1 ≤ N ≤ 2 × 105,最多 10 个测试用例。为了 剩余案例,1 ≤ N ≤ 1000。

样本

输入 4 8 1 2 0 7 2 0 2 0 6 4 8 15 16 23 42 9 3 1 4 1 5 9 2 6 5 6 9 9 9 9 9 9

输出案例#1:2 案例#2:1 案例#3:3 案例#4:0

在示例案例 #1 中,以下粗体和带下划线的数字 代表破纪录的天数:1 2 0 7 2 0 2 0。

在示例案例 #2 中,只有最后一天是破纪录的一天。

在示例案例 #3 中,记录了第一天、第三天和第六天 休息日。

在示例案例 #4 中,没有破纪录的日子。

这是我创建的解决方案。它在第一个测试用例中给出了错误的答案,但我想不出我错过的任何具体案例。

#include<iostream>
#include<algorithm>

using namespace std;

int record_breaking(long long int arr[], long long int n)
{
    long long int ans = 0;
    long long int m = arr[0];
    for(long long int i = 0; i<n; i++)
    {
        //For first element
        if(i == 0 && arr[0] > arr[1])
        {
               ans++;
        }
        
        //For the last element
        else if(i == n - 1 && arr[i] > m)
        {
                ans++;
                //cout<<arr[i]<<" is a answer "<<endl;
               
        }
        //Normal Case
        else if(arr[i] > m && arr[i] > arr[i + 1])
        {
                ans++;
                //cout<<arr[i]<<" is a answer "<<endl;
        }
        m = max(arr[i], m);
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    for(int test = 1; test <= t; test++)
    {
        long long int n;
        cin>>n;
        long long int arr[200000];
        //int *arr = new int [n];
        for(long long int i = 0; i<n; i++)
        {
            cin>>arr[i];
        }
        cout<<endl<<"Case #"<<test<<": "<<record_breaking(arr, n);
        //delete [] arr;
    }
    
    return 0;
}

请帮我找出解决方案的问题!

【问题讨论】:

  • long long int arr[200000]; 不会在任何地方都有效。如果您不知道大小,请使用std::vector
  • @Someprogrammerdude 我试过了,但它仍然给了我一个错误的答案。这是修改后的代码link

标签: c++ arrays algorithm optimization


【解决方案1】:

这会过去的...

边缘案例:

输入:

1

1

2

输出:

1

但您的代码将 0 作为输出...

怎么了:

您将第一个 if 语句添加为 (i == 0 && arr[0]>arr[1]) 但不超过一个元素...所以首先您必须检查此条件 (i = = n-1 && arr[i] > m)... 这意味着您必须做出正确的顺序...

此外,初始化 m = -1 对于上述边缘情况至关重要...

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int record_breaking(vector<int> arr, int n)
{
    int ans = 0;
    int m = -1;
    for(int i = 0; i<n; i++)
    {
        
        //For the last element
        if(i == n - 1 && arr[i] > m)
        {
                ans++;
                //cout<<arr[i]<<" is a answer "<<endl;
               
        }

        //For first element
        else if(i == 0 && arr[0] > arr[1])
        {
               ans++;
        }

        //Normal Case
        else if(arr[i] > m && arr[i] > arr[i + 1])
        {
                ans++;
                //cout<<arr[i]<<" is a answer "<<endl;
        }
        m = max(arr[i], m);
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    for(int test = 1; test <= t; test++)
    {
        int n, temp;
        cin>>n;
        vector<int> arr;
        //long long int arr[200000];
        //int *arr = new int [n];
        for(int i = 0; i<n; i++)
        {
            cin>>temp;
            arr.push_back(temp);
        }
        cout<<endl<<"Case #"<<test<<": "<<record_breaking(arr, n);
        //delete [] arr;
    }
    
    return 0;
}

【讨论】:

  • 是的,它确实有效。但是将 m 初始化为 arr[0] 有什么问题?
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多