1. 程式人生 > 實用技巧 >2018 黑龍江省大學生程式設計競賽

2018 黑龍江省大學生程式設計競賽

A - A Count Task

題面:

Count is one of WNJXYK’s favorite tasks. Recently, he had a very long string and he wondered that how many substrings which contains exactly one kind of lowercase in this long string. But this string is so long that he had kept counting for several days. His friend Kayaking wants to help him, so he turns to you for help.

InputThe input starts with one line contains exactly one positive integer $T$ which is the number of test cases.

Each test case contains one line with a string which you need to do a counting task on.OutputFor each test case, output one line containing “y” where y is the number of target substrings.Sample Input

3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa

Sample Output

10
30
55

Hint

1<=T<=20,1<=len(string)<=10^5,1<=∑len(string)<=10^5
Strings only contain lowercase English letters.




G - Flower

題面:

Rabbit loves flowers very much and she plants n pots of flowers in her house. But she never prunes them because she is lazy. So the flowers have different heights and look ugly. One day, Kayaking decides to prune the flowers to make them look beautiful. To make them have equal heights, smart Kayaking has a wonderful idea. Initially, the i-th pot of flower’s height is a[i]. Each time, Kayaking will select n-1 pots of flowers and prune them to make their height subtract 1 with his 49m knife. Exactly, the height of the flowers is at least 1. Now, Kayaking wants to know if it is possible to prune the flowers to make them have equal height. If possible, what is the minimum times he prunes the flowers. Could you tell him? InputThe input starts with a line contains exactly one positive number T which is the number of test case.Each test case starts with a line contains a number n indicates the number of flowers. Then there is a line contains n numbers indicates a[i].OutputFor each test case, print a single line containing one integer—the minimum times Kayaking prunes the flowers, or -1 if it is impossible.
Sample Input
2
5
1 2 2 2 2
5
1 2 2 2 3
Sample Output
1
-1
Hint
T<=10,n<=10^5,ai<=10^9


題解:

#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5+7 ;
#define ll long long
ll n,m,t,k,ans,cnt;
void solve() 
{
    for(cin>>t;t;t--){
        cin>>n;
        ans = 0 ; k = 0 ; cnt = -1 ;
        for(int i=1;i<=n;i++){
            cin>>k;
            ans += k ;
            cnt = max(cnt,k);
        }    
        if(n*cnt<cnt+ans)
            cout<<abs(ans-n*cnt)<<endl;
        else 
            cout<<-1<<endl;
    }    
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve();
}
View Code