1. 程式人生 > 其它 >Educational Codeforces Round 129 (Rated for Div. 2)

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 ();
    }
}