程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> URAL 1010 Discrete Function(解題報告)

URAL 1010 Discrete Function(解題報告)

編輯:C++入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved