首页 > 其他分享 >codeforce 1700-1900

codeforce 1700-1900

时间:2024-03-06 21:33:36浏览次数:24  
标签:1700 return int long del codeforce ans 1900 define

3.6

Lonely Mountain Dungeons

 算贡献算了半天还错了, 这种采用容斥可以减少细节处理, 代码注释有

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
typedef long long ll;

void solve() {
    int n, b, x; cin >> n >> b >> x;
    // cout << n << b << x << endl;
    vector<int> a(n + 1);
    map<int, int> mp;
    for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
    int maxn = *max_element(a.begin() + 1, a.begin() + 1 + n), ans = 0;

    auto calc = [&](int x, int d) {
        // x 代表人数, d 代表组数
        // 假设全排成一排( x 个人到 x 组都是一个)
        int ans = x * (x - 1) / 2;
        // t 代表 x 人分为 d 组,每组都有的共同人数
        int t = x / d;
        // del代表有 del 组多出来一个人
        int del = x % d;
        // 1. 那么对于这些人数,我们组内没有贡献的,我们容斥需要减去这些贡献(C(t, 2))
        ans -= d * t * (t - 1) / 2;
        // 2. 有 del 组多出来一个人, 对于多出来的那个人,我们会减少的贡献是他组内的 t 个人
        ans -= del * t;
        return ans * b;
    };

    for (int i = 1; i <= maxn; i++) {
        int sum = -(i - 1) * x;
        // cout << "TEST " << i << endl;
        for (auto [id, v] : mp) {
            if (id <= i) {
                // cout << id << endl;
                sum += id * (id - 1) / 2 * b * v;
                continue;
            }
            sum += v * calc(id, i);
            // cout << v * calc(id, i) << ' ' << id << endl;
        }
        ans = max(ans, sum);
    }
    cout << ans << endl;
}   

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int T = 1; cin >> T;
    while (T--) solve();
    // solve();    

    return 0;
}
View Code

Microcycle

我们取一颗最大生成树, 那么一条非树边会产生一个环, 我们找到这样一条权值最小的边去 dfs 

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
typedef long long ll;

const int N = 2e5 + 100;

struct node {
    int x, y, v;
} e[N];

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

vector<int> g[N];

int fa[N], vis[N];
int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

vector<int> cir;
void dfs(int x, int t) {
    vis[x] = 1;
    cir.push_back(x);
    if (x == t) {
        cout << cir.size() << endl;
        for (auto i : cir) cout << i << ' ';
        cout << endl;
        return;
    }
    for (auto y : g[x]) {
        if (!vis[y]) dfs(y, t);
    }
    cir.pop_back();
}

void solve() {
    int n, m; cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y, v; cin >> x >> y >> v;
        e[i] = {x, y, v};
    }
    cir.clear();
    for (int i = 1; i <= n; i++) fa[i] = i, vis[i] = 0, g[i].clear();
    sort(e + 1, e + 1 + m, cmp);
    int minn = INT_MAX / 2, id;
    for (int i = 1; i <= m; i++) {
        auto [x, y, v] = e[i];
        int fx = find(x), fy = find(y);
        if (fx != fy) {
            fa[fx] = fy;
        } else {
            minn = min(minn, v);
            id = i;
        }
    }
    for (int i = 1; i <= m; i++) {
        if (i != id) {
            auto [x, y, v] = e[i];
            g[x].push_back(y);
            g[y].push_back(x);   
        }
    }

    cout << minn << ' ';
    dfs(e[id].x, e[id].y);
}   


signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int T = 1; cin >> T;
    while (T--) solve();

    return 0;
}
View Code

 

标签:1700,return,int,long,del,codeforce,ans,1900,define
From: https://www.cnblogs.com/zhujio/p/18057642

