GBC J3 / #1 题解
至于为什么 Round #1 不是 J1,因为 J1,J2 都已经有题了,J3 是第一套出好的题。
为了方便对拍,我甚至把官方的 Gen 放出来,小数据都打好了。
J3-T1
思路
一个有意思的条件,以 开头, 结尾。如果我们可以把字符串处理成 的形式,那两个字符串就相等了。
于是我们需要找到 使得 ,直接枚举即可。
Code
#include <iostream>
#include <cstring>
using namespace std;
void sol(){
string a, b;
cin>>a>>b;
for(int i = 0;i < (int)a.size() - 1;i++){
if(a[i] == b[i] && a[i + 1] == b[i + 1] && a[i] == '0' && a[i + 1] == '1'){
cout<<"YES\n";
return;
}
}
cout<<"NO\n";
}
int main() {
int n;
cin>>n;
while(n--){
sol();
}
return 0;
}Python
def sol():
a = input().strip()
b = input().strip()
for i in range(len(a) - 1):
if a[i] == b[i] and a[i + 1] == b[i + 1] and a[i] == '0' and a[i + 1] == '1':
print("YES")
return
print("NO")
def main():
n = int(input().strip())
for _ in range(n):
sol()
if __name__ == "__main__":
main()Gen
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
srand(time(nullptr));
// int n = (int)10 - (rand() % 3);
int n = (int)2e3 - (rand() % 3);
cout<<n<<"\n";
while(n--){
// int x = (int)10 - 2 - (rand() % 5);
int x = (int)5e3 - 2 - (rand() % 5);
cout<<0;
for(int i = 1;i <= x;i++) cout<<rand() % 2;
cout<<"1\n";
cout<<0;
for(int i = 1;i <= x;i++) cout<<rand() % 2;
cout<<"1\n";
}
return 0;
}J3-T2
思路
这题其实并不难。可以发现 ,是一个非常小的范围。我们直接用 表示 有多少个。每次询问 的时候,从右往左枚举 在二进制下的最后一位,并将 备份一份到 并且进位。如果枚举到第 位 且 ,说明不能凑出 。
Code
#include <iostream>
using namespace std;
const int N = 35;
int a[N], b[N], n;
int t, x;
void sol(){
for(int i = 0;i <= 30;i++) b[i] = a[i];
for(int i = 0;i <= 30;i++){
if(x & (1 << i)){
if(b[i]) b[i]--;
else{
cout<<"NO\n";
return;
}
}
b[i + 1] += b[i] / 2;
}
cout<<"YES\n";
}
int main() {
cin>>n;
while(n--){
cin>>t>>x;
if(t == 1){
a[x]++;
}
else{
sol();
}
}
return 0;
}
Python
def sol():
b = a[:]
for i in range(31):
if x & (1 << i):
if b[i]:
b[i] -= 1
else:
print("NO")
return
b[i + 1] += b[i] // 2
print("YES")
def main():
n = int(input().strip())
for _ in range(n):
global x
t, x = map(int, input().strip().split())
if t == 1:
a[x] += 1
else:
sol()
if __name__ == "__main__":
N = 35
a = [0] * N
main()Gen
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
srand(time(0));
// int n = (int)10 - (rand() % 5);
int n = (int)1e5 - (rand() % 5);
cout<<n<<"\n";
while(n--){
int op = rand() % 2 + 1;
cout<<op<<" ";
if(op == 1){
// cout<<rand() % 5 + 1<<"\n";
cout<<rand() % 29 + 1<<"\n";
}
else{
// cout<<rand() % ((int)40 + 1)<<"\n";
cout<<rand() % ((int)1e9 + 1)<<"\n";
}
}
return 0;
}
J3-T3
思路
假设给定 ,考虑如果 的结果与 的结果一样,那么说明 所真正排序的区间是一样的。
那它们真正排序的区间是「 以后的第一个 」到「 以前的第一个 」
最后求出真正排序的区间,用 map 去重即可。
Code
// c++14
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 2e5 + 5;
map<pair<int, int>, int> vis;
int n, m;
int nxt[N], pre[N];
char str[N];
void sol(){
vis.clear();
int ans = 0;
cin>>n>>m;
cin>>(str + 1);
nxt[n + 1] = n + 1;
for(int i = 1;i <= n;i++){
if(str[i] == '1') pre[i] = pre[i-1];
else pre[i] = i;
}
for(int i = n; i; i--){
if(str[i] == '1') nxt[i] = i;
else nxt[i] = nxt[i + 1];
}
while(m--){
int l, r;
cin>>l>>r;
l = nxt[l], r = pre[r];
if(l > r) l = r = 0;
if(!vis[{l, r}]) ans++, vis[{l, r}] = 1;
}
cout<<ans<<"\n";
}
int main() {
int T;
cin>>T;
while(T--){
sol();
}
return 0;
}Python
def sol():
vis.clear()
ans = 0
n, m = map(int, input().strip().split())
str_input = input().strip()
str_input = ' ' + str_input # This mimics the C++ 1-based index adjustment
nxt[n + 1] = n + 1
for i in range(1, n + 1):
if str_input[i] == '1':
pre[i] = pre[i - 1]
else:
pre[i] = i
for i in range(n, 0, -1):
if str_input[i] == '1':
nxt[i] = i
else:
nxt[i] = nxt[i + 1]
for _ in range(m):
l, r = map(int, input().strip().split())
l = nxt[l]
r = pre[r]
if l > r:
l = r = 0
if (l, r) not in vis:
ans += 1
vis[(l, r)] = 1
print(ans)
def main():
T = int(input().strip())
for _ in range(T):
sol()
if __name__ == "__main__":
N = int(2e5 + 5)
nxt = [0] * (N + 1)
pre = [0] * (N + 1)
vis = {}
main()Gen
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
srand(time(nullptr));
// int t = 5;
int t = (int)1e4 - (rand() % 5);
cout<<t<<"\n";
for(int _ = 1;_ <= t;_++){
// int n = 10;
int n = int(2e5) / t;
int m = n;
cout<<n<<" "<<m<<"\n";
for(int i = 1;i <= n;i++){
cout<<rand() % 2;
}
cout<<"\n";
while(m--){
int l = rand() % n + 1;
int r = l + rand() % (n - l + 1);
cout<<l<<" "<<r<<"\n";
}
}
return 0;
}