OUC_Summer Training_ DIV2_#2之解题策略 715
这是第一天的CF,是的,我拖到了现在。恩忽视掉这个细节,其实这一篇只有一道题,因为这次一共做了3道题,只对了一道就是这一道,还有一道理解了的就是第一篇博客丑数那道,还有一道因为英语实在太拙计理解错了题意,然后就没看了,现在更不想看了。再次感觉自己做题就是靠脑子,找规律。不像其他大牛一样一说解题思路,就是一套一套的算法。啊有时候听到头都晕了。。。好了言归正传吧。
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 |
题意是:这儿有一个离散函数。它自变量的定义域为从1-N的整数(2〈=N〈=100000)。(每个给出的值一一对应1 to N的自变量) 每个函数值的类型均为LONGINT(C++中为signed long). 你需要去寻找函数图象上的2点,使其满足下列条件: 所有处于这2点之间的函数点是在连接这2点的直线的下方的,并且这条直线的倾角最大。输入第一行为一个数N。 然后下面N行是分别为对自变量取1-N时函数的值。输出仅一行,一对整数,即题目所求的2个点的横坐标,(也就是哪两个点,注意点的编号是从1开始的)并且第1个数必须小于第2个数。 如果有多解则只需输出第1个数最小的那对数。
分析:必然倾角最大的线段两端点相邻——如果有更好的点,为什么不靠的近些呢 所以 只要遍历一遍n-1个点就行了。
#include<stdio.h> int main() { long long int a[100010]; long long int i,n,max = -1,m,t; scanf("%lld",&n); for(i = 0;i < n;i++) { scanf("%lld",&a[i]); } for(i = 0;i < n-1;i++) { m = a[i] - a[i+1]; if(m < 0)m = -m; if(max < m) { max = m; t = i; } } printf("%lld %lld\n",t+1,t+2); }