**已关闭。**此问题需要debugging details。目前不接受回答。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
5天前关闭。
Improve this question的
为什么这段代码有运行时错误(OutOfBounds);)
这是USACO 2023年12月竞赛,铜牌1题
这里是问题链接https://www.usaco.org/index.php?page=viewproblem2&cpid=1347#
#include <iostream>
using namespace std;
long long n_h[20009]; // cow height
long long m_h[20009]; // cane height
long long space_h[20009]; // 사탕수수 아래 빈 공간 크기
long long caneat;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m; // cow num // cane num
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> n_h[i];
}
for(int i = 0; i < m; i++){
cin >> m_h[i];
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(m_h[i] <= 0){
break;
}
if(m_h[i] + space_h[i] <= n_h[j]){ // 키가 작으면 먹지 못함
n_h[j] = n_h[j] + m_h[i]; // 소의 키 키우기
m_h[i] = -1;
break;
}
else{
if(space_h[i] < n_h[j]){
caneat = 0;
caneat = n_h[j] - space_h[i];
n_h[j] = n_h[j] + caneat;
space_h[i] = space_h[i] + caneat;
m_h[i] = m_h[i] - caneat;
}
}
}
}
for(int i = 0; i < n; i++){
cout << n_h[i] << "\n";
}
return 0;
}
字符串
1条答案
按热度按时间ztyzrc3y1#
他不是一个程序员,也不需要重复使用代码。我同意竞争性编程不是学习编程的好方法。但通常竞争性编程的目的是爱好,学习算法,获得奖项,而不是学习工程。
你的数组的大小是20,009,比200,000小,n,m的限制。只需要再加一个零。
字符串
也许你必须在在线评委网站的问题板上问这个问题。无论如何,这里不是问算法问题的最佳地方。