多校题解

##[1001.Average](http://acm.hdu.edu.cn/showproblem.php?pid=5353)
Summary
$n$ people are sitting around a circle and every two adjacent people can exchange only on candy. The goal is to find a valid exchange order to make everyone have equal amount of candies (or determine it is impossible). Solution

Find the sum of the candies. If the sum is not a multiple of $n$, such order does not exist.
    let $c_{i}$ be the difference between the candies $i$-th person have and the average candies.
    Enumerate 1-st person’s operation: give a candy to 2, receive a candy from 2, do nothing.
    Then determine next one’s operation according to the value $c_{i}$:

$c_{i}$= −1: receive a candy from $i$ + 1.
    $c_{i}$ = 0: just do nothing.
    $c_{i}$ = 1: give a candy to $i$ + 1.
    otherwise, we can not find a valid exchange order.

Time complexity: $O(n)$.   
##[1002.Bipartite Graph](http://acm.hdu.edu.cn/showproblem.php?pid=5354) Summary
We are given an undirected graph $G$ with $n$ vertices and $m$ edges. You are to determine for each vertex $i$, after deleting it, whether the graph becomes a bipartite graph. Solution

For every edge $(u, v)$, set a time interval $[s, t)$, which means the edge is only available from time $s$ to time $t$.
    Every deletion will make some edges become unavailable at some time points. More specifically, if we delete $i$-th vertex, every edge adjacent to vertex $i$ will unavailable at time $i$. Then the number of available time intervals for each edge will be at most 3.

If we know whether the graph is a bipartite graph at a specific time $i$, we also know whether $i$-th vertex satisfy our will.

Solve the problem:   You are given an undirected graph $G$ with $n$ vertices and $m$ edges. Each edge will available only in a specific time interval $[st, ed)$. For every time $t$, you are to determine whether the graph is a bipartite graph.

Let function $Solve(l, r)$ solve all time from $l$ to $r$ and $E(l, r)$ is a set of edges which can exist at some time in interval $[l, r)$.

Let mid = $\frac{l+r}{2}$, for each edge in $E$, assume the interval is $[x, y)$:

$x = l$,$ y = r$, add the edge and check if it forms an odd circle with other edges added before.
    $y \leq mid$, add the edge to set $E(l,mid)$.
    $x \geq mid$, add the edge to set $E(mid, r)$.
    otherwise, add the edge to both sets $E(l,mid)$ and $E(mid, r)$.

We can use disjoint-set to maintain odd circles. After each $Solve(l, r)$, remember to resume the disjoint-set.

How to resume the disjoint-set:

Just union by rank and there is no need to use path compression.
    Before every union operation, use a stack to store the state before.
    Resuming the disjoint-set is just to restore the state according to the stack.

Time complexity: $O(m \ log^{2}\  n)$   ##[1003.Cake](http://acm.hdu.edu.cn/showproblem.php?pid=5355) Summary You are given n integers 1, 2, . . . , $n$ and an integer $m$. You need to divide the $n$ integer into $m$ equal sum parts (or determine it is impossible). Solution

If $m$ is not divisible by $\frac {n(n+1)}{2}$ or $n < 2m - 1$, it is impossible.
    If we have 2$m$ number 1, 2, . . . , 2$m$, we can divide it into $m$ equal sum parts easily.
    If $n$ is large, we can reduce $n$ to $n - 2m$. If $n (n \leq40)$ is small, we can use backtracking to find a possible solution or we can construct the solution by hand.

Time complexity: $O(n)$.   ##[1004.Deal](http://acm.hdu.edu.cn/showproblem.php?pid=5356) Summary Given are a weighted tree with $n$ vertices. There’s an item with weight $w_{i}$ in $i$-th vertex. The cost for transporting an item with weight $w$ from vertex $u$ to vertex $v$ is $w$ multiply the distance from vertex $u$ to vertex $v$. Item will be cached in the vertex it passed. You are going to make $m$ transportations and make the sum of cost maximum. Each item will be transported from a vertex containing it and closet to the destination.   Solution

Each item is independent, so just consider item by item.
    For item $i$, the first transportation must be the longest, same as the second transportation and so on.
    Using greedy to find the path. Sort all the possible transportations and get the largest $m$.

Time complexity: $O(n^{2}\  log\ n)$.     ##[1005.Easy Sequence](http://acm.hdu.edu.cn/showproblem.php?pid=5357) Summary You are given a parentheses string. For $i$-th character, you need to find the number valid strings starting with it or ending with it. For $i$-th character, you need to find the number valid substrings containing it. Solution

let $match_{i}$ be the position of matched parenthese for $i$-th character, $a_{i}$ be the number of valid substrings starting with $i$-th character, and $b_{i}$ be the number of valid substrings ending with $i$-th character.

$a_{i}=a_{match_{i+1}}+1$, $b_{i}=b_{match_{i-1}}+1$

assume $s_{i}$ is ’(’, let $up_{i}$ be smallest matched parentheses contain $s_{i}$ and $s_{match_{i}}$

$ans_{i}=ans_{match_{i}}=ans_{up_{i}}+a_{i}*b_{match_{i}}$

both $match_{i}$ and $up_{i}$ can be computed by using a stack.

Time complexity: $O(n)$.   ##[1006.First One](http://acm.hdu.edu.cn/showproblem.php?pid=5358) Summary For a given sequence $a_{1}$, $a_{2}$, . . . , $a_{n}$, we want to find the value of $\sum_{i=1}^{n} \sum_{j=i}^{n}(\left \lfloor log_{2} S(i ,j)\right \rfloor+1)*(i+j)$ Whrer S(i ,j) is the sum of $a_{i}$,$a_{i+1}$,…,$a_{j}$ Solution

$\left \lfloor log_{2}x \right \rfloor +1$ is the number of bits in the binary representation of $x$.
    Consider the contribution of $k$-th bit: if $S(i, j)\geq 2^{k}$ ,$ i + j$ is added to the answer.
    Enumerate $k$-th bit, and use two pointers to maintain the contribution.

Time complexity: $O(n\ log\ n)$.     ##[1007.Group](http://acm.hdu.edu.cn/showproblem.php?pid=5359) Summary We are given an directed graph $G$ with $n$ vertices and $m$ edges. You are to determine for each edge $i$,after deleting it, whether the number of strongly connected components increases. Solution Let us first solve the vertex deleting one:

Find all the strongly connected components, each strongly connected component can be considered independently.
    Let $G = (V,E)$ is a strongly connected graph, $G^{R} = (V,E^{R})$ is the reversal graph of $G$ (if $(u, v)$ in $G$ then $(v, u)$ in $G^{R}$), $G(s) = (V,E, s)$ be the flowgraph with start vertex $s$,$D(s)$ the set of non-trivial dominators in $G(s)$, $G^{R}(s) = (V,E^{R}, s)$ be the flowgraph with start vertex $s$, $D^{R}(s)$ the set of non-trivial dominators in $G^{R}(s)$.
    Then vertex $v\neq s$ is a strong articulation point in $G$ if and only if $v \in D(s)\cup D^{R}(s)$.
    proving it is not easy, you may google it if you have interest.

Now let us solve the edge deleting one: for each edge $x\rightarrow y$, add an extra vertex $z$ between the edge, $x \rightarrow z \rightarrow y$. We can use the mothod above to solve it. Time complexity: $O((n + m) \alpha (n + m))$   ##[1008.Hiking](http://acm.hdu.edu.cn/showproblem.php?pid=5360) Summary There are $n$ people and $i$-th people will go out if the number of people going out if no less than $l_{i} + 1$ and no larger than $r_{i} + 1$. You need to invite them one by one to make the number of people going out as large as possible. Solution

sort all the people according to $l_{i}$ and $r_{i}$.
    maintain an integer $cur$ as the number of people agree to go out yet. For all the people will agree to go out, pick the one with smallest $r$.

Time complexity: $O(n\ log\ n)$.   ##[1009.In Touch](http://acm.hdu.edu.cn/showproblem.php?pid=5361) Summary $n$ vertices are in a straight line and $i$-th vertex is in position $i$. $i$-th vertex can teleport to other vertices whose distance between $i$-th vertex is no less than $l_{i}$ and no larger than $r_{i}$ with cost $c_{i}$. For each vertex,find the minimum cost from 1-st vertex to it. Solution

let $d_{i}$ be the cost entering vertex $i$.
    use dijkstra to calculate $d_{i}$ and use disjoint-set to maintain the vertex not visited yet.
    the answer is $d_{i}-c_{i}$.

Time complexity: $O(n\ log\ n)$.   ##[1010.Just A String](http://acm.hdu.edu.cn/showproblem.php?pid=5362) Summary You are given a random string of length $n$ and an alphabet of size $m$. You are to tell the expected number of such substrings that can be rearranged to a palindrome in the random string. Solution

The answer is obvious after enumerating the length of the substring:

$\sum_{l=1}^{n}(n-l+1)m^{n-l}T(l,m)$ where $T(l,m)$ is the number of such string with length $l$ and alphabet of size $m$.

$l$ can be odd or even, let’s solve the even case first:

——$T(2n,m)=\sum_{i=0}^{n}(2i,m-1)* \begin{pmatrix}2n\\2i \end{pmatrix}$ ——$\frac {T(2n,m)}{(2n)!}=\sum_{i=0}^{n}\frac {T(2i,m-1)}{(2i)!}*\frac{1}{(2(n-i))!}$ ——Let $G_{m}(x)$ be the generating function for $\frac{T(2n,m)}{(2n)!}$ , F(x) be the generating function for $\frac {1}{(2n)!}$. Then $F(x)=cosh \sqrt {x}=\frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2} $and $G_{m}(x)=F^{m}(x)=( \frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2})^{m}$. ——$T(2n,m)$ is the coefficient of $n$-th item of $G_{m}(x)$, after doing some calculation, we have $T(2n,m)=\sum_{i=0}^{\frac{m-1}{2}}\frac{\begin{pmatrix}m\\i \end{pmatrix}*(m-2i)^{2n}}{2^{m-1}}$ For a fixed $n$, which can be computed in $O(m)$ time.

Now it is time for the odd $l$:

$T(2n+1,m)=\sum_{i=0}^{n}T(2i,m-1)* \begin{pmatrix}2n+1\\2i \end{pmatrix}$ If we precompute the value for $T(2i,m - 1)$ then $T(2n + 1,m)$ can be computed in $O(n)$ time. Time complexity: $O(n(m + n))$.   ##[1011.Key Set](http://acm.hdu.edu.cn/showproblem.php?pid=5363) Summary For a given set {1, 2, . . . , $n$}, you are to find the number of nonempty subsets with even sum. Solution Let $a$ be the number of even integers and $b$ be the number of even integers. The answer is: $2^{a}(\begin{pmatrix}b\\0 \end{pmatrix}+\begin{pmatrix}b\\2 \end{pmatrix}+$…$)=2^{a+b-1}=2^{n-1}-1$ Time complexity: $O(log\ n)$

时间: 2024-10-12 16:35:20

多校题解的相关文章

HDU 6351 Beautiful Now(DFS)多校题解

思路:一开始对k没有理解好,k是指最多k次,不需要达到.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位交换.k = 0没判断好WA了2发... 如果k >= len - 1,那么最大最小就是直接sort非前导零的答案.如果k < len - 1,那么我们交换肯定从最大位数交换,比如现在求最大值,那么我们从第一位依次判断,如果该位不是他后面最大的,那么就和后面最大的交换(如果最大的有多个,那么就每个都搜索一下),否则不消耗交换次数判断下一位.最小同理 注意前导零. 代码:

hdu5819

补多校系列,具体见多校题解http://www.cnblogs.com/duoxiao/p/5777700.html 值得注意的是如果当前i初始向左,前i个骑士最终只有1个向右 对于f[i][1]状态的转移为f[i][1]=∑ f[i-1][k]*0.5^(k-1) 因为我们只要比k-1场就可以了,那一个活下来的是骑士i还是前i-1个骑士中的无关紧要 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 typedef long long l

论直接取模与手写取模函数的时间差异

最近在多校题解中经常看到巨巨们手写函数进行取模操作,似乎是比%操作更快. %操作在计算机中的实现依靠除法,显然不如手写函数的加减法更优秀. 在进行多次加法更新取模的时候,可以写一发.不过当算法复杂度在O(n)以上的时候,这个优化意义不大. 1 #include <cstdio> 2 3 const int maxn = 5e8+11; 4 const int MOD = 1e9+7; 5 typedef long long LL; 6 7 inline void update(LL &

hdu5338 ZZX and Permutations

hdu5338 ZZX and Permutations 非原创,来自多校题解 不是自己写的,惭愧ing…… 留着以后自己参考…… lower_bound {1,2,4,5} 询问 2,返回的是 2 ,询问3 返回的是 4 是大于等于元素的值 upper_bound {1,2,4,5} 询问2,返回4,询问3,返回4,是 大于 元素的值 题意:图论的知识 1 2 3 4 5 6 (1) 1 4 5 6 3 2 (2) (1)中 数字 1 的 位置没变 所以(1) 2 的为位置 编程了 4 ,4

HDU 5338 ZZX AND PERMUTATIONS 线段树

链接 多校题解 胡搞... 题意太难懂了.. ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 310    Accepted Submission(s): 83 Problem Description ZZX likes permutations. ZZX knows that a perm

ACM2016级新生第三周训练赛

本次是弱校题解-比赛链接 备用链接 题目还是比较基础,比较简单.认真补题,学会学习. A -人见人爱A^B 题解: 求 A的B次方,我们可以用循环进行累乘操作,进而计算出次方.因为题目要求只需要求出最后三位,所以每次对 1000 求余数,最后输出即可. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> int main() { int n,m; while(sca

HDU 6060 RXD and dividing(LCA)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1,求每个集合最小斯坦纳树的最大权值和. [题解] 我们按照后序遍历染色分组,得到的一定是最优分组, 现在考虑在不同颜色的虚树上求路径权值和, 我们发现每个点增加的权值是深度减去到根的路径上已被覆盖的长度, 这个长度等于与dfs序前继的LCA的深度,因此我们在搜索的同时计算与dfs序前继的LCA即可.

hdu5336 Walk Out

hdu5336 Walk Out 题意是:入口:地图的左上角,出口,地图的右上角,求所经过的路径的二进制数最小 照着题解敲了一遍 思路是:首先 二进制 的 位数 越小 越好,其次 二进制的前缀零越多 表示 值越小 首先 在前缀是0的情况下走到最远(最远的定义是 当前坐标是 i , j 则 在保证 位数越小的情况下 则 还剩下 n-i + m-j 的路要走) 剩下的路 必须 向 右走 或者向 下 走,然后 在 其中 选取 0 尽量 多的 路 代码:非原创!!!(from 多校题解) #includ

2016 SCNUCPC 校赛非官方题解

我要举报本次校赛出题人的消极出题!!! A. 树链剖分数据结构板题 B. The background of water problem 题目大意(大写加粗的水题):给定$N$个学生和他们$K$个科目的成绩$S_i$,再给出各科目$K_i$的权重顺序$Q_i$,求排名之后,拥有id为$X$的是哪个学生. 基本思路:虽然$K$只有$10$,$S$只有$100$,但有M组查询,所以当然不能开个long long去hash每个学生.我们简单点,开个结构体,排个序,就好了. 参考代码: 官方代码(将成绩