现在的位置: 首页 > 综合 > 正文

sgu题目分类

2018年12月26日 ⁄ 综合 ⁄ 共 31312字 ⁄ 字号 评论关闭

http://www.nocow.cn/index.php/Sgu

以下分类收集于网络:

100 A+B
Author: Amber
Method: Add Directly

101 Domino
Author: Amber
Method: Euler Path
Complexity: O(N^2)

102 Coprimes
Author: Amber
Method: Calcuate Euler Function

104 Little Shop of Flowers
Author: Amber
Method: DP
Complexity: O(N^2)

105 Div 3
Author: Amber
Method: Maths
Detail:
Consider the head of the sequence.
Number: 1 12 123 1234 12345 123456 1234567 12345678 …
Mod 3: 1 0 0 1 0 0 1 0 …
The loop is (1 0 0).

106 The Equation
Author: Amber
Method: Math
Detail:
简单,但麻烦的题,考虑问题要周全。

1.先把A或B为0的情况排除。
2.用extended_gcd找到一组(x0,y0), s.t. A*x0+B*y0=D, D=GCD(A,B)
3.方程有解冲要于D|C
4.A=A/D,B=B/D,C=C/D
5.x0=x0*(-C),y0=y0*(-C)
6.把 x=x0+t*B, y=y0-t*A 带入 x1<=x<=x2, y1<=y<=y2
x1-x0<=t*B<=x2-x0
y0-y2<=t*A<=y0-y1
把A,B除过去时要注意是否变号
7.得到t的范围,输出
Notice:
1.做A,B不等式除法时要注意是否变号
2.不要吝惜Int64

107 987654321 problem
Author: Amber
Method: Maths
Detail:
When N = 9, there exists 8 solutions by maths method or search program.
When N = 10, the first digit is in range (1 – 9). So 8 * 9 = 72.
When N > 10, insert a digit in range (0 – 9) into the second digit of the solution with (N – 1) digits. So Ans = 72 * 10^(N – 10)

108 Self-numbers II
Author: Amber
Method: Filter
Version: 1 (Filter)
Detail:
Do filtering as filtering primes: N generates D(N) and filters D(N) from small to large.
1)Cuz D(N)-N<=9*lgN, use Loop queue O(9*lgMaxN) in memory.
2)Pre-calculate the digit sums from 0000 to 9999 to Sum.
So the time of D(N) is reduced from O(lgN) (digit by digit)
to O(2) (4 digits by 4 digits, and N is O(10^7))
3)sort the query
Complexity: O(Sqrt(MaxN)*lgSqrt(MaxN)) for pre-processing the sum
O(MlogM) for sorting the querys
O(N) for filtering
Real time: 1134 ms
Notice:
1)It's Read not Readln.
2)There may be two same querys!!

111 Very simple problem
Author: Amber
Method: Sqrt with HP
Detail:
Y=Sqrt(X), 设 |X| = N,则 |Y| <= N / 2
从高位到低位,枚举Y的每位数字Yi,直到Y^2恰好<=X (即这位取Yi + 1时,Y^2 > X)
State:
253102 19.12.04 13:14 hupo001 111 .PAS Accepted 197 ms 89 kb

112 a^b – b^a
Author: Amber
Method: Simulate by HP

113 Nearly prime numbers
Author: Amber
Method: Find prime divisors
Notice:
P = P1*P2 (P1 may be equal to P2)

114 Telecasting station
Author: Amber
Method: Enumerate
Detail:
It's easy to prove that the station must be in some city.
Notice:
WA too many times cuz of position may not a integer.

115 Calendar
Author: Amber
Method: Simulate

117 Counting
Author: Amber
Method: Prime Decompose
Detail:
K is decomposed to the formal of Product[Pi^Ai].
For each T, it will be decomposed to the same formal of Product[Pi^Ti].
if Ti * M >= Ai then count.

118 Digital root
Author: Amber
Method: Maths
Detail:
1.Digital Root is equivalent to "Specially Modulo 9" (replace 0 with 9).
2.A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1
=A1*(A2*…*AN + A2*…*AN-1 + … + A2 + 1)
=A1*(A2*(A3…*AN + A3*…*AN-1 + … + A3 + 1) + 1)
=…
=A1*(A2*(A3(…*(AN + 1)… + 1) + 1) + 1)
So we can use O(N) to solve it.
Complexity: O(N)

119 Magic pairs
Author: Amber
Method: Math
Detail:
预处理:
令T=(A,B,N), A=A/T,B=B/T,N=N/T,
现在(A,B,N)=1,它的解原来原来的解可以一一对应: 解(A,B)->解(A*T, B*T)

以下都是在mod N意义下的,k为正整数
证明: 解(kA,kB) (0<=k<n) 就是问题的全部解<br=""> kA 能取 P=[A,N]/A 个值,同样 kB 能取 Q=/B 个值, 这样 解(kA,kB) 能取[P,Q]个值。
下证 若(A,B,N)=1 则[P,Q]=N
由AB=(A,B)[A,B] 得P=N/(A,N),Q=N/(B,N)
又得 (A,N)*P=(B,N)*Q=N
又由(A,B,N)=((A,N),(B,N))=1,得[P,Q]=N

123 The sum
Author: Amber
Method: Simulate

124 Broken line
Author: Amber
Method: Geometry Count
Detail:
It's a classical problem in geometry:
Query whether point R(x,y) is inside a simple polygon.

for each segment P(x1,y1)-Q(x2,y2) here x1<=x2
if x1<=x if det(PQ,PR)>0 then
count++
if odd(count) then "inside" else "outside"
Do something special for "border" case.

Complexity: O(N)

125 Shtirlits
Author: Amber
Method: Brute-force
Detail:
Range is too small, Brute-force!!!
My program is slow.
If providing search order, it can be more optimal.

126 Boxes
Author: Amber
Method: Maths + Simluate
Detail:
S = A + B

127 Telephone directory
Author: Amber
Method: Simulate

130 Circle
Author: Amber
Method: DP
Detail:
A diagonal can divide the graph into two parts.
These parts are still the same as the original problem.
Complexity: O(N^2)

131 Hardwood floor
Author: Amber
Method: Set-DP
Detail:
State (N,K) (0<=K<2^M) denotes that the current is in n-th lines and the plan of (n-1)-th lines is K.
Transfer: K->K' by DFS
There are 7 transfers in total.
1: 2: 3: 4: 5: 6: 7: do noting
** ** * * *
* * ** ** ** *
in my program,
"Pow and Prev" gets the depth-th value of the last line.
"Pow shl 1 and Prev" gets the (depth-1)-th value of the last line.
Complexity: O(N*2^M*R)
R is average transfer times.
Notice:
1)把转移事先都处理好
2)使用queue向后递推的方式,可以减少DP初期的冗余(初状态只有一个!)
3)由于有queue,DP数组球没有必要每次都filchar.用了一个状态后,清0即可.

133 Border
Author: Amber
Method: Sort and Count
Complexity:
O(nlogn) for sort
O(n) for scan

135 Drawing Lines
Author: Amber
Method: Simple Maths

137 Funny Strings
Author: Amber
Method: Construct (Maths)
Detail:
记N个和为M数组成的Funny string为F(N, M)
if N <= M then
F(N, M mod N) + M div N
if N > M then
问题就是由M个1与N-M个0组成的Funny String。
它的第i个0后有F(N – M, M)中的第i个数个1。
i.e.
对于Funny string 0101101011011,统计每个0后1的个数得:
1,2,1,2,2,依然是个Funny string.

