前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线上算法赛第一次4道题AC,是啥感觉?

线上算法赛第一次4道题AC,是啥感觉?

作者头像
小码匠
发布2022-06-16 18:11:40
3490
发布2022-06-16 18:11:40
举报
文章被收录于专栏:小码匠和老码农

碎碎念

本次线上赛有很多值得纪念的

  • 最简单的A题竟然提交了2次,又是被老码农一顿奚落(⊙︿⊙);
  • 第一次30分钟内解决了A、B、D三道题,撒花✿✿ヽ(°▽°)ノ✿;
  • B、C、D三道题一次提交AC,呱唧呱唧(#^.^#)

C题其实也不难,结果一开始题就理解错了,思考的方向跑偏了,o(╥﹏╥)o 思路:map结合优先队列去做,后来发现思路行不通,所以耽误了不少时间。

期间还看了E、F两道题描述(E、F题解做了整理,以后在学习)

  • E是动态规划相关的,学艺不精,唯有继续努力,冲鸭!
  • F题赛后看题解需要用到树状数组,知识盲区,继续加油(* ̄︶ ̄)

ABC253-01

走过路过不要错过,怪事速报来了||ヽ( ̄▽ ̄)ノミ|Ю

  • 本次大赛好几位大神都在E题提交了2次,不知道为何会同时出现这种事。正常大神都是一次AC
    • 老码农嘟嘟囔囔说:你这小孩见不得别人好,离大神的功力还十万步千里呢。。。
  • tourist大神一出江湖,基本上都是第一名,得第2名都是偶尔,望尘莫及,唯有努力学习,٩(๑>◡<๑)۶

A - Median?

  • https://atcoder.jp/contests/abc253/tasks/abc253_a

小码匠题解

代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define rep(n) for(int i = 0; i < n; i++)
#define f(i, start, end) for(int i = start; i < end; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define sort_all(a) sort(a.begin, a.end)
#define fi first
#define se second
#define endl '\n'
#define out(x) cout << x << '\n'

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a, b, c;
    cin >> a >> b >> c;
    if((a + b + c) - min(a, min(b, c)) - max(a, max(b, c)) == b) {
        cout << "Yes";
    } else {
        cout << "No";
    }
    return 0;
}

参考题解:膜拜大神

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b, c;
  cin >> a >> b >> c;
  vector<int> v = {a, b, c};
  sort(v.begin(), v.end());
  cout << (b == v[1] ? "Yes" : "No") << '\n';
  return 0;
}

B - Distance Between Token

  • https://atcoder.jp/contests/abc253/tasks/abc253_b

小码匠题解

代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define rep(n) for(int i = 0; i < n; i++)
#define f(i, start, end) for(int i = start; i < end; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define sort_all(a) sort(a.begin, a.end)
#define fi first
#define se second
#define endl '\n'
#define out(x) cout << x << '\n'

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // 输入
    int n, w;
    cin >> n >> w;
    pair<int, int> a;
    string s;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        cin >> s;
        for(int j = 0; j < w; j++) {
            if(s[j] == 'o'){
                if(ans == 0) {
                    a.fi = i;
                    a.se = j;
                    ans++;
                } else {
                    cout << abs(i - a.fi) + abs(j - a.se);
                    return 0;
                }
            }
        }
    }
}

参考题解(膜拜大神)

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int xa = -1, ya = -1, xb = -1, yb = -1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'o') {
        if (xa == -1) {
          xa = i;
          ya = j;
        } else {
          xb = i;
          yb = j;
        }
      }
    }
  }
  cout << abs(xa - xb) + abs(ya - yb) << '\n';
  return 0;
}

C - Max - Min Query

  • https://atcoder.jp/contests/abc253/tasks/abc253_c

小码匠题解

代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define rep(n) for(int i = 0; i < n; i++)
#define f(i, start, end) for(int i = start; i < end; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define sort_all(a) sort(a.begin, a.end)
#define fi first
#define se second
#define endl '\n'
#define out(x) cout << x << '\n'

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long q;
    cin >> q;
    int a, b, c;
    set<int> query;
    unordered_map<int, int> temp;
    for(int i = 0; i < q; i++) {
        cin >> a;
        if(a == 1) {
            cin >> b;
            query.insert(b);
            temp[b]++;
        } else if(a == 2) {
            cin >> b >> c;
            temp[b] -= min(temp[b], c);
            if(temp[b] == 0) {
                query.erase(b);
            }
        } else if(a == 3) {
            cout << *query.rbegin() - *query.begin() << endl;
        }
    }
    return 0;
}



