Homework Introduction
#include <bits/stdc++.h>
using namespace std;
stack<int>stk, stkmin;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s == "pop") {
if (stk.empty()) {
cout << "error" << endl;
} else{
if(stk.top()==stkmin.top()){
stkmin.pop();
}
stk.pop();
}
} else if (s == "top") {
if (stk.empty()) {
cout << "error" << endl;
} else
cout << stk.top() << endl;
} else if (s == "push") {
int x;
cin >> x;
if (stk.empty()) {
stk.push(x);
stkmin.push(x);
} else {
stk.push(x);
if (x <= stkmin.top())
stkmin.push(x);
}
} else if (s == "get_min") {
if(!stkmin.empty())
cout << stkmin.top() << endl;
else cout<<"error"<<endl;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
stack<char>stk;
for(int i=0;s[i]!='@';i++){
if(s[i]=='('){
stk.push(s[i]);
}
if(s[i]==')'){
if(stk.empty()){
cout<<"NO"<<endl;
return 0;
}
else stk.pop();
}
}
if(stk.empty())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int T;
int main() {
cin >> T;
while (T--) {
string s;
cin >> s;
stack<char>stk;
int flag = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(' || s[i] == '[')
stk.push(s[i]);
else {
if (s[i] == ')' && (stk.empty() || stk.top() != '(')) {
cout << "No" << endl;
flag = 1;
} else if (s[i] == ']' && (stk.empty () || stk.top() != '[')) {
cout << "No" << endl;
flag = 1;
}
else if(s[i]==')' && stk.top()=='('){
stk.pop();
}
else if(s[i]==']' && stk.top()=='['){
stk.pop();
}
}
if (flag == 1)
break;
}
if (flag == 1)
continue;
else {
if (!stk.empty()) {
cout << "No" << endl;
} else
cout << "Yes" << endl;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
stack<char>stk;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '@') {
if(!stk.empty())stk.pop();
} else
stk.push(s[i]);
}
vector<char>v;
while (!stk.empty()) {
v.push_back(stk.top());
stk.pop();
}
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i];
}
return 0;
}
/*
in:1 2 3 4 5
out:5 3 2 4 1
out:4 3 2 1 5
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m;
int in[N], out[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> in[i];
}
while (m--) {
for (int i = 1; i <= n; i++) {
cin >> out[i];
}
int In = 1, Out = 1;
stack<int>stk;
while (In <= n) {
//1.栈为空
if (stk.empty()) {
stk.push(in[In]);
In++;
continue;
}
//2.栈不为空 且栈顶和out[Out]不相同
if (!stk.empty() && stk.top() != out[Out]) {
stk.push(in[In]);
In++;
}
//3.栈不为空,且栈顶和out[Out]相同
if (!stk.empty() && stk.top() == out[Out]) {
stk.pop();
Out++;
}
}
while (!stk.empty() && out[Out] == stk.top()) {
stk.pop();
Out++;
}
if (stk.empty() && Out == n + 1 && In == n + 1) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
stack<int>stk;
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s == "empty") {
if (!stk.empty())
cout << "no" << endl;
else
cout << "yes" << endl;
} else if (s == "size") {
cout << "size = " << stk.size() << endl;
} else if (s == "top") {
if (!stk.empty()) {
cout << "top = " << stk.top() << endl;
} else
cout << "top fail" << endl;
} else if (s == "pop") {
if (!stk.empty()) {
cout << "pop " << stk.top() << endl;;
stk.pop();
} else
cout << "pop fail" << endl;
} else {
int x;
cin >> x;
stk.push(x);
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6+5;
int n,a[N],f[N];
stack<int>stk;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>=1;i--){
while(!stk.empty() && a[i]>=a[stk.top()])stk.pop();
if(stk.empty())f[i] = 0;
else f[i] = stk.top();
stk.push(i);
}
for(int i=1;i<=n;i++){
cout<<f[i]<<" ";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
int n, a[N], f[N];
stack<int>stk;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
while (!stk.empty() && a[i] > a[stk.top()])
stk.pop();
if (!stk.empty())
f[i] = stk.top();
else
f[i] = n + 1;
stk.push(i);
}
int res = 0;
for (int i = 1; i <= n; i++) {
res += f[i] - i - 1;
}
cout << res << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e6 + 5;
int n, a[N], f[N];
stack<int>stk;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int res = 0;
for (int i = n; i >= 1; i--) {
while (!stk.empty() && a[i] >= a[stk.top()]) {
res += stk.top() - i + 1;
stk.pop();
}
if (!stk.empty()) {
res += stk.top() - i + 1;
}
stk.push(i);
}
cout << res << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
#define int long long
int n;
int h[N], v[N], f[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i] >> v[i];
}
//1.找左边比h[i]大的
stack<int>stk;
for (int i = 1; i <= n; i++) {
while (!stk.empty() && h[i] >= h[stk.top()])
stk.pop();
if (!stk.empty())
f[stk.top()] += v[i];
stk.push(i);
}
while (!stk.empty())
stk.pop();
for (int i = n; i >= 1; i--) {
while (!stk.empty() && h[i] >= h[stk.top()])
stk.pop();
if (!stk.empty())
f[stk.top()] += v[i];
stk.push(i);
}
int res = 0;
for (int i = 1; i <= n; i++) {
res = max(res, f[i]);
}
cout << res << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N];
int stk1[N], top1, stk2[N], top2;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int res = 0;
for (int i = 1; i <= n; i++) {
//top1维护单调递增
while (top1 && a[i] <= a[stk1[top1]])
--top1;
while (top2 && a[i] > a[stk2[top2]])
--top2;
//找stk1中比stk2[top2]大的数
int id = upper_bound(stk1 + 1, stk1 + top1 + 1, stk2[top2]) - stk1;
if (id <= top1)
res = max(res, i - stk1[id] + 1);
stk1[++top1] = i;
stk2[++top2] = i;
}
cout << res << endl;
return 0;
}
Problem
Please claim the assignment to see the problems.
- Status
- Live...
- Problem
- 12
- Open Since
- 2025-5-15 0:00
- Deadline
- 2025-5-22 23:59
- Extension
- 24 hour(s)