138 Games of Chess
Author: Amber
Method: Construct
Detail:
把数据排序,由于保证有解,最多场的那个人不会超过总和的一半(不会自己和自己比).
所以按以下方法填掉第一列(第一列为win,第二列lose)
即按场次从大到小依次填入第一列,若只剩一场就填到第二列去.
win lose
x
x
x
y x
y
z y
z

最后把剩下的填到第二列没填的地方.
Complexity: O(N)

140 Integer Sequences
Author: Amber
Method: Maths (Number Theory)
Complexity: O(N)
Detail:
解n元线性模方程

1.先做整理
Sum[Ai*Xi | 1<=i<=N] = B (mod P)
=> Sum[Ai*Xi | 1<=i<=N] + P*X0 = B
将P用A0代换,即令A0=P
=> Sum[Ai*Xi | 0<=i<=N] = B

2.逆推,分步迭代
先把最后一项整理出来
=> Sum[Ai*Xi | 0<=i<=N-1] + AN*XN = B

将前面的项用他们系数的GCD代换 Sum[Ai*Xi | 0<=i<=N-1] = GCD[Ai | 0<=i<=N-1]
=> GCD[Ai | 0<=i<=N-1] * T + AN*XN = B
通过Extended_GCD(GCD[Ai | 0<=i<=N-1], AN, T, XN) = GCD[Ai | 0<=i<=N], 解得(T, XN)。

而Sum[Ai*Xi | 0<=i<=N-1] = GCD[Ai | 0<=i<=N-1] 又是一个N-1规模的子问题,
最后只要将这个子问题的解 * T 就可以了。

3.综上易知有解充要条件是:B被GCD[Ai | 0<=i<=N]整除

符号:
这里 GCD[A1, A2, ..., AN] = GCD[A1, GCD[A2, GCD[..., GCD[AN-1, AN]]]]
Extended_GCD(A, B, X, Y)的结果是(X, Y)使得AX+BY=GCD(A,B).

142 Keyword
Author: Amber
Method: Enumerate + Encode
Detail:
1.Enumerate the length of answer Len increasing.
2.Encode the substrings with length Len in string, label it in table.
3.Enumerate unlabeled substrings with length Len, if not exists, goto 1.
Complexity: O(N)*O(2^MaxCodeLen)

145 Strange People
Author: Amber
Method: Binary Search + DFS
Detail: K短路问题
利用二分答案转为判定性问题,这样由于K比较小,每次只要搜K条就可以了。复杂度为O(NK)的。
还可以利用最短路标号来剪枝。
Complexity: O(NKLogW)

148 B-Station
Author: Amber
Method: Enumerate+Optimize
Detail:
确定了减压的第一层,就可以模拟出来一个计划了.O(n)
枚举所有的减压的第一层,计算出计划的价格,取最优。
单调性优化:从下往上处理, 注意到所有层都只可能从付钱到不付钱。
Sum(i)=SUM(Water(i)|i=n to i)
j为当前枚举层, 对于i>j,若有 Limit(i)<sum(j)-sum(i+1),则扣掉第i层的钱。
 这可以通过排序Sum(i+1)+Limit(i)实现。
Notice:
It will get MLE at 4 by QuickSort. (1349 kb)
It got AC by Heapsort. (449kb)

150 Mr. Beetle II
Author: Amber
Method: Enumerate + Maths
Detail:
Some useful testcase for me
Input: 8 1 1 9 4
Output: 6 3

151 Construct a triangle
Author: Amber
Method: Simple Geometry
Detail:
The median formula is 2a^2+2b^2-c^2=4m^2.
AC=BD, So 2*AM=AD<=AB+BD=AB+AC, 2*AM=AD=>AB-BD=AB-AC.
A
/|\
/ | \
/ | \
B—M—C
\ |
\ |
\|
D
Notice:
The area of the triangle can be equal to zero!

153 Playing with matches
Author: Amber
Method: DP and Find Loop
Detail:
1.Simple DP
F(n)=true if exists one di s.t. F(n-di) is false
false else
2.Find Loop
Let max be the maximum of the taken number.
Consider case n>max.
Let prev_state(n) store the binary code of F(n-max) to F(n-1) with length max.
If prev_state(i)=prev_state(j) (i<j), then="" we="" find="" the="" loop="" that="" is="" from="" f(i)="" to="" f(j-1).<br=""> Complexity: O(Min(2^M,N))

154 Factorial
Author: Amber
Method: Math
Detail:
Problem is
given Q and to find N s.t. …+[N/5^4]+[N/5^3]+[N/5^2]+[N/5]=Q -(*)
Let N=5X, write X in 5-base:
X=…+ 5^3*x3 + 5^2*x2 + 5*x1 + x0, xi=0..4
X into (*) => Q=… + (5^3+5^2+5+1)*x3 + (5^2+5+1)*x2 + (5+1)*x1 + x0
Let sum(i)=sum(pow(j)|j<=i)
Algorithm is known:
for each i from high to low digit
1.xi=Q div sum(i)
2.if xi>4 then no answer
3.Q=Q mod sum(i)
Notice
When Q=0,output 1 (not 0).

155 Cartesian Tree
Author: Amber
Method: Construct Cartesian Tree in Linear Time
Complexity:
Sort: O(nlogn) + Construct: O(n)
Detail:
Cartesian Tree(CT)
这里称Auxiliary为Proirity(优先级, Pro)
先将节点按key排序,得到CT的中序遍历。
下面是CT的线性时间构造:
设Ci是T[1...i]的笛卡尔树。
1.第i+1个结点一定属于Ci+1最右侧的路径。
(若不考虑优先级,第i+1个结点为最右侧的最右端点。)
2.自下而上检查最右路径,找到第一个优先级低于第i+1个结点的结点p.
p的右子树作为第i+1个结点的左子树.
使第i+1个结点成为p的右儿子.

可以证明每个节点最多被检查1次,因为只要在最右路径上的点被检查不是新节点的父亲,
就要转移到新节点的左子树中,不会再被检查到。
所以本算法是线性的。
Real Time: 246 ms

156 Strange Graph
Author: Amber
Method: Euler Path + Contraction
Detail:
先理解这个图: 它是由若干个子完全图组成, 子完全图中的每个点有且只有一条链与其他的某个点(本子图或其他子图)相连.
求其hamilton圈,可以先把每个子完全图压缩成一个点(称为团),在这样压缩后的新图上做euler圈,最后再把团展开(入团点->出团点),就是hamliton圈了.
因为只有团才能为奇点,所以当只有0或1个奇团时问题才有解.

我程序中Edge为前向星的原边,GrpEdge为团指出去的边(前向星).这样比较方便处理.
Complexity: O(M)

158 Commuter Train
Author: Amber
Method: Enumerate
Detail:
The maximum may occur in the case that one certain person stands at mid-point of two adjacent doors.
Complexity: O(NM(N+M))

159 Self-Replicating Numbers
Author: Amber
Method: HP
Detail:
性质P: n位数x的平方后n位仍为x.称有性质P的数为"P数".
一个显然的推论: n位P数必要条件是它的后n-1位是P数.
于是: 枚举第n位,把的n-1位P数转移到n位的P数.
这样维护一个扩展队列就可以了
剩下是高精度的问题了

162 Pyramids
Author: Amber
Method: Geometry
Detail: See readme.gsp

163 Wise King
Author: Amber
Method: Greedy

164 Airlines
Author: Amber
Method: Math + Floyd
Detail:
A hard-think proposition:
Divide M into two parts A,B such that |A|,|B|<=(M+1) div 2.
A or B must satisfy the condition.
Its proof is in Zhou Yuan's solution.