相关文章

  • Codeforces Round 931div2补题
    B.YetAnotherCoinProblem真[https://www.bilibili.com/video/BV1o2421M7pV/]不同硬币之间有倍数关系,使得一定数量后的小硬币可以被大硬币替代达到最优方案,而每个小硬币在最优下有可能的数量如下,进行枚举后找到最优方案。1:不多于2个(3个1会被3替代)3:不多于一个(2个3......
  • 1935.Codeforces Round 932 (Div. 2) - sol
    20240306逊哎~打一半去写申请书然后12点睡觉,相对成功!第二天早上起来把赛时发愣的C和F切了。CodeforcesRound932(Div.2)A.EntertainmentinMACCongratulations,youhavebeenacceptedtotheMaster'sAssistanceCenter!However,youwereextremelybore......
  • Codeforces Round 932 (Div. 2)
    CodeforcesRound932(Div.2)A-EntertainmentinMAC解题思路:如果翻转字符小于原字符,那么一直翻转即可。否则,翻转\(n-1\)次,然后添加一次。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;typedefdoubledb;......
  • Codeforces Round 930 (Div. 1)
    Preface虽然难得有Div1的CF,但由于在周四晚上学校要断电就没打这两天有空去补了下发现好像错过了最适合我的一场,AB都是一眼题,C是经典图论建模,D是DS典题没有Counting我直接爽飞,不过估计现场打的话写个ABC差不多了,D的码量还是挺大的A.BitwiseOperationWizard很有意思的一个......
  • CodeForces 1540D Inverse Inversions
    洛谷传送门CF传送门小清新题。首先容易发现每个合法的\(b\)唯一对应一个排列,大概就是每个时刻排列元素的相对顺序,然后插入到相应的位置。但是这样太麻烦了。发现题目只要求求单点的\(p\)值。这应该有更简单的方法。考虑令\(b_i\getsi-b_i\)表示\(p_i\)在前缀\(......
  • Codeforces edu 156 C题
    https://codeforces.com/contest/1886/problem/C思路这道题的核心问题是:给你一个字符串s,你要删除k个字母,你要找出删除k个字母后字典序最小的s。为了使字典序最小,我们就应该把字符串删成递增的样子stringtmp="";//tmp用来存删完后的字符串s+='$';//s的末尾加一个比'......
  • Educational Codeforces Round 162 E 点分治 虚树 树形dp
    传送门给出\(n\le2\cdot10^5\)的一棵树,每个节点有一个颜色。求出路径长度为\(2\)路径首端和尾端拥有相同颜色,且路径上其他点不存在相同颜色的点的路径条数。当时看错题了,把颜色抽出来后没法做了。后来感觉能点分治,然后把题看对了,遂写了一个极其抽象的点分治。除此之外,把某......
  • Codeforces Round 930 (Div. 1) C dij 建图
    离较好的方法差一点。考虑到了可以按照枚举属性并按照当前属性从小到大排序,这样可以从一个点到大另一个点。设当前在排序序列中点为\(i\)当\(i\)走向\(k,i>=k\)需要支付\(c_k\)的代价。而\(i\)到\(k,i<k\)则需\(k-i+c_k\)的代价。则对于不同的\(i\)由于代价没有连续性,当时想......
  • Codeforces Round 892 (Div. 2)
    \[\large\text{Round7:CodeforcesRound892(Div.2)(VP)}\]一言:所谓人,无论是谁到了最后,都会形单影只。——悠久之翼2最令人无语的是最后三分钟交代码的时候把\(\text{D}\)题交到了\(\text{E}\)题,结果能过的代码直接没有过。。\(\text{D:AndreyandEscapefr......
  • Educational Codeforces Round 120
    \[\large\text{Round1:EducationalCodeforcesRound120(VP)}\]一言:孤独的人不会伤害别人,只会不断地伤害自己罢了。——我的青春恋爱物语果然有问题\(\text{C:SetorDecrease}\)后四题唯一场切题,(别问我为什么只有这一道)。读完题之后,理一下思路,可以很容易的想到......