int main() { int n; int next; cin >> n; list<pair<int, int>> balloon; for (int i = 0; i < n; i++) { int m; cin >> m; balloon.push_back(make_pair(m, i + 1)); } while (balloon.size() != 0) { if (balloon.size() == n) { cout << balloon.front().second << ' '; next = balloon.front().first; balloon.pop_front(); } if (balloon.size() == 1) { cout << balloon.front().second << ' '; balloon.pop_front(); break; } if (next > 0) { auto it = balloon.begin(); for (int i = 1; i < next; i++) it++; cout << it->second << ' '; next = it->first; balloon.erase(it); } else if (next < 0) { auto it = --balloon.end(); for (int i = next; i < 0; i++) { if (it == balloon.begin() || balloon.size() == 2) { it = --balloon.end(); continue; } --it; } cout << it->second << ' '; next = it->first; balloon.erase(it); } } return 0; }
sky456139 6년 전
테스트 케이스도 작성해봤는데 어디서 런타임 에러가 나는걸까요? ㅜㅜ
'''
#include<iostream>
#include<list>
using namespace std;
int main() {
int n;
int next;
cin >> n;
list<pair<int, int>> balloon;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
balloon.push_back(make_pair(m, i + 1));
}
while (balloon.size() != 0) {
if (balloon.size() == n) {
cout << balloon.front().second << ' ';
next = balloon.front().first;
balloon.pop_front();
}
if (balloon.size() == 1) {
cout << balloon.front().second << ' ';
balloon.pop_front();
break;
}
if (next > 0) {
auto it = balloon.begin();
for (int i = 1; i < next; i++)
it++;
cout << it->second << ' ';
next = it->first;
balloon.erase(it);
}
else if (next < 0) {
auto it = --balloon.end();
for (int i = next; i < 0; i++) {
if (it == balloon.begin() || balloon.size() == 2) {
it = --balloon.end();
continue;
}
--it;
}
cout << it->second << ' ';
next = it->first;
balloon.erase(it);
}
}
return 0;
}
'''