当前位置:编程学习 > C/C++ >>

URAL 1010 Discrete Function(解题报告)

Description
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
input  output
3
2
6
4
1 2

这个题看了老半天才懂。这么解释吧,设离散函数为f(n),根据输入数据则是,f(1) = 2;
f(2) = 6; f(3) = 4.现在有三个点 了对吧,现在就是要从这三个点中找出两个点,使在这两个点之间的点不能再这条直线的上方,并且倾角(注意:不是斜率,是倾角)要最大。显然,如果有点在一条直线下的话那么这条直线倾角绝对不是最大的,所以满足题意的答案必然是两个相邻的点,题目就转化为了求相邻点距离的绝对值了。
[cpp]
#include<iostream> 
#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
using namespace std; 
int main() 

    long long int a[100001]; 
    int n,t,i; 
    scanf("%d",&n); 
    for(i=1;i<=n;i++) 
    { 
        scanf("%lld",&a[i]); 
    } 
    long long max=-1; 
    for(i=2;i<=n;i++) 
    { 
        if(llabs(a[i]-a[i-1])>max) 
        { 
            max=llabs(a[i]-a[i-1]); 
            t=i; 
        } 
    } 
    printf("%d %d\n",t-1,t); 
    return 0; 


作者:CSUST_ACM
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,