zoukankan      html  css  js  c++  java
  • kickstart2019 round_C B. Circuit Board

    思路:

    这题应该不止一种解法,其中的一种可以看作是leetcode85https://www.cnblogs.com/wangyiming/p/11059176.html的加强版:

    首先对于每一行,分别使用滑动窗口法将这一行划分成符合题目定义的若干合法子段s1, s2, s3, ...。对于每一段si,从起点到终点分别将每个元素分别编号为1, 2, 3, ..., |si|。

    例如:

    2 2 4 4 20
    8 3 3 3 12
    6 6 3 3 3
    1 6 8 6 4

    可以转化成

    1 2 1 2 1
    1 1 2 3 1
    1 2 1 2 3
    1 1 1 1 1

    接下来就可以将此矩阵转置,使用leetcode85的思路进行计算了:

    1 2 1 2 1
    1 1 2 3 1
    1 2 1 2 3
    1 1 1 1 1

    实现:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N = 305;
     4 int a[N][N], b[N][N];
     5 int main()
     6 {
     7     int T, r, c, k;
     8     cin >> T;
     9     for (int t = 1; t <= T; t++)
    10     {
    11         memset(b, 0x3f, sizeof b);
    12         cin >> r >> c >> k;
    13         for (int i = 0; i < r; i++)
    14             for (int j = 0; j < c; j++)
    15                 cin >> a[i][j];
    16         for (int i = 0; i < r; i++)
    17         {
    18             map<int, int> mp;
    19             int f = 0, s = 0;
    20             while (f < c)
    21             {
    22                 while (f < c)
    23                 {
    24                     if (!mp.count(a[i][f])) mp[a[i][f]] = 0;
    25                     mp[a[i][f]]++;
    26                     if (mp.rbegin()->first - mp.begin()->first > k) break;
    27                     b[i][f] = f - s + 1;
    28                     f++;
    29                 }
    30                 while (s < f && mp.rbegin()->first - mp.begin()->first > k)
    31                 {
    32                     if (mp[a[i][s]] == 1) mp.erase(a[i][s]);
    33                     else mp[a[i][s]]--;
    34                     s++;
    35                 }
    36                 b[i][f] = f - s + 1;
    37                 f++;
    38             }
    39         }
    40         int ans = 0;
    41         for (int j = 0; j < c; j++)
    42         {
    43             stack<pair<int, int>> sta;
    44             vector<int> pre;
    45             for (int i = 0; i < r; i++)
    46             {
    47                 while (sta.size() && sta.top().first >= b[i][j]) sta.pop();
    48                 if (!sta.empty()) pre.push_back(sta.top().second + 1);
    49                 else pre.push_back(0);
    50                 sta.push(make_pair(b[i][j], i)); 
    51             }
    52             while (sta.size()) sta.pop();
    53             for (int i = r - 1; i >= 0; i--)
    54             {
    55                 while (sta.size() && sta.top().first >= b[i][j]) sta.pop();
    56                 if (!sta.empty()) ans = max(ans, (sta.top().second - pre[i]) * b[i][j]);
    57                 else ans = max(ans, b[i][j] * (r - pre[i]));
    58                 sta.push(make_pair(b[i][j], i)); 
    59             }
    60         }
    61         cout << "Case #" << t << ": " << ans << endl;
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    Hadoop集群安装
    Oracle ORA01555(快照过旧)
    selenium+java+testng+ant环境搭建
    selenium ssl
    watir识别IE版本号
    ruby and watir中timeout类的用法
    使用TestNGxslt
    ruby system用法
    IE6下调用inetcpl.cpl清除COOKIE的方法
    Shell实例:字符串操作 逻辑判断
  • 原文地址:https://www.cnblogs.com/wangyiming/p/11059844.html
Copyright © 2011-2022 走看看