F - Clear The Matrix

分析

题目问将所有星变成点的花费,限制了行数(只有4行),就可以往状压DP上去靠了。

\(dp[i][j]\) 表示到第 \(i\) 列时状态为 \(j\) 的花费,只需要记录 16 位二进制,因为我们最多只能影响到 4 * 4 的星,那么每次都是从一个 4 * 4 的矩阵转移到一个 4 * 4 的矩阵,注意,转移时必须保证最左边列全部为 1 (即都是星号),那么最后答案就是 \(dp[n][(1 << 16) - 1]\)。

比如我们选定点 (i, j),将 3 * 3 的星变成点,那么变的就是左上角 (i, j - 2) 右下角 (i + 2, j) 的这个矩阵。

为了状态转移,我们会同时对一列的星进行变换,可能有多种方案,这个可以预处理再加些优化,最后合法的是很少的。

code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e3 + 10;
  4. string mp[4];
  5. int dp[2][1 << 16];
  6. int n, cnt, a[5], b[133], c[133];
  7. int calc(int xl, int xr, int yl, int yr) {
  8. int res = 0;
  9. for (int i = xl; i <= xr; i++) {
  10. for (int j = yl; j <= yr; j++) {
  11. res += 1 << ((i * 4) + j);
  12. }
  13. }
  14. return res;
  15. }
  16. void dfs(int x, int mx, int st, int cost) {
  17. if (x == 4) {
  18. b[cnt] = st;
  19. c[cnt++] = cost;
  20. return;
  21. }
  22. for (int i = 4; i >= 1; i--) {
  23. if (x + i > 4) continue;
  24. if (!x) {
  25. dfs(x + 1, i, st | calc(4 - i, 3, x, i - 1), cost + a[i]);
  26. } else {
  27. if (x + i > mx) {
  28. dfs(x + 1, x + i, st | calc(4 - i, 3, x, x + i - 1), cost + a[i]);
  29. } else {
  30. break;
  31. }
  32. }
  33. }
  34. dfs(x + 1, x, st, cost);
  35. }
  36. int main() {
  37. cin >> n >> a[1] >> a[2] >> a[3] >> a[4];
  38. for (int i = 0; i < 4; i++) {
  39. cin >> mp[i];
  40. }
  41. dfs(0, 0, 0, 0);
  42. memset(dp[0], 0x3f, sizeof dp[0]);
  43. dp[0][(1 << 16) - 1] = 0;
  44. int cur = 0;
  45. for (int i = 0; i < n; i++) {
  46. int cst = 0;
  47. for (int j = 0; j < 4; j++) {
  48. if (mp[j][i] == '.') {
  49. cst += 1 << (12 + j);
  50. }
  51. }
  52. cur = !cur;
  53. memset(dp[cur], 0x3f, sizeof dp[cur]);
  54. for (int j = 0; j < (1 << 12); j++) {
  55. dp[cur][j | cst] = dp[!cur][(j << 4) + 15];
  56. if (dp[!cur][(j << 4) + 15] == 0x3f3f3f3f) continue;
  57. for (int k = 0; k < cnt; k++) {
  58. dp[cur][j | cst | b[k]] = min(dp[!cur][(j << 4) + 15] + c[k], dp[cur][j | cst | b[k]]);
  59. }
  60. }
  61. }
  62. cout << dp[cur][(1 << 16) - 1] << endl;
  63. return 0;
  64. }

G. Yet Another Maxflow Problem

分析

一道“网络流”的题目。

本题主要注意最小割等于最大流,我们去构造这个解,即怎样才算最小割。注意到本题边的限制颇多,\(A_i\)-\(A_{i+1}\) 连有向边,\(B_i\)-\(B_{i+1}\) 连有向边,且从 A 到 B 连有向边,考虑 A 这部分,如果删掉边 \(A_i\)-\(A_{i+1}\) ,那么 \(A_{i+1}\) 下面所有边都无意义了,对于 B 这部分,删掉 \(B_i\)-\(B_{i+1}\),则 \(B_i\) 上面所有边都无意义了,有这样的性质后,我们枚举左边的边,用线段树维护删掉右边的边的代价的最小值(右边也有可能不删),用 multiset 维护全局最优解。

