跳到正文

算法·理论

代码源 Day 1

CuteGirlGC约 22 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 22 分钟

版本

  1. v1当前

按照开题顺序

Final

C The Fool

简单题,抢到首A了:)

你只要暴力比对邻居就行了。

C++
#include <iostream>

using namespace std;

const int kMaxN = 201;

int n, m, k;
string s[kMaxN][kMaxN];

bool C(string a, int i, int j) {
  if (i > n || j > m || i < 1 || j < 1) {
    return 1;
  }
  string t = s[i][j];
  int c = 0;
  for (int i = 0; i < k; i++) {
    c += a[i] != t[i];
  }
  return c >= 1;
}

int main() {
  cin >> n >> m >> k, getchar();
  for (int i = 1; i <= n; i++, getchar()) {
    for (int j = 1; j <= m; j++) {
      for (int l = 1; l <= k; l++) {
        s[i][j] = s[i][j] + (char)getchar();
      }
    }
  }
  pair<int, int> ans;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (C(s[i][j], i - 1, j) && C(s[i][j], i + 1, j) && C(s[i][j], i, j - 1) && C(s[i][j], i, j + 1)) {
        ans = {i, j};
        break;
      }
    }
  }
  cout << ans.first << ' ' << ans.second;
  return 0;
}

A Box

x∈[min⁡(u0,u1),max⁡(u0,u1)],y∈[min⁡(v0,v1),max⁡(v0,v1)],z∈[z0,z0+h]x \in [\min(u_0, u_1), \max(u_0, u_1)], y\in [\min(v_0, v_1), \max(v_0, v_1)], z\in[z0, z0+h]。

第一遍就A了。

C++
#include <iostream>

using namespace std;

int z0, h, u0, v0, u1, v1, q, x, y, z;

int main() {
  cin >> z0 >> h >> u0 >> v0 >> u1 >> v1;
  for (cin >> q; q; q--) {
    cin >> x >> y >> z;
    if (z <= z0 + h && z >= z0 && x <= max(u0, u1) && x >= min(u0, u1) && y <= max(v0, v1) && y >= min(v0, v1)) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }
  return 0;
}

E 爱上字典

错因是没有去重。

C++
#include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

const int kMaxN = 5e6 + 1;

int n, tot;
string s[kMaxN];
set<string> st;

int main() {
  string tmp = "";
  bool f = 0;
  while (1) {
    char c = getchar();
    if (isalpha(c) && f) {
      tmp = tmp + c;
    } else if (isalpha(c) && !f) {
      f = 1, tmp = tmp + c;
    } else if (!isalpha(c) && f) {
      s[++tot] = tmp, f = 0, tmp = "";
    }
    if (c == '\n') {
      break;
    }
  }
  for (int i = 1; i <= tot; i++) {
    transform(s[i].begin(), s[i].end(), s[i].begin(), ::tolower);
  }
  cin >> n;
  for (string s; n; n--) {
    cin >> s, transform(s.begin(), s.end(), s.begin(), ::tolower);
    st.insert(s);
  }
  int k = st.size();
  for (int i = 1; i <= tot; i++) {
    st.insert(s[i]);
  }
  cout << st.size() - k << '\n';
  return 0;
}

B Left Shifting 3

虽然一遍A但是我是大怨种/doge

设 s=s0s1…sns = s_0s_1\dots s_n,那么设一个新的字符串 tt 等于 s0s1…sns0s1…smin⁡(k,n)s_0s_1\dots s_ns_0s_1\dots s_{\min(k, n)},ai(i∈[1,n+min⁡(k,n)])a_i(i \in [1, n + \min(k, n)]) 为当前字符及之前所有的字符构成的字符串中 nanjing 的个数,求 max⁡i=nn+min⁡(k,n)(ai−ai−n)\max \limits_{i=n}^{n+\min(k, n)}(a_{ i} - a_{i - n})。

C++
#include <iostream>

using namespace std;

const int kMaxN = 4e5 + 1;

