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 ...
随机推荐
- 搭建MQTT代理服务器
# 解压tar zxfv mosquitto-1.4.14.tar.gz# 进入目录cd mosquitto-1.4.14# 编译make# 安装sudo make instal 1 启动代理服务在第 ...
- PHP成随机字符串
生成随机字符串 /** * 随机字符串 * @param int $len * @return string */ function randomStr($len = 32) { $chars = & ...
- c语言可变参数函数
c语言支持可变参数函数.这里的可变指,函数的参数个数可变. 其原理是,一般情况下,函数参数传递时,其压栈顺序是从右向左,栈在虚拟内存中的增长方向是从上往下.所以,对于一个函数调用 func(int a ...
- Jongmah CodeForces - 1110D
传送门 题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 题解:三个一模一样的 ...
- python基础之继承组合应用、对象序列化和反序列化,选课系统综合示例
继承+组合应用示例 1 class Date: #定义时间类,包含姓名.年.月.日,用于返回生日 2 def __init__(self,name,year,mon,day): 3 self.name ...
- MVN settings.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(二)
本文导航 -7. 安装 PHP0 -8. 安装 MariaDB 数据库 -9. 安装和配置 SSH 服务器 -10. 安装 GCC (GNU 编译器集) -11. 安装 Java 7. 安装 PHP ...
- 剑指Offer - 九度1348 - 数组中的逆序对
剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个 ...
- python学习笔记十六:读取JSON文件
读取JSON文件可以用JSON库,示例代码: #coding:utf-8 import json with open("msg.json") as jsonfile: json_d ...
- 【Merge Intervals】cpp
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...