P3156 【深基15.例1】询问学号
题目链接:P3156 【深基15.例1】询问学号 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
using namespace std;
long long a[2000010];
int main() {
long long n, m, x;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> x;
cout << a[x] << endl;
}
return 0;
}
P3613 【深基15.例2】寄包柜
题目链接:P3613 【深基15.例2】寄包柜 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long n, q, x;
string i, j, k;
map<string, string>m;
int main() {
cin >> n >> q;
for (int t = 0; t < q; t++) {
cin >> x;
if (x == 1) {
cin >> i >> j >> k;
i += '-';
i += j;
m[i] = k;
} else if (x == 2) {
cin >> i >> j;
i += '-';
i += j;
cout << m[i] << endl;
}
}
return 0;
}
P1449 后缀表达式
题目链接:P1449 后缀表达式 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <string>
#include <stack>
#include <stdlib.h>
using namespace std;
stack<long long>t;
string s, x = "";
long long a, b;
int main() {
cin >> s;
for (int i = 0; s[i] != '@'; i++) {
if (s[i] <= '9' && s[i] >= '0') {
x += s[i];
} else if (s[i] == '.') {
t.push(atoi(x.c_str()));
x = "";
} else {
a = t.top();
t.pop();
b = t.top();
t.pop();
if (s[i] == '+') {
a += b;
} else if (s[i] == '-') {
a = b - a;
} else if (s[i] == '*') {
a *= b;
} else if (s[i] == '/') {
a = b / a;
}
t.push(a);
}
}
cout << t.top();
return 0;
}
P1996 约瑟夫问题
题目链接:P1996 约瑟夫问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int>q;
int n, m, cnt = 1;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
q.push(i);
}
while (!q.empty()) {
if (cnt == m) {
cout << q.front() << ' ';
q.pop();
cnt = 1;
} else {
q.push(q.front());
q.pop();
cnt++;
}
}
return 0;
}
P1160 队列安排
题目链接:P1160 队列安排 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
using namespace std;
struct s {
int left;
int right;
} a[100010];
bool flag[100010];
int main() {
int n, m, k, p, x;
a[1].left = 0;
a[1].right = 100010;
a[0].right = 1;
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> k >> p;
if (p == 0) {
a[i].left = a[k].left;
a[i].right = k;
a[a[k].left].right = i;
a[k].left = i;
} else if (p == 1) {
a[i].left = k;
a[i].right = a[k].right;
a[a[k].right].left = i;
a[k].right = i;
}
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x;
if (flag[x] == 0) {
flag[x] = 1;
a[a[x].left].right = a[x].right;
a[a[x].right].left = a[x].left;
}
}
int b = a[0].right;
while (b != 100010) {
cout << b << ' ';
b = a[b].right;
}
return 0;
}
P1540 [NOIP2010 提高组] 机器翻译
题目链接:P1540 [NOIP2010 提高组] 机器翻译 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <queue>
using namespace std;
bool flag[1005];//记录单词是否在内存中
queue<int>q;
int main() {
int m, n, cnt = 0, x, ans = 0;
cin >> m >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (flag[x] == 1) {
continue;
}
if (cnt != m) {
q.push(x);
ans++;
cnt++;
flag[x] = 1;
} else {
flag[q.front()] = 0;
q.pop();
q.push(x);
ans++;
flag[x] = 1;
}
}
cout << ans;
return 0;
}
P2058 [NOIP2016 普及组] 海港
题目链接:P2058 [NOIP2016 普及组] 海港 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
using namespace std;
int a[300010];//国籍为i的人的数量
int b[300010];//第i个人的国籍
int c[300010];//第i个人到达时间
int main() {
int n, t, k, ans = 0, p = 0, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t >> k;
for (int j = 0; j < k; j++) {
c[++cnt] = t;
cin >> b[cnt];
if (a[b[cnt]] == 0) {
ans++;
}
a[b[cnt]]++;
}
while (t - 86400 >= c[p]) {
a[b[p]]--;
if (a[b[p]] == 0) {
ans--;
}
p++;
}
cout << ans << endl;
}
}
P1241 括号序列
题目链接:P1241 括号序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <string>
using namespace std;
bool f[105];
int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ')') {
for (int j = i - 1; j >= 0; j--) {
if (s[j] == '(' && f[j] == 0) {
f[i] = f[j] = 1;
break;
} else if (s[j] == '[' && f[j] == 0) {
break;
}
}
} else if (s[i] == ']') {
for (int j = i - 1; j >= 0; j--) {
if (s[j] == '[' && f[j] == 0) {
f[i] = f[j] = 1;
break;
} else if (s[j] == '(' && f[j] == 0) {
break;
}
}
}
}
for (int i = 0; i < s.size(); i++) {
if (f[i]) {
cout << s[i];
} else {
if (s[i] == '[' || s[i] == ']') {
cout << "[]";
}
if (s[i] == '(' || s[i] == ')') {
cout << "()";
}
}
}
return 0;
}
P4387 【深基15.习9】验证栈序列
题目链接:P4387 【深基15.习9】验证栈序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <stack>
using namespace std;
int a[100010], b[100010];
stack<int>s;
int main() {
int q, n, x;
cin >> q;
while (q--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
int j = 0;
for (int i = 0; i < n; i++) {
s.push(a[i]);
while (s.top() == b[j]) {
j++;
s.pop();
if (s.empty()) {
break;
}
}
}
if (s.empty()) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
while (!s.empty()) {
s.pop();
}
}
}
return 0;
}
P2234 [HNOI2002]营业额统计
题目链接:P2234 [HNOI2002]营业额统计 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
multiset<int>s;
int main() {
int n, x, ans;
cin >> n;
cin >> x;
s.insert(x);
s.insert(2000001);
s.insert(-2000001);
ans = x;
n--;
while (n--) {
cin >> x;
multiset<int>::iterator it = s.insert(x);
it--;
multiset<int>::iterator left = it;
it++;
it++;
multiset<int>::iterator right = it;
it--;
ans += min(abs(*it - *left), abs(*it - *right));
}
cout << ans;
return 0;
}