week5- jobs

A - the largest rectangle

Meaning of the questions:

To a histogram, find the largest rectangular area in the histogram. For example, the following image height in the histogram from left to right are 2, 1, 4, 5, 1, 3, 3, 1 they are wide, the largest rectangle is shaded.
Here Insert Picture Description

Input:
Input comprising a plurality of sets of data. Each set of data is represented by an integer number n of small rectangular histogram, you can assume that 1 <= n <= 100000. then the next n integers h1, ..., hn, satisfying 0 <= hi <= 1000000000. These histogram from left to right the digital representation of each small rectangle of height, the width of each small rectangle is 1. Test data to 0 at the end.
Output:
For each line the test data output represents an integer answer.


Ideas:

If it is determined the height of the rectangle, then left a certain point more to the left, which is the first small number left this height point, and the right end point can determine the height of the first small this point right number, which you can use monotone about whether the stack are traversed across the high end storage array using, for each point traversed once for each point, then the current cycle is determined the point where the stack is greater than the high point equal to the high current, if the stack is then deleted after the end of the cycle to determine whether the stack is empty, if empty, then prove that the left point on the far left. Otherwise the current top of the stack. The point is then pushed onto the stack. Right breakpoint operation, too, only the beginning of the tail from the head.
Note: Because the size of the data area exceeds the range can be long long int conversion


Code:

#include<iostream>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
stack<int> s;
int a[100010], l[100010], r[100010];
int main()
{
	int n;
	
	while (~scanf("%d",&n))
	{
		long long int area=0;
		if (n == 0) exit(0);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
		}
		while (!s.empty())
			s.pop();
		for (int i = 1; i <= n; i++)
		{
			while (!s.empty() && a[s.top()] >= a[i])
				s.pop();
			if (s.empty()) 
				l[i] = 0;
			else 
				l[i] = s.top();
			s.push(i);
		}
		while (!s.empty())
			s.pop();
		for (int i = n; i >=1; i--)
		{
			while (!s.empty() && a[s.top()] >= a[i])
				s.pop();
			if (s.empty()) 
				r[i] = n+1;
			else 
				r[i] = s.top();
			s.push(i);
		}
		for (int i = 1; i <= n; i++)
			area = max(area, (long long int)a[i] * (r[i] - l[i]-1));
		cout << area << endl;
	}
	return 0;
}

B - TT’s Magic Cat

Meaning of the questions:

Thanks to everyone's help last week, it finally got a cute cat. But I never thought that this is a magic cat.
One day, a magical cat decided to investigate the ability of TT, giving him a problem. N cities that is selected from the world map, a [i] denote the i-th value of the assets owned by the city.
Then, this magical cat will perform several actions. Each round of selection [l, r] cities within range, and increase the value of its assets c. Finally, given the value of the assets of each of the city after the operation q.
Can you help me to find?
The INPUT:
The first line contains two integers nq
city management and quantity.
The second line contains a sequence of elements: an integer of a1, a2, ..., an
followed q rows, each row represents one operation. I-th row comprising three integers l, r, and c, for the i-th operation.
output:
printing n integers a1, a2, ..., one per line, ai should be equal to the final value of the assets of the i-th city.


Ideas:

This question is relatively simple, subtraction section is thought to be the original difference array into an array, a single point will be changed to modify the zone, that is [l, r] in the interval -> l point + c, r + 1 point -c
then the final single prefixes modified and the final result obtained.


Code:

#include<iostream>
#include<cstring>
using namespace std;
long long a[1000001],b[1000001];
int main()
{
    ios::sync_with_stdio(false);
    int n, q;
    int r, l, c;
    a[0] = 0;
    cin >> n >> q;
    memset(b, 0, sizeof(b));
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        b[i] = a[i] - a[i - 1];
    }
    for (int i = 1; i <= q; i++)
    {
        cin >> l>> r>> c;
        b[l] += c;
        b[r + 1] -= c;
    }
    for (int i = 1; i <= n; i++)
    {
        a[i] = a[i-1]+b[i];
        cout << a[i] << " ";
    }
    return 0;
}

C - string balance

Meaning of the questions:

A string of length n s, which contains only the 'Q', 'W', 'E', 'R' four kinds of characters.
If both the number of n / 4, it is balanced by a string of four kinds of characters appear in the string.
S can now be continuous period of the same length substrings replace any string that contains only four characters, so that it becomes a balanced string, alternatively asked minimum length of the substring?
If s has balanced outputs 0 .

Input:
line representing a given character string s

Output
An integer that represents the answer


Ideas:

Scale borrowing to maintain double-pointer, the general asked for answers to a continuum, both ends of the range can not be a clear direction, it is clear that this question is this string can be used as a continuum, it can be used to determine whether the foot borrowing to meet the requirements . First set (L, R) (initial L = R = 0), respectively, to traverse the first string, respectively count the number of counts by four QWER. If the four counts are equal, it determines that the string directly balance, if not want to wait, first determines the first character, and the character count of -1. It is circulated, so that by replacing the four counts are equal MAx = max (sum1, sum2, sum3, sum4).
Then see [L, R] is the number of remaining string is FREE = (R + L-1 ) - [(MAx-sum1) + (MAx-sum2) + (MAx-sum3) + (MAx-sum4)] n is greater than or equal to 0 is a multiple of four.
If it is to meet the requirements, L ++, or does not meet the requirements of R ++; Until R <n so far;


Code:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
string s;
int sum1 = 0,sum2=0,sum3=0,sum4=0;
int l=0, r=0;

int main()
{
	int count = 1000000;
	ios::sync_with_stdio(false);
	cin >> s;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == 'Q')
			sum1++;
		if (s[i] == 'W')
			sum2++;
		if (s[i] == 'E')
			sum3++;
		if (s[i] == 'R')
			sum4++;
	}
	if (sum1 == sum2 &&sum2== sum3&&sum3== sum4)
	{
		cout << 0 << endl;
		exit(0);
	}
	if (s[0] == 'Q')
		sum1--;
	if (s[0] == 'W')
		sum2--;
	if (s[0] == 'E')
		sum3--;
	if (s[0] == 'R')
		sum4--;
	while (1)
	{
		int MAX1 = max(sum1, sum2), MAX2 = max(sum3, sum4);
		int MAX = max(MAX1, MAX2);
		int TOTAL = r- l + 1;
		int FREE = TOTAL - ((MAX - sum1) + (MAX - sum2) + (MAX - sum3) + (MAX-sum4));
		if (FREE >= 0 && FREE % 4 == 0)
		{
			count = min(TOTAL, count);
			if (s[l] == 'Q')
				sum1++;
			if (s[l] == 'W')
				sum2++;
			if (s[l] == 'E')
				sum3++;
			if (s[l] == 'R')
				sum4++;
			l++;
		}
		else
		{
			if (r + 1 >= s.size()) break;
			r++;
			if (s[r] == 'Q') 
				sum1--;
			if (s[r] == 'W') 
				sum2--;
			if (s[r] == 'E')
				sum3--;
			if (s[r] == 'R')
				sum4--;

		}


	}
	cout << count << endl;
	return 0;
	
	
	
}

D - sliding window

Meaning of the questions:

ZJM has a length of n number of columns and a size of a window k, the window can be moved back and forth on the columns. ZJM now want to know from left to right in a sliding window, the window each time the maximum value and the minimum value, respectively, What example:
the number of columns is [13-1-35367], where k is equal to 3.
the Window position the Maximum value Minimum value
[1. 3 -1] -3. 3. 5. 6. 7 -1. 3
. 1 [. 3 - 1-3]. 6. 7 -3. 3 53
. 1. 3 [-1-35] -3. 5. 7. 6. 3
. 1. 3-1 [-353] -3. 7. 5. 6
. 1. 3 -1 3 [53 . 6]. 6. 7. 3
. 1. 5. 3 -3 -1 [. 3. 6. 7]. 3. 7
the input
is input two lines. The first line of two integers n and k are the size and length of the sliding window series, 1 <= k <= n <= 1000000. The second row has n represents an integer number of columns of ZJM.
Output
Output two lines. The first output line of each sliding window position from left to right, the minimum value in the sliding window. The second line is the maximum.


Ideas:

Since the window and to maintain the maximum and minimum values ​​taken from every window, first to the k-1 to maintain a monotonically increasing queue, then starting from the k-th, k from n to continue to maintain a monotonically increasing queue, the queue element belonging to the current window, and maintenance process, if the element does not belong to the current window, the pop elements of the first team, the first team is the minimum element of each window, save them to another array. Seeking maximum queue becomes simply a monotonically increasing monotonically decreasing maintenance to queue, the other unchanged.


Code:

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
deque<int> q;
int a[1000001],mi[1000001],ma[1000001];
int main()
{
	int n, k;
	scanf("%d %d",&n,&k);
	for (int i = 1; i <= n; i++)
		scanf("%d",&a[i]);
	for (int i = 1; i <= k - 1; i++)
	{
		while (q.size() && a[q.back()] > a[i])
			q.pop_back();
		q.push_back(i);
	}
	for (int i = k; i <= n; i++)
	{
		while (q.size() && a[q.back()] > a[i])
			q.pop_back();
		q.push_back(i);
		while (q.size() && (i - q.front()) >= k)
			q.pop_front();
		mi[i] = a[q.front()];
	}
	while (!q.empty())
	{
		q.pop_back();
	}
	for (int i = 1; i <= k - 1; i++)
	{
		while (q.size() && a[q.back()] < a[i])
			q.pop_back();
		q.push_back(i);
	}
	for (int i = k; i <= n; i++)
	{
		while (q.size() && a[q.back()] < a[i])
			q.pop_back();
		q.push_back(i);
		while (q.size() && (i - q.front()) >= k)
			q.pop_front();
		ma[i] = a[q.front()];
	}
	for (int i = k; i <=n ; i++)
		printf("%d ",mi[i]);
	printf("\n");
	for (int i = k; i <=n; i++)
		printf("%d ",ma[i]);
	return 0;
}
Released seven original articles · won praise 0 · Views 121

Guess you like

Origin blog.csdn.net/weixin_44465341/article/details/105105451