find the max
Time Limit: 1000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 613 Accepted Submission(s): 286
Problem Description
有一个离散函数f(x),x = {1, 2, ,,,,N},f(x)<2^31。现在要找出2个点i,j, 使得函数在这2点之间的点都在这2点连线下方,且此连线的斜率的绝对值越大越好。
Input
输入包括多个测试实例。每个测试实例包括2行,第一行为一个整数N,2 <= N <= 100000, 然后是N个整数f(x),x=1,2...N,读到文件结束符为止.
Output
对于每个测试实例输出找到的i和j,如果有多个答案,输出字典序最小的一个。
Sample Input
3 1 2 3 3 2 6 4
Sample Output
1 2 1 2
相邻两个点之间的斜率的绝对值 最大即可。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1594
小乐一下:
当我们分折不出问题时,至少可以猜一下也是好的。
代码:
#include<cstdio>
#include<cstring>
int s[100010];
struct Node{
int num;
int i,j;
}node[100010];
int abs(int a){return a<0?-a:a;}
int main(){
int n;
int cnt;
int maxs,temp;
int i;
while(scanf("%d",&n)!=EOF){
for(i = 1;i<=n;i++){
scanf("%d",&s[i]);
}
cnt = 0;
maxs = -1;
for(i = 2;i<=n;i++){
node[cnt].num = abs(s[i]-s[i-1]);
node[cnt].i = i-1;
node[cnt].j = i;
if(node[cnt].num > maxs) {temp = cnt;maxs = node[cnt].num;}
cnt++;
}
printf("%d %d\n",node[temp].i,node[temp].j);
}
return 0;
}
伟大的梦想成就伟大的人,从细节做好,从点点滴滴做好,从认真做好。