2021-12-14 [Codeforces Round #759 (Div. 2, based on Technocup 2022 Elimination Round 3)]

Posted by niranjnn01 on Tue, 14 Dec 2021 10:30:30 +0100

A. Life of a Flower

Title Description

Petya has an interesting flower. Petya is a busy man, so she sometimes forgets to water. From the beginning of Petya's life, you have n days. You must determine what happened to his flowers in the end.

The flowers grow as follows:

If the flower is not watered for two consecutive days, it will die.
If the flower is watered on day i, it will grow 1 cm.
If the flower is watered on days i and (i − 1) (i > 1), it will grow 5 cm instead of 1 cm.
If the flower is not watered on day i, it will not grow.
At the beginning of the first day, the flower is 1 cm tall. What is its height after n days?

input

Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1≤t≤100). The test cases are described below.

The first line of each test case contains a unique integer n ( 1 ≤ n ≤ 100 ) n (1≤n≤100) n(1≤n≤100).

The second line of each test case contains n integers a 1 , a 2 , ... , a n ( a i = 0 or a i = 1 ) a_1,a_2,…,a_n (a_i=0 or a_i=1) a1, a2,..., an (ai = 0 or ai = 1). If a i = 1 a_i=1 ai = 1, the flowers are watered on the i day, otherwise they are not watered.

output

For each test case, print an integer k k k- n n The height of the flower after n days, or - 1, if the flower dies.

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 110;
int a[maxn];
void solve(){
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int len = 1 + a[1];
    for (int i = 2; i <= n; i++){
        if (a[i] + a[i - 1] == 0){
            len = -1;
            break;
        }
        if (a[i] == 1 && a[i - 1] == 1)
            len += 5;
        else if (a[i] == 1)
            len++;
    }
    cout << len << "\n";
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

B. Array Eversion

Title Description

Given a length of n n Array of n a a a.

Let's define the flip operation. order x = a n x=a_n x=an​. Then array a a a is divided into two parts: the left part and the right part. The left part contains not more than x ( ≤ x ) x (≤x) x(≤ x) a a a element. The right part contains strictly greater than 4 x ( > x ) 4x (>x) 4X (> x) a a a element. The order of elements in each section remains the same as before the operation, i.e. e. The partition is stable. Then replace the array with the concatenation of the left and right parts.

For example, if an array a a If a is [2,4,1,5,3], the inversion is as follows: [2,4,1,5,3] → [2,1,3], [4,5] → [2,1,3,4,5].

We start from the array a a A starts and performs a flip on this array. We can prove that after several extroversions, the array a a a stop the change. Output decimal k k k makes the array k k Stop the change after k turns.

input

Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1≤t≤100). The test cases are described below.

The first line contains an integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1≤n≤2⋅105).

The second line contains n n n integers a 1 , a 2 , ... , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1,a_2,...,a_n (1≤a_i≤10^9) a1​,a2​,...,an​(1≤ai​≤109).

Ensure the reliability of all test cases n n n total not more than 2 ⋅ 1 0 5 2⋅10^5 2⋅105.

output

For each test case, print an integer k k k - number of flips after the array stops changing.

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn=2e5+5;
int n, x, mx;
int a[maxn];
int ans[maxn];
int len;
void solve(){
    cin >> n;
		mx = 1;
		len = 0;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
			if(a[mx] <= a[i])
				mx = i;
		}
		ans[++len] = a[mx];
		for(int i = mx + 1; i <= n; i++){
			while(ans[len] <= a[i])
				len--;
			ans[++len] = a[i];
		}
		cout << len - 1 << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t ;
    while(t--){
         solve();
    }
    return 0;
}

C. Minimize Distance

Title Description

share n n n warehouses are located on the number axis. about 1 ≤ i ≤ n 1≤i≤n 1 ≤ i ≤ n, station i i i located x i x_i xi) point.

You are a n n The salesman of n bags of goods tried to n n Each of the n warehouses carries a bag of goods. You and n n n bags are initially at the origin 0 0 0 You can carry at most one time k k k bags. You must collect the required quantity of goods from the origin, transport them to the corresponding warehouse, and then return to the origin to pick up your next batch of goods.

Calculate the minimum distance required to transport all cargo bags to the warehouse. You do not need to return to the origin after all bags have been delivered.

input

Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 10500 ) t (1≤t≤10500) t(1≤t≤10500). The test cases are described below.

The first line of each test case contains two integers n n n and k ( 1 ≤ k ≤ n ≤ 2 ⋅ 1 0 5 ) k (1≤k≤n≤2⋅10^5) k(1≤k≤n≤2⋅105).

The second line of each test case contains n n n integers x 1 , x 2 , ... , x n ( − 1 0 9 ≤ x i ≤ 1 0 9 ) x_1,x_2,...,x_n (−10^9≤x_i≤10^9) x1​,x2​,…,xn​(−109≤xi​≤109). Some warehouses may share the same location.

Ensure the reliability of all test cases n n n total not more than 2 ⋅ 1 0 5 2⋅10^5 2⋅105.

output

For each test case, an integer is output indicating the minimum distance required to transport all cargo bags to the warehouse.

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
void solve(){
    ll n, m;
    cin >> n >> m;
    vector<ll> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];
    sort(v.begin(), v.end());
    ll ans = 0;
    for (int i = n - 1; i >= 0 && v[i] >= 0; i -= m)
        ans += v[i];
    for (int i = 0; i < n && v[i] < 0; i += m)
        ans += abs(v[i]);
    cout << 2 * ans - max(abs(v[0]), abs(v[n - 1])) << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Topics: C C++ Algorithm data structure