Educational Codeforces Round 129 (Rated for Div. 2)
Educational Codeforces Round 129 (Rated for Div. 2)
https://codeforces.com/contest/1681
A. Game with Cards
直接比较最大的谁更大,谁就是赢家。
相等情况下,先手胜
#include <bits/stdc++.h>
using namespace std;
void solve () {
int n, m;
int a = 0, b = 0;
cin >> n;
for (int i = 1; i <= n; i ++) {
int x;
cin >> x;
a = max (a, x);
}
cin >> m;
for (int i = 1; i <= m; i ++) {
int x;
cin >> x;
b = max (b, x);
}
if (a >= b)
cout << "Alice" << endl;
else
cout << "Bob" << endl;
if (a > b)
cout << "Alice" << endl;
else
cout << "Bob" << endl;
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//比较最大的
B. Card Trick
把前k个挪到后面去
由于挪动数组十分耗时,所以可以拿一个指针来前后跳动,挪k就相当于指针往后跳k格,超出了n的范围就往回跳
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
int a[N];
void solve () {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
cin >> m;
int st = 1;
for (int i = 1; i <= m; i ++) {
int x;
cin >> x;
st += x;
if (st > n)
st -= n;
}
//cout << st << ' ';
cout << a[st] << endl;
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//把前k个挪到后面去
C. Double Sort
写了一个特别慢的做法,低空飘过
memset就超时了
我真不理解,明明是一样的做法,我比别人慢100倍
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 105;
int n, m, maxn = 1e4;
int a[N], b[N];
void test () {
for (int i = 1; i <= n; i ++)
cout << a[i] << ' ';
cout << endl;
for (int i = 1; i <= n; i ++)
cout << b[i] << ' ';
cout << endl;
}
void solve () {
vector <pii> ans;
int cnt = 0;
cin >> n;
//memset (ans, 0, sizeof ans);
for (int i = 1; i <= n; i ++)
cin >> a[i];
for (int i = 1; i <= n; i ++)
cin >> b[i];
int tmp = n;
while (tmp --) {
for (int i = 1; i < n; i ++)
if (a[i] > a[i + 1] || b[i] > b[i + 1]) {
swap (a[i], a[i + 1]);
swap (b[i], b[i + 1]);
ans.push_back ({i, i + 1});
}
}
if (!is_sorted (a + 1, a + n + 1) || !is_sorted (b + 1, b + n + 1)) {
cout << -1 << endl;
return ;
}
cout << ans.size () << endl;
for (int i = 0; i < ans.size (); i ++)
cout << ans[i].first << ' ' << ans[i].second << endl;
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//不要用memset,会超时
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 105;
int n, m, maxn = 1e4;
int a[N], b[N];
void test () {
for (int i = 1; i <= n; i ++)
cout << a[i] << ' ';
cout << endl;
for (int i = 1; i <= n; i ++)
cout << b[i] << ' ';
cout << endl;
}
void solve () {
vector <pii> ans;
int cnt = 0;
cin >> n;
//memset (ans, 0, sizeof ans);
for (int i = 1; i <= n; i ++)
cin >> a[i];
for (int i = 1; i <= n; i ++)
cin >> b[i];
if (is_sorted (a + 1, a + n + 1) && is_sorted (b + 1, b + n + 1)) {
cout << 0 << endl;
return;
}
for (int i = 1; i <= n; i ++) {
if (cnt > maxn) {
cout << -1 << endl;
return;
}
if (is_sorted (a + 1, a + n + 1) && is_sorted (b + 1, b + n + 1))
break;
for (int j = 1; j <= n; j ++) {
if ((a[i] < a[j]) || (a[i] == a[j] && b[i] < b[j])) { //key
swap (a[i], a[j]);
swap (b[i], b[j]);
ans.push_back ({i, j});
}
}
}
//test ();
if (!is_sorted (b + 1, b + n + 1) || !is_sorted (a + 1, a + n + 1)) {
cout << -1 << endl;
return ;
}
cout << ans.size () << endl;
for (int i = 0; i < ans.size (); i ++)
cout << ans[i].first << ' ' << ans[i].second << endl;
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
D. Required Length
感觉是个dp,但是想不出状态转移方程,最后bfs也能过
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
int n, x;
int op, maxn, minn;
queue <pii> q;
map<int, int> mp;
int bfs () {
q.push ({x, 0});
bool vis[10];
while (!q.empty ()) {
memset (vis, false, sizeof vis);
auto t = q.front();
q.pop();
int num = t.first, cnt = t.second;
if (num >= minn) {
return cnt;
}
int tmp = num, p = 0;
while (tmp) {
p = tmp % 10;
tmp /= 10;
if (p && p != 1 && !vis[p]) {
vis[p] = true;
if (mp[num * p] == 1)
continue;
mp[num * p] =1;
q.push ({num * p, cnt + 1});
}
}
}
return -1;
}
signed main () {
cin >> n >> x;
minn = (int)pow (10, n - 1);
cout << bfs () << endl;
}
//直接bfs
//不一定选最大的相乘,考虑dp
//每次都选择最大的位数相乘
//op超过某个临界值仍然无法达成就-1