int t, n, m, k, a[kMaxN];
string s;

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> k >> s;
    if (k >= n) {
      s = s + s;
      m = n * 2;
    } else {
      s = s + s.substr(0, k);
      m = n + k;
    }
    int maxn = 0;
    for (int i = 6; i < m; i++) {
      if (s.substr(i - 6, 7) == "nanjing") {
        a[i] = a[i - 1] + 1;
      } else {
        a[i] = a[i - 1];
      }
      if (i >= n - 1) {
        maxn = max(maxn, a[i] - a[i - n + 1]);
      }
    }
    cout << maxn << '\n';
  }
  return 0;
}

F 年少的契约

错因是没有注意到时间必须合法。

首先设 dnow1=x0×30+y0×0.5,dnow2=y0×6dnow_1 = x_0 \times 30 + y_0 \times 0.5, dnow_2 = y_0 \times 6

对于每一个 time∈[x1×60+y1,x2×60+y2]time\in [x_1\times 60 + y_1, x_2 \times 60 + y_2],设 h=time60,m=time mod 60h = \dfrac{time}{60},m = time \bmod 60,再设 d1=h×30+m×0.5,d2=m×6d_1 = h \times 30 + m \times 0.5, d_2 = m \times 6,于是求当:

min⁡(∣dnow1−d1∣,360−∣dnow1−d1∣)+min⁡(∣dnow2−d2∣,360−∣dnow2−d2∣)\min(|dnow_1 - d_1|, 360 - |dnow_1 - d_1|) + \min(|dnow_2 - d_2|, 360 - |dnow_2 - d_2|)

最小时的 h,mh, m,输出即可。

C++
#include <iostream>

#define x first
#define y second

using namespace std;

int t, tp[3];
pair<int, int> a[3];

int main() {
  for (cin >> t; t; t--) {
    for (int i = 0; i < 3; i++) {
      cin >> a[i].x >> a[i].y;
      tp[i] = a[i].x * 60 + a[i].y;
    }
    double dnow1 = a[0].x * 30 + a[0].y * 0.5, dnow2 = a[0].y * 6;
    int x, y;
    double minn = 1e9;
    for (int i = tp[1]; i <= tp[2]; i++) {
      int h = i / 60, m = i % 60;
      double d1 = h * 30 + m * 0.5, d2 = m * 6;
      double tmp = min(abs(dnow1 - d1), 360 - abs(dnow1 - d1)) + min(abs(dnow2 - d2), 360 - abs(dnow2 - d2));
      if (tmp < minn) {
        x = h, y = m, minn = tmp;
      }
    }
    cout << x << ' ' << y << '\n';
  }
  return 0;
}

D Rotation

错因是 00 写成了 11。

没什么说的,就是我们知道一次拧石像的鼻子加上拧三次自己的鼻子就可以视作逆时针拧一次这个石像的鼻子,所以我们这知道最多拧三次自己的鼻子。

C++
#include <iostream>
#include <algorithm>

using namespace std;

const long long kMaxN = 1e6 + 1;

long long n, a[kMaxN], cnt[5];

int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < 4; j++) {
      cnt[j] += (a[i] - j + 4) % 4;
    }
  }
  long long ans = 1e18;
  for (long long i = 0; i < 4; i++) {
    ans = min(ans, cnt[i] + (cnt[i] * 3 + 4 - i) % 4);
  }
  cout << ans << '\n';
  return 0;
}

H Grand Prix of Ballance

这道题目其实没有什么好说的,WA的一发是因为没有注意到后面的比赛开始后前一个比赛会结束。

C++
#include <algorithm>
#include <iostream>
#include<unordered_map>

using namespace std;
using ll = long long;

const ll kMaxN = 2e5 + 1;

ll t, n, m, q, f[kMaxN], scr[kMaxN];
unordered_map<ll, ll> mp[kMaxN];
struct Player {
  ll id, score;
} p[kMaxN];

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> m >> q;
    for (ll i = 1; i <= m; i++) {
      p[i] = {i, 0};
    }
    fill(scr + 1, scr + n + 1, m);
    fill(f + 1, f + n + 1, 0);
    int lst = 0;
    for (ll op, id, x; q; q--) {
      cin >> op;
      if (op == 1) {
        cin >> id;
        f[lst] = 0;
        f[id] = 1;
        lst = id;
      } else if (op == 2) {
        cin >> id >> x;
        if (!f[x] || mp[x][id] != 0) {
          continue;
        }
        mp[x][id] = 1, p[id].score += scr[x];
        scr[x]--;
      } else {
        cin >> id >> x;
        if (!f[x] || mp[x][id] != 0) {
          continue;
        }
        mp[x][id] = 1;
      }
    }
    sort(p + 1, p + m + 1, [](Player a, Player b) {
      return a.score == b.score ? a.id < b.id : a.score > b.score;
    });
    for (ll i = 1; i <= m; i++) {
      cout << p[i].id << ' ' << p[i].score << '\n';
    }
    for (ll i = 1; i <= n; i++) {
      mp[i].clear();
    }
  }
  return 0;
}

I Ranking Prediction

错因是最后在贪心选取滚榜时做出来的题目时没有判断不能严格大于的情况,WA#2了两发。

C++
#include <algorithm>
#include <iostream>
#include <unordered_map>

using namespace std;

const int kMaxN = 1001;

int t, n, a, b, fas[kMaxN];
struct J {
  int t;
  string zt;
  char tm;
} tj[kMaxN];

int main() {
  for (cin >> t; t; t--) {
    unordered_map<char, int> mp;
    cin >> n >> a >> b;
    int s;
    cin >> s;
    for (int i = 1; i <= s; i++) {
      cin >> tj[i].t >> tj[i].tm >> tj[i].zt;
    }
    int fs = 0, cnt = 0, ans = -1, kkk, tot = 0;
    for (int i = 1; i <= s; i++) {
      if (mp[tj[i].tm] == -1) {
      } else {
        if (tj[i].zt == "ac") {
          fs += tj[i].t + mp[tj[i].tm], mp[tj[i].tm] = -1, cnt++;
        } else if (tj[i].zt == "rj") {
          mp[tj[i].tm] += 20;
        } else {
          fas[++tot] = mp[tj[i].tm] + tj[i].t;
          mp[tj[i].tm] = -1;
        }
      }
    }
    if (cnt > a || (cnt == a && fs < b)) {
      cout << 0 << '\n';
      continue;
    }
    sort(fas + 1, fas + tot + 1);
    if (tot == 0) {
      cout << -1 << '\n';
      continue;
    }
    kkk = 0;
    for (int i = 1; i <= tot; i++) {
      fs += fas[i];
      kkk++, cnt++;
      if (cnt > a || (cnt == a && fs < b)) {
        ans = kkk;
        break;
      } else if (i == tot && (cnt == a && fs >= b || cnt < a)) {
        kkk = -1;
      }
    }
    cout << kkk << '\n';
  }
  return 0;
}

G Assembly Line

这道题目就是算出每一封工件到最后一位工人的时间排序,然后对于每一个时间,和上一个时间加一取 max⁡\max,因为最后一位工人处理完上一个信件需要 1 单位时间。

错误的两发是自己在主函数玩抽象东西,然后 RE 和 函数没有返回值。

警示后人:

C++
// S 是结构体,里面的 main 就是解答
for (cin >> t; t; t--) {
  S *s = new S;
  int p = s->main();
  delete s;
}
C++
// 结构体里面的 main函数
int main() {
  cin >> n >> k;
  ...
  cout << o[n].nt - 1 << "\n";
  // 没写返回值
}

J 澡堂

注意重点吧,在获取新的 valval 的时候我们应该先把这一行全部设为极大值,然后从前往后,从后往前遍历,只要前/后有人了,就把这个距离和这个 valval 取 min⁡\min。

