A Spilit it!
Example
input
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
output
YES
NO
YES
NO
YES
NO
NO
题目大意:
在一个字符串中能否找到2k+1个子串,且有2k个回文串。
思路:
从字符串的两端开始遍历,记录字符相等的数量,最后进行判断,还有特判
#include<iostream>
#include<string>
using namespace std;
int main() {
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
string s1;
cin >> s1;
int l = 0, r = n - 1;
int ans = 0;
while (l < r)
{
if (s1[l] != s1[r])break;
ans++;
l++, r--;
}
if (k == 0) {
puts("yes");
continue;
}
else if (2 * k + 1 > n) {
puts("no");
continue;
}
if (ans >= k)puts("yes");
else puts("no");
}
}
B. Max and Mex
Example
input
5
4 1
0 1 3 4
3 1
0 1 4
3 0
0 1 4
3 2
0 1 2
3 2
1 2 3
output
4
4
3
5
3
···
题目大意:
对一段数字进行k次操作,每次选出最大值max和不在数组中出现的最小非负整数mex,并将两数之和的一半加入数组中,问最后有多少个不同的数字。
思路:
首先数据并不是规律的,先将数组排序, 通过一通找规律的操作我们发现,加入数组的数一定大于等于mex小于等于max,所以我们分成了几种情况,当mex大于等于max时,每次操作所生成的数在数组中都是不存在的,所以这时输出n+k,其他情况时,mex和max都不会改动,所以如果数组中存在这个数则直接输出数组的大小,否则输出数组的大小+1.(可以用map来实现统计不相同元素个数的操作)。
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
int a[100010];
map<long long, bool>mp;
int main() {
int t;
cin >> t;
while (t--)
{
mp.clear();
int n,k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]] = 1;
}
sort(a + 1, a + 1 + n);
ll m1 = 0, m2 = a[n];
long long res = mp.size();
if (k == 0) {
cout << res << endl;
continue;
}
for (int i = 1; i <= n; i++) {
if (m1 == a[i])
m1++;
else break;
}
long long ans = ceil((double)(m1 + m2) / 2);
if (m1 >= m2)cout << n + k << endl;
else {
if (mp[ans] == 1)
cout << res << endl;
else cout << res + 1 << endl;
}
}
}
C. Diamond Miner
Example
inputCopy
3
2
0 1
1 0
0 -1
-2 0
4
1 0
3 0
-5 0
6 0
0 3
0 1
0 2
0 4
5
3 0
0 4
0 -3
4 0
2 0
1 0
-3 0
0 -10
0 -2
0 -10
outputCopy
3.650281539872885
18.061819283610362
32.052255376143336
题目大意:
黄金矿工,每个矿工都在y轴上,所有的矿都坐落在x轴上,矿工到矿的坐标距离视为矿工的花费,矿工之间不能互相帮忙,每个矿工只能挖一个矿,请问所有矿工何时花费最小。
思路:
一道用几何知识的题目啊,首先,矿工和矿的位置,可能在正轴,也可能在负轴,既然如此,那我们就将其坐标取绝对值,(也就是按绝对值大小进行排序),此时按照序号一一对应的距离之和即为答案。(注意精度1e-15)。
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef pair<double, double>pii;
double a[100010], b[100010];
double dis(double x, double y)
{
return sqrt(x * x + y * y);
}
bool cmp(double x, double y) {
return fabs(x) < fabs(y);
}
int main() {
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
int k = 0, l = 0;
for (int i = 1; i <= n * 2; i++) {
double x, y;
scanf("%lf%lf", &x, &y);
if (x == 0)a[++k] = y;
else b[++l] = x;
}
sort(a + 1, a + l + 1, cmp);
sort(b + 1, b + k + 1, cmp);
double ans = 0;
for (int i = 1; i <= l; i++) ans += dis(a[i], b[i]);
printf("%.15lf\n", ans);
}
}
正正快补题!!!冲鸭!!!