TopCoder SRM 561 Div 1 - Problem 1000 Orienteering

传送门:https://284914869.github.io/AEoj/561.html

题目简述:

题外话:

刚开始看题没看到|C|<=300。以为|C|^2能做,码了好久,但始终解决不了一棵树中多条直径去重的问题。后来才发现|C|<=300,暴力就可以了。

不知道有哪位大佬会|C|^2的做法??

思路:

很显然,若length为树中经过所有S中的点的最短路径长度,

若size为虚树中所有边的长度和,

若dis为虚树中的最远点对的距离(即直径长度)

那么length=size*2-dis。

直观感受就是这样的,无需证明??

那么只要求出size的期望,dis的期望即可。

size的期望很好求,只要求出每条边对size的贡献即可。

对于当前边,一端的联通块中有a个点属于C,另一端的联通块中有|C|-a个点属于C。

那么这条边对size的贡献是

接着是求出dis的期望。

一个很直观的想法是枚举直径,求出可能存在于这棵虚树中的点的数量。

由于一棵树可能有多个直径,我们取其中端点标号字典序最小的一个直径。

假设当前枚举的是直径a-b。c可能存在与这棵虚树中,当且仅当

这里pair(x1,y1)<pair(x2,y2)指的是x1<x2||x1==x2&&y1<y2

所以先预处理出dis数组,暴力枚举直径,暴力枚举每一个点判断是否合法,再求贡献即可。

时间复杂度O(|C|^3)

一些需要注意的细节:直接用组合数进行计算会出锅,因为组合数太大了。所以用另一种方式计算(详见代码)

代码:

#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define _CLASSNAME_ Orienteering
#define _METHODNAME_ expectedLength
#define _RC_ double
#define _METHODPARMS_ vector <string> mp, int k
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
typedef long long LL;
typedef pair<int, int> PII;
char Mp[51][51];
int n, m, K, tot, Tot, id[51][51];
vector<int> E[2501];
void add(int x, int y) {
    if (!x || !y)return;
    //cout << x << " " << y << endl;
    E[x].pb(y); E[y].pb(x);
}
int sz[2501];
void initmap() {
    ref(i, 1, 2500)E[i].clear();
    memset(id, 0, sizeof id);
    tot = 0; Tot = 0;
    ref(i, 1, n)ref(j, 1, m)if (Mp[i][j] == ‘*‘)id[i][j] = ++tot;
    Tot = tot;
    ref(i, 1, n)ref(j, 1, m)if (Mp[i][j] == ‘.‘)id[i][j] = ++Tot;
    ref(i, 1, n)ref(j, 1, m) {
        if (i < n)add(id[i][j], id[i + 1][j]);
        if (j < m)add(id[i][j], id[i][j + 1]);
    }
    n = Tot;
}
void dfs(int fa, int x) {
    if (E[x].empty())return;
    sz[x] = (x <= tot);
    ref(i, 0, E[x].size() - 1) {
        int y = E[x][i]; if (y == fa)continue;
        dfs(x, y); sz[x] += sz[y];
    }
}
double work1() {
    dfs(0, 1);
    double res = 0;
    ref(i, 2, n) {
        res++; int a = sz[i], b = tot - a;
        if (K <= a) {//c[a][K]/c[tot][K]
            double ss = 1.0;
            ref(j, 0, K - 1)ss = ss * (a - j) / (tot - j);
            res -= ss;
        }
        if (K <= b) {//c[b][K]/c[tot][K]
            double ss = 1.0;
            ref(j, 0, K - 1)ss = ss * (b - j) / (tot - j);
            res -= ss;
        }
    }
    return res;
}
int dis[301][301];
void Dfs(int fa, int x, int S) {
    if (x <= tot)dis[0][x] = S;
    ref(i, 0, E[x].size() - 1) {
        int y = E[x][i];
        if (y == fa)continue;
        Dfs(x, y, S + 1);
    }
}
double work2() {
    double res = 0.0;
    ref(i, 1, tot) {
        Dfs(0, i, 0);
        ref(j, 1, tot)dis[i][j] = dis[0][j];
    }
    ref(i, 1, tot)ref(j, i + 1, tot) {
        int ss = 0;
        ref(k, 1, tot)if (k != i && k != j) {
            if (mp(dis[k][j], -k) < mp(dis[i][j], -i))
                if (mp(dis[i][k], -k) < mp(dis[i][j], -j))
                    ++ss;
        }
        if (ss < K - 2)continue;
        //c[ss][K-2] / c[tot][k]
        double S = 1.0*dis[i][j] / (tot - K + 1) / (tot - K + 2) * K * (K - 1);
        ref(k, 0, K - 2 - 1)S = S*(ss - k) / (tot - k);
        res += S;
    }
    return res;
}
class _CLASSNAME_ {
public:
    _RC_ _METHODNAME_(_METHODPARMS_)
    {
        n = mp.size(); m = mp[0].size(); K = k;
        ref(i, 1, n)ref(j, 1, m)Mp[i][j] = mp[i - 1][j - 1];
        initmap();
        double ans1 = work1();
        double ans2 = work2();
        return _RC_(ans1 * 2 - ans2);
    }