code

  1. #include <bits/stdc++.h>
  2. #define lson l, m, rt << 1
  3. #define rson m + 1, r, rt << 1 | 1
  4. using namespace std;
  5. const int N = 2e5 + 10;
  6. int a[N], b[N];
  7. vector<pair<int, int> > G[N];
  8. long long s[N << 4], lazy[N << 4];
  9. void pushDown(int rt) {
  10. lazy[rt << 1] += lazy[rt];
  11. lazy[rt << 1 | 1] += lazy[rt];
  12. s[rt << 1] += lazy[rt];
  13. s[rt << 1 | 1] += lazy[rt];
  14. lazy[rt] = 0;
  15. }
  16. void pushUp(int rt) { s[rt] = min(s[rt << 1], s[rt << 1 | 1]); }
  17. void build(int l, int r, int rt) {
  18. if (l == r)
  19. s[rt] = b[l - 1];
  20. else {
  21. int m = l + r >> 1;
  22. build(lson);
  23. build(rson);
  24. pushUp(rt);
  25. }
  26. }
  27. void update(int L, int R, int c, int l, int r, int rt) {
  28. if (l >= L && r <= R) {
  29. lazy[rt] += c;
  30. s[rt] += c;
  31. } else {
  32. pushDown(rt);
  33. int m = l + r >> 1;
  34. if (m >= L) update(L, R, c, lson);
  35. if (m < R) update(L, R, c, rson);
  36. pushUp(rt);
  37. }
  38. }
  39. long long query(int L, int R, int l, int r, int rt) {
  40. if (l >= L && r <= R)
  41. return s[rt];
  42. else {
  43. pushDown(rt);
  44. int m = l + r >> 1;
  45. long long res = (1LL << 62);
  46. if (m >= L) res = query(L, R, lson);
  47. if (m < R) res = min(res, query(L, R, rson));
  48. pushUp(rt);
  49. return res;
  50. }
  51. }
  52. multiset<long long> mset;
  53. long long cb[N];
  54. int main() {
  55. int n, m, q;
  56. cin >> n >> m >> q;
  57. for (int i = 1; i < n; i++) {
  58. scanf("%d%d", &a[i], &b[i]);
  59. }
  60. for (int i = 0; i < m; i++) {
  61. int u, v, w;
  62. scanf("%d%d%d", &u, &v, &w);
  63. G[u].push_back(pair<int, int>(v, w));
  64. }
  65. build(1, n, 1);
  66. for (int i = 1; i <= n; i++) {
  67. for (int j = 0; j < G[i].size(); j++) {
  68. update(1, G[i][j].first, G[i][j].second, 1, n, 1);
  69. }
  70. cb[i] = s[1];
  71. mset.insert(cb[i] + a[i]);
  72. }
  73. printf("%I64d\n", *mset.begin());
  74. while (q--) {
  75. int v, w;
  76. scanf("%d%d", &v, &w);
  77. mset.erase(mset.lower_bound(a[v] + cb[v]));
  78. a[v] = w;
  79. mset.insert(w + cb[v]);
  80. printf("%I64d\n", *mset.begin());
  81. }
  82. return 0;
  83. }

Educational Codeforces Round 34的更多相关文章

  1. Educational Codeforces Round 34 (Rated for Div. 2) A B C D

    Educational Codeforces Round 34 (Rated for Div. 2) A Hungry Student Problem 题目链接: http://codeforces. ...

  2. Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

    D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...

  3. Educational Codeforces Round 34 (Rated for Div. 2) C. Boxes Packing

    C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】

    D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Educational Codeforces Round 34 C. Boxes Packing【模拟/STL-map/俄罗斯套娃】

    C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Educational Codeforces Round 34 B. The Modcrab【模拟/STL】

    B. The Modcrab time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Educational Codeforces Round 34 A. Hungry Student Problem【枚举】

    A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. Educational Codeforces Round 34 (Rated for Div. 2)

    A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】

    B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character re ...

随机推荐

  1. 如何在华为云软件开发云上搭建JavaWeb,Maven项目

    本文将使用华为云软件开发云向大家演示如何搭建JavaWeb,Maven项目. 一.相关信息 1.华为云软件开发云简介 华为云软件开发云(DevCloud)是集华为近30年研发实践,前沿研发理念,先进研 ...

  2. 随聊——Python的前世今生

    1989年圣诞节前夕,山雨欲来风满楼,计算机程序设计语言界隐隐有大事要发生,果然不出所料.江湖人称龟叔(Guido von Rossum),就是这位祖籍荷兰的大能,在圣诞节百无聊赖的期间,发明了Pyt ...

  3. 【CSS3】透明度opacity与rgba()区别、光标cursor、display、轮廓outline与margin及border区别、em和rem区别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 【ANT】运行JMeter用例的build.xml

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  5. Python学习日记:day2

    1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...

  6. 记一次生产环境Nginx日志骤增的问题排查过程

    摘要:众所周知,Nginx是目前最流行的Web Server之一,也广泛应用于负载均衡.反向代理等服务,但使用过程中可能因为对Nginx工作原理.变量含义理解错误,或是参数配置不当导致Nginx工作异 ...

  7. bzoj 2337: [HNOI2011]XOR和路径

    Description Input Output Sample Input Sample Output HINT Source Day2 终于把这个史前遗留的坑给填了... 首先异或的话由位无关性,可 ...

  8. elasticsearch 源码本地环境搭建

    elasticsearch6.0.0 源码本地环境搭建步骤如下: 1.资源准备 ElasicSearch版本:6.0.0: https://github.com/elastic/elasticsear ...

  9. 1.1 About Percona XtraDB Cluster

    摘要: 出处:kelvin19840813 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎 ...

  10. oracle 处理时间和金额大小写的相关函数集合

    CREATE OR REPLACE FUNCTION MONEY_TO_CHINESE(MONEY IN VARCHAR2) RETURN VARCHAR2 IS C_MONEY ); M_STRIN ...