http://acm.timus.ru/problem.aspx?space=1&num=1010
1010. Discrete Function
Time limit: 1.0 second
Memory limit: 64 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
Problem Source: Third Open USTU Collegiate Programming Contest (PhysTech Cup), March 18, 2000
Tags: none (hide tags for unsolved problems)
Difficulty: 245 Printable version Submit solution Discussion (62)
All submissions (21340) All accepted submissions (5687) Solutions rating (4180)
解析:只计算相邻的斜率即可;
注意绝对值
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=1000000+10;
double y[maxn];
int main()
{
int n;
int i,j;
double max,k;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
cin>>y[i];
int x1;
max=-1;
for(i=2;i<=n;i++)
{
k=y[i]-y[i-1];
if(max<fabs(k))
{x1=i-1;
max=fabs(k);
}
}
printf("%d %d\n",x1,x1+1);
}
return 0;
}

本文探讨如何在离散函数中找到两个点,使得所有位于这两点之间的点都位于连接这两点的直线下方,并且该直线的斜率最大。通过计算相邻点的斜率并寻找最大的绝对值斜率来确定所需点。
922

被折叠的 条评论
为什么被折叠?



