Codeworks round 757 (Div. 2) problem solving Report

Portal

C o d e f o r c e s   R o u n d   757   ( D i v . 2 ) \rm Codeforces ~ Round ~ 757 ~ (Div. 2) Codeforces Round 757 (Div.2)

A Divan and a Store

have n n n items, No i i The price of i commodities is a i a_i ai​.

Here you are l l l and r r r. Indicates that the merchant will only choose the price in [ l , r ] [l,r] Items in [l,r].

Businessmen have k k k yuan, ask you how many goods a businessman can buy at most.

common T T Group T data.

about 100 % 100\% 100% data, guaranteed 1 ≤ T ≤ 100 , 1 ≤ n ≤ 100 , 1 ≤ l ≤ r ≤ 1 0 9 , 1   l e q k ≤ 1 0 9 , 1 ≤ a i ≤ 1 0 9 1\leq T \leq 100,1 \leq n \leq 100,1 \leq l \leq r \leq 10^9,1 \ leq k \leq 10^9,1 \leq a_i \leq 10^9 1≤T≤100,1≤n≤100,1≤l≤r≤109,1 leqk≤109,1≤ai​≤109.

sol

Greedy, buy the remaining goods with the lowest price every time.

First, select the items that meet the conditions and put them in the priority queue (minimum priority).

Then pop up one by one and calculate the amount of money > k > k >K or the queue is empty, end the program.

Time complexity O ( T n log ⁡ n ) \mathcal O(Tn \log n) O(Tnlogn).

#include <bits/stdc++.h>

using namespace std;

#define int long long

int t;

int n, l, r, k;

signed main()
{
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &n, &l, &r, &k);
        priority_queue<int, vector<int>, greater<int>> q;
        for (int i = 1, a; i <= n; ++i)
        {
            scanf("%lld", &a);
            if (a < l || a > r)
                continue;
            q.push(a);
        }
        int ans = 0, sum = 0;
        while (!q.empty())
        {
            int now = q.top();
            q.pop();
            sum += now;
            // cout << now << "\n";
            if (sum <= k)
                ans++;
            else
                break;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

B Divan and a New Project

In a street, cover n + 1 n+1 n+1 building, the coordinates of each building are x i x_i xi, satisfied ∀ j ≠ i , x i ≠ x j \forall j \ne i,x_i \ne x_j ∀ j  = i,xi  = xj , and x i x_i xi is an integer.

Move all buildings from 0 0 0 to n n n label.

Someone is numbered 0 0 0 building, respectively, to be numbered i i i. buildings a i a_i ai times, this person's round-trip number is i i The time taken for a trip to the building of i is 2 × ( ∣ x i − x 0 ∣ ) 2 \times(|x_i - x_0|) 2×(∣xi​−x0​∣).

Please arrange the coordinates of each building to minimize the time spent by this person.

common T T Group T data.

about 100 % 100\% 100% data, guaranteed 1 ≤ T ≤ 1 0 3 , 1 ≤ n ≤ 2 ∗ 1 0 5 , 0 ≤ a i ≤ 1 0 6 1\leq T \leq 10^3,1 \leq n \leq 2*10^5,0 \leq a_i \leq 10^6 1≤T≤103,1≤n≤2∗105,0≤ai​≤106.

sol

First, we number 0 0 The coordinates of the building of 0 are set as 0 0 0 is convenient to do.

Remember the answer as a n s ans ans, then a n s = ∑ i = 1 n 2 ∗ a i ∗ ∣ x 0 − x i ∣ ans=\sum\limits_{i=1}^{n}{2*a_i*|x_0-x_i|} ans=i=1∑n​2∗ai​∗∣x0​−xi​∣.

To make a n s ans At least, let a i a_i ai large building distance x 0 x_0 x0 , near.

So according to a i a_i ai , sort from large to small, in x 0 x_0 x0. Just put the building in the order of left and right.

See the code for the specific implementation.

Time complexity O ( T n ) \mathcal O(Tn) O(Tn).

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int _ = 2e5 + 7;

int t;

int n;

struct abc
{
    int v, id;
} a[_];

int b[_];

int ans;

bool cmp(abc a, abc b)
{
    return a.v > b.v;
}

signed main()
{
    scanf("%lld", &t);
    while (t--)
    {
        ans = 0;
        scanf("%lld", &n);
        for (int i = 1; i <= n; ++i)
            scanf("%lld", &a[i].v), a[i].id = i;
        sort(a + 1, a + n + 1, cmp);
        int k = 0;
        for (int i = 1; i <= n; i += 2)
        {
            b[a[i].id] = ++k;
        }
        k = 0;
        for (int i = 2; i <= n; i += 2)
        {
            b[a[i].id] = --k;
        }
        for (int i = 1; i <= n; ++i)
        {
            ans += 2 * a[i].v * abs(b[a[i].id]);
        }
        cout << ans << "\n";
        cout << "0 ";
        for (int i = 1; i <= n; ++i)
            cout << b[i] << " ";
        cout << "\n";
    }
    return 0;
}

C Divan and bitwise operations

There is an unknown sequence a a a. Now know m m m pieces of information, each star information is given in the form of l r x, representing the interval [ l , r ] [l,r] The bitwise or of [l,r] is x x x. Guarantee a a Each number in a is included in the interval [ l , r ] [l,r] [l,r] at least once.

Please output the sequence a a Exclusive or sum of all subsequences of a   m o d   ( 1 0 9 + 7 ) \bmod (10^9+7) mod(109+7).

common T T Group T data.

about 100 % 100\% 100% data, guaranteed 1 ≤ T ≤ 1 0 3 , 1 ≤ n , m ≤ 2 ∗ 1 0 5 , 1 ≤ l ≤ r ≤ n , 0 ≤ x ≤ 2 30 − 1 1\leq T \leq 10^3,1 \leq n,m \leq 2*10^5,1 \leq l \leq r \leq n,0 \leq x \leq 2^{30}-1 1≤T≤103,1≤n,m≤2∗105,1≤l≤r≤n,0≤x≤230−1.

sol

Obviously, we can get the bitwise or all of the entire sequence x x The bitwise or of x is set to S S S.

If S S Section of S i i i bit is 0 0 0, contribution is 0 0 0.

Otherwise there will always be one 1 1 1. Exactly one of the corresponding contributions is 2 i 2^{i} 2i, the total contribution is 2 i × 2 n − 1 2^{i}\times2^{n-1} 2i×2n−1.

that A n s = S × 2 n − 1 Ans=S \times 2^{n-1} Ans=S×2n−1.

Time complexity O ( T n ) \mathcal O(Tn) O(Tn).

See the code for the specific implementation.

#include <bits/stdc++.h>

#define int long long

using namespace std;

inline int read()
{
    int s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar())
        if (c == '-')
            w = -1;
    for (; isdigit(c); c = getchar())
        s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}

