Timus 1010. Discrete Function 要求根据给定的离散函数在直角坐标系中的对应点的连线中找出倾角最大的。
Problem Source: Third Open USTU Collegiate Programming Contest (PhysTech Cup), March 18, 2000
1010. Discrete Function
Time Limit: 1.0 second
Memory Limit: 16 MB
There is a discrete function. It is specified for integer arguments from 1 to N (2 ≤ N
≤ 100000). Each value of the function is longint (signed long in C++).
You have to find such two points of the function for which all points
between them are below than straight line connecting them and
inclination of this straight line is the largest.
Input
There is an N in the first line. Than N lines follow with the values of the function for the arguments 1, 2, …, N respectively.
Output
A
pair of integers, which are abscissas of the desired points, should be
written into one line of output. The first number must be less then the
second one. If it is any ambiguity your program should write the pair
with the smallest first number.
Sample
input | output |
---|---|
3 2 6 4 |
1 2 |
解答如下:
1 using System;
2
3 namespace Skyiv.Ben.Timus
4 {
5 // http://acm.timus.ru/problem.aspx?space=1&num=1010
6 sealed class T1010
7 {
8 static void Main()
9 {
10 int i0 = 0, n = int.Parse(Console.ReadLine());
11 long r0 = 0, v1 = int.Parse(Console.ReadLine());
12 for (int i = 0; i < n - 1; i++)
13 {
14 long v2 = int.Parse(Console.ReadLine());
15 long r = Math.Abs(v1 - v2);
16 v1 = v2;
17 if (r0 < r)
18 {
19 r0 = r;
20 i0 = i;
21 }
22 }
23 Console.WriteLine("{0} {1}", i0 + 1, i0 + 2);
24 }
25 }
26 }
2
3 namespace Skyiv.Ben.Timus
4 {
5 // http://acm.timus.ru/problem.aspx?space=1&num=1010
6 sealed class T1010
7 {
8 static void Main()
9 {
10 int i0 = 0, n = int.Parse(Console.ReadLine());
11 long r0 = 0, v1 = int.Parse(Console.ReadLine());
12 for (int i = 0; i < n - 1; i++)
13 {
14 long v2 = int.Parse(Console.ReadLine());
15 long r = Math.Abs(v1 - v2);
16 v1 = v2;
17 if (r0 < r)
18 {
19 r0 = r;
20 i0 = i;
21 }
22 }
23 Console.WriteLine("{0} {1}", i0 + 1, i0 + 2);
24 }
25 }
26 }
这题实际上是很简单的,只要意识到所求倾角最大的连线的两个端点必定是相邻的点就容易了。
两点连线的倾角的正切值(tan)等于这两点的纵坐标之差除以横坐标之差。由于这两点是相邻的点,所以横坐标之差为 1,也就是说,直接等于纵坐标之差。
题目中说的:“Each value of the function is longint (signed long in C++).”,即:该离散函数的值域是 C++ 中的 signed long 数据类型,实际上相当于 C# 中的 int ( 即: System.Int32 ) 数据类型,取值范围是 -2,147,483,648 到 +2,147,483,647。所以可以用 int.Parse() 方法来读取输入。但是进行计算时必须使用 C# 的 long ( 即: System.Int64 ) 数据类型,相当于 C++ 中的 signed long long 数据类型,取值范围是 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807,不然计算时就有可能溢出。