Codeforce GoodBye2020 A - C

3 篇文章 0 订阅
本文探讨了两个算法问题:一是计算在特定约束下能形成的三角形的不同面积,二是如何通过增加音乐曲目的音符多样性来提高歌曲的吸引力。对于前者,涉及排列组合和坐标系中的几何概念;对于后者,关注的是如何在不改变歌曲长度的情况下增加不同音符的数量。这两个问题都涉及到在有限资源下优化特定指标的问题。
摘要由CSDN通过智能技术生成

A. Bovine Dilemma

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Argus was charged with guarding Io, which is not an ordinary cow. Io is quite an explorer, and she wanders off rather frequently, making Argus' life stressful. So the cowherd decided to construct an enclosed pasture for Io.

There are nn trees growing along the river, where Argus tends Io. For this problem, the river can be viewed as the OXOX axis of the Cartesian coordinate system, and the nn trees as points with the yy-coordinate equal 00. There is also another tree growing in the point (0,1)(0,1).

Argus will tie a rope around three of the trees, creating a triangular pasture. Its exact shape doesn't matter to Io, but its area is crucial to her. There may be many ways for Argus to arrange the fence, but only the ones which result in different areas of the pasture are interesting for Io. Calculate the number of different areas that her pasture may have. Note that the pasture must have nonzero area.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then tt test cases follow, each one is described in two lines.

In the first line of each test case there is a single integer nn (1≤n≤501≤n≤50) denoting the number of trees growing along the river. Next line contains nn distinct integers x1<x2<…<xn−1<xnx1<x2<…<xn−1<xn (1≤xi≤501≤xi≤50), the xx-coordinates of trees growing along the river.

Output

In a single line output an integer, the number of different nonzero areas that triangles with trees as vertices may have.

Example

input

Copy

8
4
1 2 4 5
3
1 3 5
3
2 6 8
2
1 2
1
50
5
3 4 5 6 8
3
1 25 26
6
1 2 4 8 16 32

output

Copy

4
2
3
1
0
5
3
15

Note

In the first test case, we have 66 non-degenerate triangles with the following areas: 0.50.5, 0.50.5, 11, 1.51.5, 1.51.5 and 22. The pasture can have 44 different areas, and thus 44 is the answer.

In the second test case, we have 33 non-degenerate triangles with the following areas: 11, 11 and 22. The pasture can have 22 different areas, so 22 is the answer.

The following two drawings present the situation in the second test case. The blue triangles in the first drawing have area 11. The red triangle in the second drawing has area 22.

 

code:

#include<bits/stdc++.h>
using namespace std;
int a[105];
int main()
{
 
    int T, n, sum = 0;
    set<int> S;
    cin >> T;
    while(T--)
    {
        S.clear();
        memset(a, 0, sizeof(a));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
                S.insert(a[j] - a[i]);
        cout << S.size() << endl;
    }
    return 0;
}

B. Last minute enhancements

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception.

His song consists of nn notes, which we will treat as positive integers. The diversity of a song is the number of different notes it contains. As a patron of music, Euterpe watches over composers and guides them throughout the process of creating new melodies. She decided to help Athenaeus by changing his song to make it more diverse.

Being a minor goddess, she cannot arbitrarily change the song. Instead, for each of the nn notes in the song, she can either leave it as it is or increase it by 11.

Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤100001≤t≤10000) — the number of test cases. Then tt test cases follow, each one is described in two lines.

In the first line of each test case there is a single integer nn (1≤n≤1051≤n≤105) denoting the length of the song. The next line contains a sequence of nn integers x1,x2,…,xnx1,x2,…,xn (1≤x1≤x2≤…≤xn≤2⋅n)(1≤x1≤x2≤…≤xn≤2⋅n), describing the song.

The sum of nn over all test cases does not exceed 105105.

Output

For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence.

Example

input

Copy

5
6
1 2 2 2 5 6
2
4 4
6
1 1 3 4 4 5
1
1
6
1 1 1 2 2 2

output

Copy

5
2
6
1
3

Note

In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence 1,3–,2,2,6–,7–1,3_,2,2,6_,7_, which has 55 different elements (increased elements are underlined).

In the second test case, Euterpe can increase the first element to obtain the sequence 5–,45_,4, which has 22 different elements.

In the third test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence 1,2–,3,4,5–,6–1,2_,3,4,5_,6_, which has 66 different elements.

code:

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{

    int T, n, sum = 0;
    set<int> S;
    cin >> T;
    while(T--)
    {
        S.clear();
        memset(a, 0, sizeof(a));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for(int i = 1; i < n; i++)
        {
            if(a[i] == a[i - 1] && a[i] != a[i + 1])
            {
                a[i]++;
                S.insert(a[i]);
            }
            else
                S.insert(a[i]);
        }
        S.insert(a[n] + 1);
        //for(int i = 1; i <= n; i++)
           // cout << a[i] << " ";
        //cout << endl;
        cout << S.size() << endl;
    }
    return 0;
}

C. Canine poetry

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

After his wife's tragic death, Eurydice, Orpheus descended to the realm of death to see her. Reaching its gates was uneasy, but passing through them proved to be even more challenging. Mostly because of Cerberus, the three-headed hound of Hades.

Orpheus, a famous poet, and musician plans to calm Cerberus with his poetry and safely walk past him. He created a very peculiar poem for Cerberus. It consists only of lowercase English letters.

We call a poem's substring a palindrome if and only if it reads the same backwards and forwards. A string aa is a substring of a string bb if aa can be obtained from bb by deleting several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.

Unfortunately, Cerberus dislikes palindromes of length greater than 11. For example in the poem abaa the hound of Hades wouldn't like substrings aba and aa.

Orpheus can only calm Cerberus if the hound likes his poetry. That's why he wants to change his poem so that it does not contain any palindrome substrings of length greater than 11.

Orpheus can modify the poem by replacing a letter at any position with any lowercase English letter. He can use this operation arbitrarily many times (possibly zero). Since there can be many palindromes in his poem, he may have to make some corrections. But how many, exactly? Given the poem, determine the minimal number of letters that have to be changed so that the poem does not contain any palindromes of length greater than 11.

Input

The first line of the input contains a single integer tt (1≤t≤1051≤t≤105) denoting the number of test cases, then tt test cases follow.

The first and only line of each test case contains a non-empty string of lowercase English letters, Orpheus' poem.

The sum of the length of Orpheus' poems in all test cases will not exceed 105105.

Output

You should output tt lines, ii-th line should contain a single integer, answer to the ii-th test case.

Example

input

Copy

7
babba
abaac
codeforces
zeroorez
abcdcba
bbbbbbb
a

output

Copy

1
1
0
1
1
4
0

Note

In the first test case, we can replace the third character with c and obtain a palindrome-less poem bacba.

In the second test case, we can replace the third character with d and obtain a palindrome-less poem abdac.

In the third test case, the initial poem already doesn't contain any palindromes, so Orpheus doesn't need to change anything there.

code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string S;
    int T;
    cin >> T;
    while(T--)
    {
        S.clear();
        cin >> S;
        int ans  = 0;
        for(int i = 0; i < S.size(); i++)
        {
            if(i > 0 &&S[i] == S[i - 1] && S[i] != '0')
                S[i] = '0';
            else if(i > 1 && S[i] == S[i - 2] && S[i] != '0')
                S[i] = '0';
        }
        for(int i = 0; i < S.size(); i++)
            if(S[i] == '0')
                ans++;
        cout << ans << endl;
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值