table of Contents
Contest Info
Data:2019.6.30
Solved:4/7
Solutions
A. Stickers and Toys
Meaning of the questions:
There \ (A \) Items \ (s \) a, \ (B \) Items \ (t \) months, these items will now be loaded into the \ (n \) boxes, each box is only about three situations:
- Only a \ (A \) items
- Only a \ (B \) items
- A \ (A \) articles and a \ (B \) Item
Now you ask, how many boxes you want to take at least can guarantee you at least have a \ (A \) items and a \ (B \) items.
Ideas:
The pigeonhole principle, be apparent to \ (A \) article, taking at least \ (n - s + 1 \ ) a box can have a \ (A \) items.
Similarly, for the \ (B \) article to take at least \ (n - t + 1 \ ) boxes.
The answer is \ (Min (n - s +1 , n - t + 1) \)
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s, t;
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &s, &t);
int res = max(n - s + 1, n - t + 1);
printf("%d\n", res);
}
return 0;
}
B. Letters Shop
The meaning of problems:
there is a string \ (s \) , Always ask a string \ (t \) , ask a minimum of \ (s \) prefix character makes this prefix may have composed a string \ (t \) .
A thought:
can maintain a prefix and the number of characters, then two.
Code One:
#include <bits/stdc++.h>
using namespace std;
#define N 200010
int n, m, lens, lent;
char s[N], t[N];
int sum[N][27];
int cnt[27];
bool ok(int x) {
for (int i = 0; i < 26; ++i) {
if (sum[x][i] < cnt[i]) {
return 0;
}
}
return 1;
}
int main() {
while (scanf("%d", &n) != EOF) {
memset(sum, 0, sizeof sum);
scanf("%s", s + 1); lens = strlen(s + 1);
for (int i = 1; i <= lens; ++i) {
++sum[i][s[i] - 'a'];
for (int j = 0; j < 26; ++j) {
sum[i][j] += sum[i - 1][j];
}
}
scanf("%d", &m);
while (m--) {
scanf("%s", t + 1); lent = strlen(t + 1);
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= lent; ++i) {
++cnt[t[i] - 'a'];
}
int l = 1, r = n, res = -1;
while (r - l >= 0) {
int mid = (l + r) >> 1;
if (ok(mid)) {
r = mid - 1;
res = mid;
} else {
l = mid + 1;
}
}
printf("%d\n", res);
}
}
return 0;
}
Thinking II:
Maintenance \ (S \) string class of the characters of \ (I \) th location, will be apparent to \ (T \) string for each type of character has \ (X \) th words, \ (S \) length greater than or equal to the prefix string of such character \ () X \ a position where a.
Code II:
#include <bits/stdc++.h>
using namespace std;
#define N 200010
int n, m, lens, lent;
char s[N], t[N];
int sum[N][27];
int cnt[27];
bool ok(int x) {
for (int i = 0; i < 26; ++i) {
if (sum[x][i] < cnt[i]) {
return 0;
}
}
return 1;
}
int main() {
while (scanf("%d", &n) != EOF) {
memset(sum, 0, sizeof sum);
scanf("%s", s + 1); lens = strlen(s + 1);
for (int i = 1; i <= lens; ++i) {
++sum[i][s[i] - 'a'];
for (int j = 0; j < 26; ++j) {
sum[i][j] += sum[i - 1][j];
}
}
scanf("%d", &m);
while (m--) {
scanf("%s", t + 1); lent = strlen(t + 1);
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= lent; ++i) {
++cnt[t[i] - 'a'];
}
int l = 1, r = n, res = -1;
while (r - l >= 0) {
int mid = (l + r) >> 1;
if (ok(mid)) {
r = mid - 1;
res = mid;
} else {
l = mid + 1;
}
}
printf("%d\n", res);
}
}
return 0;
}
C. Vasya And Array
Meaning of the questions: