Program design thinking and practice Week5 operations (3/4 / data classes)

Program design thinking and practice Week5 operations (3/4 / data classes)

A - the largest rectangle

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.

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 numbers represent the height of the histogram from left to right in each of the small rectangle, 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.

Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000

problem analysis

Each rectangle has two parameters, one of which is its location (number), the second is its height. For each rectangle, the left and right can be "extended", provided that the adjacent rectangular height greater than or equal itself. In this way, we can determine the leftmost and rightmost range, and then find the largest rectangular area of a rectangle where the last comparison.

DETAILED DESCRIPTION up monotonically stack. Each rectangle in the most distant reach the right example. Set a stack, traverse the rectangle from its beginning, if the stack is not empty, the top of the stack height of the rectangle position is greater than the current height of the rectangle, the pop-up, so that the stack is always the most distant of the element to the right extension application.

#include <iostream>
#include <stack>
using namespace std;
int h[100010];
int l[100010];
int r[100010];
int n;
long long ans=0;
int main() {
    while (cin>>n&&n!=0) {
        ans=0;
        for (int i=1; i<=n; i++) {
            cin>>h[i];
            l[i]=0;
            r[i]=n+1;
        }
        stack<pair<int, int> > s;
        for (int i=1; i<=n; i++) {
            while (!s.empty()&&s.top().second>h[i]) {
                r[s.top().first]=i;
                s.pop();
            }
            s.push(pair<int, int>(i,h[i]));
        }
        stack<pair<int, int> > s2;
        for (int i=n; i>=1; i--) {
            while (!s.empty()&&s.top().second>h[i]) {
                l[s.top().first]=i;
                s.pop();
            }
            s.push(pair<int, int>(i,h[i]));
        }
        for (int i=1;i<=n; i++) {
            if((long long)(r[i]-l[i]-1)*h[i]>ans)
            ans=(long long)(r[i]-l[i]-1)*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⋅105) — the number of cities and operations.

The second line contains elements of the sequence a: integer numbers a1,a2,...,an (−106≤ai≤106).

Then q lines follow, each line represents an operation. The i-th line contains three integers l,r and c (1≤l≤r≤n,−105≤c≤105) 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.

Examples

Input
4 2
-3 6 8 4
4 4 -2
3 3 1
Output
-3 6 9 2
Input
2 1
5 -2
1 2 4
Output
9 2
Input
1 2
0
1 1 -8
1 1 -6
Output
-14

problem analysis

Title mean, given all of the city's assets, so that each city's assets have a range of a change in value, after multiple operations, given all the city's assets

if through each city, will be time-consuming unacceptable . Using a differential method, a differential new array, the mapping relation is, B [. 1] = A [. 1], B [n-] = A [n-] -a n--. 1 . To [l, R & lt] x changes within the scope of the asset, the difference need only change the l th array x, r + 1 th first change -x. Finally, the available methods cumulative sum of assets obtained after the change in each city.

#include<stdio.h>
typedef long long ll;
ll a[200005];
ll b[200005];
int main()
{	
	
	ll l,r,n,q,c;
	scanf("%lld %lld",&n,&q);
	for(ll i =1 ; i<=n ;i++){
		scanf("%lld",&a[i]);
	}
	for(ll i=1; i<=n; i++){
		b[i]=a[i]-a[i-1];
	}
	for (ll i=1; i <= q; i++)
	{
		scanf("%lld %lld %lld",&l,&r,&c);
		b[l] +=c;
		b[r+1] -= c;
	}
	printf("%lld",b[1]);
    long long sum = b[1];
	for(ll i=2;i<=n;i++)	
	{
		sum +=b[i];
		printf(" %lld",sum);
	}
	printf("\n");
	return 0;
}

C - string balance

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 represents a given character string s

Output

An integer that represents the answer

Examples

Input
QWER
Output
0
Input
QQWE
Output
1
Input
QQQW
Output
2
Input
QQQQ
Output
3

Note

. 1 <= n-<^ = 10. 5, n-multiple of 4, the character string contains only the 'Q', 'W', 'E' and 'R'.

problem analysis

This is a ruler emulated subject.


First, the number of statistics for each character to determine whether the conditions have been met.

If the imbalance, by means of a mobile section, such that by changing an element within a certain range, the entire string to achieve balance, to determine the minimum interval. Code has comments.

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    vector<char> chars;//{'Q','W','E','R'};
    chars.push_back('Q');//为四个字符编号,便于统计数目
    chars.push_back('W');
    chars.push_back('E');
    chars.push_back('R');
    map<char, int> cnt;
    bool balance = true;
    for (auto ch : s)//统计每个字符出现的次数
        ++cnt[ch];
    int len = s.size();
    int n = len / 4;
    for (auto ch : chars)
    {
        cnt[ch] -= n;
        if (cnt[ch] > 0)
            balance = false;
    }
    if (balance == true) {
        cout << 0 << endl;
        return 0;
    }
        
    int left = 0, right = 0, num = len;
    while (left <= right && right < len)
    {
        bool find = true;
        --cnt[s[right]];//假设所选区间最右边的元素被替换
        while (find)
        {
            for (auto ch : chars)//遍历字符串
            {
                if (cnt[ch] > 0)//尚且不平衡
                {
                    find = false;//中断循环
                    break;
                }
            }
            if (find == true)//如果通过改变区间最右边元素平衡
            {
                num = min(num, right - left + 1);
                ++cnt[s[left++]];//左端点向右移动
            }
        }
        ++right;//右端点向右移动
    }
    cout << num << endl;
    return 0;
}

D - sliding window sliding window

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.

Window position	                Minimum value   Maximum value
[1  3  -1] -3  5  3  6  7 	    -1	            3
 1 [3  -1  -3] 5  3  6  7 	    -3	            3
 1  3 [-1  -3  5] 3  6  7 	    -3	            5
 1  3  -1 [-3  5  3] 6  7 	    -3	            5
 1  3  -1  -3 [5  3  6] 7 	    3	            6
 1  3  -1  -3  5 [3  6  7]	    3	            7

Input

Enter the 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.

Sample Input
8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7

problem analysis

Deque with an array of analog. In the case of the minimum required window, if the queue is not empty, all of the current element number is greater than or equal ejected into the team of the air, so that the smallest element is stored in the current window. Each inspection of the head of the queue is not expired, expired pop up

another situation the same way discussion. You can use stl, still it does not time out, however, one thing to note, pop_back () when, with equal numbers, if the number is equal to shelters, to be wrong.

#include<stdio.h>
int arr[1000005];
int dq[1000005];
int ans1[1000005];
int ans2[1000005];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i){
    	scanf("%d",&arr[i]);
	}
	int l=0,r=-1;
	for(int i=0;i<n;++i)
	{
		while(r>=l&&arr[dq[r]]>=arr[i])//不空,则从尾到头,弹出所有比当前元素大的 
		{
			r--;
		}
		dq[++r]=i;//当前元素序号入队 
		if(i>=k-1&&i-dq[l]>=k) //队首过期 
		{
			l++;
		}
		ans1[i]=arr[dq[l]];
	}
	l=0,r=-1;
	for(int i=0;i<n;++i)
	{
		while(r>=l&&arr[dq[r]]<=arr[i])
		{
			r--;
		}
		dq[++r]=i;
		if(i>=k-1&&i-dq[l]>=k)
		{
			l++;
		}
		ans2[i]=arr[dq[l]];
	}
	printf("%d",ans1[k-1]);
	for(int i=k;i<n;++i){
		printf(" %d",ans1[i]);
	}
	printf("\n%d",ans2[k-1]);
	for(int i=k;i<n;++i){
		printf(" %d",ans2[i]);
	}
	printf("\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12635508.html