Codeforces Round #349 (Div. 2)

终于又回到熟悉的Round了

数学 A - Pouring Rain

设个未知数,解方程,还好没有hack点

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
const double PI = acos (-1.0);

int main() {
    double d, h, v, e; scanf ("%lf%lf%lf%lf", &d, &h, &v, &e);
    double V = PI * (d / 2.0) * (d / 2.0) * h;
    double addv = PI * (d / 2.0) * (d / 2.0) * e;
    if (addv > v) {
        puts ("NO");
    } else {
        double t = -V / (addv - v);
        puts ("YES");
        printf ("%.8f\n", t);
    }
    return 0;
}

数学 B - Coat of Anticubism

题意:求增加最小长度的一根木棍,使得构成一个多边形。

分析:那么构成三角形,原来n条木棍分成A,B两边,A和B接近(A<=B),那么另一条边满足A + C > B,即C = B +1 - A

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
int a[N];

int main() {
    int n; scanf ("%d", &n);
    ll sum = 0;
    for (int i=0; i<n; ++i) {
        scanf ("%d", a+i);
        sum += a[i];
    }
    std::sort (a, a+n);
    ll del = 1000001000;
    ll A, B, presum = 0;
    for (int i=0; i<n; ++i) {
        presum += a[i];
        ll res = sum - presum;
        ll tmp = presum - res;
        if (abs (tmp) < del) {
            if (tmp < 0) {
                del = -tmp;
                A = presum;
                B = res;
            } else {
                del = tmp;
                A = res;
                B = presum;
            }
        }
    }
    ll C = B + 1 - A;
    printf ("%I64d\n", C);

    return 0;
}

DP C - Reberland Linguistics

题意:问后缀由若干个长度为2或3的子串构成,且相邻的子串不能相同。

分析:The only restriction — it is not allowed to append the same string twice in a row! 英语太渣不知道这是连续,相邻的意思。那么dp[i][0]表示i开始长度为2的子串能否可行,如果可行,那么dp[i-3][1]一定可行,因为长度不相等;还有如果长度相等的判断一下,即dp[i-2][0]。

#include <bits/stdc++.h>

const int N = 1e4 + 5;
bool dp[N][2];

int main() {
    std::string str;
    std::cin >> str;
    int n = str.length ();
    dp[n-2][0] = dp[n-3][1] = true;
    std::set<std::string> ans;
    for (int i=n-1; i>=5; --i) {
        if (dp[i][0]) {
            ans.insert (str.substr (i, 2));
            dp[i-3][1] = true;
            if (str.substr (i-2, 2) != str.substr (i, 2)) {
                dp[i-2][0] = true;
            }
        }
        if (dp[i][1]) {
            ans.insert (str.substr (i, 3));
            dp[i-2][0] = true;
            if (str.substr (i-3, 3) != str.substr (i, 3)) {
                dp[i-3][1] = true;
            }
        }
    }
    std::cout << ans.size () << ‘\n‘;
    for (auto s: ans) {
        std::cout << s << ‘\n‘;
    }
    return 0;
}

图论 D - World Tour

题意:找4个点按照顺序走,a->b->c->d,每次点到下一个点走的是最短路,问走的长度总和最大是多少。

分析:先计算dis(u, v),枚举b和c,对于b来说在反向图中找距离最远的点,因为a!=b a!=c,所以存最优的前3个点;对于c来说在原图中找距离最远的点,存最优的前4个点。

#include <bits/stdc++.h>

const int N = 3e3 + 5;
const int M = 5e3 + 5;
const int INF = 0x3f3f3f3f;
int dis[N][N];
std::vector<int> G[N], rG[N];
std::vector<std::pair<int, int> > bin[N], bout[N];
bool vis[N];
int n, m;

void BFS() {
    memset (dis, INF, sizeof (dis));
    for (int i=1; i<=n; ++i) {
        std::queue<int> que;
        dis[i][i] = 0;
        que.push (i);
        while (!que.empty ()) {
            int u = que.front (); que.pop ();
            for (auto v: G[u]) {
                if (dis[i][v] > dis[i][u] + 1) {
                    dis[i][v] = dis[i][u] + 1;
                    que.push (v);
                }
            }
        }
    }
}

void sort_out() {
    for (int i=1; i<=n; ++i) {
        memset (vis, false, sizeof (vis));
        vis[i] = true;
        bout[i].push_back (std::make_pair (0, i));
        std::queue<std::pair<int, int> > que;
        que.push (std::make_pair (0, i));
        while (!que.empty ()) {
            std::pair<int, int> pu = que.front (); que.pop ();
            for (auto v: G[pu.second]) {
                if (!vis[v]) {
                    vis[v] = true;
                    bout[i].push_back (std::make_pair (pu.first + 1, v));
                    que.push (std::make_pair (pu.first + 1, v));
                }
            }
        }
        std::sort (bout[i].begin (), bout[i].end (), std::greater<std::pair<int, int> > ()); //dis[i][v], v
        if (bout[i].size () > 4) {
            bout[i].resize (4);
        }
    }
}

void sort_in() {
    for (int i=1; i<=n; ++i) {
        memset (vis, false, sizeof (vis));
        vis[i] = true;
        bin[i].push_back (std::make_pair (0, i));
        std::queue<std::pair<int, int> > que;
        que.push (std::make_pair (0, i));
        while (!que.empty ()) {
            std::pair<int, int> pu = que.front (); que.pop ();
            for (auto v: rG[pu.second]) {
                if (!vis[v]) {
                    vis[v] = true;
                    bin[i].push_back (std::make_pair (pu.first + 1, v));
                    que.push (std::make_pair (pu.first + 1, v));
                }
            }
        }
        std::sort (bin[i].begin (), bin[i].end (), std::greater<std::pair<int, int> > ()); //dis[v][i], v
        if (bin[i].size () > 3) {
            bin[i].resize (3);
        }
    }
}

int main() {
    scanf ("%d%d", &n, &m);
    for (int u, v, i=0; i<m; ++i) {
        scanf ("%d%d", &u, &v);
        G[u].push_back (v);
        rG[v].push_back (u);
    }
    BFS ();
    sort_in ();
    sort_out ();
    int a, b, c, d;
    int best = 0;
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=n; ++j) {
            if (i == j || dis[i][j] == INF) {
                continue;
            }
            int k, l;
            for (int ii=bin[i].size ()-1; ii>=0; --ii) {
                int tot = dis[i][j];
                std::pair<int, int> &x = bin[i][ii];
                if (x.second != i && x.second != j) {
                    k = x.second;
                    tot += x.first;
                    for (int jj=bout[j].size ()-1; jj>=0; --jj) {
                        std::pair<int, int> &y = bout[j][jj];
                        if (y.second != i && y.second != j && y.second != k) {
                            l = y.second;
                            tot += y.first;
                            if (best < tot) {
                                best = tot;
                                a = k; b = i; c = j; d = l;
                            }
                            tot -= y.first;
                        }
                    }
                }
            }
        }
    }
    printf ("%d %d %d %d\n", a, b, c, d);

    return 0;
}

  

时间: 2024-11-08 20:03:03

Codeforces Round #349 (Div. 2)的相关文章

Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in d

Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will enti

Codeforces Round #349 (Div. 2) D. World Tour 【spfa+暴力枚举】

/* *********************************************** Author :Maltub Email :[email protected] Blog :htttp://www.xiang578.com ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #inclu

Codeforces Round #349 (Div. 2) C. Reberland Linguistics 【DP】

/* *********************************************** Author :Maltub Email :[email protected] Blog :htttp://www.xiang578.com ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #inclu

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd