天梯赛L2题解(053-056)
L2-053 算式拆解
如果你WA2了,注意数字可能很长,超出longlong范围
但是实际上我们并不需要算出来的数字,所以用字符串存就好了
甚至有更简单好写的做法,但我懒得写了
#include <bits/stdc++.h>
using namespace std;
#define int long long
void ylh_() {
string s;
cin >> s;
int n = s.length();
stack<string> num;
stack<char> op;
auto work = [&]() -> void {
auto b = num.top();
num.pop();
auto a = num.top();
num.pop();
char c = op.top();
op.pop();
int res = 0;
if (b == "OooOOOO00O0o" && a == "OooOOOO00O0o") {
cout << c << '\n';
} else if (a == "OooOOOO00O0o" && b != "OooOOOO00O0o") {
cout << c << b << '\n';
} else if (b == "OooOOOO00O0o" && a != "OooOOOO00O0o") {
cout << a << c << '\n';
} else {
cout << a << c << b << '\n';
}
num.push({ "OooOOOO00O0o" });
};
for (int i = 0; i < n; ++i) {
if (isdigit(s[i])) {
int j = i;
string cur;
while (j < n && isdigit(s[j])) {
cur += s[j];
++j;
}
i = j - 1;
num.push({ cur });
} else if (s[i] == '(') {
continue;
} else if (s[i] == ')') {
work();
} else {
op.push(s[i]);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-054 三点共线
如果只枚举两个点那么复杂度会大大减少,因为题目按照1,0排序,所以我们按这个顺序枚举就行,卡卡常剪剪枝就能过
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int st[3][N * 2 + 10];//全局数组更快
void ylh_() {
int n;
cin >> n;
vector<vector<int>> seg(3);
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
if (!st[y][x + N]) {
seg[y].push_back(x);
}
st[y][x + N] = 1;
}
for (int i = 0; i <= 1; ++i) {
sort(seg[i].begin(), seg[i].end());
}
auto print = [&](int x1, int y1, int x2, int y2, int x3, int y3) {
cout << '[' << x1 << ", " << y1 << "] ";
cout << '[' << x2 << ", " << y2 << "] ";
cout << '[' << x3 << ", " << y3 << "]\n";
};
int f = 0;
for (int x1 : seg[1]) {
for (int x0 : seg[0]) {//存答案还要排序可能更慢,这样不用存
int x2 = 2 * x1 - x0;
if (x2 > N)
continue;
if (x2 < -N)
break;//自己手画一下发现这个可以改成break,是一个剪枝
if (st[2][x2 + N]) {
f = 1;
print(x0, 0, x1, 1, x2, 2);
}
}
}
if (!f) {
cout << -1;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-055 胖达的山头
简单差分
#include <bits/stdc++.h>
using namespace std;
#define int long long
void ylh_() {
int n;
cin >> n;
vector<int> a(24 * 3600 + 10);
for (int i = 1; i <= n; ++ i) {
int h, m, s, time;
char c;
cin >> h >> c >> m >> c >> s;
time = h * 3600 + m * 60 + s;
a[time]++;
cin >> h >> c >> m >> c >> s;
time = h * 3600 + m * 60 + s;
a[time + 1]--;
}
int ans = a[0];
for (int i = 1; i <= 24 * 3600; ++ i) {
a[i] += a[i - 1];
ans = max(a[i], ans);
}
cout << ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2-056 被n整除的n位数
感觉答案不会太多,所以爆搜试试,发现可过
#include <bits/stdc++.h>
using namespace std;
#define int long long
void ylh_() {
int n, a, b;
cin >> n >> a >> b;
int f = 0;
int val = 0;
auto dfs = [&](auto&& dfs, int w) {
if (w == n) {
if (val >= a && val <= b) {
f = 1;
cout << val << '\n';
}
return;
}
for (int i = 0; i <= 9; ++i) {
if (w == 0 && i == 0)
continue;
val = val * 10 + i;
if (val % (w + 1) == 0) {
dfs(dfs, w + 1);
}
val /= 10;
}
};
dfs(dfs, 0);
if (!f) {
cout << "No Solution";
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
ylh_();
}
}
L2总算完结了

浙公网安备 33010602011771号