Codeforces Round #453 ( Div. 2) Editorial ABCD
1 second
256 megabytes
standard input
standard output
Pig is visiting a friend.
Pig's house is located at point 0, and his friend's house is located at point m on an axis.
Pig can use teleports to move along the axis.
To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.
Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds.

Determine if Pig can visit the friend using teleports only, or he should use his car.
The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of teleports and the location of the friend's house.
The next n lines contain information about teleports.
The i-th of these lines contains two integers ai and bi (0 ≤ ai ≤ bi ≤ m), where ai is the location of the i-th teleport, and bi is its limit.
It is guaranteed that ai ≥ ai - 1 for every i (2 ≤ i ≤ n).
Print "YES" if there is a path from Pig's house to his friend's house that uses only teleports, and "NO" otherwise.
You can print each letter in arbitrary case (upper or lower).
3 5
0 2
2 4
3 5
YES
3 7
0 4
2 5
6 7
NO
The first example is shown on the picture below:

Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.
The second example is shown on the picture below:

You can see that there is no path from Pig's house to his friend's house that uses only teleports.
题意:pig要从0到m,他只能乘坐这个teleport过去,每次teleport能从ai出发到[ai,bi]中的任意一个位置。请问pig能只乘坐teleport到达m吗?
题解:就是看0~m上是否有没有被线段覆盖过的部分,经典做法for一遍就好了。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e3+;
int a[N];
bool flag;
int main()
{
int n,m,l,r;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d%d",&l,&r);
a[*l]++;
a[*r+]--;
}
flag=;
int ans=;
for(int i=;i<=*m;i++)
{
ans+=a[i];
if(ans==)
flag=;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
return ;
}
1 second
256 megabytes
standard input
standard output
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.
Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.
You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.
It is guaranteed that you have to color each vertex in a color different from 0.
You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).
The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi.
The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into.
It is guaranteed that the given graph is a tree.
Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.
6
1 2 2 1 5
2 1 1 1 1 1
3
7
1 1 2 3 1 4
3 3 1 1 1 2 3
5
The tree from the first sample is shown on the picture (numbers are vetices' indices):

On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):

On seond step we color all vertices in the subtree of vertex 5 into color 1:

On third step we color all vertices in the subtree of vertex 2 into color 1:

The tree from the second sample is shown on the picture (numbers are vetices' indices):

On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):

On second step we color all vertices in the subtree of vertex 3 into color 1:

On third step we color all vertices in the subtree of vertex 6 into color 2:

On fourth step we color all vertices in the subtree of vertex 4 into color 1:

On fith step we color all vertices in the subtree of vertex 7 into color 3:

题意:给你一颗树,初始无色,为颜色0。然后输入指定树上每个结点的颜色。你每次染色必须选定一个结点,把该结点的子树全染成同一颜色。问你至少需要多少次染色才能把整颗树染成指定的颜色。
题解:写个dfs,如果当前结点u和子结点p的颜色不同,那么u的子树的染色数+=p的子树的染色数,否则u的子树的染色数+=p的子树的染色数-1。初始每个结点染色数为1。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e5+;
struct edg
{
int next,to;
}edge[N*];
int head[N],ecnt;
void addedge(int u,int v)
{
edge[++ecnt]=(edg){head[u],v};
head[u]=ecnt;
return ;
}
int col[N],num[N];
int n,m,k,t;
void dfs(int u,int pre)
{
int p;
num[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
p=edge[i].to;
if(p!=pre)
{
dfs(p,u);
if(col[u]==col[p])
num[u]+=num[p]-;
else
num[u]+=num[p];
}
}
return ;
}
int main()
{
scanf("%d",&n);
ecnt=;
clr_1(head);
for(int i=;i<=n;i++)
{
scanf("%d",&k);
addedge(k,i);
addedge(i,k);
}
for(int i=;i<=n;i++)
scanf("%d",col+i);
dfs(,);
printf("%d\n",num[]);
return ;
}
2 seconds
256 megabytes
standard input
standard output
Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where h is the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root.
Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.
Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.
The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.
The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree.
The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.
If there is only one tree matching this sequence, print "perfect".
Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print
integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root.
These treese should be non-isomorphic and should match the given sequence.
2
1 1 1
perfect
2
1 2 2
ambiguous
0 1 1 3 3
0 1 1 3 2
The only tree in the first example and the two printed trees from the second example are shown on the picture:

