Educational Codeforces Round 49 (Rated for Div. 2)

C - Minimum Value Rectangle

题意:给n根木棒,选4根组成长方形,使得这个长方形的周长的平方比上其面积最小。

题解:对那个式子求导,发现对于同一个长来说,是长和宽越接近,上式越小。那么排序之后每个和他附近的一个组装一下就行了。

map<int, int> m;
vector<int> v;

void test_case() {
    int n;
    scanf("%d", &n);
    m.clear();
    for(int i = 1; i <= n; ++i) {
        int ai;
        scanf("%d", &ai);
        m[ai]++;
    }
    v.clear();
    for(auto &j : m) {
        if(j.second == 2 || j.second == 3)
            v.push_back(j.first);
        else if(j.second >= 4) {
            printf("%d %d %d %d\n", j.first, j.first, j.first, j.first);
            return;
        }
    }
    ll fz, fm, a, b;
    n = v.size();
    for(int i = 0; i < n - 1; ++i) {
        ll nfz = 2ll * (v[i] + v[i + 1]);
        nfz *= nfz;
        ll nfm = 1ll * v[i] * v[i + 1];
        if(i == 0 || nfz * fm < fz * nfm) {
            a = v[i];
            b = v[i + 1];
            fz = nfz;
            fm = nfm;
        }
    }
    printf("%lld %lld %lld %lld\n", a, a, b, b);
}

事实上可能不需要map,直接来一波快排,然后跑一遍nxt。

int a[1000005];
int b[1000005];

void test_case() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    int w, h, btop = 0;
    for(int i = 1, nxt; i <= n; i = nxt) {
        for(nxt = i + 1; nxt <= n && a[nxt] == a[i]; ++nxt);
        int cnt = nxt - i;
        if(cnt >= 4) {
            printf("%d %d %d %d\n", a[i], a[i], a[i], a[i]);
            return;
        }
        if(cnt >= 2)
            b[++btop] = a[i];
    }
    ll fz, fm;
    for(int i = 1; i < btop; ++i) {
        ll nfz = 2ll * (b[i] + b[i + 1]);
        nfz *= nfz;
        ll nfm = 1ll * b[i] * b[i + 1];
        if(i == 1 || nfz * fm < fz * nfm) {
            w = b[i];
            h = b[i + 1];
            fz = nfz;
            fm = nfm;
        }
    }
    printf("%d %d %d %d\n", w, w, h, h);
}

D - Mouse Hunt

题意:有n个房间,有1个老鼠,开始可能在任意一个房间,在第i个房间放陷阱花ci,老鼠在第i个房间下一次就会到ai。求最便宜的陷阱总额。

题解:基环树的内向树。乱搞一点直接套Kosaraju缩点,然后缩点之后的代表点放这堆点的最小值,然后把所有0出度的ci加起来。

不过这个跟反图没有任何关系,加边然后去重甚至都不需要加边和去重直接统计出度(因为0始终是0,非0的出度去重后出度也不会是0)。

namespace SCC {
    const int MAXN = 2e5;

    int n;
    vector<int> G[MAXN + 5], BG[MAXN + 5];

    int c1[MAXN + 5], cntc1;
    int c2[MAXN + 5], cntc2;
    int s[MAXN + 5], cnts;

    int n2;
    vector<int> V2[MAXN + 5];
    //vector<int> G2[MAXN + 5], BG2[MAXN + 5];

    const int INF = 0x3f3f3f3f;
    int C1[MAXN + 5], C2[MAXN + 5];

    void Init(int _n) {
        n = _n;
        cntc1 = 0, cntc2 = 0, cnts = 0;
        for(int i = 1; i <= n; ++i) {
            G[i].clear();
            BG[i].clear();
            c1[i] = 0;
            c2[i] = 0;
            s[i] = 0;
            V2[i].clear();
            //G2[i].clear();
            //BG2[i].clear();

            C2[i] = INF;
        }
        for(int i = 1; i <= n; ++i)
            scanf("%d", &C1[i]);
        for(int i = 1, v; i <= n; ++i) {
            scanf("%d", &v);
            G[i].push_back(v);
            BG[v].push_back(i);
        }
    }

    /*void AddEdge(int u, int v) {
        G[u].push_back(v);
        BG[v].push_back(u);
    }*/

    void dfs1(int u) {
        c1[u] = cntc1;
        for(int v : G[u]) {
            if(!c1[v])
                dfs1(v);
        }
        s[++cnts] = u;
    }

    void dfs2(int u) {
        V2[cntc2].push_back(u);
        C2[cntc2] = min(C2[cntc2], C1[u]);
        c2[u] = cntc2;
        for(int v : BG[u]) {
            if(!c2[v])
                dfs2(v);
        }
    }

    void Kosaraju() {
        for(int i = 1; i <= n; ++i) {
            if(!c1[i]) {
                ++cntc1;
                dfs1(i);
            }
        }

        for(int i = n; i >= 1; --i) {
            if(!c2[s[i]]) {
                ++cntc2;
                dfs2(s[i]);
            }
        }
    }

    /*void Build() {
        n2 = cntc2;
        for(int i = 1; i <= n2; ++i) {
            for(auto u : V2[i]) {
                for(auto v : G[u]) {
                    if(c2[v] != i) {
                        G2[i].push_back(c2[v]);
                        BG2[c2[v]].push_back(i);
                    }
                }
            }
        }

        for(int i = 1; i <= n2; ++i) {
            sort(G2[i].begin(), G2[i].end());
            G2[i].erase(unique(G2[i].begin(), G2[i].end()), G2[i].end());
            sort(BG2[i].begin(), BG2[i].end());
            BG2[i].erase(unique(BG2[i].begin(), BG2[i].end()), BG2[i].end());
        }
    }*/

    void Solve() {
        ll ans = 0;
        n2 = cntc2;
        for(int i = 1; i <= n2; ++i) {
            bool cnt = 0;
            for(auto u : V2[i]) {
                for(auto v : G[u]) {
                    if(c2[v] != i) {
                        cnt = 1;
                        break;
                    }
                }
                if(cnt)
                    break;
            }
            if(!cnt)
                ans += C2[i];
        }
        printf("%lld\n", ans);
    }
}

void test_case() {
    int n;
    scanf("%d", &n);
    SCC::Init(n);
    SCC::Kosaraju();
    SCC::Solve();
}

要是真按基环树去做,是不是要从入度0的点一个一个dfs下去找环,然后有环回溯的时候顺带找一波最小值?老套路用个全局变量记录重复点的id,直到回溯到它都标记为环。注意dfs的时候要染色,遇到其他颜色dfs过的就直接退出。不过我这不是有现成的缩点模板吗?

原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11979991.html

时间: 2024-11-09 12:47:28

Educational Codeforces Round 49 (Rated for Div. 2)的相关文章

Educational Codeforces Round 49 (Rated for Div. 2) ABCD

A. Palindromic Twist You are given a string ss consisting of nn lowercase Latin letters. nn is even. For each position ii (1≤i≤n1≤i≤n) in string ss you are required to change the letter on this position either to the previous letter in alphabetic ord

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html