2024牛客暑期多校訓練營1

Luckyblock發表於2024-07-17

目錄
  • 寫在前面
  • H
  • C
  • A
  • I
  • D
  • B
  • 寫在最後

寫在前面

比賽地址:https://ac.nowcoder.com/acm/contest/81596

以下按個人向難度排序。

4h 調一題而且沒調出來之我是超級紅溫怪

賽後換了個寫法 30min 重構一發改了改過了我草,好相似。

單刷卡題太難頂!

H

經典閱讀理解簽到。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
const int kN = 1e5 + 10;
//=============================================================
int n, m;
struct group {
  std::string name;
  int p, t;
} a[kN], b[kN];
std::map <std::string, int> vis;
std::map <std::string, int> rank1, rank2;
//=============================================================
bool cmp(group fir_, group sec_) {
  if (fir_.p != sec_.p) return fir_.p > sec_.p;
  return fir_.t < sec_.t;
}
//=============================================================
int main() {
  // freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n;

  for (int i = 1; i <= n; ++ i) {
    std::cin >> a[i].name >> a[i].p >> a[i].t;
    vis[a[i].name] = vis[a[i].name] + 1;
  }
  std::sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; ++ i) rank1[a[i].name] = i;

  std::cin >> m;
  for (int i = 1; i <= m; ++ i) {
    std::cin >> b[i].name >> b[i].p >> b[i].t;
    vis[b[i].name] = vis[b[i].name] + 1;
  }
  std::sort(b + 1, b + m + 1, cmp);
  for (int i = 1; i <= m; ++ i) rank2[b[i].name] = i;

  int rk1 = rank1["lzr010506"], rk2 = rank2["lzr010506"];
  int ans1 = rk1, ans2 = rk2;
  for (int i = 1; i < rk1; ++ i) {
    if (vis[a[i].name] == 2) -- ans1;
  }
  for (int i = 1; i < rk2; ++ i) {
    if (vis[b[i].name] == 2) -- ans2;
  }

  std::cout << std::min(ans1, ans2) << "\n";
  return 0;
}

C

簽到。

模擬即可,增刪權值 \(v_i\) 的影響即 \(\plusmn i\times v_i\)

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
const LL p = 1e9 + 7;
//=============================================================
//=============================================================
//=============================================================
int main() {
  //freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int q; std::cin >> q;

  std::stack<int> st;
  LL s = 0;
  while (q --) {
    int t, v; std::cin >> t >> v;
    for (int i = 1; i <= t; ++ i) {
      s = (s - (1ll * st.top() * st.size()) % p + p) % p;
      st.pop();
    }
    st.push(v);
    s = (s + 1ll * st.size() * v % p) % p;
    std::cout << s << "\n";
  }
  return 0;
}

A

組合。

dztle 大神想的,看懂了再補。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
const int kN = 5020;
//=============================================================
LL n, m, q, c[kN][kN];
//=============================================================
LL qpow(LL x_, LL y_) {
  LL ret = 1ll;
  while (y_) {
    if (y_ & 1) ret = ret * x_ %q;
    x_ = x_ * x_ % q, y_ >>= 1ll;
  }
  return ret;
}
//=============================================================
int main() {
  //freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m >> q;

  c[0][0] = 1;
  for (int i = 1; i <= n; ++ i) {
    c[i][0] = c[i][i] = 1;
    for (int j = 1; j < i; ++ j) {
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % q;
    }
  }

  LL ans = 0;
  for (int i = 1; i <= n; ++ i) {
    LL v = c[n][i];
    v = c[n][i] * qpow(2, (m - 1) * (n - i)) % q;
    v = v * qpow(qpow(2, i) - 1, m - 1) % q;
    ans = (ans + v) % q;
  }
  std::cout << ans << "\n";
  return 0;
}

I

模擬,圖論(?

沒想好就直接開寫導致紅溫 4h 我是超級大傻逼。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
#define pr std::pair
#define mp std::make_pair
#define piii std::pair<int,std::pair<int,int> >
const int kN = 1e6 + 10;
//=============================================================
int n, m;
std::string map[kN];
int ans[kN][4], tag[kN][4];
std::set<int> mirrors;
std::map<std::string, int> direction;
//=============================================================
piii mp3(int x_, int y_, int z_) {
  return mp(x_, mp(y_, z_));
}
int oppo(int dir_) {
  if (dir_ == 0) return 1;
  if (dir_ == 1) return 0;
  if (dir_ == 2) return 3;
  return 2;
}
int id(int x_, int y_) {
  return m * x_  + y_;
}
void dfs1(int x_, int y_, int dir_) {
  if (x_ < 0 || x_ >= n || y_ < 0 || y_ >= m) return ;

  ans[id(x_, y_)][oppo(dir_)] = mirrors.size();

  int nx, ny, now_reflected = 1, ndir;

  if (map[x_][y_] == '|') {
    if (dir_ == 0) nx = x_ - 1, ny = y_, ndir = 0, now_reflected = 0;
    else if (dir_ == 1) nx = x_ + 1, ny = y_, ndir = 1, now_reflected = 0;
    else if (dir_ == 2) nx = x_, ny = y_ + 1, ndir = 3;
    else if (dir_ == 3) nx = x_, ny = y_ - 1, ndir = 2;
  } else if (map[x_][y_] == '-') {
    if (dir_ == 0) nx = x_ + 1, ny = y_, ndir = 1;
    else if (dir_ == 1) nx = x_ - 1, ny = y_, ndir = 0;
    else if (dir_ == 2) nx = x_, ny = y_ - 1, ndir = 2, now_reflected = 0;
    else if (dir_ == 3) nx = x_, ny = y_ + 1, ndir = 3, now_reflected = 0;
  } else if (map[x_][y_] == '/') {
    if (dir_ == 0) nx = x_, ny = y_ + 1, ndir = 3;
    else if (dir_ == 1) nx = x_, ny = y_ - 1, ndir = 2;
    else if (dir_ == 2) nx = x_ + 1, ny = y_, ndir = 1;
    else if (dir_ == 3) nx = x_ - 1, ny = y_, ndir = 0;
  } else if (map[x_][y_] == '\\') {
    if (dir_ == 0) nx = x_, ny = y_ - 1, ndir = 2;
    else if (dir_ == 1) nx = x_, ny = y_ + 1, ndir = 3;
    else if (dir_ == 2) nx = x_ - 1, ny = y_, ndir = 0;
    else if (dir_ == 3) nx = x_ + 1, ny = y_, ndir = 1;
  }
  if (now_reflected && !mirrors.count(id(x_, y_))) {
    mirrors.insert(id(x_, y_));
  }
  dfs1(nx, ny, ndir);
}
void solve1(int x_, int y_, int dir_) {
  mirrors.clear();
  dfs1(x_, y_, dir_);
}

void dfs2(int x_, int y_, int dir_, int start_, int reflected_) {
  if (x_ < 0 || x_ >= n || y_ < 0 || y_ >= m) exit(0);

  int nx, ny, now_reflected = 1, ndir;
  if (dir_ == 0) nx = x_ - 1, ny = y_;
  if (dir_ == 1) nx = x_ + 1, ny = y_;
  if (dir_ == 2) nx = x_, ny = y_ - 1;
  if (dir_ == 3) nx = x_, ny = y_ + 1;
  if (0 <= nx && nx < n && 0 <= ny && ny < m) {
    if (map[nx][ny] == '|') {
      if (dir_ == 0) ndir = 0, now_reflected = 0;
      else if (dir_ == 1) ndir = 1, now_reflected = 0;
      else if (dir_ == 2) ndir = 3;
      else if (dir_ == 3) ndir = 2;
    } else if (map[nx][ny] == '-') {
      if (dir_ == 0) ndir = 1;
      else if (dir_ == 1) ndir = 0;
      else if (dir_ == 2) ndir = 2, now_reflected = 0;
      else if (dir_ == 3) ndir = 3, now_reflected = 0;
    } else if (map[nx][ny] == '/') {
      if (dir_ == 0) ndir = 3;
      else if (dir_ == 1) ndir = 2;
      else if (dir_ == 2) ndir = 1;
      else if (dir_ == 3) ndir = 0;
    } else if (map[nx][ny] == '\\') {
      if (dir_ == 0) ndir = 2;
      else if (dir_ == 1) ndir = 3;
      else if (dir_ == 2) ndir = 0;
      else if (dir_ == 3) ndir = 1;
    }
  }
  if (reflected_ && !mirrors.count(id(x_, y_))) {
    mirrors.insert(id(x_, y_));
  }
  if (tag[id(x_, y_)][dir_] != 0) {
    ans[id(x_, y_)][dir_] = mirrors.size();
    return ;
  }
  if (!start_) tag[id(x_, y_)][dir_] = 1;

  dfs2(nx, ny, ndir, 0, now_reflected);
  ans[id(x_, y_)][dir_] = mirrors.size();
}
void solve2(int x_, int y_, int dir_) {
  mirrors.clear();
  dfs2(x_, y_, dir_, 1, 0);
}
void init() {
  for (int i = 0; i < n; ++ i) solve1(i, 0, 3);
  for (int i = 0; i < n; ++ i) solve1(i, m - 1, 2);
  for (int j = 0; j < m; ++ j) solve1(0, j, 1);
  for (int j = 0; j < m; ++ j) solve1(n - 1, j, 0);

  for (int i = 0; i < n; ++ i) {
    for (int j = 0; j < m; ++ j) {
      for (int k = 0; k < 4; ++ k) {
        if (ans[id(i, j)][k] != -1) continue;
        solve2(i, j, k);
      }
    }
  }
}
//=============================================================
int main() {
  // freopen("1.txt", "r", stdin);
  direction["above"] = 0;
  direction["below"] = 1;
  direction["left"] = 2;
  direction["right"] = 3;

  std::ios::sync_with_stdio(0), std::cin.tie(0);

  // dfs2(-1, -1, -1, -1, -1);

  std::cin >> n >> m;
  for (int i = 0; i < n; ++ i) std::cin >> map[i];
  for (int i = 0; i < n; ++ i) {
    for (int j = 0; j < m; ++ j) {
      for (int k = 0; k < 4; ++ k) {
        ans[id(i, j)][k] = -1;
      }
    }
  }
  init();

  int q; std::cin >> q;
  while (q --) {
    int x, y; std::string dir; std::cin >> x >> y >> dir;
    std::cout << ans[id(x - 1, y - 1)][direction[dir]] << "\n";
  }
  return 0;
}
/*
2 3
/-\
--/
2
1 2 right
2 1 right

3 4
/-\\
|/\|
\-/|
3
1 1 right
1 2 right
2 2 below

3 4
/-\\
|/\|
\-/|
3
1 1 right
1 2 right
2 2 below

3 4
/-\\
|/\|
\-/|
1
1 1 right

3 3
/\-
|||
\/|
1
1 1 right

3 3
--\
|||
--/
1
1 1 right

4 5
/---\
|/-\|
|\-/|
\---/
1
2 2 right
*/

D

二進位制,資料結構。

//
/*
By:Luckyblock
*/
#include <bits/stdc++.h>
#define LL long long
#define low(x) ((x)&(-x))
const int kN = 5e5 + 10;
const int kM = 2097152 + 10;
//=============================================================
LL sum[kN];
//=============================================================
struct Bit {
  int lim, t[kM];
  void init(int lim_) {
    lim = lim_;
  }
  void insert(int pos_, int val_) {
    pos_ = pos_ + 1;
    for (int i = pos_; i <= lim; i += low(i)) t[i] += val_;
  }
  int sum(int pos_) {
    pos_ = pos_ + 1;

    int ret = 0;
    for (int i = pos_; i; i-= low(i)) ret += t[i];
    return ret;
  }
  int query(int l_, int r_) {
    return sum(r_) - sum(l_ - 1);
  }
} bit[21];
void modify(LL pos_, int val_) {
  for (int i = 0; i <= 20; ++ i) {
    int j = (1 << (i + 1));
    bit[i].insert((j - pos_ % j) % j, val_);
  }
}
//=============================================================
int main() {
  // freopen("1.txt", "r", stdin);
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int q; std::cin >> q;
  for (int i = 0; i <= 20; ++ i) bit[i].init((1 << (i + 1)) + 1);

  int n = 0;
  while (q --) {
    int t, v; std::cin >> t >> v;
    for (int i = 1; i <= t; ++ i) modify(sum[n - 1], -1), -- n;
    sum[n + 1] = sum[n] + v, ++ n;
    modify(sum[n - 1], 1);

    int ans = 0;
    for (int i = 0; i <= 20; ++ i) {
      int j = (1 << (i + 1)), l = (1 << i), r = j - 1, cnt = 0;
      l = (l - sum[n] % j + j) % j, r = (r - sum[n] % j + j) % j;
      
      if (l <= r) cnt = bit[i].query(l, r);
      else cnt = bit[i].sum(r) + bit[i].query(l, j - 1);
      if (cnt % 2 == 1) ans |= (1 << i);
    }
    std::cout << ans << "\n";
  }
  return 0;
}

B

組合,容斥。

寫在最後

學到了什麼:

  • I:想好在寫!
  • D:字尾字首和均可相互轉化。

參考:

  • https://www.cnblogs.com/cjjsb/p/18306107

相關文章