HHTC_学校集训编程题目(10)(个人赛_4)
记录记录这几天做的题目
其实个人赛已经结束了,组队赛都开始了,太懒,现在才记录
简单题就不记录了,记录一下有点收获的题目
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.
Input
The 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.
Output
For 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
这道题目主要就是利用等差公式,因为字符串的字串个数可以利用等差公式来求
题目意思看懂了就好,其实也不难,主要就是能不能想到利用等差公式
AC代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
char s[100005];
int main(){
int n;
cin>>n;
while(n--){
long long ans = 0;
memset(s,0,sizeof(s));
cin>>s;
for(int i=0;s[i];i++){
long long cnt = 1;
while(s[i+1] && s[i] == s[i+1]){
cnt++;
i++;
}
ans += (cnt + 1) * cnt / 2;
}
cout<<ans<<endl;
}
return 0;
}
F - Similar Strings
Putting two similar strings together will create a very strong power that can quake the earth into parts and the person who did that can be called the Destroyer of the World. In order to stop the Destroyer of the World, WNJXYK decided to check every string in this world to make sure there is no pair of strings are similar. But some strings are too long that WNJXYK cannot judge whether they are similar to others, so he turns to you for help.
Now WNJXYK need you to determine the maximum s, for string A has a substring A’ and string B has a substring B’ whose length are both s and they differ on at most K positions.
Input
The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains three lines. The first line contains an integer K and each of the following two lines contains a string indicates stringA or stringB
Output
For each test case, output one line containing “y” where y is the maximum s.
Sample Input
3
3
qwertypoi
qwetyrio
0
qqqqq
qqqaqqqq
10
qwertyuiop
asdfghjklzxcvbnm
Sample Output
6
4
10
字符串匹配,与常规的有点不同,他给出了一个数字,表示有多少个字母可以不同
暴力枚举不知道会不会超时,利用尺取法进行匹配~~
AC代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
char str1[5005],str2[5005];
int ans,n;
void doit(char str1[],char str2[]){
int len = min(strlen(str1),strlen(str2));
int cnt = 0 , left ,right = 0; //cnt就是统计有多少个不同的字符
for(left = 0;left < len; left++){
while(right < len){
if(str1[right] != str2[right]){
if(cnt + 1 > n)
break;
cnt++;
}
right++;
}
ans = max(ans,right - left);
if(str1[left] != str2[left])
cnt--;
}
}
int main() {
int T;
scanf("%d",&T);
while(T--){
ans = 0;
scanf("%d",&n);
scanf("%s%s",str1,str2);
int len = strlen(str1);
for(int i=0;i<len;i++)
doit(str1 + i, str2);
len = strlen(str2);
for(int i=0;i<len;i++)
doit(str2 + i, str1);
printf("%d\n",ans);
}
return 0;
}
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?
Input
The 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].
Output
For 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
剪花,每次只能剪n-1朵花,问能不能剪成一样高的花
思维题啊,每次只能剪n-1朵花,那么规定n-1朵不动,剩下的一朵花加1
又由于最高的花肯定是一直在减少的,所以统计所有的花与最高的花的差sum
sum<最高的花,那么就能剪完
AC代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
long long a[100005];
int main() {
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
long long sum = 0;
for(int i=0;i<n;i++)
sum += (a[n-1]-a[i]);
if(a[n-1]-sum<1)
printf("-1\n");
else
printf("%d\n",sum);
}
return 0;
}
H - Overflow
Kayaking is a naughty boy and he loves to play water. One day, Kayaking finds a bucket. The bottom area of the bucket is S and the height is H. Initially, there is V volume water in the bucket. What makes Kayaking happy is that there are N cube woods beside the bucket. The side length of the i-th cube woods is L[i] and its density is P[i]. Kayaking wants to put all the cube woods to the bucket. And then he will put a cover at the top of the bucket. But CoffeeDog doesn’t allow unless Kayaking can tell CoffeeDog the height of the water in the bucket after Kayaking put all the cuboid woods to the bucket. Could you help him?
It is guaranteed that the cube woods aren’t overlapping. And after putting the wood to the bucket, the bottom of the wood is parallel to the bottom of the bucket.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of cube woods.
Then comes N line. Each line has two real numbers L[i] and P[i].
The last line contains three integers S, H and V.
Output
For each test cases, print a single line containing one real number—the height of the water in the bucket(the number should be rounded to the second digit after the decimal point).
Sample Input
1
2
1 1.5
1 0.5
5 100 25
Sample Output
5.30
一道物理题目,回顾一下啊哈哈哈哈
就是求加入木块后水的高度,给出了木块的体积和密度,已知水的密度是1
那么如果木块的密度大于1,肯定是沉入水中的,否则浮在水上,浮在水上利用浮力公式
经过化简其实就是质量
AC代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int T,n;
double x,y;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
double sum = 0.0;
while(n--){
scanf("%lf %lf",&x,&y);
double v = x*x*x;
double m = x*y;
if(y >= 1.0){
sum += v;
}else{
sum += m;
}
}
double S,H,V;
scanf("%lf %lf %lf",&S,&H,&V);
sum = sum*1.0/S + V*1.0/S;
if(sum >= H)
printf("%.2lf\n",H);
else
printf("%.2lf\n",sum);
}
return 0;
}
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.
Input
The 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.
Output
For each test case, output one line with one number represents the minimum operations.
Sample Input
2
4
2 3 4 1
4
2 1 4 3
Sample Output
3
2
Hint
1<=T<=20,1<=n<=100000
题目意思就是求最小交换次数
就是把小的数先交换到前面,那么肯定交换次数最少了
嗯~~,这道题目怎么说呢,一开始思路就走错了!!
一开始用啥结构体标记下标,结果做不出来,后来更改了方法,使用了map标记,成功解决
AC代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 5;
int a[maxn] = {0};
int main() {
int t, n, temp, cnt = 0;
map<int, int> ans;
scanf("%d", &t);
while (t--) {
cnt = 0;
ans.clear();
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &temp);
a[i] = temp;
ans[temp] = i;
}
for (int i = 1; i <= n; i++) {
if (a[i] != i) {
cnt++;
ans[a[i]] = ans[i];
temp = ans[i];
ans[i] = i;
swap(a[i], a[temp]);
}
}
printf("%d\n", cnt);
}
return 0;
}