Complexity: O(N^3)

165 Basketball
Author: Amber
Method: Construct(Greedy)
Detail:
If the current summation is negative, we add a positive number if we can, otherwise add a negative.
After adding a number, if the summation exceeded the limit, there must exist no solution.
The graphics of the current summation is a asway sequence between -Limit to Limit.

167 I-country
Author: Amber
Method: Complex-DP
Detail:
State (Line, Left, Right, GoLeft, GoRight, Remain)
"Line" denotes the current line of process.
"Left" and "Right" denotes the segment from "Left" bound to "Right" bound of the current line which are chosen.
"GoLeft" denotes whether the Left bound of the previous lines is lefter than the current's.
"GoRight" denotes whether the Right bound of the previous lines is righter than the current's.
"Remain" denotes how many cells remain to be chosen.

169 Numbers
Author: Amber
Method: Math
Detail:
Let n=abc…x
s.t. a,b,c…<>0
last digit x s.t. 1<=x<=8, otherwise p(n)=0 or p(n+1)=0
Let r=p(n)/x
Let k,k' (k<=k') s.t. n=krx -(1),
n+1=k'r(x+1) -(2)
Let k'=k+d
(1)(2) => krx+1=krx+kr+dr(x+1) => kr+dr(x+1)=1 -(3)
Prove k=k':
Let d>0
Cuz x+1>1, dr(x+1)+kr>1.Conflict, so k=k', d=0
d=0 into (3) => kr=1 => k=1, r=1
So n=111111…x (k-1 ones)

Let condition P(x) is that x | 111111…x.
If P(x) and P(x+1), then 111111…x is perfect.

P(1),P(2),P(5) are always true.
P(3)=3|111111…3 = 3|1*(k-1)+3 = 3|k-1
P(6)=2|111111…6 && 3|111111…+6 = 3|k-1
P(9)=9|111111…9 = 9|1000..00+…+1000+100+10+9 = 9|k-1
P(4),P(8) are true iff k=1.

P(7)=7|111111…7
num: 1111110 111110 11110 1110 110 10
mod 7: 0 6 1 4 5 3
Loop length is 6
so P(7)=6|k-1

170 Particles
Author: Amber
Method: Simulate
Detail: Count the distance between the '-' in two strings.

171 Sarov zones
Author: Amber
Method: Greedy
Detail:
1.Sort zones by level to Z
2.Sort students by weight to S
3.For each student S whose weight is from heavy to light,
If a non-full zone Z[j] whose level is the highest lower than S can be found, then S->Z[j].
else S isn't processed at this moment.
4.For each unassigned S, find any non-full zone Z[j], S->Z[j].
Complexity:
O(M^2) for sort zones
+O(NlogN) for sort students
+O(NM) for greedy
+O(N) for output

174 Walls
Author: Amber
Method: Disjoint-Set
Detail:
有序地给出不相交(可相接)的线段,求线段i,使得放入1,2,..,i后产生第一个闭区域。

由于只有相接情况,可以把端点作为元素,线段为合并条件。
若线段两端的点在合并前已经在同一个集合内,则闭区域出现,退出。
则可利用并查集实现。
下面就剩下查找重合点的问题,有如下几种方法:
1)可以用有序化点集,二分查找。 (预处理)
2)Hash (动态处理)
3)动态查找的数据结构 (Treap等) (动态处理)
1)AC了,2)AC了,3)没做 :P
Version: 1 (Binary search)
Complexiy: O(MlogM) 这里把并查集的复杂度视为常数

176 Flow construction
Author: Amber
Method: Preflow + Binary Search
Detail:
The minimum flow with lower bounds.
在有下界网络流模型中建立的无源汇网络,有一个重要的性质。边(N, 1)的流值,等于当前网络中的总流值。
所以枚举边(N, 1)的上限,找到一个上限恰好使得所有必要弧都满流。这个上限就是最小流值。
由于满足单调性,所以可以二分枚举上限。
Notice:
The edge (N, 1) may appear in input.
So the flow of (N, 1) doesn't denote the flow of the network.
It should be divided into two part to think, which are a part of flow for input edge (N, 1)
and the other part of flow for denoting the flow of the network.

177 Square
Author: Amber
Method: Count by Next Pointer
Detail:
A classical problem.
Count by a next pointer array.
Complexity: O(N*(N+M))

178 Chain
Author: Amber
Method: Maths
Detail:

179 Brackets light
Author: Amber
Method: Construct
Clariry:
寻找最右边的可以变为")"的"("(记为x)(也就是x的之前有"("可以与改变后的x匹配),把它改成")"后
,再将它后面的括号排成"((((…))))"+"))..))"的形式。
"+"前的"((((…))))"保证了字典序最小
"+"后的"))..))"是为了与x前的"((..(("匹配的。

181 X-Sequence
Author: Amber
Method: Simulate and Find Loop
Detail: Find the loop of the sequence.
Complexity: O(M^2)

183 Painting the balls
Author: Amber
Method: DP with Optimization
Detail:
State (i,j) (1<=i<=n-1,1<=j<=m-1,i+j<=n) denotes last two black balls i, i+j //O(NM) for states
Transfer (i,j)=min (i-k,k) (i-k+m>=i+j, i-k>=1, k>=1) //O(M) for transfer
If N-M+1<=i, (i,j) is a finish state.
Optimization: //O(1) for transfer
Enumerate j from m-1 to 1, the range of k gets larger cuz of 1<=k<=m-j.
Set a var CurMin for each i which records min (i-k,k) for the larger and larger range of k.
Complexity: O(NM)
Memory: O(M^2)
Notice:
PreMod(i) is i mod M.
It's for scroll array.

185 Two shortest
Author: Amber
Method: Dijsktra + Ford Fulkerson
Detail:
1.It's an undirect graph G.
Dijkstra it!
If an edge (u,v) in G s.t. d(v) = d(u) + w(u,v) then it will remain in new graph G'.
G' is a graph that is shortest-path directed from G.
Namely, Now we get a directed graph G'. Any path from 1 to N in G' is a shortest path.
2.Find two augment paths by flow, and augment them.

186 The chain
Author: Amber
Method: Greedy
Detail:
Each time choose the minimal item, unchain the item to links.
Check if it's enough to chain remain chains.

187 Twist and whirl — want to cheat
Author: Amber
Method: Simulate discretely
Detail:
不断的拆分线段, 每次分裂出2个线段, 模拟即可.
Complexity: O(M^2)

188 Factory guard
Author: Amber
Method: Simulate
Detail:
1.Enumerate any i,j
2.Find first meeting distance Delta of i,j, and then they will complete a circle together for each meeting
So solve (Delta+Len*k)/(|Vi|+|Vj|)<=T, k is integer, k+1 is the number of i,j meeting.
Complexity: O(N^2)

189 Perl-like Substr
Author: Amber
Method: Simulate + String Processing
Detail: 关键就是把复杂Substr函数参数,转为平常的Copy函数的参数。

191 Exhibition
Author: Amber
Method: Simulate
Detail:
The statement is so hard to understand.
Abstract it as following:
Consider a sequence which consists of 4 kinds of chars – abAB.
There are two principles to change the sequence:
1.replace a with B or Aba
2.replace b with A or Bab
Does there exist a way to change a single element a or b at the beginning to a given sequence which consists of AB?

Consider the leftest a or b called P of inital state and the leftest A or B called Q of terminal state.
If Up(P)=Q, only use A->Aba or B->Bab
else only use A->B or B->A.
Simulate in that way by stack.
Complexity: O(N)

