D. Colored Portals
1.E. Arranging The Sheep2.C. Team3.C. RationalLee4.B. Preparing Olympiad5.B. Find The Array6.E. Accidental Victory7.A. Learning Languages8.A. k-th divisor9.B. Quasi Binary10.蓝桥杯-地宫取宝11.D. Divide by three, multiply by two12.D. Game With Array13.B. Composite Coloring14.洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows15.洛谷 P3353 在你窗外闪耀的星星16.洛谷 P8818 [CSP-S 2022] 策略游戏17.Educational Codeforces Round 164 (Rated for Div. 2)18.E. Chain Reaction19.Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D. Valid BFS?20.D. Distance in Tree21.D. Multiplication Table22.C. Mixing Water23.B. Greg and Graph24.D. Another Problem About Dividing Numbers25.Acwing 143. 最大异或对26.Acwing 3485. 最大异或和27.C. Checkposts28.E. Lomsat gelral29.D. Powerful array30.D. Deleting Divisors31.Codeforces Round 950 (Div. 3)32.Codeforces Round 951 (Div. 2)33.SuntoryProgrammingContest2024(AtCoder Beginner Contest 357)34.Codeforces Round 952 (Div. 4)35.AtCoder Beginner Contest 35836.B. Modulo Sum37.D. Soldier and Number Game38.AtCoder Beginner Contest 35939.Codeforces Round 954 (Div. 3)40.Codeforces Round 955 (Div. 2, with prizes from NEAR!)41.AtCoder Regular Contest 18042.D - Ghost Ants43.AtCoder Beginner Contest 361)44.Codeforces Round 957 (Div. 3)45.E. Decode46.Educational Codeforces Round 168 (Rated for Div. 2)47.B. Parity and Sum48.P1972 [SDOI2009] HH的项链49.Codeforces Round 966 (Div. 3) VP50.https://codeforces.com/contest/2004/problem/B51.C. Splitting Items
52.D. Colored Portals
53.Codeforces Round 967 (Div. 2)54.D. Determine Winning Islands in Race55.C. Turtle and Good Pairs56.Codeforces Round 970 (Div. 3)(VP)57.G. Sakurako's Task58.Codeforces Round 971 (Div. 4)59.C. Lazy Narek60.B. Regular Bracket Sequence61.C. News Distribution62.B. Karen and Coffee63.G. 2^Sort64.C. Johnny and Another Rating Drop65.A. Greg and Array66.D2. Magic Powder - 267.C. Schedule Management68.C. Constanze's Machine69.C. Soldier and Cards70.C. Berland Regional71.E. Anna and the Valentine's Day Gift72.B. Polo the Penguin and Matrix73.B. Fortune Telling74.C. Powers Of Two75.D. Palindromes Coloringhttps://codeforces.com/contest/2004/problem/D
题意:给定n个字符串,每个字符串有2个字符,如果两个字符串中有相同的字符,则这两个位置互相到达,到达的代价为坐标距离。并且所有字符串的种类只有6种,给定m个查询,求两个地方到达的最小代价。
思路:直接暴力,为6种字符串编号。如果查询位置上的字符串有相同字符,那可以直接算距离。如果没有相同字符,则检查距离l和r最近的其他4种字符串的位置,取代价最小的一个即可。
总结:感觉代码写复杂了
map<string, int> mapp{{"BG", 0}, {"BR", 1}, {"BY", 2}, {"GR", 3}, {"GY", 4}, {"RY", 5}};
void solve(){
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<string> t(n);
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
t[i] = s;
a[i] = mapp[s];
}
vector<array<int, 6>> b(n + 1, {-1, -1, -1, -1, -1, -1});
for (int i = 0; i < n; ++i) {
b[i + 1] = b[i];
b[i + 1][a[i]] = i;
}
vector<array<int, 6>> c(n + 1, {-1, -1, -1, -1, -1, -1});
for (int i = n - 2; i >= 0; --i) {
c[i] = c[i + 1];
c[i][a[i + 1]] = i + 1;
}
while (m --) {
int l, r;
cin >> l >> r;
l --;
r --;
if (l > r) {
swap(l, r);
}
if (t[l][0] == t[r][0] || t[l][0] == t[r][1] || t[l][1] == t[r][0] || t[l][1] == t[r][1]) {
cout << r - l << '\n';
}
else {
int t1 = a[l];
int t2 = a[r];
bool ok = false;
for (int i = 0; i < 6; ++i) {
if (i != t1 && i != t2) {
if (b[l][i] != -1 || b[r][i] != -1 || c[l][i] != -1 || c[r][i] != -1) {
ok = true;
break;
}
}
}
if (!ok) {
cout << "-1\n";
}
else {
int res = (int)1e9;
for (int i = 0; i < 6; ++i) {
if (i != t1 && i != t2) {
if (b[l][i] != -1) {
res = min(res, abs(l - b[l][i]) + abs(r - b[l][i]));
}
if (c[l][i] != -1) {
res = min(res, abs(c[l][i] - l) + abs(r - c[l][i]));
}
if (c[r][i] != -1) {
res = min(res, abs(c[r][i] - l) + abs(c[r][i] - r));
}
if (b[r][i] != -1) {
res = min(res, abs(r - b[r][i]) + abs(l - b[r][i]));
}
}
}
cout << res << '\n';
}
}
}
}
合集:
记录随手刷过的编程题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】