牛客小白月赛105
牛客小白月赛105
2024.11.22
C.
大力讨论,写了一个小时挂了4发,应该一开始想一个好写的方法再动键盘的(早点重写的)。
D.
并查集板题
E.
Q.给定一括号序列,你可任意安排匹配的'()'由里向外删除顺序,问每对括号删除之前最多可以删除多少对括号。
括号匹配+反向答案:res[i]=n-栈里元素个数 / 也可生成一棵树 (()())
-
E题还是比较有意思的。
C
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
const int mod = 998244353;
const int N = 10 + 5e5;
void _();
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
_();
return 0;
}
void _()
{
int m;
cin >> m;
string a, b;
cin >> a >> b;
a = "x" + a;
b = "x" + b;
a += 'x';
b += 'x';
int res = 0;
for (int i = 1; i <= m; i++)
{
int f = 0;
if (a[i] == '.')
{
if (b[i] == '.')
{
if (a[i - 1] == a[i] && a[i - 1] - b[i - 1])
f = 1;
if (a[i + 1] == a[i] && a[i + 1] - b[i + 1])
f = 1;
}
else if (a[i - 1] == a[i + 1])
f = 1;
}
res += f;
f = 0;
if (b[i] == '.')
{
if (a[i] == '.')
{
if (b[i - 1] == b[i] && b[i - 1] - a[i - 1])
f = 1;
if (b[i + 1] == b[i] && a[i + 1] - b[i + 1])
f = 1;
}
else if (b[i - 1] == b[i + 1])
f = 1;
}
res += f;
}
cout << res << endl;
}
// void _()
// {
// int m;
// cin >> m;
// string a, b;
// cin >> a >> b;
// a = "x" + a;
// b = "x" + b;
// a += 'x';
// b += 'x';
// vector<int> cnt(m + 1);
// for (int i = 1; i <= m; i++)
// cnt[i] = (a[i] == '.') + (b[i] == '.');
// int res = 0;
// for (int i = 1; i <= m; i++)
// if (cnt[i])
// {
// if (cnt[i] == 1)
// {
// int f = 0;
// if (i == 1 || i == m)
// {
// int t = i == 1 ? 2 : m - 1;
// if (a[i] == '.')
// {
// if (a[i] - a[t])
// res++;
// }
// else if (b[i] - b[t])
// res++;
// }
// else
// {
// if (a[i] == '.')
// {
// if (a[i - i] == a[i] && a[i] == a[i + 1])
// f = 1;
// if (a[i - 1] - a[i] && a[i] - a[i + 1])
// f = 1;
// }
// else if ((b[i - i] == b[i] && b[i] == b[i + 1]) || (b[i - i] - b[i] && b[i] - b[i + 1]))
// f = 1;
// }
// // if (f)
// // bug(i);
// res += f;
// }
// else
// {
// int f = 0;
// if (i > 1 && a[i] == a[i - 1] && a[i - 1] - b[i - 1])
// f = 1;
// if (i < m && a[i] == a[i + 1] && a[i + 1] - b[i - 1])
// f = 1;
// res += f;
// f = 0;
// if (i > 1 && b[i] == b[i - 1] && a[i - 1] - b[i - 1])
// f = 1;
// if (i < m && b[i] == b[i + 1] && a[i + 1] - b[i - 1])
// f = 1;
// res += f;
// }
// }
// // bug3(b[m - 1], b[m], b[m + 1]);
// if (m == 1 && cnt[m] == 1)
// res = 1;
// cout << res << endl;
// }
D
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
const int mod = 998244353;
const int N = 10 + 5e5;
void _();
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
_();
return 0;
}
// 带权并查集
vector<int> p, vs, es; // 集合数 点数 边数 (对一个连通块而言)
void init(int n1) // p[x]不一定为根节点 find(x)一定是根节点
{
int n = n1 + 2;
p.assign(n, 0);
vs.assign(n, 0);
es.assign(n, 0);
for (int i = 1; i <= n1; i++)
p[i] = i, vs[i] = 1, es[i] = 0;
}
int find(int x) // 找到根节点
{
if (p[x] == x)
return x;
int px = find(p[x]);
return p[x] = px;
}
bool same(int a, int b)
{
return find(a) == find(b);
}
void merge(int a, int b) // 合并集合
{
int pa = find(a);
int pb = find(b);
if (pa == pb) // pa pb 均为根节点 p[pa]==pa
{
es[pa]++; // 1个集合 边+1
return;
}
p[pb] = p[pa]; // 改变b的根节点
vs[pa] += vs[pb]; // 将b合并进a
es[pa] += es[pb] + 1; // 2个集合
}
int size(int a) // 集合内的元素的个数
{
return vs[find(a)];
}
// init(n);
void _()
{
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
init(n);
for (int i = 1; i <= n; i++)
cin >> a[i];
while (m--)
{
int a, b;
cin >> a >> b;
merge(a, b);
}
int res = 0;
map<int, vector<int>> has;
for (int i = 1; i <= n; i++)
has[find(i)].push_back(a[i]);
for (auto [x, a] : has)
{
map<int, int> cnt;
int max_w = 0;
for (auto v : a)
cnt[v]++, max_w = max(max_w, cnt[v]);
res += a.size() - max_w;
}
cout << res << endl;
}
E
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
const int mod = 998244353;
const int N = 10 + 5e5;
void _();
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
string s;
cin >> s;
stack<int> stk;
vector<int> res(n + 1);
bool has_ans = 1;
int st = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] == '(')
stk.push(++st);
else
{
if (!stk.size())
{
has_ans = 0;
break;
}
res[stk.top()] = n - stk.size();
stk.pop();
}
if (stk.size())
has_ans = 0;
if (!has_ans)
{
cout << -1 << endl;
return;
}
for (int i = 1; i <= n; i++)
cout << res[i] << ' ';
}
// void _()
// {
// int n;
// cin >> n;
// string s;
// cin >> s;
// stack<int> stk;
// map<int, vector<int>> has;
// int st = 0;
// int cnt = 1;
// int f = 1;
// for (auto v : s)
// {
// if (v == '(')
// stk.push(++st);
// else
// {
// if (stk.size() == 0)
// {
// f = 0;
// break;
// }
// has[cnt].push_back(stk.top());
// stk.pop();
// if (stk.size() == 0)
// ++cnt;
// }
// }
// if (stk.size())
// f = 0;
// if (!f)
// {
// cout << -1 << endl;
// return;
// }
// int sum = 0;
// for (auto &[x, a] : has)
// sum += a.size();
// vector<int> res(n + 1);
// for (auto &[x, a] : has)
// if (a.size())
// {
// // bug(a.size());
// int ans = sum - a.size();
// // bug2(sum, a.size());
// sort(a.rbegin(), a.rend());
// for (int i = 0; i < a.size(); i++)
// res[a[i]] = ans + i;
// }
// for (int i = 1; i <= n; i++)
// cout << res[i] << ' ';
// cout << endl;
// }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】