题意:给你一串序列a0~ah,ai代表深度为i的结点的个数,问你能不能构造出两棵符合序列的不同构的树,如果能,输出ambiguous以及描述任意两棵不同构并且符合序列的树。不能则输出perfect。
题解:很显然,只要ai≥ 2并且 ai-1 ≥2,那么这个序列就能造出不同构的两棵树,否则该序列只能表示唯一的一棵树。
那么怎么构造这棵树呢。我们做个ai前缀和prei代表该层最后一个结点的编号。那么我们第一棵树把所有同层结点连接在上一层最后一个结点上就行了。第二棵树找到第一个ai≥ 2并且 ai-1 ≥2的i层,一个点链到上层的倒数第二个结点,其他都链到最后一个结点。对于其余层和第一棵树的做法一样。这样就能构造出不同构的两棵树了。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+;
int a[N],pre[N];
int fa[N];
bool flag;
int n,m,k;
int main()
{
scanf("%d",&n);
a[]=pre[]=;
for(int i=;i<=n+;i++)
{
scanf("%d",a+i);
if(a[i-]>= && a[i]>=)
flag=;
pre[i]=pre[i-]+a[i];
}
if(!flag)
{
printf("perfect\n");
}
else
{
printf("ambiguous\n");
for(int i=;i<=n+;i++)
for(int j=;j<=a[i];j++)
printf(" %d",pre[i-]);
printf("\n");
flag=;
for(int i=;i<=n+;i++)
if(flag && a[i-]>= && a[i]>=)
{
for(int j=;j<=a[i]-;j++)
printf(" %d",pre[i-]);
printf(" %d",pre[i-]-);
flag=;
}
else
{
for(int j=;j<=a[i];j++)
printf(" %d",pre[i-]);
}
printf("\n");
}
return ;
}
2 seconds
256 megabytes
standard input
standard output
Suppose you have two polynomials
and
. Then polynomial
can be uniquely represented in the following way:

This can be done using long division. Here,
denotes the degree of polynomial P(x).
is called the remainder of division of polynomial
by polynomial
, it is also denoted as
.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials
. If the polynomial
is zero, the result is
, otherwise the result is the value the algorithm returns for pair
. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair
to pair
.
You are given a single integer n (1 ≤ n ≤ 150) — the number of steps of the algorithm you need to reach.
Print two polynomials in the following format.
In the first line print a single integer m (0 ≤ m ≤ n) — the degree of the polynomial.
In the second line print m + 1 integers between - 1 and 1 — the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.
If there is no answer for the given n, print -1.
If there are multiple answer, print any of them.
1
1
0 1
0
1
2
2
-1 0 1
1
0 1
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is
(x2 - 1, x) → (x, - 1) → ( - 1, 0).
There are two steps in it.
题意:给定多项式的最高次数,类似于高精度除法,做多项式除法(具体公式题目中表示出来了),然后有了多项式除法延伸出多项式gcd的欧几里得算法。让你构造两个最高次数小于等于n的多项式,并且其中一个最高次数要等于n,并且他们能做n步的欧几里得算法。
题解:一个玄学构造题,我们设定p0=1,p1=x,那么pn=pn-1*x±pn-2,加和减具体看加减上去以后会不会出现除-1,0,1以外的数。同样的还可以构造pn=pn-1*x+pn-2mod 2,
超过1的系数mod 2
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+;
int a[],acnt;
int b[],bcnt;
int c[],ccnt;
void dfs(int k)
{
if(k==)
{
a[]=;
acnt=;
bcnt=;
return ;
}
dfs(k-);
ccnt=acnt;
for(int i=;i<acnt;i++)
c[i]=a[i];
for(int i=acnt;i>;i--)
a[i]=a[i-];
a[]=;
acnt++;
int flag=;
for(int i=;i<bcnt;i++)
if(a[i]+b[i]> || a[i]+b[i]<-)
{
flag=-;
break;
}
for(int i=;i<acnt;i++)
if(i<bcnt)
a[i]+=flag*b[i];
for(int i=;i<ccnt;i++)
b[i]=c[i];
bcnt=ccnt;
return ;
}
int main()
{
int n;
scanf("%d",&n);
dfs(n);
printf("%d\n",acnt-);
for(int i=;i<acnt;i++)
printf("%d ",a[i]);
printf("\n");
printf("%d\n",bcnt-);
for(int i=;i<bcnt;i++)
printf("%d ",b[i]);
printf("\n");
return ;
}
Codeforces Round #453 ( Div. 2) Editorial ABCD的更多相关文章
- Codeforces Round #453 (Div. 1)
Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...
- Codeforces Round #590 (Div. 3) Editorial
Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...
- Codeforces Round #747 (Div. 2) Editorial
Codeforces Round #747 (Div. 2) A. Consecutive Sum Riddle 思路分析: 一开始想起了那个公式\(l + (l + 1) + - + (r − 1) ...
- Codeforces Round #544 (Div. 3) Editorial C. Balanced Team
http://codeforces.com/contest/1133/problem/Ctime limit per test 2 secondsmemory limit per test 256 m ...
- Codeforces Round #453 (Div. 1) 901C C. Bipartite Segments
题 http://codeforces.com/contest/901/problem/C codeforces 901C 解 首先因为图中没有偶数长度的环,所以: 1.图中的环长度全是奇数,也就是说 ...
- Codeforces Round #682 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...
- Codeforces Round #678 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...
- Codeforces Round #676 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...
- Codeforces Round #675 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...
随机推荐
- 对于所有对象都通用方法的解读(Effective Java 第三章)
这篇博文主要介绍覆盖Object中的方法要注意的事项以及Comparable.compareTo()方法. 一.谨慎覆盖equals()方法 其实平时很少要用到覆盖equals方法的情况,没有什么特殊 ...
- GCC在C语言中内嵌汇编 asm __volatile__ 【转】
转自:http://blog.csdn.net/pbymw8iwm/article/details/8227839 在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达 ...
- C函数前向声明省略参数
这样的不带参数的函数声明,在c中是合法的,表示任意参数:当然我们自己写代码最好不要这样写了,但是读老代码还是会遇到: #include <stdio.h> void fun(); int ...
- imx6设备树pinctrl解析【转】
转自:http://blog.csdn.net/michaelcao1980/article/details/50730421 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在移植linu ...
- 工具===代替cmd的conemu设置
conemu设置 Win+Alt+P进入设置界面,字体设置: 隐藏右上角菜单和窗口标题. (Ctrl + ~ 隐藏/显示terminal) 设置背景图片 避免误操作,关闭/新建确认 设置win+w默认 ...
- quazip 在windows msvc 2005 下的编译
quazip 在windows msvc 2005 下的编译 http://blog.csdn.net/v6543210/article/details/11661427
- Linux下通过jstat命令查看jvm的GC情况
jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: jstat [-命令选项] [vmid] [间隔时间/毫秒] [查询次数] 注意!!!:使用的jdk版本是jdk8. ...
- vim常用命令(复习版)(转)
原文链接:http://blog.csdn.net/love__coder/article/details/6739670 1.光标移动 上:k 下:j 左:l 『字母L小写』 右:h 上一行行首:- ...
- JavaScript 正则表达式的入门与使用
知道正则表达式已经很久了,粗略会看懂一些,不过以前没有系统的学习,最近在看<JS权威指南>,刚好看到了看到正则表达式部分,就比较系统的学习了正则表达式. 先说一下正则表达式的一些基本知识 ...
- JS获取 KindEditor textarea 标签 html内容失败的解决方法。
KindEditor 这个 东西 研究的不多,JS在通过调用getElementById 获取文本id的内容时候,KindEditor 尚未将内容同步,官方给的解决方法是afterBlur: func ...