Amazon Hiring Campus 2013 - Final 6
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的更多相关文章
- [2013 Final] Colors
Description Ziyao has a big drawing board with N * M squares. At the beginning, all the squares on t ...
- LOJ#2764. 「JOI 2013 Final」JOIOI 塔
题目地址 https://loj.ac/problem/2764 题解 真的想不到二分...不看tag的话... 考虑二分答案转化为判定问题,那么问题就变成了能不能组合出x个JOI/IOI,考虑贪心判 ...
- [转]基于AWS的自动化部署实践
作者 徐桂林 发布于 2014年1月22日 -------------------------------------------------------------------- 1. 背景 在过去 ...
- CODECHEF Oct. Challenge 2014 Children Trips
@(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...
- 高德全链路压测平台TestPG的架构与实践
导读 2018年十一当天,高德DAU突破一个亿,不断增长的日活带来喜悦的同时,也给支撑高德业务的技术人带来了挑战.如何保障系统的稳定性,如何保证系统能持续的为用户提供可靠的服务?是所有高德技术人面临的 ...
- 「题解」:[loj2763][JOI2013]现代豪宅
问题 A: 现代豪宅 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 (题目译自 $JOI 2013 Final T3$「現代的な屋敷」) 你在某个很大的豪宅里迷路了.这个豪宅由东 ...
- 「CSP-S模拟赛」2019第三场
目录 T1 「POI2007」山峰和山谷 Ridges and Valleys 题目 考场思路(几近正解) 正解 T2 「JOI 2013 Final」 现代豪宅 题目 考场思路(正解) T3 「SC ...
- 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 ...
- Google 2013 campus test-R1
Reading Phone Number #include<iostream> #include<fstream> #include<vector> #includ ...
随机推荐
- Android模拟器PANIC: Could not open:问题解决方法
最主要的就是环境变量没有配置或者我们使用的是绝对路径配置环境变量.这时我们只需要修改一下Android的环境变量就可以了. 具体解决方法如下: ①在环境变量中创建变量名:ANDROID_SDK_HOM ...
- eclipse修改编译路径
右击项目--properties--java build path--点击source,修改最下方的路径即可
- BZOJ 2818 GCD(欧拉函数)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37161 题意:gcd(x, y) = 质数, 1 <= x, ...
- Problem C Andy's First Dictionary(set的使用)
题目链接:Problem C 题意:输入一个文本,找出所有不同的单词,按照字典序从小到大输出,单词不区分大小写. 思路:将字母序列都存为小写,非字母的字符变成空格,然后利用stringstream实现 ...
- strstr 的使用
Problem E: Automatic Editing Source file: autoedit.{c, cpp, java, pas} Input file: autoedit.in Outpu ...
- JavaScript 验证提交文件的信息
前言 目前工作任务终于告一段落了,今天发现之前写的文件上传的代码有点小瑕疵,就是上传图片如果超过 2M 就会出错,因为七牛云好像限制了上传图片的大小,所以就用 JavaScript 在文件选中之后,上 ...
- 网上下载的“上下3D”和“左右3D”影片该如何播放?
我们平常买的红蓝3D眼镜智能播放红蓝3D片源.网上找3D电影的时候,虽试图去找红蓝3D格式电影,但总会找到不少“左右格式”或者"上下格式"影片.正常播放后发现有两重画面.这种3D电 ...
- QT 获取文件MD5值
/* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...
- addChildViewController ipad 中Controller的嵌套和叠加
1.addChildViewController 在 base controller中添加子的controller,会自动调用子的controller中viewDidload,viewWillAppe ...
- BZOJ 2463 谁能赢呢? (博弈论)
题解:简单博弈论 #include <cstdio> int main(){ int n; while(scanf("%d",&n),n!=0) if (n&a ...