B. The Meeting Place Cannot Be Changed
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.

At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.

You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.

Input

The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.

The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.

Output

Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.

Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if  holds.

Examples
input
3
7 1 3
1 2 1
output
2.000000000000
input
4
5 10 3 2
2 3 2 4
output
1.400000000000
Note

In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.

题意:

从南到北一条啊路上分布着n个人,每人有一个速度,问他们在哪点能够以最短的时间相遇,输出时间精确到1e-6.

代码:

//很显然要相遇的位置一定在最那边的位置和最北边的位置的中间,因为这个位置又是
//用时最少的,所以时间t关于位置x是凹函数,即时间先减后增。这样三分就可以确定
//花费最小时间的位置x然后得出t。但是x不是连续的,如果求得的x不是整数就不能确定t。
//可以二分t,若某一个t对应一大一小两个x,t就继续减小直到最低点。
#include<bits/stdc++.h>
using namespace std;
int n;
double a[],b[];
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%lf",&a[i]);
for(int i=;i<n;i++) scanf("%lf",&b[i]);
double lef=0.0,rig=1e9,mid;
while(rig-lef>=1e-){
mid=(lef+rig)/;
double lowx=,higx=1e9;
for(int i=;i<n;i++){
lowx=max(lowx,a[i]-mid*b[i]);//小x
higx=min(higx,a[i]+mid*b[i]);//大x
}
if(lowx<=higx) rig=mid;//向南走的人中最北边的那个和向北走的人中最南边的那个如果有交叉,时间可以再减小
else lef=mid;
}
printf("%.6lf\n",rig);
return ;
}
C. Andryusha and Colored Balloons
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.

The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if ab and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.

Andryusha wants to use as little different colors as possible. Help him to choose the colors!

Input

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park.

Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path.

It is guaranteed that any square is reachable from any other using the paths.

Output

In the first line print single integer k — the minimum number of colors Andryusha has to use.

In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

Examples
input
3
2 3
1 3
output
3
1 3 2
input
5
2 3
5 3
4 3
1 3
output
5
1 3 2 5 4
input
5
2 1
3 2
4 3
5 4
output
3
1 2 3 1 2
Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

Illustration for the first sample.

In the second example there are following triples of consequently connected squares:

  • 1 → 3 → 2
  • 1 → 3 → 4
  • 1 → 3 → 5
  • 2 → 3 → 4
  • 2 → 3 → 5
  • 4 → 3 → 5

We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.Illustration for the second sample.

In the third example there are following triples:

  • 1 → 2 → 3
  • 2 → 3 → 4
  • 3 → 4 → 5

We can see that one or two colors is not enough, but there is an answer that uses three colors only.Illustration for the third sample.

题意:

n个点,n-1条边,要求相邻的三个点的颜色不能有相同的,需要多少颜色,各是什么颜色。

代码:

//建图之后dfs 重点是染色时没想到怎么处理。
#include<bits/stdc++.h>
using namespace std;
int n,tol,ans,head[],col[];
struct node
{
int v,next;
}nodes[];
void Add(int x,int y)
{
nodes[tol].v=y;
nodes[tol].next=head[x];
head[x]=tol++;
nodes[tol].v=x;
nodes[tol].next=head[y];
head[y]=tol++;
}
void dfs(int x,int f)
{
int c=;//
for(int i=head[x];i!=-;i=nodes[i].next){
int y=nodes[i].v;
if(y==f) continue;
if(c==min(col[x],col[f])) c++;
if(c==max(col[x],col[f])) c++;
col[y]=c;ans=max(ans,c);c++;//父亲和祖父都没用过c颜色,他兄弟考虑c++颜色。
dfs(y,x);
}
}
int main()
{
scanf("%d",&n);
int a,b;tol=;
memset(head,-,sizeof(head));
for(int i=;i<n-;i++){
scanf("%d%d",&a,&b);
Add(a,b);
}
ans=;
memset(col,,sizeof(col));
col[]=;col[]=;
dfs(,);
printf("%d\n",ans);
printf("%d",col[]);
for(int i=;i<=n;i++) printf(" %d",col[i]);
printf("\n");
return ;
}

Codeforces Round #403 (Div. 2) B 三分 C dfs的更多相关文章

  1. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. 【三分】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    三分显然,要注意EPS必须设成1e-6,设得再小一点都会TLE……坑炸了 #include<cstdio> #include<algorithm> #include<cm ...

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...

  5. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  6. 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D

    http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...

  7. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

    地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...

  9. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons

    地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...

随机推荐

  1. PNG和PVR之间互相转换的脚本

    项目经常会将png和pvr之间互相转换,这里mark一个脚本,会将当前目录下的文件全部批量转换 png转换成pvr @echo off path %path%;"C:\Program Fil ...

  2. Jedis 与 MySQL的连接线程安全问题

    Jedis的连接是非线程安全的 下面是set命令的执行过程,简单分为两个过程,客户端向服务端发送数据,服务端向客户端返回数据,从下面的代码来看:从建立连接到执行命令是没有进行任何并发同步的控制 pub ...

  3. Ext JS 6学习文档–第2章–核心概念

    核心概念 在下一章我们会构建一个示例项目,而在这之前,你需要学习一些在 Ext JS 中的核心概念,这有助于你更容易理解示例项目.这一章我们将学习以下知识点: 类系统,创建和扩展类 事件 Ext JS ...

  4. USACO 1.1.3 Friday the Thirteenth 黑色星期五

    Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...

  5. c# 有无符号值进一步了解

    1.编写过程中用到了short类型(有符号型,值范围含负值).两个正数之和得负. 改为int或unsigned short 均可. 2.注意,short型(-32768,32767)举例:做自加运算, ...

  6. ifream爱恨情缘

    开幕场景 iframe.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  7. 算法与数据结构3.3 calculator

    ★实验任务 小 V 发明了一个神奇的整数计算器: 给定一个合法的表达式,这个计算器能求出这个表达式的最终答案. 表达式可能包含: +:运算符,整数加法.如 1+1=2 -:运算符,整数减法.如 1-1 ...

  8. 第二次作业 编程题 PAT 1001A+B Format

    Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...

  9. lintcode-11-二叉查找树中搜索区间

    二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= k2 ...

  10. iOS- 显示数据列表最常用的一个控件UITableView

    相信做过iOS的程序员,最熟悉的控件一定少不了UITableView,最常用的控件也一定少不了UITableView! 今天分享一下自己对UITableView的实现大体思路,和整理出来的学习笔记! ...