Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
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的更多相关文章
- 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 ...
- Codeforces Round #418 (Div. 2)
A: 不细心WA了好多次 题意:给你一个a序列,再给你个b序列,你需要用b序列中的数字去替换a序列中的0,如果能够替换,则需要判断a是否能构成一个非递增的序列,a,b中所有的数字不会重复 思路:就是一 ...
- 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 ...
- Codeforces Round #418 (Div. 2)D
给n个圆要么包含,要么相分离,没有两个公共点,当成一棵树,把包含的面积大的放在上面 如图最上面的par记为-1,level记为0,当par==-1||level==1时就加否则减, 就是第一,二层先加 ...
- Codeforces Round #418 (Div. 2) A+B+C!
终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...
- Codeforces Round #418 (Div. 2) C
Description Nadeko's birthday is approaching! As she decorated the room for the party, a long garlan ...
- Codeforces Round #418 (Div. 2) B
Description Sengoku still remembers the mysterious "colourful meteoroids" she discovered w ...
- Codeforces Round #418 (Div. 2) A
Description A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight ...
- 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 ...
随机推荐
- 初探12C碰到的那些“坑”
一个昏天黑地的早上,刚搭建的系统忽然遭遇严重数据库问题.于是,主要人物闪亮登场了,他们分别是友商人员小灰和DBA小Y. 事情的开始,小Y接到小灰紧急救助电话... 小灰:小Y,我是友商的小灰,刚搭建的 ...
- Spring Cloud 入门 Consul-Client服务提供
前面介绍了 Rureka Client服务提供, 只需要改pom.xml部分内容 1.pom.xml <?xml version="1.0" encoding="U ...
- 统计寄存器AX中1 的个数
;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...
- oracle中序列,同义词的创建
序列 序列是用来生成唯一,连续的整数的数据库对象.序列通常用来自动生成主机那或唯一键的值.序列可以按升序排序, 也可以按降序排序.例如,销售流水表中的流水号可以使用序列自动生成. 创建序列语法: cr ...
- python--pexpect
大家好,最近工作比较忙,所以没时间来更新博客.趁着还没在下个版本来临之前,来这边再更新更新.是之前学习到的一些老知识点,就当来巩固一下了.开心QAQ 今天给大家介绍的是--Pexpect Expect ...
- 一个简单的WPF MVVM实例【转载】
引用地址:http://blog.csdn.net/yl2isoft/article/details/20838149 1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 ...
- OracleWeblogic12C安装教程
一,安装WebLogic Server 1. 双击exe安装文件 2.准备安装文件 3. 生成向导序列 4. 选择安装路径 5. 开始安装 经过以上步骤,weblogic已经成功安装到了你的电脑上,但 ...
- 【TP5.1】HTML标签自动转义,导致CKEditor保存内容无法正常显示!
问题:使用Thinkphp5.1 开发的时候显示CKEditor保存的内容不符合预期. 希望的样子,肯定是不显示<p><b>等标签,而是下面的样子. 因为刚开始使用TP5.1和 ...
- Makefile (2) gdb
gdb调试 1.用debug的方式编译 -g 2.打上断点 3.单步调试 step into 进入函数里面 step over 运行整个函数 step return 跳出当前函数 4.继续运行 5.打 ...
- C语言进阶——变量属性05
C语言变量属性: C语言的变量可以有自己的属性 在定义变量的时候加上“属性”关键字 “属性”关键字指明变量的特有意义 语法:property type value_name; auto关键字: aut ...