Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque


题意: 给\(n(n <= 1000)\)个圆,圆与圆之间不存在相交关系,只存在包含与不包含关系,现在问你把这一堆圆分成两组,求每组圆的异或并和的最大值。

思路:最简单的做法是贪心的做法,算出每个圆被包含的次数,为偶数则贡献为正,奇数为贡献为负。

现在主要学习一下树形dp的做法

由于圆不存在相交的情况,所以可以把所有的圆建成树,即把每棵树上的点分成两组,分别建成两棵树

使得ans=所有偶数深度的点\(\sum{area_i}\) - 所有奇数深度的点\(\sum{area_j}\)最大。

由于每棵树是独立的,对每棵树做树形dp就好了

然而题解的状态定义简直难想的一p,没做过类似套路的题,根本想不出嘛,还是贪心做这道题比较靠谱

不过套路还是要学的

dp[u][p1][p2] 表示假定在u的祖先中p1个点给了第一颗树,剩下p2个点给了第二棵树,以u为根的子树能获得的最大价值(题做的少,感觉这样的定义好奇特啊)

考虑u分给第一棵树 或者 第二颗树 则有如下转移方程

\(\begin{equation}
dp[u][p1][p2] = max
\begin{cases}
\sum_{v \epsilon child(u)} dp[v][p1+1][p2]+(p1\%2==0?1:-1)*area[u] \\
\sum_{v\epsilon child(u)}dp[v][p1][p2+1]+(p2\%2==0?1:-1)*area[u]
\end{cases}
\end{equation}\)

这个方程看起来似乎可以从上往下做状态转移啊,仔细一想树一旦分叉,祖先结点就被考虑多次,转移就出错了,所以只能从下到上。

注意到状态转移其实只跟奇偶有关

则可以定义成dp[u][0/1][0/1]表示假定在u的祖先中给了第一棵树的点数的奇偶,给了第二颗树的点数的奇偶,子树u能获得的最大价值。

于是可以写成这样 \(dp[u][0/1][0/1]\)

\(\begin{equation}
dp[u][i][j] = max
\begin{cases}
(\sum_{v \epsilon child(u)} dp[v][i\ xor\ 1][j])+(i==0?1:-1)*area[u] \\
(\sum_{v\epsilon child(u)}dp[v][i][j\ xor\ 1])+(j==0?1:-1)*area[u]
\end{cases}
\end{equation}\)

  • 分析一下,由于每个点贡献是它在树中的深度有关,如果我们不假定它的祖先的状态的话,那么就无法从下到上做计算的。算出子树的值转移给祖先,这样最后的答案就是dp[root][0][0] (树根没有祖先当然是0,0)
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define LL long long
#define P pair<string,int>
#define ls(i) seg[i].lc
#define rs(i) seg[i].rc
using namespace std;
using namespace __gnu_pbds;
const int N = 1e3 + 10;
const double PI = acos(-1.0);
vector<int> G[N];
int vis[N];
long double area[N];
struct circle{
int x,y,r;
circle(){};
bool operator<(const circle &rhs)const{
if(r != rhs.r) return r < rhs.r;
if(x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
}c[N];
LL sqr(int x){
return 1LL * x * x;
}
bool contain(int a,int b){
return sqr(c[a].x - c[b].x) + sqr(c[a].y - c[b].y) <= sqr(c[a].r - c[b].r);
}
double dp[N][2][2];
void dfs(int u){
double s[2][2] = {0};
for(int i = 0;i < G[u].size();i++){
int v = G[u][i];
dfs(v);
for(int j = 0;j < 2;j++)
for(int k = 0;k < 2;k++)
s[j][k] += dp[v][j][k];
}
for(int i = 0;i < 2;i++)
for(int j = 0;j < 2;j++){
dp[u][i][j] = max(s[i^1][j]+(i==0?1:-1)*area[u],s[i][j^1]+(j==0?1:-1)*area[u]);
}
}
int main()
{
int n;
cin>>n;
for(int i = 1;i <= n;i++) scanf("%d%d%d",&c[i].x,&c[i].y,&c[i].r);
sort(c+1,c+n+1);
for(int i =1 ;i <= n;i++) vis[i] = 0;
for(int i = 1;i <= n;i++){
area[i] = PI * c[i].r * c[i].r;
for(int j = 1 ;j < i;j++){
if(!vis[j] && contain(i,j)){
G[i].push_back(j);
vis[j]++;
assert(vis[j]==1);
}
}
}
double ans = 0;
for(int i = 1;i <= n;i++) if(!vis[i]){
dfs(i);
ans += dp[i][0][0];
}
printf("%.10lf\n",ans);
return 0;
}

Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque的更多相关文章

  1. Codeforces Round #418 (Div. 2).C two points

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

  2. Codeforces Round #418 (Div. 2)

    A: 不细心WA了好多次 题意:给你一个a序列,再给你个b序列,你需要用b序列中的数字去替换a序列中的0,如果能够替换,则需要判断a是否能构成一个非递增的序列,a,b中所有的数字不会重复 思路:就是一 ...

  3. Codeforces Round #418 (Div. 2) B. An express train to reveries

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  4. Codeforces Round #418 (Div. 2)D

    给n个圆要么包含,要么相分离,没有两个公共点,当成一棵树,把包含的面积大的放在上面 如图最上面的par记为-1,level记为0,当par==-1||level==1时就加否则减, 就是第一,二层先加 ...

  5. Codeforces Round #418 (Div. 2) A+B+C!

    终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...

  6. Codeforces Round #418 (Div. 2) C

    Description Nadeko's birthday is approaching! As she decorated the room for the party, a long garlan ...

  7. Codeforces Round #418 (Div. 2) B

    Description Sengoku still remembers the mysterious "colourful meteoroids" she discovered w ...

  8. Codeforces Round #418 (Div. 2) A

    Description A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight ...

  9. Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

随机推荐

  1. linux命令讲解

    1.vi命令 1.光标移动到文件的最后一行 G     :$     ]] 2.光标移动到文件的第一行 :0     gg     [[ 3.从光标所在位置将光标移动到当前行的开头 0     ^   ...

  2. MySQL 如何生成月份表

    MySQL 如何生成月份表 如果遇到按照月份统计信息的时候,常用的统计方式就是用month表去连接order表,下面就是生成月份表的过程 1.首先是建表 CREATE TABLE `sn_month` ...

  3. delphi的消息对话框

    delphi的消息对话框,类似VFP中的WAIT和MESSAGEBOXdelphi的消息对话框,类似VFP中的WAIT和MESSAGEBOX1.最简单的是:showmessage() 它只有一个OK按 ...

  4. PHP 日期处理函数 date() 、mktime()

    一.前言 php是世界上最好的语言! 二.介绍 mktime()函数获取当周\当天\当月 /** * 微程-日期工具函数 week: 当周 day: 当天 month: 当月 * @author 狗蛋 ...

  5. 从0开始学习 Git

    1. 什么是Git? Git 是 Linux 发明者 Linus 开发的一款新时代的版本控制系统,那什么是版本控制系统呢?怎么理解?网上一大堆详细的介绍,但是大多枯燥乏味,对于新手也很难理解,这里我只 ...

  6. Mplab X IDE 安装DMCI

     DMCI在Mplab 8中是默认安装的,在 Mplab X IDE中是作为插件,默认不安装.   找到     勾选前面的复选框,点击安装

  7. Java 的单元测试

    有点需要注意,当 JUnit 主线程退出,子线程也会跟着退出,需要使用子线程的 join() 方法使主线程等待 Maven 依赖 <dependency> <groupId>j ...

  8. MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':ge

    数据库表里命名有这个字段,可怎么就是报错呢,大神的解释: 加上之后立马好用!!!

  9. Android 网络通用类 NetUtil

    1.整体分析 1.1.源代码如下,可以直接Copy. public class NetUtil { /** * 用户是否连接网络 * * @param context Context */ publi ...

  10. PHP.19-验证码生成

    生成验证码 思路:先定义验证码函数getCode() //绘制验证码 $num = 4; //字符长度 getCode($num, 2); 1.创建画布,分配颜色 imagecreatetruecol ...