- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在练习上一轮Google Kick Start 2020提出的问题。这个问题称为Record Breaker,如下所示:
Isyana is given the number of visitors at her local theme park on Nconsecutive days. The number of visitors on the i-th day is Vi. A dayis record breaking if it satisfies both of the following conditions:The number of visitors on the day is strictly larger than the numberof visitors on each of the previous days. Either it is the last day,or the number of visitors on the day is strictly larger than thenumber of visitors on the following day. Note that the very first daycould be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input The first line of the input gives the number of test cases, T. Ttest cases follow. Each test case begins with a line containing theinteger N. The second line contains N integers. The i-th integer isVi.
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 numberof record breaking days.
Limits Time limit: 20 seconds per test set. Memory limit: 1GB. 1 ≤ T ≤100. 0 ≤ Vi ≤ 2 × 105.
Test set 1 1 ≤ N ≤ 1000.
Test set 2 1 ≤ N ≤ 2 × 105 for at most 10 test cases. For theremaining cases, 1 ≤ N ≤ 1000.
Sample
Input 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 99 9 9 9
Output Case #1: 2 Case #2: 1 Case #3: 3 Case #4: 0
In Sample Case #1, the bold and underlined numbers in the followingrepresent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are recordbreaking days.
In Sample Case #4, there is no record breaking day.
#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;
}
请帮助我找出解决方案的问题!
最佳答案
这会过去...
边缘保护套:
输入:
1个
1个
2
输出:
1个
但是您的代码给出了0作为输出...
怎么了:
您首先要添加if语句为(i == 0 && arr [0]> arr [1]),但是元素个数不超过...因此,这里首先您必须检查这种情况(i == n- 1 && arr [i]> m)...这意味着您必须进行正确的订购...
另外,对于上述情况,将m = -1 Crucial初始化。
#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;
}
关于c++ - Google Kickstart 2020问题记录破坏者错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63190624/
任何人都可以在日志记录方面比较 kafka、编年史队列和破坏者之间的底层设计和性能?似乎 kafka 拥有大多数用户,但不要避免 GC。 最佳答案 我认为您可能对 Kafka 在日志管道中的使用方式感
这个问题已经有答案了: How to get the CPU cycle count in x86_64 from C++? (5 个回答) 已关闭 4 年前。 所以我正在尝试在 Windows 的
我是一名优秀的程序员,十分优秀!