A - 最大矩形
给一个直方图,求直方图中的最大矩形的面积。例如,下面这个图片中直方图的高度从左到右分别是2, 1, 4, 5, 1, 3, 3, 他们的宽都是1,其中最大的矩形是阴影部分。
Input
输入包含多组数据。每组数据用一个整数n来表示直方图中小矩形的个数,你可以假定1 <= n <= 100000. 然后接下来n个整数h1, …, hn, 满足 0 <= hi <= 1000000000. 这些数字表示直方图中从左到右每个小矩形的高度,每个小矩形的宽度为1。 测试数据以0结尾。
Output
对于每组测试数据输出一行一个整数表示答案。
Example Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Example Output
8
4000
题意:
给出一个直方图,在这个直方图中存在的所有矩形中,找出面积最大的一个,并输出最大面积。
分析:
这道题主要考察单调栈,具体计算方法如下:
1.分别找出当前矩形左边和右边第一个比该矩形高度低的矩形下标
2.求出两者之差即为宽度
3.高度与宽度之积为面积
利用一个单调栈对所有小矩形进行遍历:
1.如果当前矩形高度大于栈顶的矩形高度,入栈,宽度加一
2.反之出栈,计算面积
3.面积进行比较求出最大值输出
代码如下:
#include<iostream>
using namespace std;
long long int l[100010] = { 0 };
long long int r[100010] = { 0 };
long long int h[100010];
long long int s[100010];
int main()
{
int n;
long long int ans;
while (cin >> n)
{
ans = 0;
if (n == 0)
break;
for (int i = 0; i < n; i++)
cin >> h[i];
int t = 0;
for (int i = n - 1; i >= 0; i--)
{
while (t > 0 && h[i] <= h[s[t - 1]])
t--;
if (t == 0)
r[i] = n;
else
r[i] = s[t - 1];
s[t++] = i;
}
t = 0;
for (int i = 0; i < n; i++)
{
while (t > 0 && h[i] <= h[s[t - 1]])
t--;
if (t == 0)
l[i] = 0;
else
l[i] = s[t - 1] + 1;
s[t++] = i;
}
for (int i = 0; i < n; i++)
{
if (((r[i] - l[i]) * h[i]) >= ans)
ans = (r[i] - l[i]) * h[i];
}
cout << ans << endl;
}
return 0;
}
B - TT’s Magic Cat
Thanks to everyone’s help last week, TT finally got a cute cat. But what TT didn’t expect is that this is a magic cat.
One day, the magic cat decided to investigate TT’s ability by giving a problem to him. That is select n cities from the world map, and a[i] represents the asset value owned by the i-th city.
Then the magic cat will perform several operations. Each turn is to choose the city in the interval [l,r] and increase their asset value by c. And finally, it is required to give the asset value of each city after q operations.
Could you help TT find the answer?
Input
The first line contains two integers n,q (1≤n,q≤2*10^5) — the number of cities and operations.
The second line contains elements of the sequence a: integer numbers a1,a2,…,an (−10^6 ≤ ai ≤ 10^6).
Then q lines follow, each line represents an operation. The i-th line contains three integers l,r and c (1≤l≤r≤n,−10^5 ≤ c ≤ 10^5) for the i-th operation.
Output
Print n integers a1,a2,…,an one per line, and ai should be equal to the final asset value of the i-th city.
Example Input
4 2
-3 6 8 4
4 4 -2
3 3 1
Example Output
-3 6 9 2
题意:
有n个城市,用a[i]表示每个城市的资产 ,有q个操作,每次将区间[l,r]内的城市资产都加c。最后输出每个城市的资产。
分析:
本题主要考察差分数组b[i]的实现和使用。即:
b[i]=a[i]; i=0
b[i]=a[i]-a[i-1]; i>0
根据以上公式可以推导出:
当i>0时,a[i]=数组b[i]的前i项和。
所以以上对a[i]的操作就可以转化为对b[i]的操作,即:
对数组a[i]中从a[l]到a[r]全部进行+c操作,等价于对b[l]进行+c操作,再对b[r+1]进行-c操作。
所以只需要对b[i]进行操作并按序输出。
代码如下:
#include<cstdio>
using namespace std;
long long int a[1000000] = { 0 };
long long int b[1000000] = { 0 };
int main()
{
int n;
int q;
int l, r, c;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
{
scanf("%lld", &a[i]);
if (i == 0)
b[i] = a[i];
else
b[i] = a[i] - a[i - 1];
}
for (int i = 0; i < q; i++)
{
scanf("%d%d%d", &l, &r, &c);
b[l - 1] += c;
b[r] -= c;
}
long long int ans;
for (int i = 0; i < n; i++)
{
if (i == 0)
ans = b[i];
else
ans += b[i];
printf("%lld ", ans);
}
return 0;
}
C - 平衡字符串
一个长度为 n 的字符串 s,其中仅包含 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符。
如果四种字符在字符串中出现次数均为 n/4,则其为一个平衡字符串。
现可以将 s 中连续的一段子串替换成相同长度的只包含那四个字符的任意字符串,使其变为一个平衡字符串,问替换子串的最小长度?
如果 s 已经平衡则输出0。
Input
一行字符表示给定的字符串s
Output
一个整数表示答案
Example Input
QWER
Example Output
0
Notes
1<=n<=10^5
n是4的倍数
字符串中仅包含字符 ‘Q’, ‘W’, ‘E’ 和 ‘R’
题意:
一个字符串,只含有QWER四个字母,试着改变最小的连续的一部分,使得四个字母个数相等。
分析:
这道题主要考察尺取法。
使用length函数求出字符串长度,则当平衡时每个字符出现的次数应该为num=len/4。
分别定义变量Q、W、E、R对四个字母个数进行统计,如果均为num,则输出0。否则的话,去除不包含区间 [l, r] 中的字符,更新变量Q、W、E、R。
然后开始尺取寻找,先通过替换使 4 个字符数量一致,再判断剩余空闲位置是否为4的倍数,若满足要求;更新变量Q、W、E、R,继续寻找更小的区间,l++;否则当前区间[l,r]不满足要求,则更新变量Q、W、E、R,r++,扩大区间范围继续寻找。
代码如下:
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
string s;
cin >> s;
int len = s.length();
int num = len / 4;
int ans = len + 1;
int l = 0, r = 0;
int Q = 0, W = 0, E = 0, R = 0;
for (int i = 0; i < len; i++)
{
if (s[i] == 'Q')
Q++;
if (s[i] == 'W')
W++;
if (s[i] == 'E')
E++;
if (s[i] == 'R')
R++;
}
if (Q == num && W == num && E == num && R == num)
ans = 0;
else
{
for (int i = l; i <= r; i++)
{
if (s[i] == 'Q')
Q--;
if (s[i] == 'W')
W--;
if (s[i] == 'E')
E--;
if (s[i] == 'R')
R--;
}
while (r < len && l < len)
{
int MAX = max(max(Q, W), max(E, R));
int length = r - l + 1;
length -= 4 * MAX - Q - W - E - R;
if (length >= 0 && length % 4 == 0)
{
if (s[l] == 'Q')
Q++;
if (s[l] == 'W')
W++;
if (s[l] == 'E')
E++;
if (s[l] == 'R')
R++;
ans = min(ans, r - l + 1);
l++;
}
else
{
r++;
if (s[r] == 'Q')
Q--;
if (s[r] == 'W')
W--;
if (s[r] == 'E')
E--;
if (s[r] == 'R')
R--;
}
}
}
cout << ans;
return 0;
}
D - 滑动窗口
ZJM 有一个长度为 n 的数列和一个大小为 k 的窗口, 窗口可以在数列上来回移动. 现在 ZJM 想知道在窗口从左往右滑的时候,每次窗口内数的最大值和最小值分别是多少. 例如:
数列是 [1 3 -1 -3 5 3 6 7], 其中 k 等于 3.
Input
输入有两行。第一行两个整数n和k分别表示数列的长度和滑动窗口的大小,1<=k<=n<=1000000。第二行有n个整数表示ZJM的数列。
Output
输出有两行。第一行输出滑动窗口在从左到右的每个位置时,滑动窗口中的最小值。第二行是最大值。
Example Input
8 3
1 3 -1 -3 5 3 6 7
Example Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
题意:
找出窗口在移动时,窗口中的最大值和最小值,并按要求输出。
分析:
此题主要考察单调队列。
利用STL中的双端队列deque,利用单调递增队列来求出当前窗口的最小值,利用单调递减队列来求出当前窗口的最大值,当元素不属于当前窗口时, 弹出队首元素;当元素不满足单调性时,弹出队尾元素,否则的话从队尾入队。然后一边操作一边进行输出。
代码如下:
#include<cstdio>
#include<deque>
using namespace std;
int a[1000100];
deque <int> q;
int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
q.clear();
for (int i = 0; i < k - 1; i++)
{
while (!q.empty() && a[q.back()] > a[i])
q.pop_back();
q.push_back(i);
}
for (int i = k - 1; i < n; i++)
{
while (!q.empty() && a[q.back()] > a[i])
q.pop_back();
q.push_back(i);
while (!q.empty() && q.back() - q.front() + 1 > k)
q.pop_front();
printf("%d ", a[q.front()]);
}
printf("\n");
q.clear();
for (int i = 0; i < k - 1; i++)
{
while (!q.empty() && a[q.back()] < a[i])
q.pop_back();
q.push_back(i);
}
for (int i = k - 1; i < n; i++)
{
while (!q.empty() && a[q.back()] < a[i])
q.pop_back();
q.push_back(i);
while (!q.empty() && q.back() - q.front() + 1 > k)
q.pop_front();
printf("%d ", a[q.front()]);
}
return 0;
}