题目:
Southeastern European Regional Programming ContestBucharest, RomaniaOctober 19, 2002
Problem B
Beer Land
Input File: B.DAT
Program Source File: B.PAS or B.C or B.CPP or B.JAVA
King ACM 2002 received some clear ground and some money through inheritance. Terms of the will and testament require the heir to build n towns. He was also required to build the least quantity of two-way roads between the towns such that one could travel from any town to any other town even if one road was closed under repair. Carefully, all such roads must be constructed as geometrically straight lines. Further, the condition that a traveller be able to get from one town to another (even with one road closed) needs to be satisfied by traversing the roads as straight lines in their entirety --- not stopping partway along one road to switch to another.
Because of rich soil in those lands, the future inhabitants of the place have decided to produce beer. On the crossroads (a crossroad is merey the intersection of two or more roads) the king plans to build beer stands (one stand per a crossroad). So, if three or four (or twenty) roads intersect in a single point, there’s still only one appropriate place for a beer stand. The King must choose locations for the towns and the roads. You need to advise him so that he builds as many beer stands as possible.
Input: The first line of input contains the number of test cases. For each case there is a line containing a single integer n (1<=n<=32767). This represents the number of towns to be built.
Output: For each n, print a line containing the maximum number of beer stands possible to build.
Sample Input
3
3
4
5
Sample Output
0
1
5
解答
2003-09-09.22:14:37
其实只要找到了规律,编程并不困难。
首先最少需要k个边使得k个点连通且保证切断任意一条边后仍然连通。连接方法只有一种情况:一个环。
奇数个点时,每条边最多与k-3个边交叉,所以交叉点最多是k*(k-3)/2
偶数个点时,有2条边的交点数量比其他的多1,此时交点数量最大。是:( (k-3)*2+(k-4)(k-2) )/2
另外,要注意,3点之内输出为0,否则按上述公式得到负数:)
注:由于模拟比赛中运行环境的问题,输出必须使用cout才有效
代码如下:
Code: |
#include "stdio.h" #include "iostream.h" int main(int argc, char* argv[]) { int i,n,k; scanf("%d",&n); for(i=0;i scanf("%d",&k); if(k<3)cout<<0< { cout< else cout<<(k-3)+(k-4)*(k-2)/2< return 0; } |
kensta
信箱 主页 时间:2003-09-09.22:16:03
测试数据如下:
15
1
2
3
4
5
6
7
8
9
10
100
1000
10000
32766
32767
标准输出如下:
0
0
0
1
5
7
14
17
27
31
4801
498001
49980001
536739847
536788994
( 网页浏览 )
文章来源:http://acm.tongji.edu.cn/people/kaikai/blog/blog.php?job=art&articleid=a_20041119_180921