参考题解(膜拜大神)

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  multiset<int> s;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int x;
      cin >> x;
      s.insert(x);
    }
    if (op == 2) {
      int x, c;
      cin >> x >> c;
      while (c--) {
        auto it = s.find(x);
        if (it == s.end()) {
          break;
        }
        s.erase(it);
      }
    }
    if (op == 3) {
      cout << (*prev(s.end()) - *s.begin()) << '\n';
    }
  }
  return 0;
}

D - FizzBuzz Sum Hard

  • https://atcoder.jp/contests/abc253/tasks/abc253_d

小码匠题解

代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

#define rep(i, n) for(int i = 0; i < n; i++)
#define f(i, start, end) for(int i = start; i < end; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define sort_all(a) sort(a.begin, a.end)
#define fi first
#define se second
#define endl '\n'
#define out(x) cout << x << '\n'


int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n, a, b, c;
    cin >> n >> a >> b;
    c = a / __gcd(a, b) * b;
    long long d, e, f;
    d = n / c;
    e = n / a;
    f = n / b;
    cout << (1 + n) * n / 2 - (e * a + a) * e / 2 - (f * b + b) * f / 2 + (d * c + c) * d / 2;
    return 0;
}

参考题解(膜拜大神)

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, a, b;
  cin >> n >> a >> b;
  auto Sum = [&](long long x) {
    return x * (x + 1) / 2;
  };
  long long c = a / __gcd(a, b) * 1LL * b;
  cout << Sum(n) - Sum(n / a) * a - Sum(n / b) * b + Sum(n / c) * c << '\n'; 
  return 0;
}

E - Distance Sequence

  • https://atcoder.jp/contests/abc253/tasks/abc253_e

参考题解(官方提供)

代码语言:javascript
复制
動的計画法で解きます。

dp[i][j] を、A の先頭から i 項を決めて、i 項目が j であるような場合の数とします。この dp の遷移は

dp[i+1][j]=(dp[i][1]+…+dp[i][j−K])+(dp[i][j+K]+…+dp[i][M])

です。(なお、1>j−K のときやj+K>M のとき、K=0 のときは微妙に遷移が異なるので注意してください。)この dp では状態数が O(NM) 、遷移が O(M) となり、全体の計算量は O(NM 
2
 ) となり実行時間制限に間に合いません。

ここで、dp[i+1] への遷移を考える際に事前に dp[i] の累積和を求めておくことで、遷移が O(1) で可能になり、全体の計算量が O(NM) になります。これは十分高速です。
  • 参考代码
代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<long long>> dp(N, vector<long long>(M, 0));
  for (int i = 0; i < M; i++){
    dp[0][i] = 1;
  }
  for (int i = 0; i < N - 1; i++){
    vector<long long> S(M + 1);
    S[0] = 0;
    for (int j = 0; j < M; j++){
      S[j + 1] = S[j] + dp[i][j];
      S[j + 1] %= MOD;
    }
    for (int j = 0; j < M; j++){
      dp[i + 1][j] = S[M];
      if (K > 0){
        dp[i + 1][j] -= S[min(j + K, M)] - S[max(j - K + 1, 0)];
      }
      dp[i + 1][j] += MOD;
      dp[i + 1][j] %= MOD;
    }
  }
  long long ans = 0;
  for (int i = 0; i < M; i++){
    ans += dp[N - 1][i];
  }
  ans %= MOD;
  cout << ans << endl;
}

参考题解(膜拜大神)

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a; swap(a, m);
    u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}
 
template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;
 
  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
    value = normalize(x);
  }
 
  template <typename U>
  static Type normalize(const U& x) {
    Type v;
    if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    else v = static_cast<Type>(x % mod());
    if (v < 0) v += mod();
    return v;
  }
 
  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }
 
  Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
  Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }
 
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
    uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
    uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (mod())
    );
    value = m;
#else
    value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
    long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }
 
  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
 
  friend const Type& abs(const Modular& x) { return x.value; }
 
  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
 
  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
 
  template <typename V, typename U>
  friend V& operator>>(V& stream, Modular<U>& number);
 
 private:
  Type value;
};
 
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
 
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
 
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
 
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
 
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
 
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
 
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
 
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1) res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}
 
template <typename T>
bool IsZero(const Modular<T>& number) {
  return number() == 0;
}
 
template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}
 
// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
  return stream << number();
}
 
// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, long long>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}
 
