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 Input3 qwertyuiop qqwweerrttyyuuiioopp aaaaaaaaaaSample Output
10 30 55Hint
1<=T<=20,1<=len(string)<=10^5,1<=∑len(string)<=10^5 Strings only contain lowercase English letters.
题解:
计算每个单词的对相同的单词段的贡献度,然后累和。
#includeusing namespace std; const int mxn = 1e5+7 ; #define ll long long ll n,m,t,k,ans,cnt; int a[mxn] , b[mxn] , c[2*mxn] , d[mxn] , vis[mxn]; void solve() { string str ; for(cin>>t;t;t--){ cin>>str; ans = 0 ; for(int i=0;i ){ k = 1; while(str[i]==str[i+1] && i<str.size()) k++ , i++ ; ans+=(1+k)*k/2; } cout< endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
E - A Hard Allocation
题面:
One day, WNJXYK came out an excellent idea that he could give his friends his special cakes which made of kiwis to eat in parties. But the process of making this kind of special cakes is very complex. One day, his friend Kayaking gave him a formulation which allows WNJXYK to produce one special cake with exact one kiwi in no time. Now let us have a big party over anyone’s imagination.
To simply this question we can assume that WNJXYK invited m friends to his house and he only had n kiwis this time. He wanted to produce special cakes with Kayaking’s formulation and give them to his friends.
Usually, it maybe not possible for people to get exactly same number of cakes in some situations. We can assume that people who received the most one get x cakes and people who received the least get y cakes. WNJXYK wondered that what is the minimal value of |x-y|?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 exactly two positive integer n,m which has been explained above.OutputFor each test case, output one line containing “y” where y is the number of minimal difference.Sample Input3 5 5 3 10 6 7Sample Output
0 1 1Hint
1<=T<=100,1<=n,m<=1000
题解:
#includeusing namespace std; const int mxn = 1e5+7 ; #define ll long long ll n,m,t,k,ans,cnt; void solve() { string str ; for(cin>>t;t;t--){ cin>>n>>m; if(n%m!=0) cout<<"1\n"; else cout<<"0\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
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 Input2 5 1 2 2 2 2 5 1 2 2 2 3Sample Output1 -1HintT<=10,n<=10^5,ai<=10^9
题解:
#includeusing 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 ans) cout< endl; else cout<<-1<<endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
J - The puzzle
题面:
Kayaking is playing a puzzle game containing n different blocks. He marks the blocks with integers from 1 to n, which show the blocks’ original positions. Each time he can exchange two blocks and he wants to know how many times he needs at least to restore the puzzle.InputThe input starts with one line contains exactly one positive integer which is the number of test cases.
Each test case contains two lines.
The first line contains an integer, which indicates the number of puzzle pieces.
The second line contains n different integers, the i-th number means the mark of the block in the i-th position.OutputFor each test case, output one line with one number represents the minimum operations.Sample Input2 4 2 3 4 1 4 2 1 4 3Sample Output
3 2Hint
1<=T<=20,1<=n<=100000
题解:
#includeusing namespace std; const int mxn = 1e5+7 ; #define ll long long ll n,m,t,k,ans,cnt; int a[mxn] ; void solve() { for(cin>>t;t;t--){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } ans = 0 ; while(1){ int ok = 1 ; for(int i=1;i<=n;i++){ if(i!=a[i]){ swap(a[i],a[ a[i] ]); ans++; ok = 0 ; } } if(ok){ cout< endl; break; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }