코딩테스트/구현|기타
[구현] 백준 23294 웹 브라우저1, 23300 웹 브라우저2 C++
유(YOO)
2023. 9. 15. 19:31
https://www.acmicpc.net/problem/23294
https://www.acmicpc.net/problem/23300
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, q, c;
cin >> n >> q >> c;
vector<int> cap(n+1, 0);
for(int i=1; i<=n; i++) cin >> cap[i];
deque<int> back, front;
int page = -1;
int cache = 0;
char ch;
for(int i=0; i<q; i++) {
cin >> ch;
if (ch == 'A') {
for(auto f : front) cache -= cap[f];
front.clear();
if (page != -1) back.push_back(page);
cin >> page;
cache += cap[page];
while (cache > c) {
cache -= cap[back.front()];
back.pop_front();
}
}
else if (ch == 'B') {
if (back.size() > 0) {
front.push_back(page);
page = back.back();
back.pop_back();
}
}
else if (ch == 'F') {
if (front.size() > 0) {
back.push_back(page);
page = front.back();
front.pop_back();
}
}
else {
int cnt = 0;
int size = back.size();
for(int j=0; j<size; j++) {
int temp = back.front();
back.pop_front();
if (j == 0) back.push_back(temp);
else if (temp != back.back()) back.push_back(temp);
else cnt += cap[temp];
}
cache -= cnt;
}
}
cout << page << "\n";
if (back.size() == 0) cout << -1;
else {
for(int i=back.size()-1; i>=0; i--) cout << back[i] << " ";
}
cout << "\n";
if (front.size() == 0) cout << -1;
else {
for(int i=front.size()-1; i>=0; i--) cout << front[i] << " ";
}
cout << "\n";
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
deque<int> back, front;
int page = -1;
char c;
for(int i=0; i<q; i++) {
cin >> c;
if (c == 'A') {
front.clear();
if (page != -1) back.push_back(page);
cin >> page;
}
else if (c == 'B') {
if (back.size() > 0) {
front.push_back(page);
page = back.back();
back.pop_back();
}
}
else if (c == 'F') {
if (front.size() > 0) {
back.push_back(page);
page = front.back();
front.pop_back();
}
}
else {
int size = back.size();
for(int i=0; i<size; i++) {
int temp = back.front();
back.pop_front();
if (i == 0) back.push_back(temp);
else if (temp != back.back()) back.push_back(temp);
}
}
}
cout << page << "\n";
if (back.size() == 0) cout << -1;
else {
for(int i=back.size()-1; i>=0; i--) cout << back[i] << " ";
}
cout << "\n";
if (front.size() == 0) cout << -1;
else {
for(int i=front.size()-1; i>=0; i--) cout << front[i] << " ";
}
cout << "\n";
return 0;
}