CodeForces - 1159 A and B

A - A pile of stones

读题真是我过不去的坎

Vasya has a pile, that consists of some number of stones. n times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.

You are given n operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations.

Input
The first line contains one positive integer n — the number of operations, that have been made by Vasya (1≤n≤100).

The next line contains the string s, consisting of n symbols, equal to “-” (without quotes) or “+” (without quotes). If Vasya took the stone on i-th operation, si is equal to “-” (without quotes), if added, si is equal to “+” (without quotes).

Output
Print one integer — the minimal possible number of stones that can be in the pile after these n operations.

Examples
Input
3

Output
0
Input
4
++++
Output
4
Input
2
-+
Output
1
Input
5
+±++
Output
3
Note
In the first test, if Vasya had 3 stones in the pile at the beginning, after making operations the number of stones will be equal to 0. It is impossible to have less number of piles, so the answer is 0. Please notice, that the number of stones at the beginning can’t be less, than 3, because in this case, Vasya won’t be able to take a stone on some operation (the pile will be empty).

In the second test, if Vasya had 0 stones in the pile at the beginning, after making operations the number of stones will be equal to 4. It is impossible to have less number of piles because after making 4 operations the number of stones in the pile increases on 4 stones. So, the answer is 4.

In the third test, if Vasya had 1 stone in the pile at the beginning, after making operations the number of stones will be equal to 1. It can be proved, that it is impossible to have less number of stones after making the operations.

In the fourth test, if Vasya had 0 stones in the pile at the beginning, after making operations the number of stones will be equal to 3.

//
// Created by DELL on 2020/2/27.
//

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main(int argc,char* argv[]) {
    int n,Ans = 0; scanf("%d",&n);
    char ch;
    for(int i=1; i<=n; i++) {
        cin >> ch;
        if(ch == '+') Ans++;
        else if(Ans > 0) Ans--;
    }
    printf("%d\n",Ans);
    return 0;
}

B - Expansion coefficient of the array

Let’s call an array of non-negative integers a1,a2,…,an a k-extension for some non-negative integer k if for all possible pairs of indices 1≤i,j≤n the inequality k⋅|i−j|≤min(ai,aj) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.

You are given an array of non-negative integers a1,a2,…,an. Find its expansion coefficient.

Input
The first line contains one positive integer n — the number of elements in the array a (2≤n≤300000). The next line contains n non-negative integers a1,a2,…,an, separated by spaces (0≤ai≤109).

Output
Print one non-negative integer — expansion coefficient of the array a1,a2,…,an.

Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6,4,5,5] is equal to 1 because |i−j|≤min(ai,aj), because all elements of the array satisfy ai≥3. On the other hand, this array isn’t a 2-extension, because 6=2⋅|1−4|≤min(a1,a4)=5 is false.

In the second test, the expansion coefficient of the array [0,1,2] is equal to 0 because this array is not a 1-extension, but it is 0-extension.

//
// Created by DELL on 2020/2/27.
//

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#define Maxn 300002
#define  LL long long
using namespace std;
LL a[Maxn];
int main(int argc,char* argv[]) {
    int n,pos, Mininum = 1e9+10 ;
    LL Ans = 1e9+10;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%lld",&a[i]);
        LL dist = max(i - 1,n - i);
        Ans = min(Ans,a[i] / dist);
    }
    printf("%lld\n",Ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/104536297