SMU Summer 2023 Contest Round 4
SMU Summer 2023 Contest Round 4
A. Telephone Number
满足第一个8后面存在10个字符即可
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n,m;
void solve(){
cin >> n;
string s;
cin >> s;
int p = s.find_first_of('8') ;
if(p > n - 11 || p == -1 || n < 11)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int Ke_scholar = 1;
cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}
B. Lost Numbers
因为只有6个数,所以可以直接用\(next\_permutation\)全排列暴力匹配,不要关闭流同步!!!
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n,m;
void solve(){
int res[5];
for(int i = 1;i <= 4;i ++){
cout << "? " << i << ' ' << i + 1 << endl;
fflush(stdout);
cin >> n;
res[i] = n;
}
int ans[] = {0,4,8,15,16,23,42};
do{
if(ans[1] * ans[2] == res[1] && ans[2] * ans[3] == res[2] && ans[3] * ans[4] == res[3] && ans[4] * ans[5] == res[4]){
cout << '!';
for(int i = 1;i <= 6;i ++)
cout << ' ' << ans[i];
cout << endl;
fflush(stdout);
break;
}
}while(next_permutation(ans + 1,ans + 7));
}
signed main()
{
// ios::sync_with_stdio(false);
// cin.tie(nullptr);cout.tie(nullptr);
int Ke_scholar = 1;
// cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}
C. News Distribution
用并查集维护每个人所在的圈子,然后循环每个人所在的圈子\(++\)
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n,m;
void solve(){
cin >>n >> m;
vector<int> fa(n + 1);
vector<int> ans(n + 1,0);
iota(fa.begin(),fa.end(),0);
auto find = [&](auto self,int x)->int {
return fa[x] == x ? x : (fa[x] = self(self,fa[x]));
};
auto add = [&](int x,int y){
fa[find(find,x)] = find(find,y);
};
for(int i = 1;i <= m;i ++){
int k,o,p;
cin >> k;
if(k) cin >> o;
for(int j = 1;j < k;j++){
cin >> p;
add(o,p);
}
}
for(int i = 1;i <= n;i++)
ans[find(find,i)]++;
for(int i = 1;i <= n;i ++){
cout << ans[find(find,i)] << ' ';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int Ke_scholar = 1;
// cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}
D. Bicolored RBS
\(r\)和\(b\)左右括号每相隔一个交叉染色即可
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int zr,zb;
void solve(){
string s;
cin >> n >> s;
stack<char> r,b;
for(int i = 0;i < n;i ++){
if(s[i] == '('){
if(zr & 1){
cout << 0;
}else{
cout << 1;
}
zr++;
}else{
if(zb & 1){
cout << 0;
}else{
cout << 1;
}
zb++;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int Ke_scholar = 1;
// cin >> Ke_scholar;
while(Ke_scholar--)
solve();
return 0;
}

浙公网安备 33010602011771号