Codeforces Round 907 (Div. 2)

Codeforces Round 907 (Div. 2)

A

判断i不为2的幂时有无出现递减

#include 
 
using namespace std;
const int N = 2000;
int a[N];
 
void solve()
{
    int n;
    std::cin >> n;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        //i不为2的幂时出现递减
        if ((i & (i - 1)) != 0 && a[i] < a[i - 1]) {
            std::cout << "NO\n";
            return;
        }
    }
    std::cout << "YES\n"; 
}
 
int main()
{
    int T;
    cin >> T;
    while (T --)
    {
        solve();
    }
    return 0;
}

B

q的种类其实不会超过31个,因此直接模拟

#include 
 
using namespace std;
 
long long qp[31] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824};
long long book[31];
 
void solve()
{
    int n, q;
    cin >> n >> q;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    while (q--)
    {
        int x;
        cin >> x;
        if (book[x])
            continue;
        book[x] = 1;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] % qp[x] == 0)
            {
                a[i] += qp[x - 1];
            }
        }
    }
    memset(book, 0, sizeof(book));
    for (int i = 1; i <= n; i++)
        cout << a[i] << " ";
    cout << endl;
}
 
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

C

排序后平A从前往后打小怪,大招从后往前打大怪
发现一半的伤害是大招打出,一半的伤害是平A打出,可以简便计算

#include 

using namespace std;
const int N = 2e5 + 5;
int a[N];

void solve()
{
    long long n, sum = 0;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    //对前一半的用A的到的连击数
    sum /= 2; 
    sort(a.begin() + 1, a.end());
    long long ans = 0;
    for (int i = n; i >= 1; i--)
    {   
        //大招
        if (sum >= a[i])
            sum -= a[i], a[i] = 0, ans++;
        else if (sum)
            a[i] -= sum, sum = 0, ans++;
    }
    //A
    for (int i = 1; i <= n; i++)
        ans += a[i];
    cout << ans << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T --)
    {
        solve();
    }
    return 0;
}

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