193 Chinese Girls' Amusement
Author: Amber
Method: Math
Detail:
Discuss:
Odd(N)
Ans=N div 2 , cuz GCD(Ans,Ans*2+1)=1
Even(N)
Odd(N div 2)
Ans=N div 2-1, cuz GCD(Ans,2(Ans+1))=1
Even(N div 2)
Ans=N div 2-2, cuz GCD(Ans,2(Ans+2))=1

194 Reactor Cooling
Author: Amber
Method: Preflow push
Detail: The feasible flow with lower bounds.
1.先将原图连接上边(N, 1),容量无限,组成无源汇网络。
2.建立一个超级源S,超级汇T。连接上边(T,S),容量无限,再次组成无源汇网络。
对于边(x, y), 容量下界l,容量上界h,应分离出必要弧(即一定要满足的下界)。
边(x, y),等价的变换为三条边(x, T) -> (T, S) -> (S, y)。他们容量均为l。
而分离出必要弧的(x, y)的容量为h-l。
3.去掉(T,S)。从S到T做最大流,若S或T的邻边都满流,则所有必要弧被满足,问题有解,否则无解。
4.还原所有必要弧,则问题还原成1中的无源汇网络。网络中的流值必定是一个可行解。
因为无源汇网络满足流量平衡,且所有必要弧都满流。
Notice:
Why???
第1步中连接上边(N, 1),不能AC。没有连接上边(N, 1),就AC了。
但不能解释下面这个数据。
3 2
1 2 1 3
2 3 2 4
连接上边,则有解;否则无解。

195 New Year Bonus Grant
Author: Amber
Method: Tree-DP
Complexity: O(N)
Detail: 每个节点分发送礼物(可以不发送)(记为0),还是收到礼物(记为1)讨论。
F(x, 1) = 1 + Sum[F(x.child, 0)]
F(x, 0) = Sum[F(x.child, 0)] + Max[F(x.child, 1) - F(x.child, 0)]

197 Nice Patterns Strike Back
Author: Amber
Method: Set-DP
Clarity:

Complexity:

198 Get Out!
Author: Amber
Method: Find circle and judge point inside polygon
Detail:
先将物体本身的大小r,加到障碍物的大小中去: R' = R + r。这样就可以把物体看成是一个点,问这个点是否被圆障碍物围住。
若两个圆有没有相交,则可以认为,人可以从他们之间穿过去;相交则不行。
所以若两个圆相交,则在圆心之间连一条线段,表示这条线段不能通过。问题转化为是否存在一个圈,将物体点围住。
主算法:枚举所有圈,再判断点是否在圈对应的多边形内(上)。
有2个问题:
1.枚举所有圈是NP的。(如完全图的圈数是N!)
我们利用DFS Tree得到的圈是O(M)级别的,少了的那些圈是什么?
考虑DFS Tree:(u是v的祖先, v是w的祖先)

|—|
Root …-u-a-v-b-w
|_______|

存在两条后向边, (w, v), (w, u)。
我们可以找到两个圈,(v, b, w, v),(u, a, v, b, w, u)。
少了一个圈:(u, a, v, w, u),发现是上面两个圈的一个合成。
如图:DFS Tree只能找到A, B两个圈,A, B的合成(外围的轮廓)是找不到的。
但本题要求判断点是否在圈内,若点即不在A, 也不在B,必然就不在A, B的合成内。
所以枚举所有圈是没有必要的,只要枚举DFS Tree得到的圈。
/—\ /—\
| A | B |
\—/ \—/

2.如何快速判断点是否在多边形内(上)。
判断Q是否在多边形{Pi}内,这里使用一种优美的算法:
对于满足Min(Pi.x, P(i+1).x) <= Q.x < Max(Pi.x, P(i+1).x)的边(Pi, P(i + 1))。
若Det(Pi, P(i+1), Q) > 0,则Count++
若Det(Pi, P(i+1), Q) < 0,则Count--
若|Count| = 2,则Q在多边形内
若|Count| = 0,则Q在多边形外
即以Q做x轴垂线,考察它与多边形的所有交点所在的边,相临交点所对应的边的方向一定不同。
利用边方向交错的性质,就可以找到Q是否在多边形内了。
由于是统计型的判断,所以在DFS Tree中可以用部分和的方法,存下当前点到根的路径上的统计信息。
找到圈就可以O(1)判断了。

3.圈对应的多边形不是简单多边形
上面的判断方法,在不是简单多边形的情况下某些中心区域可能漏判。但是由于图是依据圆相交的情况建立。虽然不是平面图。
但是这些中心必然也是一个另外可以拓展到的一个圈的内部。
Complexity: O(N^2)

200 Cracking RSA
Author: Amber
Method: Gauss Elimination
Detail:
若干个数的积是平方,要求每种质因数的个数为偶数。
列出线形模方程,解出自由变量的数目x,答案为2^x-1。
Complexity: O(T*N^2)
Notice: None

201 Non Absorbing DFA
Author: Amber
Method: DP
Detail:
由于Xi=1的转移相当于多走几次字符j,可以转化为新的Phi。
若出现圈则令圈上的节点为死结点(自动机不能走到的)。
状态(l, i),表示到第l步第i个状态的合法字符串数。这样就可以DP了。
Notice:
Test 1, WA 10 times.
数据没有像题目中所说的那样一行一行读入。可能所有数据在同一行上。
Test 11, WA 3 times.
计算新转移的时候没有认真设计算法。

203 Hyperhuffman
Author: Amber
Method: Simulate by Queue
Detail:
Simulate according to the definition of Huffman tree which is to choose two smallest nodes and to merge them each time.
O(nlogn) by Heap
But the data are in order, so we can maintain two ordered queues to solve it in O(N).
A huffman property that the values of mergings are increasing one by one time.
One queue is the data queue.
The other queue is the new-node queue.
Each time choose two smallest node from the heads of two queues, merge them and push it to the tail of the second queue.
Complexity: O(N)

205 Quantization Problem
Author: Amber
Method: DP
Detail:
题目比较令人费解:给出一列数Si和一个m*s的矩阵A(m≤s且m,s都是2的整数次方)。求一列数Ki,使K0=0,且 Sum[|A[K(i-1) mod M, Ki], Si|]最小。
简单DP即可。
Complexity: O(NMS)

207 Robbers
Author: Amber
Method: Greedy
Detail:
ki = [xi/y*m] or [xi/y*m]+1 only.

Let ki=[xi/y*m] firstly, so it may remaining some coins.
diff=|ki/m-xi/y|
Sort robber by diff, and distribute remaining coins to robbers (a man gets a added coin) by diff desc order.
Complexity: O(nlogn)

208 Toral Tickets
Author: Amber
Method: Math – Polya
Detail:
1)enumerating permutations.
2)finding the amount of circle in permutation.
3)calculate answer by Inc,Div based on HP
Complexity:
O(n^2) for enumerating
*O(n^2) for finding
=O(n^4)
Notice:
It's Rotate not Mirror!!!!!

210 Beloved Sons
Author: Amber
Method: Hungary for Max Weighted Match
Complexity: O(N^3)

214 Weird Dissimilarity
Author: Amber
Method: DP (like LCS)
Clariry:
The problem is
A is subseq of P. B is subseq of Q. A,B is given. Minimize Dist(P,Q).
F= Min(
F+D[A,MinA[A]]
F+D[MinB],B[j]]
F+D[A,B]
)
Here
MinA s.t. D[x,MinA[x]]=Min(D[x,y]|for y=1..n)
MinB s.t. D[MinB[y],y]=Min(D[x,y]|for x=1..n)
Complexity: O(n^2)

217 Two Cylinders
Author: Amber
Method: Simpson
Detail:
Let r1<=r2. Cylinder r1 lies on x axis, and r2 lies on y axis.
(x,y,z) in intersection, s.t.
z^2+x^2<=r2^2
z^2+y^2<=r1^2
=>
f(z)=sqrt(r1^2-z^2)*sqrt(r2^2-z^2)
ans=8*integral[ f(z) from 0 to r1 ]
It's hard to find directly.

The trapezia approximation formula costs too many time to get a low precision.
So the parabola approximation formula (Simpson) is fit.

Given a(lower);b(upper);n(the number of dividings);f[x](integral function)
trapezia: Ans=((b-a)/n)*((f[a]+f)/2+Sum[f[a+i*(b-a)/n],(i,1,n-1)])
simpson: Ans=(b-a)/(3n)*(f[a]+f+2Sum[f[a+2i*(b-a)/n],(i,1,n/2-1)]+4Sum[f[a+(2i-1)*(b-a)/n],(i,1,n/2)])

see http://www2.gliet.edu.cn/cai/dept7/math/shuxueshiyan/shiyan5.htm
Complexity: O(N), N is the number of dividings.
Notice: None

218 Unstable Systems
Author: Amber
Method: Binary Search+Match
Detail: Max-Min Match Problem
Complexity: O(NMlogW), W denotes the Weight Range.
Notice:
1.It's important to match unmatch nodes firstly.
2.Weight may be negative.

220 Little Bishops
Author: Amber
Method: Normal-DP
Detail:
求n*n棋盘上放k个象的方案数

把棋盘相间的染成黑白格(用+,-表示),如:
+-+-+
-+-+-
+-+-+
-+-+-
+,-之间的格子不互相影响。把+,-分离出来(分别称为case A,case B),把棋盘45度旋转。
case A case B:
+ –
+++ —-
+++++ —-
+++ –
+
这样可以把象看成是车(走直线)。这个问题和原问题是等价的。问题还可以等价转换为:
+ –
+ –
+++ —-
+++ —-
+++++

令 n(i) 为 第i行的格子数
F(i,j)= //前i行要放j个象
F(i-1,j)+ //本行不放
F(i-1,j-1)*(n(i)-j+1) //本行放, 方法有 n(i)-(j-1) 种,因为j-1个象已经放好,占了恰好j-1列。
按上述方法分别处理case A,case B.
Complexity: O(n^2)
Notice:
1)
When N=1, case A and case B are overlap.So do special processing.
2)
Assign i bishops to case A, and K-i bishops to case B.
s.t. 0<=i<=NumA, 0<=K-i<=NumB
Let Left=Max(0,K-NumB) Right=Min(NumA,K)
So Left<=i<=Right

221 Big Bishops
Author: Amber
Method: Normal-DP
Detail:
求n*n棋盘上放k个象的方案数, 大数据版。
详见SGU 220
Complexity: O(n^2)
Notice: 详见SGU 220

222 Little Rooks
Author: Amber
Method: Math
Detail:
求n*n棋盘上放k个车的方案数
P(n,k)*C(n,k)
Complexity: O(k)
Notice: None

223 Little Kings
Author: Amber
Method: Set-DP
Detail:
求n*n棋盘上放k个王的方案数
基于状态压缩(编码)的DP. (参见noi2001 cannon)
Complexity: O(StateNum*N^3)*O(StateNum)
StateNum=O(2^n)
Notice: None

224 Little Queens
Author: Amber
Method: DFS
Detail:
求n*n棋盘上放k个后的方案数
Notice:
这里没有任何剪枝和优化,算法其实可以更快的(可以快10倍)

226 Colored graph
Author: Amber
Method: BFS
Detail:
Split a vertex (n) by three vertex (n,c) which means reaching n through last edge with color c.
then BFS.
Complexity: 3*O(N+M)

228 Archipelago
Auther: Amber
Method: Geometry
Detail:
已知正n边形的两个点与他们的编号,求正n边形.
主要就是找圆心.
注意方向的处理.
Complexity: O(N)

230 Weighings
Author: Amber
Method: Topological Sort
Detail:
The problem is to find any of solutions which can not conflict with the inequality.
Sort it by topological order by a queue.
Complexity: O(N+M)

231 Prime Sum
Author: Amber
Method: Filter Primes + Enumerate
Detail:
利用 奇素数+偶素数(2)=奇素数
实际上就是找孪生素数.
Complexity: O(N)

238 Uncle Vasya and Bags for Potatoes
Author: Amber
Method: Maths
Detail: 每次操作等于把一个根节点x的第二层结点与所有根结点(除了x)交换,记为Op(x)。
同一个操作Op(x)出现两次以上(含)是没有意义的,图没有变化。
所以一个操作序列(无重复项),唯一的确定一种图。
若操作序列最后一步是Op(x),则操作序列前一步一定是Op(y),y是x在原图的的父亲。
显然这是必要的,否则x决不会到根层。
进一步,可知若要执行操作序列最后一步是Op(x),则操作序列至少是x的祖先序列。
并且这是充分的。若在祖先序列中插入一项的话。则必然被插入的位置原来的操作Op(x)一定不能执行。
必须再调用一次x的父亲。这与操作序列无重复项矛盾。

239 Minesweeper
Author: Amber
Method: DFS
Detail: 给出扫雷游戏 N*2 的左列信息,求右列的可能的放置数

240 Runaway
Author: Amber
Method: Binary Search + Dijkstra
Detail:
易知容忍度是满足二分的性质的。
因为变化率为正,所以早到一条边,比迟到这条边时的温度低。
这个贪心,可以利用最短路标号的性质满足。
Complexity: O(N^2LogH)

242 Student's Morning
Author: Amber
Method: Preflow push
Detail: The feasible flow with lower bounds.

244 Height, Bisector and Median
Author: Amber
Method: Math Discuss + Binary Search
Detail:
Put one point of triangle in (0, H). And the base edge is on x axis.
One necessary condition of the existence of solutions is H <= D <= M.
If H = D = M, there exists a solution of the isoceles triangle.
If H = D < M, there exists no solution.
If H < D = M, there exists no solution because the 0-area triangle is not allowed
If H < D < M, there exists solution that the point D of the bisector is between point H and point M.
The bisector inference of Stewart theorem:
D' = 2 * Sqrt / (B + C), Here 2P = A + B + C.
We can search the two points at the base edge.
S = Sqrt[M^2 - H^2], B = Sqrt[H^2 + (S - X)^2], C = Sqrt[H^2 + (S + X)^2], Here X is the searching value.
D' is monotony. So we can binary search X.

246 Black & White
Author: Amber
Method: Find the principle
Detail: 对于这种一维数列的数学题,一般写暴力程序,观察猜想出规律即可。
Notice: 当N=MaxLongint, (N + 1) div 2 会溢出,要用N div 2 + 1

247 Difficult Choice
Author: Amber
Method: Maths + HP
Detail:
猜想得出 Floor[C(2P, P) / P] + 2。
Thanks to CX.

248 Integer Linear Programming
Author: Amber
Method: Math+Enumerate
Detail:
when n=1, if v mod c[1]=0 then ans=v div c[1] else no answer.
when n=2, solve (c[1],c[2],v)
Let gcd(c[1],c[2])=d
Find all solution of c[1]*x+c[2]*y=v :
x=x0+t*c[2]/d
y=y0-t*c[1]/d

Cuz x>=0, y>=0, the range of t can be got.
Discuss c[1]>c[2] and c[1]<=c[2], and minimize x+y=x0+y0+t*(c[2]-c[1])/d.

when n=3, enumerate x[1]. Solving (c[2],c[3],v-x[1]*c[1]) is the same as that when n=2.

Notice:
Number must be Int64.
e.g. X:=X*C; that 10^6*10^6=10^12!!!

249 Matrix
Author: Amber
Method: Construct
Detail:
考虑线形情况L[x],这里L[x],L[x+1]满足相邻定义。
N L
L1: 000 001
L2: 000 001 011 010
L3: 000 001 011 010 110 111 101 100
….
可以发现 Ln 由 L(n-1) 和 L(n-1)的反转(前面添一个1)组成
所以可以构造出L.

令Matrix为D[x,y]
模仿网格的编码P[x,y]=(x-1)*M+y
构造出D[x,y]=L[x]*2^m+L[y]
Complexity: O(2^(n+m))

254 Strange Random
Author: Amber
Method: Simulate Directly
Detail: 空间正好开得下,直接模拟,当模拟到一定次数后,就清理一遍。经试验, 大概清理10-20次最快。

255 Winsock 3 Beta
Author: Amber
Method: Math + (Precompute + BinarySearch)
Detail:
A means a sequence of the valid M.
A(0)=1, A(n)=A(n-1)+n
Seek for each query in A by binary search.

256 Balloons
Author: Amber
Method: Set-DP
Detail: 由于休息时间最多只有4分钟,且每分钟只有1个人工作,所以状态表示中只需要保存前4分钟工作的人即可。
即F[t, s]:s表示前4分钟工作的人的序列,t表示当前时间。
Complexity: O(Ans * C(N, 4)), 这里Ans <= M

259 Printed PR
Author: Amber
Method: Greedy
Detail:
由于打印的总时间是一定的
希望后面打印的送出的时间尽量短
贪心: 送出的时间越长的越早打印
Complexity: O(n^2) for sort
O(n) for greedy

260 Puzzle
Author: Amber
Method: Gauss Elimination
Detail:
i表示当前所在方程
j表示当前处理变量
若没有方程有j变量,则j++;
否则,交换方程i与有j变量的方程,用方程i,消去所有含有j变量的方程的j变量,j++,i++。
Notice:
写这题之前,我的高斯消元有2种错误的写法。
1. 化为对角矩阵,实际上只有可逆矩阵才可以这样。
2. 化为上三角矩阵,错误的认为第i个方程没有第i个变量就可以跳过。

261 Discrete Roots
Author: Amber
Method:
n的原根(primitive root modulo n) g 定义为:
对于所有与n互质的数与每次乘g模n的操作组成能组成循环群, 记为Zn*.
称g为n的原根.
n有原根 当且仅当 n 有形式 1, 2, 4, p^k, 2p^k,这里p >= 3的素数, k >= 1 的整数。
原根的快速判定:
若g ^ (Phi(n) / Pi) mod n都不等于1,则g为n的原根。
Phi(n)为n的欧拉函数,即1到n-1与n互质的数的个数。这里Pi为Phi(n)的质因数。

本题中n为质数, 则Phi(n) = n – 1, 找到原根g就可以将{1, 2, .. n-1}的数与{g^1, g^2, …, g^(n-1)}建立一一对应关系。

x^k = a (mod n)
令g^y = x, g^t = a,则有 g^yk = g^t (mod n)。
因为n是质数,所以方程左右都不可以为0。就可以将这n-1个取值与指数建立对应关系。
<=> ky = t (mod (n – 1)),关于y解模线性方程就可以了。

下面就是g^t = a怎么求解:令s = floor(sqrt(p)),则有 t = b * s + r(0 <= r < s), 即有 g^t = g^bs * g^r
将所有g^r放入有序表中,从小到大枚举b,g^bs * g^r = a 则把g^r看成未知数解模线性方程。
若解能在有序表中二分查找到,则停止枚举,t = bs + r。
Complexity: O(G(P) * H(P - 1)) + O(Sqrt(P) * (Log(P) + Log(Sqrt(P))) + O(LogP)
G(N)为N的原根,H(N)为N的质因数个数。
Notice:
程序中变量名N,表示不一定需要是质数;而变量名P,表示一定是质数。

262 Symbol Recognition
Author: Amber
Method: IDS
Detail:
首先可以排除所有K块都一样的格子,剩下L个格子。
答案A下界为Ceil[LogK],而A增加1,则重复的概率就大大减小(因为相同格子都去掉了)。
所以可以从小到大枚举A,再枚举所有的方案。
因为K只有6,就是说A的下界为3。
复杂度似乎是 O(C(100, 3) + C(100, 4) + …),估计最多迭代到6。
而实际上复杂度远没有那么高。
因为排除所有K块都一样的格子, 则若L越大, 在A很小的时候就越容易找到解; 反之, 若L比较小, A可能比较大, 但C(L, A)依然很小.

264 Travel
Author: Amber
Method: Adjusting (Stable marriage problem)
Detail:
问题是求完全二分图的稳定完备匹配。
对于每条边有两个权: 由x指向y: Wx(x, y); 由y指向x: Wy(y, x), 权值均不相同.
稳定:不存在x与y匹配, x'与y'匹配, 使得 Wx(x, y') > W(x, y) 且 Wy(y', x) > Wy(y', x).

完备婚姻的算法 — 延迟认可算法

每一位女士被标记落选记号开始。
找到一位落选女士u,在所有尚未拒绝她的男士中选择一位被她排名最优先的男士v。
若这位男士v, 原来没有配对女士;或有一个临时配对女士w, 但在v看来w不优于u。
则让男士v选择女士u,拒绝原来的临时配对女士w,将u标记未落选,将w标记落选。
这时实际上只是临时决定让v配对u。最终的决定是在算法结束时才确定,即标号修改法。
也是调整法思想的体现。

由于每次总有一个女士被一个男士拒绝。所以最多拒绝N * (N – 1)次。
算法复杂度为: O(N^2)。

267 Optimist vs. Pessimist
Author: Amber
Method: Geometry+Data processing
Detail:
Problem is hard to understand.
Given set R, N=|R|, K
Maximize and minimize GotArea(A) for all A s.t. Area(A)=max and |A|=K.
Area(A) is the area summary of each x in A.
GotArea(A) is the area summary of each x in A which can be cut.

Maintain a orderly ranklist for first K elements.
Let S is the last of ranklist.
Keeping Area(ranklist)=max, replace S by all x that x's area is S and find maximum and minimum of GotArea.

269 Rooks
Author: Amber
Method: DP+HP
Detail:
由于放上一个rook,就等价于删去其所在列和行。所以有序化的数据后,问题是等价的。
f(n,k)=f(n-1,k-1)*(r(n)-(k-1)) //选
+f(n-1,k) //不选
可用滚动数组优化。
Complexity: O(NK), Here HP operation is considered O(1).
Notice:
可以透过"沟"攻击, 比如下面p可以攻击q.
xxp
x
xxq

271 Book Pile
Author: Amber
Method: Data Processing(Loop Queue)
Detail:
维护一个长度为K的有一个是否反向的标志的数据块。
因为只关心最顶上的K个数据,所以只要有add操作,就可以输出掉(pop)一个最下面的数据,然后加入当前add的数据至顶(push)。
用一个双端循环队列(两端都可以加数据)来实现这个数据块,有两个指针head,tail。正向的话,tail为顶部。反向则head为顶部。
具体见程序。
Notice:
(1)
String operation in Pascal is slow!!! I got TLE n times.
TLE -> AC
String -> String[8]
(2)
Be careful that Boundary K=0!

272 Evacuation plan
Author: Amber
Method: BFS + DFS
Detail:
先求出最短路标号
再搜索所有最短路
因为题目要求是一个极大的方案,不是最大,所以搜索的时候,可以打访问标记。
因为找到的最短路可能会阻挡其他的最短路。
故复杂度是O(N + M)的。

273 Game Po
Author: Amber
Method: DP
Notice:
数组DP的速度很快。
记忆化搜索的速度特别慢, TLE n times
State:
DP
396257 13.07.06 20:05 hupo001 273 .PAS Accepted 300 ms 557 kb
记忆化搜索
396252 13.07.06 19:46 flower 273 .PAS Accepted 1976 ms 1029 kb
Complexity: O(4*N^3*L), 实际上远没有这么高

274 Spam-filter
Author: Amber
Method: Simulate
Notice: Some detail such as Word isn't empty etc.

275 To xor or not to xor
Author: Amber
Method: Recursion
Detail:
问题:二进制下L位的N个数Ai,选出一些,按xor运算,组成L位的最大的数X.
可以这样以递归思想处理:
0.重复1-4,直到L=1
1.找出一个第L位为1的数A(i),找到转2,否则转4
2.若X的第L位为0,则X=X xor A(i).
3.使所有第L位为1的数A(j), A(j)'=A(j) xor A(i)
4.这样问题的规模减小到: L-1位的N个数Ai',组成L-1位的最大数X'.
由于可能有A(i)'=A(i) xor A(j),
所以这样处理出的X,就有很多个相同项: X=(A1 xor A1 xor … xor A1) xor (A2 xor A2 xor … xor A2) xor …
但由于有P xor P=0,可以两两消掉相同项.这样的X就是题目所求的X,X=A(i1) xor A(i2) xor …,此法是可行的.
Complexity: O(64*N)

276 Andrew's Troubles
Author: Amber
Method: Simulate

278 Fuel
Author: Amber
Method: Find convex hull
State: Accepted
Detail:
See sgu_278_report.doc

285 What? Where? When?
Author: Amber
Method: Set-DP
Detail:
箭头转动:随机一个位子,若当前位子已取过,则转到下一个位子,直到箭头所指的位子未取过。
就是说,在已取过的位子形成的集合不同的情况下,每个位子取到的概率不同。所以这些概率都需要分别计算的。
所以采用集合型DP。
Complexity: O(6 * 6 * 2^13 * 6)
Notice:
题目中,对于如何箭头转动的描述,我一直搞错,拖了很久。

288 Best Tournament Schedule
Author: Amber
Method: Construct
Detail:
若N为奇数,那么增加一个人表示轮空的,使得N为偶数。
设第r轮X的对手为Xr。
若2*X = r (mod (N – 1)),则Xr = N
否则X + Xr = r (mod (N – 1)),(这里把编号0看成第N – 1个人)
命题1:同一轮中没有对手重复的,即若X <> X', 不存在Xr = X'r。
证:反设Xr = X'r,则 X + Xr = r, X' + X'r = r (mod (N – 1)) => X = X' 矛盾。
命题2:同一人中没有与重复对手比赛,即若r <> r', 不存在Xr = Xr'。
证:反设Xr = Xr',则 X + Xr = r, X + Xr' = r' (mod (N – 1)) => r = r' 矛盾。
综上构造成立。
Complexity: O(N^2)

289 Challenging Tic-Tac-Toe
Author: Amber
Method: Memorized Search
Detail: Search all the states by Max-Min Search

291 Evolution
Author: Amber
Method: BFS
Notice:
The test range in description may have some mistakes.
1 <= Q,C <= 1000, But I got WA.
When MaxSize = 1001, I got AC.

292 Field for the Cemetery
Author: Amber
Method: Maths + HP
Detail:
A = Q mod N
B = C mod N
情况1:如果A + B <= N就直接放,最后剩下一个A * B的空区域,Ans = (Q * C - A * B) / N
情况2:如果A + B > N 就先在外面交叠的放一圈,如:
AAAB
D B
D B
DCCC
把Q * C的问题转化为(Q – 2A) * (C – 2B)的问题。而(Q – 2A) mod N = N – A,(C – 2B) mod N = N – B。
因为A + B > N, 有N – A + N – B <= N,就是情况1。所以Ans = (Q * C - (N - A) * (N - B)) / N。
分析:
(N - A) * (N - B) = N ^ 2 - (A + B) * N + A * B --(1)
当A + B <= N时 (1)式 >= A * B
当A + B > N时 (1)式 < A * B
故这样分类摆放是最优的。

293 Game with Q an C
Author: Amber
Method: Construct
Detail:
由于我们要保证每次加入2个字符后,只能用2个交换维护,就是说只能有4处与上次回文串加2个字符不同。
什么样的子串有这个性质呢?
维护一种稳态: 就是以QCQC…QC这样交错开头, 多出的字符都放在中间, 正中间是含有奇数个字符的字符。
后一部分是前部分的翻转.
如: 5个C, 8个Q: QCQC|QQCQQ|CQCQ (第一竖线前为交错部分, 第一竖线后到正中间是多出的字符. 正中间是奇数个字符的C。
可以证明,最多只要2次交换维护就可从上一个稳态,到这个稳态。

算法就是每次构造稳态,找到那些地方的字符与上次的不同,交换即可。
Complexity: O(N^2)

295 Identifier Duplicated!
Author: Amber
Method: Maths (Combinations)
Detail: Ans = 2^[Duality Letter number]
* [Sum all space's number cases which will be inserted into the gaps between words.]

296 Sasha vs. Kate
Author: Amber
Method: Greedy
Detail:
For each K, delete the last digit of the non-increasing order from start point.

297 Fair-play
Author: Amber
Method: Maths
Detail: Mod Sum Directly

298 King Berl VI
Author: Amber
Method: Shrink strong connected block and Topological sort
Detail:
题目就是给出了m个不等式,可以抽象成有向图。
若图中有非0圈,则不等式中有矛盾,无解。
若图中有0圈,则圈中的点是等价的,可以收缩圈(强连通分支)。
这样就得到一个DAG。
在正向图中,拓扑排序时,可以更新到达每个节点的最小值。
同样在反向图中,求出到达每个节点的最大值。
若出现某个节点的最大值<最小值,则无解。
最后,由于minimize AN - A1,只要令A1为其最大值,AN为其最小值,再做一次拓扑排序的更新即可。
Complexity: O(N + M)

299 Triangle
Author: Amber
Method: Sort+HP

300 Train
Author: Amber
Method: Enumerate
Detail: Find the minimum circle length leaded by the intersection point.

301 Boring. Hot. Summer…
Author: Amber
Method: Dijkstra + Topological scan by distance order
Detail: 先求出X – Y的最短路图,最短路图是DAG。所以可以以离X的距离作为拓补序,进行统计。
Complexity: O(N^2)

302 BHTML 1.0
Author: Amber
Method: Stack Processing

304 Mars Stomatology
Author: Amber
Method: Greedy + DP
Detail:
If there is only 1 gum, then we can use greedy method
that after sorting the values of teeth, scan the values from small to large until exceed the money limit.
State (M, N) denotes minimum values of choosing M gum, N teeth.
F[M, N] = F[M - 1, N - K] + Cost[1..K]
Here Cost[1..K] means the summation of the first k sorted values from small to large.
K is the number of the teeth connecting to M.
Complexity: O(N^2M), but in fact it should be O(N^2).

305 Exhibition
Author: Amber
Method: Special Match
Complexity: O(N^3)
Detail:
We use hash to implement the large part Y. But the used node is O(N) at most.

 


ACM Sgu Vol 3 弱题总结

2008年4月6日星期日

 
300.给一条与坐标轴平行的铁轨,可能自交,问设计的火车最长是多少。
直接模拟,每找到一个相交就拿构成环的长度去更新答案
P[j]..........
| |
| |
P[i]---O-----P[i-1]
|
|
P[j-1]
302.无聊模拟题

304.容量为P(P<=106)的背包,N个物品,每个物品属于一个类别,如果在第i类里取了至少一个物品,则必须花费额外Bi的容量。问取到的物品数最多是多少。
DP:用f(i,j)表示只取前i类物品取了j个的情况 O(N^2)
f(i-1,j)-->f(i,j+k)
305.N(N<=100)个等差数列Ai,Bi (X=Ai+Bi*k k为自然数) 用1~M(M<=10^9)中的N个数给等差数列标号,使得拥有在自己序列中的标号的等差数列总数最大 虽然M很大,但是一个等差数列最多用到前N个数,待选标号的集合不超过N^2, 然后最大匹配 ~_~ 309.给N个整点(N<=20000)求最小的长度D使得三个边长为D的正方形能覆盖点集(允许Overlap). 首先要二分答案,然后 我一开始想肯定要覆盖最左/右/上/下的一条边,但是确定了一个左边。。另一个坐标无法确定?后来经过提示发现:放正方形的时候,最小覆盖当前点集的矩形的四个角其中一个一定要被正方形盖到。但是我想了好久也不知道究竟应该放哪个角。。后来发现枚举每次应该盖哪个角就行了
=.= 310.长度为N01串,每个长度为M的子串都有至少K1,问有多少种方案。
简单的状态压缩Dp...

311.有两种操作:1.运来价格为P(P<=10^6..整数!)的Ice-Cream K个(K<=10^6) 2.一个人用T(T<=10^12)的钱买最便宜的n个Ice-Cream,如果当前有>=n个Ice-Creams而且最便宜的n个价格不超过T。。就输出"HAPPY"否则输出"UNHAPPY"
关于价格的线段树,flag记当前区间是否被整体删除,sum cost分别表示在当前价格区间[a,b]内共有的冰激凌数量和总价值
316.无聊模拟题

317.一条数轴上的路,信使要从0移动到T,但是他必须骑马移动。路上有N(N<=5000)个马厩,每个马厩有若干匹马,每匹马有两个属性:速度和能跑的距离,问到达终点所用的最短时间。马的总数<=10^5 f[i]=f[j]+cost[j][i] 顺推,把从每个点出发的马按速度排序,每次递推找出当前速度最快的马,如果跑当前这个点路程不够就抛弃(后面的点更不行了@_@)
用堆维护即可 <i)>318. 有一些用户和一些资源,每个用户都有一些需要的资源,要求找出一种资源的分组方式,使得每个用户都能得到他想要的资源,并且不能得到任何一个他不想要的资源(每个用户可以享有多个资源组),并且资源分的组数最少。 
如果两个资源的需求情况(需要的人的集合)是一样的,这两个资源就是等价的,显然等价具有传递性。。DisjointSet..方法很多..

320. n*m的方格上写了0~9的数字,同数字的相邻格看作属于同一个势力。如果一个势力的格子数达到或超过k,则称其为一个大势力。如果一个格子属于大势力,或被某个大势力完全包围(切断了它到整个地图边缘的所有途径),则称其为危险的。问有多少危险的格子。
这题在集训队论文里有一个神奇的方法:将小势力块每个方格拆开看成大势力,从边界沿着不同大势力之间的隔离墙往里面灌水,如果每个格子四个角都能被灌到,那么它是能被访问到的。
注意这里的隔离墙不仅指边还指这种情况
AC
BA 两个A有顶点相连,这里我们把这个顶点看作隔离墙的门=.=...水是不能往门里流的,B或C边的水只能拐弯而不能穿过门

用朴素的方法+一定的优化 或者构图求割点也可以

321.简单的树贪心

322.给出两个生成树,每次可以在第一棵生成树上减去一条边同时加上一条边,要最少的操作次数变为第二棵生成树,在每次操作结束后都要求图依然是生成树。
每次找一条在第二棵不在第一棵的边,连上它,形成的环里面肯定有二不包含的边(否则还是生成树?),删去

323.有一张无向图,每条边有一个权值和一个颜色。修改某条边的颜色的费用是这条边的权值。求最小的花费使得产生某种颜色的生成树
就是Kruskal..先把所有边排序,然后枚举生成树需要的颜色,判断一下就行了,而且剪枝很方便

324.无聊模拟题 减号随便分配
325.to do...

326.给定了一些队已经比赛的成绩,以及他们之间剩余的比赛场次,问第一个队友没有取胜希望(胜利数达到所有队中最大)
先让第一个队把剩下的比赛赢掉,其他队把跟外部队的比赛全部输掉,然后内部分配比赛的“胜利果实”。显然每个队不能超过S1-Si..网络流分配一下这些比赛结果

327.给一些串,找到一个最短的包含他们全部的回文串。 不会做 :( 貌似是Dp :(
328.找规律的SG函数题
329.把一个边长是N(N<=5)的三角形划分成X2个小三角形,用4种图案给它染色,要求每条边两侧的颜色相同,给了4种图案的个数,问有多少种方案。
记忆化搜索+状态压缩..我的常数控制得不好..只能去const :(

330.给两个数AB,问每次给A加上A的一个大于1小于A的约数,能否得到B。并输出一个方案。
显然观察偶数的二进制数********0 每次加上 10...0(一共是原数右端连续的0的个数,前提是不要超界) 这样总可以得到比它大的任何偶数
把A B分别搞成两个偶数A' B' A<=A' B<=B' A'<=B',用上面的方法就行了 334.把一个9个格子组成的连通图形切成尽量少的几份,然后拼成一个3*3的正方形。
加深搜索,每层搜索一种“零件”的形状,同时枚举在原图形的位置(细节有小技巧,略)

336.不会做 :(

337.简单的枚举 O(N^2)

340.无聊模拟题 again..
342.高精度对小数取余 + 简单的Dp
F[i]表示当前位付钱(显然跟前面一位就没关系了。)
G[i]表示当前位找钱(前一位得多付一个来填这一位的洞)

344.BFS
346.不无聊的模拟题 @_@
347.排列N个串使得合成的串字典序最小
排序: 两个串Sa Sb,若Sa+Sb<sb+sa digit="" n=""><=14) 显然第一个数我们可以随便选(不妨为0),然后搜索 @_@ 秒杀 353.无聊模拟题 355.给1~N涂色,A如果是B的约数 A B不能同色..问最少需要多少种颜色 看sample一眼就看出来怎么做了吧... 357.一个电视遥控器,某些键坏掉了。。问最少需要多少部从A调到B。 按数字选节目只可能有一次。。枚举一下“换乘点” 358........ 359.@_@_@_@_@_@ 361.N*M的格子 2*3和3*2的矩形内都正好有两个旗子
给最小方案 枚举一行。。就出来了 362....... 365.简单的递推 366.60000个pair Ai,Bi(-50<=Ai,Bi<=50) 选出20个使得SA-SB最小,满足最小的情况下SA+SB最大 把Ai-Bi分类后Dp即可

抱歉!评论已关闭.