上海市计算机学会竞赛平台 | 2022 三星级挑战
此文章仅供学习交流,不得抄袭刷分!!!
2022/5/26 目前以全部更完
最大回撤
#include <iostream>
using namespace std;
int main()
{
int n, min = -100001, large = -100001, a;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a;
min = max(min, large - a);
large = max(large, a);
}
cout << min;
return 0;
}
最后一击
#include <iostream>
using namespace std;
long long n, a, b, x = 1, y = 1, cont = 0, sum = 0;
bool q = false, g = false, d = false;
int main()
{
cin.tie(0);
cin >> n >> a >> b;
while(sum < n)
{
if (a * y > b * x)
sum++, x++, q = true, g = false, d = false;
else if (a * y == b * x)
sum += 4, x++, y++, q = false, g = false, d = true;
else
sum++, y++, q = false, g = true, d = false;
}
if (q == true)
cout << "A";
else if (g == true)
cout << "B";
else if (d == true)
cout << "C";
return 0;
}
平衡点
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
long long a[100005];
long long pre[100005], suf[100005];
long long ans, L, R;
int main()
{
std::ios::sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
pre[i] = pre[i - 1] + a[i]; //前缀和
ans += a[i] * (i - 1); //初始为最大
}
for(int i = n; i >= 1; i--)
suf[i] = suf[i + 1] + a[i]; //后缀和
R = ans;
for(int i = 1; i <= n; i++)
{
L += pre[i];
R -= suf[i + 1];
ans = min(ans, abs(L - R));
}
cout << ans;
return 0;
}
栈的判断
#include <iostream>
#include <cmath>
#include <stack>
using namespace std;
int n, x;
stack<int>stak;
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i++) {
int tmp;
cin >> tmp;
if(stak.empty() && x <= tmp) {
while(x < tmp)
stak.push(x++);
x++;
}
else if(stak.top() == tmp)
stak.pop();
else if(stak.top() < tmp) {
while(x < tmp)
stak.push(x++);
x++;
}
else if(stak.top() > tmp) {
cout << "Invalid" << endl;
return 0;
}
}
cout << "Valid" << endl;
return 0;
}
排队安排
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[1000000], c;
int main() {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; ++i)
if (c <= a[i])
++c;
cout << c << endl;
return 0;
}
逆波兰式
#include<bits/stdc++.h>
using namespace std;
stack<int> st;
string line;
int main() {
getline(cin,line);
for(int i=0; i<line.size(); i++) {
char tem=line[i];
if(tem==' ') continue;
if(tem<='9' and tem>='0') {
st.push(tem-'0');
continue;
}
int now=0;
int b= st.top();
st.pop();
int a=st.top();
st.pop();
if(tem=='+') st.push((a+b)%10);
if(tem=='-') st.push((a-b)%10);
if(tem=='*') st.push(a*b%10);
}
int ans=st.top();
ans=ans%10;
if (ans<0) ans=10+ans;
cout<<ans;
return 0;
}
股票市场
#include <iostream>
using namespace std;
long long n, m, stock = 0, a[100001];
bool flag = false;
int main() {
cin.tie(0);
cin >> n >> m;
cin >> a[0]