Let's assume that there is a simple market for beans. Every day there is a published bean price in the market. Traders can buy or sell at the published price. There is a trader who time travelled to future and brought back the price information for a number of days in the future. If you have this information and you are allowed to buy and sell many times. How do you make the maximum profit? The price information will be given as an array of numbers. Each number is for a day’s trading price. The numbers are all integers to simplify the problem. You will need to return the index of the buy-in point and sell-out point for maximum profit.

Rules:

1) The input line length less than 1000, and the trading price length less than 100;

2) The trading price is positive integer;

3) The trading prices are delimited  by ' '(single space);

4) Please make sure every buying and selling period shortest. especially, please ouput '-' if all the trading prices are the same or no trading point;

Sample Input and Output:

Input 1

1 3 5 4 2 8 10

Output 1

1 3 5 7

To make the maximum profit, you should buy at $1 and sell at $5, and then buy at $5 and sell it at $10. so the output is "1 3 5 7".

Input 2

1 1 1 3 5 4 2 2 2 8 10

Ouput 2

3 5 9 11

/////////////////////////////
//C Sample
////////////////////////////
#include <stdio.h>
#include <string.h> void calculateAndPrint(int array[], int length){
//Your Code is here int low = ,//最低购入点
hight = ,//最高出售点
profit= ;//是否有交易
while(low + < length)
{
//寻找最小购入点
while(low + < length&&array[low] >= array[low + ])
++low ;
//出售不能早于购入(由上循环可以,如果下一点不为空的话则会比low处值大因此可以作为hight起点)
hight= low + 1;
//等待最大出售点
while(hight + < length&&array[hight] < array[hight + ])
++hight;
//判断是否有交易
if(low < hight&&hight + <= length){
printf("%d %d ",low + ,hight + );
//重新寻求最大增区间
low = hight + ;
//至此,则有交易进行
profit = ;
}
}
if(!profit)
//Code Over
printf("-");
} int splitAndConvert(char* strings,int array[]){
char*tokenPtr = strtok(strings," ");
int i=;
while(tokenPtr!=NULL){
array[i] = atoi(tokenPtr);
i++;
tokenPtr=strtok(NULL," ");
}
return i;
} int main(){
char line[] = {} ;
while(gets(line)){
int array[] = {};
int length = splitAndConvert(line,array);
if(length==){
break;
}
calculateAndPrint(array, length);
printf("\n");
}
return ;
}

这道题,解题的思路就是寻找单调增区间:这样才能使收益最大。

如:

Input 1

1 3 5 4 2 8 10

Output 1

1 3 5 7

则,在第一个1处(1号)购入,在第一个5处(3号)出售,收益最大;然后在第一个2处(5号)购入,在第一个10处(7号)出售,收益最大。

Amazon Hiring Campus 2013 - Final 6的更多相关文章

  1. [2013 Final] Colors

    Description Ziyao has a big drawing board with N * M squares. At the beginning, all the squares on t ...

  2. LOJ#2764. 「JOI 2013 Final」JOIOI 塔

    题目地址 https://loj.ac/problem/2764 题解 真的想不到二分...不看tag的话... 考虑二分答案转化为判定问题,那么问题就变成了能不能组合出x个JOI/IOI,考虑贪心判 ...

  3. [转]基于AWS的自动化部署实践

    作者 徐桂林 发布于 2014年1月22日 -------------------------------------------------------------------- 1. 背景 在过去 ...

  4. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

  5. 高德全链路压测平台TestPG的架构与实践

    导读 2018年十一当天,高德DAU突破一个亿,不断增长的日活带来喜悦的同时,也给支撑高德业务的技术人带来了挑战.如何保障系统的稳定性,如何保证系统能持续的为用户提供可靠的服务?是所有高德技术人面临的 ...

  6. 「题解」:[loj2763][JOI2013]现代豪宅

    问题 A: 现代豪宅 时间限制: 1 Sec  内存限制: 256 MB 题面 题目描述 (题目译自 $JOI 2013 Final T3$「現代的な屋敷」) 你在某个很大的豪宅里迷路了.这个豪宅由东 ...

  7. 「CSP-S模拟赛」2019第三场

    目录 T1 「POI2007」山峰和山谷 Ridges and Valleys 题目 考场思路(几近正解) 正解 T2 「JOI 2013 Final」 现代豪宅 题目 考场思路(正解) T3 「SC ...

  8. Amazon Kindle Device is hiring in Beijing Shanghai and Shenzhen!

    This is Angela from recruitment team of Amazon Kindle Device Software & Applications, we are exp ...

  9. Google 2013 campus test-R1

    Reading Phone Number #include<iostream> #include<fstream> #include<vector> #includ ...

随机推荐

  1. SpringMVC入门一:helloWorld

    玩了一下SpringMVC, 感觉挺清爽的 好像没有struts那么臃肿( 可能是高级的东西我还不会用 哈 ) 例子中一共有俩方法: 一个Controller直接返回字串的方法, 另一个通过Dao层返 ...

  2. 我用过的Linux命令--修改主机名

    下面的所有设置都是在CentOS中实验的,其他的Linux系统的玩法是一样的,只是配置文件的目录可能不一样. 局域网中的主机名类似于广域网中的域名,修改主机名有两种方式: (1)在当前会话中修改主机名 ...

  3. Linux 网络编程: daytime Service

    前言 如果你这段时间过得很舒服,那就证明你荒废以一段时间.如果你这段时间过得很辛苦,那么恭喜,你又进步了.最近入党的事情忙得焦头烂额,博客也拖了好久没写,主要也是因为要装 xinetd 服务一直没装好 ...

  4. PHP_Yii框架_专辑<一>

    一.PHP主流框架 cakephp—速度比较慢.CI(codeIgniter)—小型.symfony. TP(thinkphp)—国人开发.小型.zendframework(官方)—大型 Yii: 特 ...

  5. Android 开发笔记“浅谈DDMS视图”

    DDMS 的全称是Dalvik Debug Monitor Service,即Dalvik调试监控服务,是一个可视化的调试监控工具.它主要是对系统运行后台日志的监控,还有系统线程,模拟器状态的监控.此 ...

  6. Oracle系统视图

    VIEW TABLES 1.DBA_TABLES --查看系统里所有表的信息,只有DBA权限用户才可查看 SELECT * FROM DBA_TABLES WHERE OWNER='HR' AND T ...

  7. MVC-03 控制器(2)

    五.ActionResult解说 ActionResult的继承图: 类型 Controller辅助方法 用途帮助 ContentResult Content 回传一个用户自定义的文字属性 Empty ...

  8. 将默认首页设置成index.do的方法

    变态欺骗法,今天csdn一个前辈的,学习了,公司服务器是weblogic的,也可以欺骗. 但是我又非常迫切.非常盼望.非常渴望使用index.do做首页,怎么办? Tomcat中用一段注释: When ...

  9. HDU 3756 Dome of Circus

    不会做,参见别人的程序: /* 底面为xy平面和轴为z轴的圆锥,给定一些点,使得圆锥覆盖所有点并且体积最小 点都可以投射到xz平面,问题转换为确定一条直线(交x,z与正半轴)使得与x的截距r 和与z轴 ...

  10. ORACLE中date类型字段的处理

    (1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...