• AtCoder Beginner Contest 231(D-F,H)


    D - Neighbors (atcoder.jp)

            (1)题意

                    给出M组关系,问是否有一个排列,能表示A[i]和B[i]相邻

            (2)思路

                    考虑如果有环,显然不能满足排列,因为排列中度数最多为2,若有超过2的显然也不行。因此用并查集维护一下即可。        

            (3)代码

    1. #include
    2. #define rep(i,z,n) for(int i = z;i <= n; i++)
    3. #define per(i,n,z) for(int i = n;i >= z; i--)
    4. #define PII pair
    5. #define fi first
    6. #define se second
    7. #define vi vector
    8. #define vl vector
    9. #define pb push_back
    10. #define sz(x) (int)x.size()
    11. #define all(x) (x).begin(),(x).end()
    12. using namespace std;
    13. using ll = long long;
    14. const int N = 2e5 + 10;
    15. struct DSU {
    16. vector<int> f,siz;
    17. int n;
    18. DSU(int _n) {
    19. n = _n;
    20. f.resize(n + 1);
    21. siz.resize(n + 1,1);
    22. iota(f.begin(),f.end(),0);
    23. }
    24. inline int find(int x) {
    25. if(x == f[x]) return x;
    26. return f[x] = find(f[x]);
    27. }
    28. inline bool same(int x,int y) {
    29. x = find(x),y = find(y);
    30. return x == y;
    31. }
    32. inline void merge(int x,int y) {
    33. if(same(x,y)) return ;
    34. x = find(x),y = find(y);
    35. siz[y] += siz[x];
    36. f[x] = y;
    37. }
    38. //目前连通块个数
    39. inline int connect() {
    40. int res = 0;
    41. for(int i = 1;i <= n;i ++) {
    42. res += (i == find(i));
    43. }
    44. return res;
    45. }
    46. //求某一个联通块得大小
    47. inline int count(int x) {
    48. x = find(x);
    49. return siz[x];
    50. }
    51. };
    52. int deg[N];
    53. void solve()
    54. {
    55. int n,m;
    56. cin >> n >> m;
    57. DSU dsu(n);
    58. bool cycle = false;
    59. rep(i,1,m) {
    60. int u,v;
    61. cin >> u >> v;
    62. if(dsu.same(u,v)) {
    63. cycle = true;
    64. }
    65. else {
    66. dsu.merge(u,v);
    67. deg[u] ++,deg[v] ++;
    68. }
    69. }
    70. if(cycle) {
    71. cout << "No" << '\n';
    72. return;
    73. }
    74. rep(i,1,n) {
    75. if(deg[i] > 2) {
    76. cout << "No" << '\n';
    77. return;
    78. }
    79. }
    80. cout << "Yes" << '\n';
    81. }
    82. int main()
    83. {
    84. ios::sync_with_stdio(false);
    85. cin.tie(0),cout.tie(0);
    86. int T = 1;
    87. // cin >> T;
    88. while(T --) solve();
    89. return 0;
    90. }

    E - Minimal payments (atcoder.jp)

            (1)题意

                   阿特科德王国使用的硬币有N种: A1​日元、A2​日元、……、AN​日元硬币。
    这里,1=A1​<…

                  如果只用这些硬币支付一件价值X日元的商品,那么支付时使用的硬币和作为零钱退回的硬币的最少总数是多少?

                  当Y是一个至少为X的整数时,求正好表示Y日元所需的硬币数与正好表示Y−X日元所需的硬币数的最小和。

            (2)思路

                    对于每一种支付,我们有两种决策,要么就是支付到最多的,然后剩下的用小币去凑,要么就是你多支付一张,小的就凑你多出来的那部分,因此直接记忆化dp即可。

            (3)代码

    1. #include
    2. #define rep(i,z,n) for(int i = z;i <= n; i++)
    3. #define per(i,n,z) for(int i = n;i >= z; i--)
    4. #define PII pair
    5. #define fi first
    6. #define se second
    7. #define vi vector
    8. #define vl vector
    9. #define pb push_back
    10. #define sz(x) (int)x.size()
    11. #define all(x) (x).begin(),(x).end()
    12. using namespace std;
    13. using ll = long long;
    14. const int N = 2e5 + 10;
    15. ll a[N];
    16. map> dp;
    17. const ll inf = 9e18;
    18. inline ll dfs(ll X,int f)
    19. {
    20. if(f == 0 && X) return inf;
    21. if(dp[X].count(f)) return dp[X][f];
    22. ll r = X % a[f],p = X / a[f];
    23. if(!r) return p;
    24. return dp[X][f] = min(dfs(r,f - 1) + p,dfs(a[f] - r,f - 1) + p + 1);
    25. }
    26. void solve()
    27. {
    28. int n;
    29. ll X;
    30. cin >> n >> X;
    31. rep(i,1,n) cin >> a[i];
    32. cout << dfs(X,n);
    33. }
    34. int main()
    35. {
    36. ios::sync_with_stdio(false);
    37. cin.tie(0),cout.tie(0);
    38. int T = 1;
    39. // cin >> T;
    40. while(T --) solve();
    41. return 0;
    42. }

    F - Jealous Two (atcoder.jp)

            (1)题意

                    有一个长度为N的A数组,A[i]代表A对i这件物品的好感度,有一个长度为N的B数组,B[i]代表B对i这件物品的好感度,现在让你求有多少对[i,j]满足A[i] >= A[j]并且B[i] <= B[j]。

            (2)思路

                    很明显这是一个二维偏序问题,我们直接sort+树状数组秒了(特殊处理一下重复的就行)。

            (3)代码

    1. #include
    2. #define rep(i,z,n) for(int i = z;i <= n; i++)
    3. #define per(i,n,z) for(int i = n;i >= z; i--)
    4. #define PII pair
    5. #define fi first
    6. #define se second
    7. #define vi vector
    8. #define vl vector
    9. #define pb push_back
    10. #define sz(x) (int)x.size()
    11. #define all(x) (x).begin(),(x).end()
    12. using namespace std;
    13. using ll = long long;
    14. const int N = 4e5 + 10;
    15. vector<int> ver;
    16. int a[N],b[N];
    17. int get(int x)
    18. {
    19. return lower_bound(all(ver),x) - ver.begin() + 1;
    20. }
    21. PII z[N];
    22. template <typename T>
    23. struct Fenwick {
    24. const int n;
    25. std::vector a;
    26. Fenwick (int n) : n(n), a(n + 1) {}
    27. void clear() {
    28. for(int i = 1;i <= n;i ++) {
    29. a[i] = 0;
    30. }
    31. }
    32. void add(int pos, T x) {
    33. for (int i = pos; i <= n; i += i & -i) {
    34. a[i] += x;
    35. }
    36. }
    37. T query(int x) {
    38. T res = 0;
    39. for (int i = x; i; i -= i & -i) {
    40. res += a[i];
    41. }
    42. return res;
    43. }
    44. T query(int l, int r) {
    45. if (l == 0 || l > r) {
    46. return 0;
    47. }
    48. return query(r) - query(l - 1);
    49. }
    50. //找到大于k得第一个地方
    51. T kth(int k) {
    52. int pos = 0;
    53. for(int j = 31 - __builtin_clz(n);j >= 0;j --) {
    54. if(pos + (1 << j) <= n && k > a[pos + (1 << j)]) {
    55. pos += 1 << j;
    56. k -= a[pos];
    57. }
    58. }
    59. return pos + 1;
    60. }
    61. };
    62. //使用Fenwick fen(n)
    63. void solve()
    64. {
    65. int n;
    66. cin >> n;
    67. rep(i,1,n) {
    68. cin >> a[i];
    69. ver.pb(a[i]);
    70. }
    71. rep(i,1,n) {
    72. cin >> b[i];
    73. ver.pb(b[i]);
    74. }
    75. sort(all(ver));
    76. ll Ans = 0;
    77. mapint,int>,int> cnt;
    78. rep(i,1,n) {
    79. a[i] = get(a[i]);
    80. b[i] = get(b[i]);
    81. z[i] = {a[i],-b[i]};
    82. cnt[z[i]] ++;
    83. }
    84. for(auto [x,y]: cnt) Ans += 1ll * y * (y - 1) / 2;
    85. sort(z + 1,z + 1 + n);
    86. Fenwick<int> fen(400000);
    87. rep(i,1,n) {
    88. fen.add(-z[i].se,1);
    89. Ans += i - fen.query(-z[i].se - 1);
    90. }
    91. cout << Ans;
    92. }
    93. int main()
    94. {
    95. ios::sync_with_stdio(false);
    96. cin.tie(0),cout.tie(0);
    97. int T = 1;
    98. // cin >> T;
    99. while(T --) solve();
    100. return 0;
    101. }

    H - Minimum Coloring (atcoder.jp)

            (1)题意

                  我们有一个行数为H,列数为W的网格。让 (i,j)表示从上往下第i行和从左往下第j列的正方形。在这个网格上有N个白色棋子,编号为1到N。棋子i在(Ai​,Bi​)上。你可以支付Ci​的费用将棋子i变成黑棋,求每行每列至少有一颗黑子所需的最小总费用。

            (2)思路

                    很显然的一个列拆点,把行向列连边的费用流,不过这题特殊的是必须行列都有,我们考虑如下建图。

                    把每一行当作一个节点,从S->i流M的流量,从i->P流m-1的流量,从p向每一列流n-1的流量,从列向t流n的流量,最后给点的边的[u,v]的价值w  相当于从u->(v + n)流1的流量费用为w,最后跑费用流即可。因为题目保证会出现每行每列至少出现一个,因此一定会把n*m的流量流满。

            (3)代码

    1. #include
    2. #define rep(i, a, b) for (int i = a; i < (b); ++i)
    3. #define all(x) begin(x), end(x)
    4. #define sz(x) (int)(x).size()
    5. #define uniq(x) x.resize(unique(all(x)) - x.begin())
    6. #define ff first
    7. #define ss second
    8. #define pb push_back
    9. #define emb emplace_back
    10. using namespace std;
    11. using ull = unsigned long long;
    12. using ll = long long;
    13. using pii = pair<int, int>;
    14. using vi = vector<int>;
    15. template <typename T>
    16. using min_heap = priority_queue, greater>;
    17. #define RN 100005
    18. struct SimplexMinCostMaxFlow {
    19. using Flow = ll;
    20. using Cost = ll;
    21. struct Network {
    22. int nxt, to;
    23. Flow cap;
    24. Cost cost;
    25. };
    26. Network net_pool[RN * 2];
    27. int ncnt = 1;
    28. #define nnode(x) net_pool[x]
    29. #define nnxt(x) nnode(x).nxt
    30. #define nto(x) nnode(x).to
    31. #define ncap(x) nnode(x).cap
    32. #define ncost(x) nnode(x).cost
    33. int head[RN], fa[RN], fe[RN], pi[RN], mark[RN], cyc[RN], ti;
    34. inline void addEdge(int u, int v, Flow cap, Cost cost) {
    35. nnode(++ncnt) = (Network){head[u], v, cap, cost};
    36. head[u] = ncnt;
    37. nnode(++ncnt) = (Network){head[v], u, 0, -cost};
    38. head[v] = ncnt;
    39. }
    40. void initTree(int x) {
    41. mark[x] = 1;
    42. for (int i = head[x]; i; i = nnxt(i)) {
    43. int v = nto(i);
    44. if (!mark[v] && ncap(i)) {
    45. fa[v] = x, fe[v] = i;
    46. initTree(v);
    47. }
    48. }
    49. }
    50. int phi(int x) {
    51. if (mark[x] == ti) return pi[x];
    52. return mark[x] = ti, pi[x] = phi(fa[x]) - ncost(fe[x]);
    53. }
    54. void pushFlow(int e, Cost &cost) {
    55. int pen = nto(e ^ 1), lca = nto(e);
    56. ti++;
    57. while (pen) mark[pen] = ti, pen = fa[pen];
    58. while (mark[lca] != ti) mark[lca] = ti, lca = fa[lca];
    59. int e2 = 0, f = ncap(e), path = 2, clen = 0;
    60. for (int i = nto(e ^ 1); i != lca; i = fa[i]) {
    61. cyc[++clen] = fe[i];
    62. if (ncap(fe[i]) < f) f = ncap(fe[e2 = i] ^ (path = 0));
    63. }
    64. for (int i = nto(e); i != lca; i = fa[i]) {
    65. cyc[++clen] = fe[i] ^ 1;
    66. if (ncap(fe[i] ^ 1) <= f) f = ncap(fe[e2 = i] ^ (path = 1));
    67. }
    68. cyc[++clen] = e;
    69. for (int i = 1; i <= clen; i++) {
    70. ncap(cyc[i]) -= f, ncap(cyc[i] ^ 1) += f;
    71. cost += 1ll * ncost(cyc[i]) * f;
    72. }
    73. if (path == 2) return;
    74. int laste = e ^ path, last = nto(laste), cur = nto(laste ^ 1);
    75. while (last != e2) {
    76. mark[cur]--;
    77. laste ^= 1;
    78. swap(laste, fe[cur]);
    79. swap(last, fa[cur]);
    80. swap(last, cur);
    81. }
    82. }
    83. pair maxflow(int s, int t) {
    84. int lhead = head[s], lhead2 = head[t];
    85. addEdge(t, s, 0x7fffffffff, -0x7f7f7f7f);
    86. initTree(t);
    87. mark[t] = ti = 2, fa[t] = 0;
    88. Cost cost = 0;
    89. for (int i = 2, pre = ncnt; i != pre; i = i == ncnt ? 2 : i + 1) {
    90. if (ncap(i) && ncost(i) < phi(nto(i ^ 1)) - phi(nto(i)))
    91. pushFlow(pre = i, cost);
    92. }
    93. head[s] = lhead, head[t] = lhead2, ncnt -= 2;
    94. Flow flow = ncap(ncnt + 2);
    95. cost += 1ll * flow * 0x7f7f7f7f;
    96. return {flow, cost};
    97. }
    98. };
    99. SimplexMinCostMaxFlow flow;
    100. int main() {
    101. ios::sync_with_stdio(false);
    102. cin.tie(0),cout.tie(0);
    103. int n, m, q;
    104. int s, t, p;
    105. cin >> n >> m >> q;
    106. s = n + m + 1,p = n + m + 2,t = n + m + 3;
    107. for(int i = 1;i <= n;i ++) {
    108. flow.addEdge(s,i,m,0);
    109. flow.addEdge(i,p,m - 1,0);
    110. }
    111. for(int i = 1;i <= m;i ++) {
    112. flow.addEdge(i + n,t,n,0);
    113. flow.addEdge(p,i + n,n - 1,0);
    114. }
    115. for(int i = 1;i <= q;i ++) {
    116. int x,y,z;
    117. cin >> x >> y >> z;
    118. flow.addEdge(x,y + n,1,z);
    119. }
    120. cout << flow.maxflow(s,t).second;
    121. // cout << ans.second << '\n';
    122. return 0;
    123. }

  • 相关阅读:
    Facebook 广告投放中有哪些不允许的做法
    hyperf的启动源码分析(二)——请求如何到达控制器
    一个重构:开闭原则案例
    【Jmeter】如何愉快的玩耍JSR223 Assertion
    Linux手动更新时间Linux同步集群其他节点时间
    Qt 关于QT_BEGIN_NAMESPACE宏的作用
    [附源码]java毕业设计班级风采网站
    Ubuntu20.04环境下Baxter机器人开发环境搭建
    华为云MVP马超:云原生时代开发者的挑战与出路
    RichView TRVStyle ListStyle 列表样式(项目符号编号)
  • 原文地址:https://blog.csdn.net/scanner___yw/article/details/133586373