2579번 - 계단 오르기
#include
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
int n, m;
cin >> n;
vector > st(n+1, make_pair(0, 1));
for(int i = 1; i <= n; i++) {
cin >> m;
st[i].first = m;
}
st[2].first += st[1].first;
st[2].second += 1;
if(st[1].first > st[2].first){
st[3].first += st[1].first;
}else{
st[3].first += st[2].first;
st[3].second++;
for(int i = 4; i <= n; i++){
if(st[i-1].second == 2){
st[i].first += st[i-2].first;
if(st[i-1].first < st[i-2].first){
st[i].first += st[i-1].first;
st[i].second++;
cout << st[n].first;
댓글을 작성하려면 로그인해야 합니다.
bbcc8417 3년 전
#include
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
int n, m;
cin >> n;
vector > st(n+1, make_pair(0, 1));
for(int i = 1; i <= n; i++) {
cin >> m;
st[i].first = m;
}
st[2].first += st[1].first;
st[2].second += 1;
if(st[1].first > st[2].first){
st[3].first += st[1].first;
}else{
st[3].first += st[2].first;
st[3].second++;
}
for(int i = 4; i <= n; i++){
if(st[i-1].second == 2){
st[i].first += st[i-2].first;
}else{
if(st[i-1].first < st[i-2].first){
st[i].first += st[i-2].first;
}else{
st[i].first += st[i-1].first;
st[i].second++;
}
}
}
cout << st[n].first;
}