您的位置:首页 > 其它

一天一道算法题---5.26---思维锻炼

2014-05-27 00:52 190 查看
感谢 微信平台: 一天一道算法题 ----- 大家没事都可以去关注他 --- 不是做广告的----

题目大意: 给你一个长度为n的整数序列A1,A2,……,An,找出两个整数Ai和Aj(i<j),使得Ai-Aj尽量大

反正 暴力肯定超时..... 数据大小 我也就给出了 反正 尽量用最好算法去解 就是了

嗯 它给的是o(n)的时间复杂度和空间度 但是很容易转换成o(1)空间度 o(n)时间复杂度的写法

这边给出个 与这题意相同的一个 题目 发现竟然是我以前WA的 怪不得 那么熟悉 。。。

戳我

好吧 无限WA中 不管了 先把错误代码贴上来 郁闷……

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int main()
{
int t, n;
long long temp, l, r;
long long x, y;
long long num;
long long mmax, result;
scanf("%d", &t);//T组数据
while (t--)
{
scanf("%d", &n);//n个元素大小的数组
scanf("%lld", &x);
mmax = x;
l = temp = 0;//左边元素位置  temp起到 暂时存储的作用 如果没有小的元素出现 就不需要改变 L 的值
r = 1;//右边元素位置
for (int i = 1; i<n; i++)
{
scanf("%lld", &num);//其余的n-1个元素
if (i == 1 || mmax - num > result)
{
result = mmax - num;
l = temp;
r = i;
}
if (num > mmax)
{
temp = i;
mmax = num;
}
}
printf("%lld %lld %lld\n", result, l + 1, r + 1); //分别+1 是因为求的是 元素位置
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: