1010. Discrete Function
Time Limit: 1.0 second
Memory Limit: 16 MB
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 |
水题,(可是由于数据类型的失误,竟然没有A过,还想着用库函数来着,,可是都没有64位的库,在C++中,各种long都是32位的。。)斜率最大的2点必定是相邻的2点。
#include <iostream>
#include <cmath>
using namespace std;
__int64 a[100000];
int main()
{
int n, i, num[2];
__int64 slope, maxSlope = 0;
cin>>n;
for (i=0; i<n; i++)
cin>>a[i];
for (i=1; i<n; i++)
{
slope = a[i] - a[i-1];
if (slope < 0)
slope = -slope;
if (slope > maxSlope)
{
maxSlope = slope;
num[0] = i-1;
num[1] = i;
}
}
cout<<num[0]+1<<" "<<num[1]+1<<endl;
}