/*
using ModType = int;
 
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
 
constexpr int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
 
/*vector<Mint> fact(1, 1);
vector<Mint> inv_fact(1, 1);
 
Mint C(int n, int k) {
  if (k < 0 || k > n) {
    return 0;
  }
  while ((int) fact.size() < n + 1) {
    fact.push_back(fact.back() * (int) fact.size());
    inv_fact.push_back(1 / fact.back());
  }
  return fact[n] * inv_fact[k] * inv_fact[n - k];
}*/
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<Mint> f(m, 1);
  for (int it = 1; it < n; it++) {
    vector<Mint> pref(m + 1);
    for (int i = 0; i < m; i++) {
      pref[i + 1] = pref[i] + f[i];
    }
    for (int i = 0; i < m; i++) {
      int L = max(0, i - k + 1);
      int R = min(m - 1, i + k - 1);
      if (k == 0) {
        f[i] = pref[m];
      } else {
        f[i] = pref[m] - (pref[R + 1] - pref[L]);
      }
    }
  }
  cout << accumulate(f.begin(), f.end(), Mint(0)) << '\n';
  return 0;
}

F - Operations on a Matrix

  • https://atcoder.jp/contests/abc253/tasks/abc253_f

参考题解(膜拜大神)

代码语言:javascript
复制
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
 
template <typename T>
class fenwick {
 public:
  vector<T> fenw;
  int n;
 
  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }
 
  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }
 
  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  vector<array<int, 4>> qs(q);
  vector<int> when(n, 0);
  vector<int> what(n, 0);
  vector<long long> res(q, -1);
  vector<vector<pair<int, int>>> ask(q);
  fenwick<long long> fenw(m);
  for (int i = 0; i < q; i++) {
    auto& p = qs[i];
    cin >> p[0] >> p[1] >> p[2];
    if (p[0] == 1) {
      cin >> p[3];
      --p[1]; --p[2];
      fenw.modify(p[1], p[3]);
      fenw.modify(p[2] + 1, -p[3]);
    } else {
      if (p[0] == 2) {
        --p[1];
        when[p[1]] = i;
        what[p[1]] = p[2];
      } else {
        --p[1]; --p[2];
        res[i] = what[p[1]] + fenw.get(p[2]);
        ask[when[p[1]]].emplace_back(p[2], i);
      }
    }
  }
  fenwick<long long> fenw2(m);
  for (int i = 0; i < q; i++) {
    for (auto& p : ask[i]) {
      res[p.second] -= fenw2.get(p.first);
    }
    auto& p = qs[i];
    if (p[0] == 1) {
      fenw2.modify(p[1], p[3]);
      fenw2.modify(p[2] + 1, -p[3]);
    }
  }
  for (int i = 0; i < q; i++) {
    if (qs[i][0] == 3) {
      cout << res[i] << '\n';
    }
  }
  return 0;
}

参考题解(官方提供)

代码语言:javascript
复制
#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
 
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> t(q), a(q), b(q), c(q);
  vector<vector<int>> subt(q);
  vector latest(n, pair(-1, 0));
  vector<long long> ans;
  for (int i = 0; i < q; ++i) {
    cin >> t[i];
    if (t[i] == 1) {
      cin >> a[i] >> b[i] >> c[i];
      a[i] -= 1;
    } else if (t[i] == 2) {
      cin >> a[i] >> b[i];
      a[i] -= 1;
      latest[a[i]] = pair(i, b[i]);
    } else {
      cin >> a[i] >> b[i];
      a[i] -= 1;
      const auto& [j, x] = latest[a[i]];
      const int id = ans.size();
      ans.emplace_back(x);
      c[i] = id;
      if (j >= 0) {
        subt[j].push_back(i);
      }
    }
  }
  atcoder::fenwick_tree<long long> fen(m + 1);
  for (int i = 0; i < q; ++i) {
    if (t[i] == 1) {
      fen.add(a[i], c[i]);
      fen.add(b[i], -c[i]);
    } else if (t[i] == 2) {
      for (const int j : subt[i]) {
        ans[c[j]] -= fen.sum(0, b[j]);
      }
    } else {
      ans[c[i]] += fen.sum(0, b[i]);
    }
  }
  for (const long long x : ans) {
    cout << x << '\n';
  }
  return 0; 
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小码匠和老码农 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 碎碎念
  • 本次线上赛有很多值得纪念的
    • 小码匠题解
      • 参考题解:膜拜大神
      • B - Distance Between Token
        • 小码匠题解
          • 参考题解(膜拜大神)
          • C - Max - Min Query
            • 小码匠题解
              • 参考题解(膜拜大神)
              • D - FizzBuzz Sum Hard
                • 小码匠题解
                  • 参考题解(膜拜大神)
                  • E - Distance Sequence
                    • 参考题解(官方提供)
                      • 参考题解(膜拜大神)
                      • F - Operations on a Matrix
                        • 参考题解(膜拜大神)
                          • 参考题解(官方提供)
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档