题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小,单链树(左/右偏数)的深度总和最大.若d在这个范围内,则一定能构造出来:否则一定构造不出来. 1.初始构造一颗单链树,依次把底部的节点放入上面的层,直到满足深度总和为d 2.若当前深度总和sum > d,则先拿掉底端节点. 拿掉后,若sum依然比d大,就直接把底端节点放入有空位的最上层: 拿掉后s…
题意:给定n个点的初始坐标x和速度v(保证n个点的初始坐标互不相同), d(i,j)是第i个和第j个点之间任意某个时刻的最小距离,求出n个点中任意一对点的d(i,j)的总和. 题解:可以理解,两个点中初始坐标较小的点的速度更大时,总有一个时刻后面的点会追上前面的点,d(i,j) =0. 否则,即后面的点的速度 <= 前面的点的速度时,两点之间的距离只会越来越大,d(i,j) = abs(xi - xj) (初始距离). 可以用直线来辅助理解:x = xi + v*t,横轴为t,纵轴为x,若两直线…
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和m个元素的数组p,p中元素表示可调换(p=[3,2]表示a中下标为3和下标为3+1的元素可调换,2和2+1可调换),问能否使得原数组成为非降序数组 思路:暴力,模仿冒泡排序 AC代码: #include<bits/stdc++.h> typedef long long ll; using name…
第一次写博客 ,请多指教! 翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法: 其实这道题是没有必要用线段树的,树状数组就能够解决.但是个人感觉把线段树用熟了会比树状数组更有优势一点 不多废话  http://codeforces.com/contest/1311/problem/F 题目链接 题意是 给你一堆点的位置和他们运动的速度(可正可负),然后时间无限往后推的情况下问你他们之间所有点的最小距离之和.(不明白自行读题) n的范围是2,200000: 显然单纯的一个…
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in ss . I.e. if s=s= "abca&…
A.题意:通过加奇数减偶数的操作从a到b最少需要几步 签到题 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <deque> #include <map> using na…
output standard output You are given an array aa of length nn . You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm , where 1≤pi<n1≤pi<n . The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]a[pi+1] . You can app…
You are given three integers a≤b≤ca≤b≤c . In one move, you can add +1+1 or −1−1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this ope…
You are given two positive integers aa and bb . In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0 ) and replace aa with a+xa+x ; choose any positive even integer yy (y>0y>0 ) and replace aa with a−…
题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度v都比另一个小的时候,他们才不会相遇,所以最小的初位置想减也是abs(xi-xj) 因为速度-10^8<=v<=10^8的范围,需要离散化 将初位置进行从小到大排序,进行循环,他的速度(设v1)在所有速度的哪个位置(设pos),x1代表v1这个初始值 那么在这个位置pos之前的树状数组里存着的也是…
A. Add Odd or Subtract Even 思路: 相同直接为0,如果两数相差为偶数就为2,奇数就为1 #include<iostream> #include<algorithm> using namespace std; int main(){ int kk; scanf("%d",&kk); while(kk--){ int n,m; cin>>n>>m; if(n==m) {printf("0\n&qu…
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the g…
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsmemory limit per test256 megabytes 问题描述 Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positiv…
题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的话就是自己置一个值,有k个子节点,那么每个子节点的值范围 就是1-k,1-k只能用一次 思路:贪心不好取,我肯定是要排列完才知道当前的值是哪个,但是我可以知道当前节点应该是取子节点中排名第几的那个,从而推出根节点1的排名,然后再输出就行,因为是要从 子节点那里递归上来,所以我们采取树形dp #inc…
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is playing a math game with Varda. Let's define for positive integer x as a product of factorials of its digits. For example, . First, they choose a decimal…
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every verte…
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with n vertexes. Each its edge has unit length. You…
E. Berland Local Positioning System Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem/E Description In Berland a bus travels along the main street of the capital. The street begins from the main square and looks l…
地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Ilya is very fond of graphs, especially trees. During his last trip…
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结点到根节点的权值和.但是有些si=-1,这就相当于丢失了当前结点的数据. 假设原本每个点的权值为ai,那么现在求sum{ai}的最小为多少,ai为非负数. 题解: 这题可以单独看每一条链上的s值,假设当前结点为u,儿子结点v,那么就有几种情况: 1.su==-1&&sv==-1,这种不用管,继…
地址:http://codeforces.com/contest/764/problem/C 题目: C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Each New Year Timofey and his friends cut down a tree of n vertices…
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定不能全是1,由于假设有一对相邻位全为1,那么这两个的AND值为1.又由于OR值是仅仅要有1.结果就为1.所以这位结果肯定为1.所以就推出了一个dp转移方程.dp[i][j]表示第i位上的数为j时的总个数.那么有: dp[i][0]=dp[i-1][0]+dp[i-1][1]; dp[i][1]=dp…
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0,0) on an infinite grid. You also have the sequence of instructions of this robot. It is written as…
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction   as a sum of three distinct positive fractions in form . Help Vladik with that, i.e for a g…
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edg…
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图,要求不能有自环,重边,且满足距离数组和度数限制,输出图中的边:如果无解,输出 -1 数据规模:1 ≤ k <  n ≤ 105,0 ≤ d[i] < n 做法分析 第一眼做法:SPFA 或者 BFS,想了想,还是乱搞 根据 d 数组直接构造这个图,因为最短路具有最优子结构,所以,d[i] 为 0…
F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order whil…
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最大. 解法:考虑树形dp,若当前节点是nownownow,我们可以假设编号最大的一些数分布在当前节点的子节点中,显然,若当前节点为叶子节点,那么这个这个编号就是最大的.然后往上回溯,假设当前节点取得是maxmaxmax,那么就从子节点中取最大的数为当前节点的答案,如果取得是minminmin的话,就…
题意:给你一棵树, 只能删度数为偶数的点, 问你能不能将整个图删完, 如果能输入删除的顺序. 思路:对于一棵树来说, 如果里面的点的个数是偶数个则肯定不可能, 偶数个点有奇数条边,而你每次删只能删偶数条边. 那么我们对于每个父亲儿子来说, 如果儿子的子树的大小为奇数, 那么肯定先删父亲, 反之先删儿子, 建立关系图, 跑一遍拓扑序就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se secon…
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive intege…