zoukankan      html  css  js  c++  java
  • 上浮法或漂浮法

    题意:给一张白纸,按顺序往上面贴有颜色且不透明的矩形,求最后能看到的颜色以及每种颜色的面积。

    思路:对每个矩形而言,它最后能有多少被看见,取决于它后面的矩形。所以从后往前处理,就像一个矩形从最底下上浮,碰到它上面的矩形就分成若干块,到水面上时更新答案。递归表示剩余的可见矩形,然后模拟上浮过程。

     1 struct Node {
     2     int x1, y1, x2, y2, color;
     3     void inp() {
     4         scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &color);
     5         x1 ++;
     6         y1 ++;
     7     }
     8 };
     9 Node node[1007];
    10 int n, cnt[2600];
    11 
    12 void dfs(int pos, int color, int l, int r, int u, int d) {
    13     while (pos < n && (node[pos].x2 < l || node[pos].x1 > r || node[pos].y1 > u || node[pos].y2 < d)) pos ++;
    14     if (pos == n) {
    15         cnt[color] += (r - l + 1) * (u - d + 1);
    16         return ;
    17     }
    18     if (l < node[pos].x1) { dfs(pos + 1, color, l, node[pos].x1 - 1, u, d); l = node[pos].x1; }
    19     if (r > node[pos].x2) { dfs(pos + 1, color, node[pos].x2 + 1, r, u, d); r = node[pos].x2; }
    20     if (d < node[pos].y1) { dfs(pos + 1, color, l, r, node[pos].y1 - 1, d); d = node[pos].y1; }
    21     if (u > node[pos].y2) { dfs(pos + 1, color, l, r, u, node[pos].y2 + 1); u = node[pos].y2; }
    22 }
    23 
    24 int main() {
    25     //freopen("in.txt", "r", stdin);
    26     int T, a, b;
    27     cin >> T;
    28     while (T--) {
    29         cin >> a >> b >> n;
    30         rep_up0(i, n) {
    31             node[i].inp();
    32         }
    33         mem0(cnt);
    34         rep_down0(i, n) {
    35             dfs(i + 1, node[i].color, node[i].x1, node[i].x2, node[i].y2, node[i].y1);
    36         }
    37         int sum = 0;
    38         rep_up1(i, 2500) sum += cnt[i];
    39         cnt[1] += a * b - sum;
    40         rep_up1(i, 2500) {
    41             if (cnt[i]) {
    42                 printf("%d %d
    ", i, cnt[i]);
    43             }
    44         }
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    idea常用快捷键及操作
    Ubuntu 装nexus
    ubuntu安装gitlab
    ubuntu安装jdk,maven,tomcat
    ubuntu安装gitlab-ci-runner、注册
    ubuntu开启远程shell,开启上传下载
    Ubuntu安装软件提示boot空间不足
    POJ3461 KMP简单变形输出模式串在主串出现的次数
    涨姿势stl map['a']['a']=b;
    对链表的操作(数据结构线性表算法设计练习)
  • 原文地址:https://www.cnblogs.com/jklongint/p/4471242.html
Copyright © 2011-2022 走看看