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
  • 相关阅读:
    小波变换的引入,通俗易懂
    Leetcode 437. Path Sum III
    Leetcode 113. Path Sum II
    Leetcode 112 Path Sum
    Leetcode 520 Detect Capital
    Leetcode 443 String Compression
    Leetcode 38 Count and Say
    python中的生成器(generator)总结
    python的random模块及加权随机算法的python实现
    leetcode 24. Swap Nodes in Pairs(链表)
  • 原文地址:https://www.cnblogs.com/jklongint/p/4471242.html
Copyright © 2011-2022 走看看