比赛链接:Here
1562A. The Miracle and the Sleeper
题意:
- 给出
求出最大化的
(
)
思路:
很容易就看出 时
%3D%5Cleft%5Clfloor%5Cfrac%7Br-1%7D%7B2%7D%5Cright%5Crfloor#card=math&code=r%5Cbmod%20%5Cleft%28%5Cleft%5Clfloor%5Cfrac%7Br%7D%7B2%7D%5Cright%5Crfloor%2B1%5Cright%29%3D%5Cleft%5Clfloor%5Cfrac%7Br-1%7D%7B2%7D%5Cright%5Crfloor) 就是最大可能的答案,
但如果 的话最大值应该是
时间复杂度:#card=math&code=%5Cmathcal%7BO%7D%281%29)
void solve() {
ll l, r;
cin >> l >> r;
if (r / 2 >= l) cout << ((r - 1) / 2) << "\n";
else cout << r % l << "\n";
}
1562B. Scenes From a Memory
题意:
- 给出一个字符串
#card=math&code=s%5C%3B%20%28%7Cs%7C%5Cle50%29) 请问可以从字符串中删除的最大位数是多少使得字符串数字变为非素数? 如:
删除第二位的
变为
思路:
说实话赛时想复杂了,因为在 的情况下其实素数并不多(实际也只要考虑
位数的情况就ok),即使直接去模拟情况都可以。
但利用素数筛就能很快AC了
bool prime[110];
int n;
string s;
void solve() {
for (int i = 0; i < n; i++) {
if (s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9') {
cout << 1 << endl;
cout << s[i] << endl;
return;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!prime[(s[i] - '0') * 10 + (s[j] - '0')]) {
cout << 2 << endl;
cout << s[i] << s[j] << endl;
return;
}
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
for (int i = 2; i < 100; ++i) {
prime[i] = 1;
for (int j = 2; j * j <= i; ++j)
if (i % j == 0)prime[i] = 0;
}
int _; for (cin >> _; _--;) {
cin >> n >> s;
solve();
}
}
1562C. Rings
题意:
- 给出一个
字符串,以及函数
%3A#card=math&code=f%28s%29%3A) 将 $01 $ 转为十进制整数,请找出两对下标
%2C(l2%2Cr_2)#card=math&code=%28l_1%2Cr_1%29%2C%28l_2%2Cr_2%29) 使得 %7D)%20%3D%20k%5Ctimes%20f(s%7B(l2%2Cr_2)%7D)#card=math&code=f%28s%7B%28l1%2Cr_1%29%7D%29%20%3D%20k%5Ctimes%20f%28s%7B%28l_2%2Cr_2%29%7D%29) 成立
思路:
麻了,前面应是没想到怎么去构造一个这样的等式,手写 和
等特殊情况的时候想到我只要定位到第一个
#card=math&code=0%28pos%29) 的位置就可以了,然后以
为界划分为两部分就肯定能构造
%7D)%20%3D%20k%5Ctimes%20f(s%7B(l_2%2Cr_2)%7D)#card=math&code=f%28s%7B%28l1%2Cr_1%29%7D%29%20%3D%20k%5Ctimes%20f%28s%7B%28l_2%2Cr_2%29%7D%29)
const int N = 1e5 + 10;
int main() {
// ! 记得注释掉,麻了,输入流乱了导致后面debug半天没找到问题
// cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
ll n; cin >> n;
char s[N];
scanf("%s", s + 1);
ll cnt0 = 0, cnt1 = 0;
for (int i = 1; i <= n; ++i) if (s[i] == '1') cnt1 += 1;
if (cnt1 == n) {
cout << "1 " << n - 1 << " 2 " << n << "\n";
continue;
}
int p = 0;
for (int i = 1; i <= n; ++i)
if (s[i] == '0') {
p = i;
break;
}
if (p - 1 < (n / 2)) cout << p << " " << n << " " << p + 1 << " " << n << "\n";
else cout << 1 << " " << p << " " << 1 << " " << p - 1 << "\n";
}
}
1562D. Two Hundred Twenty One (Easy and Hard)
题意:
题意待补
思路:
把 号转为
再维护前缀和,可以快速判断奇偶(D1)
核心在于利用前缀和进行二分(D2)
不过有人用 map + set
也过了就很神奇(TQL
// D1
const int N = 3e5 + 10;
int a[N], sum[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, q;
string s;
cin >> n >> q >> s;
int id = 0;
for (int i = 0; i < s.size(); ++i) {
if (i & 1) {
if (s[i] == '+') a[++id] = -1;
else a[++id] = 1;
} else {
if (s[i] == '-') a[++id] = -1;
else a[++id] = 1;
}
sum[id] = sum[id - 1] + a[id];
}
while (q--) {
int l, r; cin >> l >> r;
if (abs(sum[r] - sum[l - 1]) == 0) cout << "0\n";
else if (abs(sum[r] - sum[l - 1]) & 1) cout << "1\n";
else cout << "2\n";
}
}
}
// D2
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, q;
cin >> n >> q;
string _s;
cin >> _s;
vector<int> which(n);
for (int i = 0; i < n; i++) {
which[i] = (_s[i] == '-') != (i & 1);
}
vector<int> psum(n + 1, 0);
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + (which[i] ? -1 : 1);
}
while (q--) {
int l, r;
cin >> l >> r; l--; r--;
if (psum[r + 1] == psum[l]) cout << "0\n\n";
else {
if ((r + 1 - l) & 1) cout << "1\n";
else {
cout << "2\n";
cout << (r + 1) << ' ';
r--;
}
int d = min(psum[l], psum[r + 1]) + abs(psum[l] - psum[r + 1]) / 2;
// d -> d+1
int s = l;
int e = r + 1;
while (s + 1 < e) {
int m = (s + e) / 2;
if ((psum[m] <= d) == (psum[s] <= d)) s = m;
else e = m;
}
cout << s + 1 << "\n";
}
}
}
}