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( ...
随机推荐
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- 关于Solaris系统“mpt_sas”驱动
1.mpt_sas 驱动源文件所在系统源代码中目录: illumos-soulos/usr/src/uts/common/sys/scsi/adapters/mpt_sas -- 头文件 illum ...
- Ubuntu 14.04 ThinkPad E431无线网卡驱动安装
Ubuntu 14.04下安装无线网卡驱动. sudo apt-get install linux-headers-generic build-essential dkms sudo apt-get ...
- inetdev_init && inetdev_destroy
inetdev_init为传入设备分配和绑定ip控制块,查看其调用关系如下: fs_initcall(inet_init)---->inet_init---->ip_init----> ...
- .NET Core 2.0.5安装具体步骤
.NET Core 2.0.5 comprises: .NET Core Runtime 2.0.5 .NET Core SDK 2.1.4 SDK Installer SDK Binaries ...
- redis 的优化
1.pipeling “请求-响应”模式的服务器在处理完一个请求后就开始处理下一个请求,不管客户端是否读取到前一个请求的响应结果.这让客户端不需要发一个请求等一个响应的串行,可以一次发送多个请求,再最 ...
- tomcat修改内存
windows: 修改bin/catalina.bat, 第一行添加 set JAVA_OPTS=-Xms256m -Xmx512m linux: 修改bin/catalina.sh 第一行添加 JA ...
- hdu 1087(LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- JAVA 语言类的特性(成员、重载、构造方法、静态成员)
一.类的私有成员和公有成员 1.私有成员 修饰符private 如果在类的声明前加上修饰符private,则无法从该类的外部访问到该类的内部成员,而只能被该类自身访问和修改,而不嗯那个被其他类, ...
- MyBatis 返回(批量)新增数据的自增id
<insert id="save" parameterType="Vote" useGeneratedKeys="true" keyP ...