Doubledimensions
Coder
I am training for USACO bronze, and I encountered this problem in a contest:
USACO
I had no idea how to solve it, so I found a youtube tutorial explaining the dynamic programming that is used to solve the problem. Here is the python code they gave:
This code gets 100% on the USACO analysis.
Here is my C++ code:
These look like these do the same thing, but one gets full credit, while the other gets around half of them wrong. Can someone please tell me the bug in my C++?
USACO
I had no idea how to solve it, so I found a youtube tutorial explaining the dynamic programming that is used to solve the problem. Here is the python code they gave:
Python:
N = int(input())
Grass = list(map(int, input().split()))
Adjustment = 0
Increase = 0
Decrease = 0
for i in range(N):
Grass[i] += Adjustment
if Grass[i] < 0:
Increase -= Grass[i]
else:
Decrease += Grass[i]
Adjustment = Adjustment + Increase - Decrease - Grass[i]
print(Decrease + Increase)
This code gets 100% on the USACO analysis.
Here is my C++ code:
C++:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
int main(){
int N;
cin >> N;
int X;
vector<int> Grass;
for (int i = 0; i < N; i++)
{
cin >> X;
Grass.push_back(X);
}
int Adjustment = 0;
int Increase = 0;
int Decrease = 0;
for (int i = 0; i < N; i++){
Grass[i] += Adjustment;
if (Grass[i] < 0) {
Increase -= Grass[i];
} else{
Decrease += Grass[i];
}
Adjustment = Adjustment + Increase - Decrease - Grass[i];
}
cout << Increase + Decrease;
}
These look like these do the same thing, but one gets full credit, while the other gets around half of them wrong. Can someone please tell me the bug in my C++?