首页 > 其他分享 > Educational Codeforces Round 3

Educational Codeforces Round 3

时间:2022-12-18 12:44:32浏览次数:54  
标签:Educational int ll mid long Codeforces ss include Round

Educational Codeforces Round 3

https://codeforces.com/contest/609
3/6:ABC
D赛后过了
CD题解等下写

A. USB Flash Drives

前缀和

#include <bits/stdc++.h>

using namespace std;
const int N = 105;
int n, a[N];

int main () {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)        cin >> a[i];
    sort (a + 1, a + n + 1, greater<int>());
    for (int i = 1; i <= n; i++) {
        m -= a[i];
        if (m <= 0) {
            cout << i;
            break;
        }
    }
}

B. The Best Gift

容斥思想?
任意 \(n\) 个数中选两个的方案数是 \(C_{n}^{2}\)
其中除去各相同的方案数就是答案。

#include <bits/stdc++.h>
#define int long long

using namespace std;
int a[15];

signed main () {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        a[x] ++;
    }
    int ans = n * (n - 1) / 2;
    for (int i = 1; i <= m; i++) {
        ans -= a[i] * (a[i] - 1) / 2;
    }
    cout << ans;
}

C. Load Balancing

小思维,考虑到总和是不变的。所以最终所有数字的局面是固定的,那么只需排序之后计算差值即可。
最终局面就是:

#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 1e5 + 5;
int a[N], sum, ans, n;

signed main () {
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> a[i], sum += a[i];
    sort (a + 1, a + n + 1);
    //cout << sum << endl;
    int val = sum / n, cnt = sum - val * n;
    //cout << val << ' ' << cnt << endl; //有n - cnt个val, cnt个val + 1
    for (int i = 1; i <= n - cnt; i++) {
        ans += abs (a[i] - val);
    }
    for (int i = n - cnt + 1; i <= n; i++) {
        ans += abs (a[i] - (val + 1));
    }
    cout << ans / 2 << endl;
}

D. Gadgets for dollars and pounds

二分。

// LUOGU_RID: 97555092
#include <bits/stdc++.h>
#define ll long long

using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 5, inf = 1e9;
int n, m, k, s;
pair<ll, int> a[N], b[N];
int ss[3], id[3];
pii result[N];

struct Node {
    int id, type;
    ll cost;
}p[N];

bool cmp (Node p1, Node p2) {
    ll sum1 = 0, sum2 = 0;
    return 1ll * ss[p1.type] * p1.cost < 1ll * ss[p2.type] * p2.cost;
}

bool check (int mid) {
    id[1] = a[mid].second, id[2] = b[mid].second;
    ss[1] = a[mid].first, ss[2] = b[mid].first;
    sort (p + 1, p + m + 1, cmp);
    //for (int i = 1; i <= m; i++)    cout << ss[p[i].type] * p[i].cost << ' ';
    //cout << endl;
    ll sum = 0;
    for (int i = 1; i <= k; i++) {
        sum += 1ll * ss[p[i].type] * p[i].cost;
        //cout << sum << ' ';
        if (sum > s)    return false;
    }
    for (int i = 1; i <= k; i++) {
        result[i].first = p[i].id;
        result[i].second = id[p[i].type];
    }
    return true;
}

signed main () {
    cin >> n >> m >> k >> s;
    ll mina = 1e9, minb = 1e9;
    int id1, id2;
    for (int i = 1; i <= n; i++) {
        ll x;   cin >> x;
        if (x < mina)   mina = x, id1 = i;
        a[i] = {mina, id1};
    }
    for (int i = 1; i <= n; i++) {
        ll x;   cin >> x;
        if (x < minb)   minb = x, id2 = i;
        b[i] = {minb, id2};
    }

    for (int i = 1; i <= m; i++) {
        int x;
        ll y;
        cin >> x >> y;
        p[i] = {i, x, y};
    }
   
    int l = 1, r = n, ans = -1;
    while (l <= r) {
        int mid = l + r >> 1;
        if (check (mid)) {
            ans = mid, r = mid - 1;
        }
        else    l = mid + 1;
        //cout << l << ' ' << r << endl;
    }
    if (ans == -1)     cout << "-1\n";
    else {
        cout << ans << endl;
        for (int i = 1; i <= k; i++)  cout << result[i].first << ' ' << result[i].second << endl;
    }
}


//n天,m物品,买k个,现有s元
//两种货币a,b
//给出n天对应的汇率以及所有物品的两种价格
//最快能在第几天把k件物品买完,在哪天买的第几种物品
//一种商品只能购买一次,但是一天可以买多种商品。
//钱是可以一次性转换的

E. Minimum spanning tree for each edge

F. Frogs and mosquitoes

标签:Educational,int,ll,mid,long,Codeforces,ss,include,Round
From: https://www.cnblogs.com/CTing/p/16990185.html

相关文章

  • Codeforces Round #838 (Div. 2)
    题目链接A核心思路:首先我们得清楚一个点,那么就是如果有不符合条件的数列,那么我们也只有以下两种操作:把其中一个偶数变为奇数把其中一个奇数变为偶数所以这个问题就......
  • Educational Codeforces Round 140 D
    D.Playoff题链我们观察样例或者自己想一想就可以知道这个答案一定是一段连续的数字那么我们考虑如何确定这个答案的上下界即可而且只要给出的字符串s有一个0那么我......
  • CodeForces-300#B 题解
    题意给定\(n\)个数,保证\(n\mid3\),要将这\(n\)个数分配到\(\dfrac{n}{3}\)个三元组,有\(m\)个要求\(a,b\),每个要求表示\(a,b\)要在同一个三元组里,求最后的分......
  • Codeforces Round 838 (Div. 2)
    B.MakeArrayGood题意:给定n个数,每次可以对其中一个数进行操作,其中,在操作数量不超过n的前提下,构造一种操作使得任意两个数中,大的数可以被小的数整除。分析:结论:所......
  • 「Editorial」Codeforces Contest 1149
    C.TreeGenerator™容易发现树上一条路径一定形如))...)((...(。也就是对于任意子段,去掉匹配了的括号后还剩下的部分。而这个东西还是不太好表示,我们有如下引理:这个值......
  • Codeforces Round #837 (Div. 2)
    A.HossamandCombinatorics(CF1771A)题目大意给定一个长度为\(n\)的数组\(a\),问有多少个数对其差的绝对值等于该数组的极差。解题思路若最大值和最小值相等,则答案......
  • Codeforces Round #838 (Div. 2) D
    D.JourneytoUn'Goro题链考虑一个三元组内一定可以排除一个非0的xyz我们询问xz和yz要是gx==gy那么我们的z一定不是0否则gx=pxgy=py排除z要是gx!=gy那么......
  • Educational Codeforces Round 2
    EducationalCodeforcesRound2https://codeforces.com/contest/6003/6:ABDA.ExtractNumbers小模拟。把一个字符分成两部分输出,遇到';'或','视为单词分隔符,非负......
  • Codeforces Round #838 (Div. 2) D. GCD Queries
    题意有个长度为n的排列p,[0,1,2,...n-1],你可以进行至多2*n次询问,每次询问两个i,j,返回gcd(pi,pj),让你在规定时间内猜出0在哪两个位置之一思路这是一道交互题,询问的上限是2n......
  • Educational Codeforces Round 139 (Rated for Div. 2)
    EducationalCodeforcesRound139(RatedforDiv.2)数组开小,身败名裂场。CF1766AExtremelyRound直接前缀和递推预处理一下每个\(n\)的答案,询问直接输出即可。CF......