const int mod = 1e9 + 7;

inline int qpow(int x, int y)
{
    int res = 1;
    while (y)
    {
        if (y & 1)
            res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}

int T, n, m;

signed main()
{
    T = read();
    while (T--)
    {
        n = read(), m = read();
        int ans = 0;
        while (m--)
        {
            read(), read();
            ans = ans | read();
        }
        ans = ans * qpow(2, n - 1) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}

D Divan and Kostomuksha

Given sequence a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1, a2,..., an, rearrangement required a a a. Make

∑ i = 1 n gcd ⁡ ( a 1 , a 2 , . . . , a i ) \sum\limits_{i=1}^n \gcd(a_1,a_2,...,a_i) i=1∑n​gcd(a1​,a2​,...,ai​)

maximum.

Output this maximum value.

sol

Obviously, a dp.

set up c n t i cnt_i cnti , represents an array a a Yes in a i i The number of elements that are multiples of i, d p i dp_i When dpi , is expressed, the included factor can be obtained i i i is the maximum value that can be obtained by the end arrangement.

There is a transfer equation
d p i = max ⁡ j = 1 f i × p r i j + i × ( c n t i − c n t i × p r i j ) dp_i=\max_{j=1}{f_{i \times pri_j} + i \times (cnt_i - cnt_{i \times pri_j})} dpi​=j=1max​fi×prij​​+i×(cnti​−cnti×prij​​)
The final answer is satisfaction c n t i = n cnt_i=n cnti = n d p i dp_i Maximum value of dpi #.

seek c n t cnt cnt can screen out all prime numbers before enumerating i i i and prime set p r i pri pri, calculation c n t i cnt_i cnti​.

Time complexity O ( w log ⁡ log ⁡ w ) \mathcal O(w \log \log w) O(wloglogw), where w w w is the value range.

See the code for the specific implementation.

#include <bits/stdc++.h>

using namespace std;

#define int long long

inline int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

const int _ = 2e7 + 1;

int n;

int ans;

int cnt[_ + 7];

int dp[_ + 7];

bool vis[_];

vector<int> primes;

void init()
{
    for (int i = 2; i < _; ++i)
    {
        if (!vis[i])
            primes.push_back(i);
        for (auto p : primes)
        {
            if (p * i > _)
                break;
            vis[p * i] = 1;
            if (i % p == 0)
                break;
        }
    }
}

signed main()
{
    init();
    n = read();
    for (int i = 1, x; i <= n; ++i)
        cnt[read()]++;
    for (auto p : primes)
        for (int j = _ / p; j >= 1; --j)
            cnt[j] += cnt[j * p];
    for (int i = _ - 1; i >= 1; --i)
    {
        dp[i] = cnt[i] * i;
        for (auto p : primes)
        {
            int j = p * i;
            if (j > _)
                break;
            dp[i] = max(dp[i], dp[j] + i * (cnt[i] - cnt[j]));
        }
        if (cnt[i] == n)
            ans = max(ans, dp[i]);
    }
    printf("%lld\n", ans);
    return 0;
}

Tags: CodeForces

Posted on Sun, 28 Nov 2021 01:03:12 -0500 by andrewb