week5作业题 A最大矩形
问题描述思路:
用单调栈来找到能延伸的最左边和最右边的距离即可,分别从左端和右端进行两次遍历即可找到最左和最右的延伸距离,因为单调栈遇到比自己小的会弹出,即只有高度比当前柱形的高度高才可以进栈,也满足我们的条件,比当前柱形低则会弹栈。最后一步就是找到最大的矩形面积,进行遍历两两矩形比较即可找到最大的矩形面积。
#include <iostream>
#include<stack>
#include<algorithm>
using namespace std;
void history()
{
long long n = 0, result = 0;
while (cin >> n && n != 0)
{
result = 0;
long long* A = new long long[100010], * S = new long long[100010], * R = new long long[100010], * L = new long long[100010];
for (int i = 1; i <= n; i++)
{
cin >> A[i];
}
A[0] = -1, A[n + 1] = -1;
stack<pair<long long, long long> >q;
for (int i = 1; i <= n + 1; i++)//从左往右寻找
{
if (q.empty() || A[i] >= q.top().first)
{
pair<long long, long long > a;
a.first = A[i];
a.second = i;
q.push(a);
}
else if (A[i] < q.top().first)
{
while (!q.empty() && A[i] < q.top().first)
{
pair<long long, long long>a;
a.first = q.top().first;
a.second = q.top().second;
R[a.second] = i - a.second;//直接记录多长便于计算
q.pop();
}
pair<long long, long long > a;
a.first = A[i];
a.second = i;
q.push(a);
}
}
stack<pair<long long, long long> >p;
for (int i = n; i >= 0; i--)//从右往左寻找
{
if (p.empty() || A[i] >=p.top().first)
{
pair<long long, long long > a;
a.first = A[i];
a.second = i;
p.push(a);
}
else if (A[i] < p.top().first)
{
while (!p.empty() && A[i] < p.top().first)
{
pair<long long, long long>a;
a.first = p.top().first;
a.second = p.top().second;
L[a.second] = a.second - i;
p.pop();
}
pair<long long, long long> a;
a.first = A[i];
a.second = i;
p.push(a);
}
}
for (int i = 1; i <= n; i++)
{
result = max(result, (R[i] + L[i] - 1) * A[i]);//对结果进行比对找到最大值
}
cout << result << endl;
}
}
int main(int argc, char** argv)
{
history();
return 0;
}
B TT’s Magic Cat
问题描述:**思路:**这道题就是这个嗯,英文很不友好(对我这个英文渣渣来说),但是题不难,一会就搞定了,就是简单的差分法的应用。
#include<iostream>
#include<queue>
using namespace std;
void history()
{
long long n, q,l,r,c;
cin >> n >> q;
long long* A = new long long[n + 5], * B = new long long[n + 5], result = 0;
for (int i = 0; i < n; i++)
{
cin >> A[i];
if (i == 0)
{
B[0] = A[0];
}
else
{
B[i] = A[i] - A[i - 1];
}
}
for (int i = 0; i < q; i++)
{
cin >> l >> r >> c;
B[l - 1] = B[l - 1] + c;
B[r] = B[r] - c;
}
for (int i = 0; i < n; i++)
{
result = result + B[i];
cout << result << " ";
}
}
int main()
{
history();
return 0;
}
C: 平衡字符串
题目描述:
思路:
这道题用的尺取法,也就是双指针一个窗口可变的滑动窗口,通过判断字母的个数是否满足要求的n/4,如若不满足要求的条件,则移动窗口,直到最后满足条件。
#include<iostream>
using namespace std;
long long min(long long a, long long b)
{
return a < b ? a : b;
}
long long max(long long a, long long b)
{
return a > b ? a : b;
}
void history()
{
string A;
cin >> A;
long long l = 0, r = 0, temp = 0, ans = A.length(),sum1=0,sum2=0,sum3=0,sum4=0,MAX=0,total,free;
int n = A.length();
for (int i = 0; i < n; i++)
{
if (A[i] == 'Q')
{
sum1++;
}
else if (A[i] == 'W')
{
sum2++;
}
else if (A[i] == 'E')
{
sum3++;
}
else if (A[i] == 'R')
{
sum4++;
}
}
if (sum1 == sum2 && sum3 == sum4 &&sum2==sum3)//四个连等一直触发
{
ans = 0;
}
else
{
if (A[0] == 'Q')
{
sum1--;
}
else if (A[0] == 'W')
{
sum2--;
}
else if (A[0] == 'E')
{
sum3--;
}
else if (A[0] == 'R')
{
sum4--;
}
while (r <= n - 1)
{
MAX = max(max(sum1, sum2), max(sum3, sum4));
total = r - l + 1;
free = total - MAX * 4 + sum1 + sum2 + sum3 + sum4;
if (free % 4 == 0 && free >= 0)
{
ans = min(total, ans);
if (A[l] == 'Q')
{
sum1++;
}
else if (A[l] == 'W')
{
sum2++;
}
else if (A[l] == 'E')
{
sum3++;
}
else if (A[l] == 'R')
{
sum4++;
}
l++;
}
else
{
if (A[r + 1] == 'Q')
{
sum1--;
}
else if (A[r + 1] == 'W')
{
sum2--;
}
else if (A[r + 1] == 'E')
{
sum3--;
}
else if (A[r + 1] == 'R')
{
sum4--;
}
r++;
}
}
}
cout << ans;
}
int main()
{
history();
return 0;
}
D:滑动窗口
题目描述:
思路:
这道题是滑动窗口的最简单的一个应用,简单说就是找到数组内指定长度内的数字的最小值与最大值,这样从左到右遍历数组并且找到最大值与最小值进行输出就行。
#include<iostream>
#include<algorithm>
using namespace std;
void history()
{
int n, k,c=0,d=-1;
scanf_s("%d%d", &n, &k);
int* A = new int[n + 5], l = 0, r = k - 1, * B = new int[n + 5], * C = new int[n + 5];
for (int i = 0; i < n; i++)
{
scanf_s("%d", &A[i+1]);
}
for (int i = 1; i <= n; i++)
{
if (c <= d && B[c] < i - k + 1)
{
c++;
}
while (c <= d && A[B[d]] >= A[i])
{
d--;
}
B[++d] = i;
if (i >= k)
{
printf("%d ", A[B[c]]);
}
}
printf("\n");
c = 0, d = -1;
for (int i = 1; i <= n; i++)
{
if (c <= d && B[c] < i - k + 1)
{
c++;
}
while (c <= d && A[B[d]] <= A[i])
{
d--;
}
B[++d] = i;
if (i >= k)
{
printf("%d ", A[B[c]]);
}
}
}
int main()
{
history();
return 0;
}