Codeforces-Round-650-Div-3

菜鸡的$div3$补题现场……

A:Short Substrings

  • 思路:

输出头尾 然后两个一组输出即可。

  • 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Copyright(c)
* Author : tiketiskte
**/
#include <bits/stdc++.h>
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
#define ll long long
#define SZ(X) (int)X.size()
#define INF 0x3f3f3f3f

using namespace std;

int t;
void solve() {
string a, b;
cin >> b;
int len = b.length();
cout << b[0];
for(int i = 1; i < len - 1; i +=2) {
cout << b[i];
}
cout << b[len - 1] << endl;
}
int main()
{
IOS
cin >> t;
while(t--) {
solve();
}
// system("pause");
return 0;
}

B:Even Array

  • 思路:

分别判断两组的$cnt$是否一样 如果一样 则可以互换$cnt$次,否则,输出$-1$

注意分开判断 一起判断不能确定能否互换。

  • 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Copyright(c)
* Author : tiketiskte
**/
#include <bits/stdc++.h>
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
#define ll long long
#define SZ(X) (int)X.size()
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 40 + 5;
int t;
int n, a[maxn];
void solve() {
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i++) {
if(i % 2 == 0 && a[i] % 2 != 0) {
cnt1++;
} else if(i % 2 != 0 && a[i] % 2 == 0) {
cnt2++;
}
}
if(cnt1 == cnt2) {
cout << cnt1 << endl;
} else {
cout << "-1" << endl;
}
}
int main()
{
IOS
cin >> t;
while(t--) {
memset(a, 0, sizeof(a));
cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
}
system("pause");
return 0;
}

C:Social Distance

  • 思路:

考虑每一个人实际占据了$k + 1$个位置,然后我们可以考虑如果在一个序列$s$的第一个为$1$和最后一个为$1$的位置,可以定义两个变量$l, r$来遍历。

考虑$s$全为0的特殊情况,注意向上取整 一般采用$+k$来操作,手动模拟一下即可理解。

然后$ans$ 分别加上$l$ $r$两端的可以坐下人的地方,向上取整,在计算过程中,先预留再取整抵消操作。

然后再从$l$ $r$开始,看连续的$0$有多少个 进行两边预留,注意向上取整。

细节太多….这种题目还是做不来呜呜呜

  • 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Copyright(c)
* Author : tiketiskte
**/
#include <bits/stdc++.h>
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
#define ll long long
#define SZ(X) (int)X.size()
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 2e5 + 5;

int t, n, k;
int ans;
char s[maxn];
void solve() {
cin >> n >> k;
cin >> s;
ans = 0;
int l = 0, r = n - 1;
for(;s[l] != '1' && l < n; l++);
for(;s[r] != '1' && r >= 0; r--);
cout << l << " " << r << endl;
if(l == n) {
cout << (n + k) / (k + 1) << endl;
return;
}
ans += l / (k + 1);
ans += (n - r - 1) / (k + 1);
int cnt = 0;
for(int i = l; i <= r; i++) {
if(s[i] == '1') {
ans += (cnt - 2 * k + k) / (k + 1);
cnt = 0;
} else {
cnt++;
}
}
cout << ans << endl;
}
int main()
{
IOS
cin >> t;
while(t--) {
solve();
}
system("pause");
return 0;
}

D: