神奇的一场,两题可以出现在排名首页。
Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to ai. Vasya and Petya want to divide all pieces of pizza into two continuous sectors in such way that the difference between angles of these sectors is minimal. Sector angle is sum of angles of all pieces in it. Pay attention, that one of sectors can be empty.
The first line contains one integer n (1 ≤ n ≤ 360) — the number of pieces into which the delivered pizza was cut.
The second line contains n integers ai (1 ≤ ai ≤ 360) — the angles of the sectors into which the pizza was cut. The sum of all ai is 360.
Print one integer — the minimal difference between angles of sectors that will go to Vasya and Petya.
4 90 90 90 90
0
3 100 100 160
40
1 360
360
4 170 30 150 10
0
In first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.
In third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.
In fourth sample Vasya can take 1 and 4 pieces, then Petya will take 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0.
Picture explaning fourth sample:
Both red and green sectors consist of two adjacent pieces of pizza. So Vasya can take green sector, then Petya will take red sector.
A水题。
#include
#include
#include
#include
#include
While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.
In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).
The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Print one integer — the answer to the problem.
4 2 1 1 3 5 7
3
4 2 0 5 3 1 7
4
5 3 1 3 3 3 3 3
25
In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).
In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).
In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.
B题STL练习题
先把所有数字升序排序,之后枚举每对点对(i,j)中的 j 。对于一个固定的 j ,满足条件的 i 肯定在已经排序好的数组当中构成了一段连续的区间,且Ai<=Aj。设这段下标区间对应的元素范围为[l,r],则
l=(Ai/x-k)*x+1
r=l+k-1
用stl的lower_bound和upper_bound找到 l r 对应的下标位置即可。
需要特别注意k=0的情况,此时 l r是不满足上述公式的,需要特判。
#include
#include
#include
#include
#include
C题刘汝佳白书P160原题
分解质因子,构造只有0/1的异或方程组求解
详见 Hdu 5833 题解
#include
#include
#include
#include
#include