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 }

 

转载于:https://www.cnblogs.com/wangyiming/p/11059844.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值