笨蛋难题四

来源:互联网 发布:unity3d demo合集 编辑:程序博客网 时间:2024/03/29 22:10

笨蛋难题四

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价,为了防止别人发现他的秘密。他决定对于这家公司的 股票最多买一次,现在笨蛋已经将股票价格列了出来。(这已经不是笨蛋的难题了,他已经解决 呵 呵)。只想难为难为你呀,从股票价格表上,你能算出笨蛋的每股股票最多能赚多少钱吗?
                 

输入
第一行一个n,表示n天(小于100000)
第二行 给出n天每股的价格
输出
每股最多赚多少钱
样例输入
4947 267 359 7717669 735 322 794 397 565 181  
样例输出
504472
来源

峰巅


思路:


   一开始以为是用什么结构体之类的东西去做的,结果再看一次题目果断是贪心算法。要从前到后两个数最大的差。


AC代码:


 #include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>using namespace std;#define T 100005#define inf 0x3f3f3f3f//struct node//{//int val,num;//}a[T];//bool cmp(node& a,node& b)//{//return a.val<b.val;//}int main(){    /*freopen("input.txt","r",stdin);*/int n,m,i,j,k;while(~scanf("%d",&n)){scanf("%d",&j);for(i=1,k=0;i<n;++i){scanf("%d",&m);if(j-m<0){k=max(k,m-j);}else{j = m;}}/*scanf("%d",&a[i].val),a[i].num=i;*//*sort(a,a+n,cmp);*/printf("%d\n",k);}    return 0;}        

优秀代码:


 #include <iostream>#include <cstring>#include <cstdio>#include <string>#include <algorithm>#include <vector>#include <map>using namespace std;const int N = 1e5 + 10;typedef long long LL;/*使用方法:int a;a = Scan(); */int Scan(){    int res = 0 , ch ;    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )    {        if( ch == EOF )  return 1 << 30 ;    }    res = ch - '0' ;    while( ( ch = getchar() ) >= '0' && ch <= '9' )        res = res * 10 + ( ch - '0' ) ;    return res ;}int main(){    int i,j,n;    while(~scanf("%d",&n))    {        int cnt = 1<<30 , ans = 0;        for(i = 0;i < n;i++)        {            j = Scan();            cnt = cnt > j ? j:cnt;            ans = ans > j-cnt ? ans: j-cnt;        }        printf("%d\n",ans);    }}        


0 0
原创粉丝点击