Codeforces Round 908 (Div. 2)

Codeforces Round 908 (Div. 2)

文章目录

  • Codeforces Round 908 (Div. 2)
    • A
    • B
    • C
    • D

A

获取对战的最后一个即为胜者

#include 

using namespace std;

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    cout << s[n-1] << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while(T --){
        solve();
    }
    return 0;
}

B

只要有两个出现两次以上的数字即为合法
对着两个数字分别使用两种组合即可

#include 

using namespace std;
const int N = 1e5;

int a[N];

void solve()
{
    int n;
    unordered_map<int, int> m;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        m[a[i]]++;
    }
    int s1 = -1, s2 = -1, cnt = 0;
    for (auto &&[x, y] : m)
    {
        if (y >= 2)
        {
            if (s1 == -1)
                s1 = x;
            else
                s2 = x;
            cnt++;
            if (cnt == 2)
                break;
        }
    }
    if (cnt < 2)
    {
        cout << "-1\n";
        return;
    }
    vector<int> k(5);
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == s1)
        {
            if (k[1] == 0)
            {
                cout << "1 ";
                k[1] = 1;
            }
            else if (k[2] == 0)
            {
                cout << "2 ";
                k[2] = 1;
            }
            else
                cout << "1 ";
        }
        else if (a[i] == s2)
        {
            if (k[3] == 0)
            {
                cout << "1 ";
                k[3] = 1;
            }
            else if (k[4] == 0)
            {
                cout << "3 ";
                k[4] = 1;
            }
            else
                cout << "1 ";
        }
        else
        {
            cout << "1 ";
        }
    }
    cout << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

C

执行操作后, x总是数组的最后一个元素。
若x大于n则非法
旋转操作至多进行min(n,k)次

#include 

using namespace std;

const int N = 2e5 + 10;

void solve()
{
    int n, k;
    cin >> n >>  k;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int last = n - 1;
    for (int i = 0; i < min(k,n); i++)
    {
        if (a[last] > n)
        {
            cout << "No\n";
            return;
        }
        last += n - a[last];
        if (last >= n)
            last -= n;
    }
    cout << "Yes\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

D

b数组顶多贡献一次反向排序再归并就是最小的方案

#include 

using namespace std;

const int N = 2e5 + 10;

void solve()
{
    int n , m;
    cin >> n >> m;
    vector<int> a(n), b(m), c(n + m);
    for(int i = 0 ; i < n ; i ++)cin >> a[i];
    for(int i = 0 ; i < m ; i ++)cin >> b[i];
    sort(b.rbegin() , b.rend()); //反向排序
    merge(a.begin(), a.end(), b.begin(), b.end(),c.begin(), greater<int>()); //归并排序
    for (int i = 0; i < n + m; i++) {
        cout << c[i] << ' ';
    }
  cout << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(算法,算法,数据结构,c++)