注意其实如果当前行是 kk,那么与他相对应的行是 4×⌊k+12⌋−k−14\times\lfloor \dfrac{k + 1}{2} \rfloor - k - 1。

C++
#include <iostream>
#include <map>

using namespace std;

const int kMaxN = 1001, kMaxM = 2e5 + 1;

int n, m, q, val[kMaxN][kMaxN], good[kMaxN][kMaxN], haveman[kMaxN][kMaxN];
pair<int, int> a[kMaxM], best[kMaxN];

void GetBest(int h) {
  int isgood = 0, value = 0;
  best[h] = {0, 0};
  for (int i = 1; i <= m; i++) {
    if (!haveman[h][i] && val[h][i] > value ||
        (val[h][i] == value && good[h][i] && !isgood)) {
      isgood = good[h][i], value = val[h][i], best[h] = {h, i};
    }
  }
}

void GetVal(int h) {
  int lst = 0;
  bool f = 1;
  fill(val[h] + 1, val[h] + m + 1, 1e9);
  for (int i = 1; i <= m; i++) {
    if (haveman[h][i]) {
      lst = i;
    }
    if (lst != 0) {
      val[h][i] = i - lst;
    }
  }
  lst = m + 1;
  for (int i = m; i >= 1; i--) {
    if (haveman[h][i]) {
      lst = i;
    }
    if (lst != m + 1) {
      val[h][i] = min(val[h][i], lst - i);
    }
  }
  if (lst == m + 1) {
    fill(val[h] + 1, val[h] + m + 1, m);
  }
}

int main() {
  cin >> m >> n >> q;
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = 1; j <= m; j++) {
      val[i][j] = m, good[i][j] = 1;
    }
    best[i] = {i, 1};
  }
  for (int op, x; q; q--) {
    cin >> op >> x;
    if (op == 1) {  // come one man
      pair<int, int> bst;
      int value = -1, isgood = 0;
      for (int i = 1; i <= 2 * n; i++) {
        if (best[i].first != 0 && val[best[i].first][best[i].second] > value ||
            (val[best[i].first][best[i].second] == value && !isgood &&
             good[best[i].first][best[i].second])) {
          value = val[best[i].first][best[i].second];
          bst = best[i];
          isgood = good[best[i].first][best[i].second];
        }
      }
      if (value == -1) {
        a[x] = {-1, -1};
      } else {
        a[x] = bst;
        int k = bst.first;
        haveman[k][bst.second] = 1;
        GetVal(k);
        good[((k + 1) / 2) * 4 - 1 - k][bst.second] = 0;
        GetBest(k), GetBest(((k + 1) / 2) * 4 - 1 - k);
      }
    } else {  // go away a man
      int k = a[x].first;
      haveman[a[x].first][a[x].second] = 0;
      good[((k + 1) / 2) * 4 - 1 - k][a[x].second] = 1;
      GetVal(a[x].first);
      GetBest(a[x].first), GetBest(((k + 1) / 2) * 4 - 1 - k);
    }
    // cout << "=========================\n";
    // for (int i = 1; i <= 2 * n; i++) {
    //   for (int j = 1; j <= m; j++) {
    //     cout << val[i][j] << " \n"[j == m];
    //   }
    // }
    // puts("");
    // for (int i = 1; i <= 2 * n; i++) {
    //   for (int j = 1; j <= m; j++) {
    //     cout << good[i][j] << " \n"[j == m];
    //   }
    // }
    // puts("");
    // for (int i = 1; i <= 2 * n; i++) {
    //   for (int j = 1; j <= m; j++) {
    //     cout << haveman[i][j] << " \n"[j == m];
    //   }
    // }
    // cout << "-------------------------\n";
  }
  for (int i = 1;; i++) {
    if (a[i].first == 0) {
      break;
    }
    cout << a[i].first << ' ' << a[i].second << '\n';
  }
  return 0;
}

撒花*★,°:.☆( ̄▽ ̄)/$:.°★* 。

原文