Educational Codeforces Round 129 (Rated for Div. 2) A - D 题解
A. Game with Cards#
看最大的在谁那,谁就赢
如果最大的都一样,则先手赢
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define endl '\n'
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int a = 0;
int n;
cin >> n;
for(int i=0; i<n; i++){int x; cin >> x; a = max(a, x);}
int m;
cin >> m;
int b = 0;
for(int i=0; i<m; i++) {int x; cin >> x; b = max(b, x);}
if(b > a) cout << "Bob" << endl;
else cout << "Alice" << endl;
if(a > b) cout << "Alice" << endl;
else cout << "Bob" << endl;
}
return 0;
}
B. Card Trick#
它的移动并不会改变牌之间的相对位置
也就是相当于把牌看做一个环,有一个指针指向第一个,如果洗了 n 张牌,则指针向后了 n 位
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define endl '\n'
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int num[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i=0; i<n; i++) cin >> num[i];
ll tp = 0;
int m;
cin >> m;
for(int i=0; i<m; i++)
{
int x;
cin >> x;
tp += x;
}
tp %= n;
cout << num[tp] << endl;
}
return 0;
}
C. Double Sort#
排序后的位置是唯一的,所以考虑排序后,a b 数组原来的位置能不能同时换到排序后的位置 if(a[j] == aa[i] && b[j] == bb[i] && vis[j] == 0
,如果所有数字都能,则证明有解
然后就选择排序一样,把原来的 a b 数组排到排好序的位置上,输出答案即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define endl '\n'
#define pii pair<int, int>
const ll maxn = 110;
const ll inf = 1e17 + 10;
int a[maxn], b[maxn], aa[maxn], bb[maxn];
int vis[maxn];
int num[maxn];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i=1; i<=n; i++) {cin >> a[i]; aa[i] = a[i];}
for(int i=1; i<=n; i++) {cin >> b[i]; bb[i] = b[i];}
for(int i=1; i<=n; i++) vis[i] = 0;
sort(aa + 1, aa + n + 1);
sort(bb + 1, bb + n + 1);
int f = 1;
for(int i=1; i<=n && f; i++)
{
int way = 0;
for(int j=1; j<=n; j++)
{
if(a[j] == aa[i] && b[j] == bb[i] && vis[j] == 0)
{
way = j;
break;
}
}
if(way == 0) f = 0;
else vis[way] = i;
}
if(f == 0) cout << -1 << endl;
else
{
vector<pii>ans;
for(int i=1; i<=n; i++)
{
int way = 0;
for(int j=i; j<=n; j++)
{
if(vis[j] == i)
{
way = j;
break;
}
}
if(i != way)
{
ans.push_back(make_pair(i, way));
vis[way] = vis[i];
}
}
cout << ans.size() << endl;
for(int i=0; i<ans.size(); i++)
cout << ans[i].first << " " << ans[i].second << endl;
}
}
return 0;
}
D. Required Length#
bfs 直接搜就好了
一开始还以为会爆 unsigned long long,在接近边界的时候还特判了一下
结果发现是小学数学没学好
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef unsigned long long ll;
#define endl '\n'
#define pii pair<ll, ll>
const ll maxn = 110;
const ll inf = 1e17 + 10;
map<ll, int>vis;
ll n, m, nn = 1;
ll cnt[11];
int bfs(ll s)
{
queue<pii>q;
q.push(make_pair(s, 0));
while(q.size())
{
pii now = q.front();
q.pop();
if(now.first == 0) return now.second;
if(vis[now.first]) continue;
vis[now.first] = now.second;
ll t = now.first, len = 0;
while(t)
{
cnt[t % 10] = 1;
t /= 10;
len++;
}
if(len >= n) return now.second;
for(int i=2; i<=9; i++)
if(cnt[i])
q.push(make_pair(now.first * i, now.second + 1));
for(int i=0; i<=9; i++) cnt[i] = 0;
}
return -1;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
cin >> n >> m;
for(int i=0; i<n; i++) nn *= 10;
cout << bfs(m) << endl;
return 0;
}
分类:
codeforces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】