    // BEGIN CUT HERE
public:
    void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
private:
    template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << ‘\"‘ << *iter << "\","; os << " }"; return os.str(); }
    void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << ‘\"‘ << endl; cerr << "\tReceived: \"" << Received << ‘\"‘ << endl; } }
    void test_case_0() {
        string Arr0[] = { "*#..#",
            ".#*#.",
            "*...*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; double Arg2 = 3.8333333333333353; verify_case(0, Arg2, expectedLength(Arg0, Arg1));
    }
    void test_case_1() {
        string Arr0[] = { "*#..#",
            ".#*#.",
            "*...*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; double Arg2 = 8.0; verify_case(1, Arg2, expectedLength(Arg0, Arg1));
    }
    void test_case_2() {
        string Arr0[] = { "#.#**",
            "....#",
            "#*#**",
            "**#*#",
            "#..##",
            "*#..#",
            ".#.#.",
            "....*" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; double Arg2 = 10.825000000000024; verify_case(2, Arg2, expectedLength(Arg0, Arg1));
    }
    void test_case_3() {
        string Arr0[] = { "###################",
            "#*###############*#",
            "#.....#######.....#",
            "#*###*.#.*.#.*###*#",
            "#*####*.*#*.*####*#",
            "#*#####*###*#####*#",
            "###################" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; double Arg2 = 30.272233648704244; verify_case(3, Arg2, expectedLength(Arg0, Arg1));
    }
    void test_case_4() {
        string Arr0[] = { "**##*.**#..#.*...*#...*#..#.##..#..#.#*...#.##*##.",
            ".#..###..#..#.#.##..#.#.*#.*..#..#.#*..##.#*...*..",
            "..#.....###.#*.##..#.#.#*..#.#..#....#..#...#*####",
            ".#.##*#.*#..#*#*.#.#...*.#.*#.#.##.#*.##.#.#..*...",
            "..*.*#*.###.#..#.#..##.##.*#..#.....#.....#..#.#.#",
            ".#.##.#..##..*#..#.#...#*##*#*..#.#.#.#.##.##.#.#*",
            "..##....#..#.#*#...*.##...#.#.####...#.#*.....#...",
            ".#.*#.##.*#*.#*.#.#.#..#.#..#.#*#.###..##.##.#.##*",
            ".*.#*..*.#.#...#.*##.#.**.#.*...**..*#..#.#.#*.#..",
            ".#*.#*##....##.#.#*..*.###.#.##.##.#.#.#....#.#*.#",
            "*.#..#*#.#*#*....#.#.#..*#**...##.#.#.**#*##.*.#..",
            ".#*.##..##..##.#.#..#.#.###.###...#...#*#..##*#.#.",
            "#..#*.#..*.###..#.#...#.###.#.#*#.#.#**##.#...*.#*",
            "..#..#.#.##.#..#.**.##*#.#**.**..#.#..#...#.##*#..",
            ".#*#.#.*..#.*#...#.#...#...#.##.#..*#*.##*....###.",
            ".*.#.#.#.#*#..*##.**.##*##..#.*#.#*###..*.#.##.#..",
            ".#......#...#.#.*#.#.#..#..#.#*#....#*.#*#.*#..*.#",
            "#..####..#*#...#*.#..#.###...#.#.#.###*#..##*##.#.",
            ".#.*..#.#...#.#..#.##...#..#.#.#.#.###..##..*.*.*.",
            ".#.#.#.#..##.*..#.*.#.##.#..##*...#.#..#.#.##.#.##",
            ".#..#*.#.#..#.##..##..#.*..#.*#.#...##....#...###.",
            ".#.#.#.#*.#.#..#.#..#..#.#.*#...#.##...#.##.##.*..",
            ".#...#.#.##.#.#..*#.*#..###..#.#.#*###.##...#*.##.",
            ".#.##.*.......*.#.*#.#.#*###..*...*..#.*.##.#.#..#",
            "...###*####*#.#..##*...#..#..##.#.#.#..##*#*.*.*#.",
            "#.#.#....*#..#.#.#.#.##..#*.#...#..#.#*#...#.##.*.",
            "..*.#*##.#.#*#.###...#..##.#.#.#*###*#.*#.#.*###.#",
            "##*##..##...#.....##.#.#.**#..#*.....##.#..#*.#.*.",
            ".....#.*.##..##.##*.*#...#.#.#.##.#*#.**..#..#.#.#",
            "##.#.#*##.#.#.*.*.#.#*#.#.#....*...#*##*##.#....#.",
            "*.**#**....*..##.#*.*.**..##.###.##.....##...##.**",
            "#.####.##*#*##..#.*#*#.##*...#.##..#.##....#*..##.",
            "....#...##.#...#*.#..##.##.#*..*.#....##.#.*##...#",
            "#.#..*##*..#.#..#..#..#*....#.##..##.#*##.##.*##..",
            "..#.#*.*.##.#.#*#.#*##.###.##...#............#*.#.",
            "#.#.##.#....*....*..##..*#.#.#.###.#.#.#.###..#..#",
            ".#**..#*#.#*#*#.#.#...*##....##.#*..#..#*..*#..#..",
            "...#*#.....#..#.#..#*#.*##.#..#.#.##..#.*#*#.#...#",
            ".#*.###.#.#.#.#.*#*##.##..#.#*..#...#.#.#..#*.*#..",
            "#*.#.#.#..#..#..#....*#.*##..##.#.#..#...##.#.#..#",
            "*.#..#..#...#..##.#*#..#.#*#.#.#.###..#.#*...#.#..",
            "#...#.#...#.#.#..#.*.#*.....**.*..#*##.#*.##....##",
            "#*#....#*#..#.*.###*#..#*##.##.#.#...#.*.##.##.##.",
            "..##*##*..#*#.#..#*.*##*.##.#...#.#.#.#.#..*#.##..",
            "#...#*##.#*#**.##.*#.*.##..*.#*#**....#**##...*.*#",
            "*#.##......*#.##.#.#.##**.#.#.#.#.#.##..#...#*#*#*",
            "*....##.#.#..#.....#..##.#....*....#.#.##.#.#.##**",
            "#.##*#...#..#.#.##..#..##.##.##.##........##.#*#.#",
            "..#...#.#*#*..*#..*#.*#.#......##.#.#.#*#..#..****",
            ".###.#..#...#.#..#..#.#...#.#.#...**.#..*#*.*##*#." }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 150; double Arg2 = 1309.4951033725558; verify_case(4, Arg2, expectedLength(Arg0, Arg1));
    }

    // END CUT HERE
};
// BEGIN CUT HERE
int main()
{
    _CLASSNAME_ user;
    user.run_test(-1);
    getchar();
    return 0;
}
// END CUT HERE
时间: 2024-11-08 23:55:51

TopCoder SRM 561 Div 1 - Problem 1000 Orienteering的相关文章

TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization &amp; Codeforces 839 E

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

TopCoder SRM 558 Div 1 - Problem 1000 SurroundingGame

题目简述  一个人在一个n * m棋盘上玩游戏,想要占领一个格子有两个方法: 在这个格子放一个棋子.  这个格子周围(四联通)的格子中**都有棋子**. 在(i, j)中放棋子需要花费cost[i][j],占领(i, j)能获得benefit[i][j].求一种放置棋子的方法,使得总收益(收益 - 花费)最大. 2<=n,m<=20 分析 一眼看上去, 状压? 我是不是dp学傻了..根本想不出 网络流? 嗯,此题是一道非常套路的网络流练习题. 如果想不到对棋盘进行黑白染色,就GG了. 所以套路

TopCoder SRM 559 Div 1 - Problem 900 CircusTents

题目简述: n个实心圆,两两没有交集,在第一个圆上找一个点,使得它到另外一个圆上某个点的最短距离的最小值尽量大,两个点之间的最短距离是指连接两个点且中途不进入任何一个实心圆内部的路径的长度的最小值. 二分答案: 很显然,这题跟二分答案有关. 思路: 我们先考虑,如果第一个圆上的点确定了下来,它到别的所有的圆的距离的最小值. The First Case: The Second Case: 图中蓝色线是最短的路径. 当然还有第三种情况,就是路径过程中与别的圆相交了.不过这样肯定不是所有的路中最短的

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 596 DIV 1 250

body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } Problem Statement      You have an array with N elements. Initially, each element is 0. You can perform the following operations: Increment operation:

[topcoder]SRM 633 DIV 2

第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #include <vector> #include <algorithm> using namespace std; class Target { public: vector <string> draw(int n) { vector<string> result(n,

[topcoder]SRM 646 DIV 2

第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs.com/lautsie/p/4245242.html BFS和DFS都可以,注意的是,写的时候,可以往que里几个东西一起扔,就不用建立对象了.也可以直接用二维矩阵记录blocked和visited. 剪枝什么的,最基本的是发现其实在步数限制的情况下,棋盘就是有界的了. 第三题:http://ap

Topcoder SRM 638 DIV 2 (大力出奇迹)

水题,就是一个暴力.大力出奇迹. Problem Statement   There is a narrow passage. Inside the passage there are some wolves. You are given a vector <int> size that contains the sizes of those wolves, from left to right. The passage is so narrow that some pairs of wolv