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 ...
随机推荐
- 重拾CSS基础—开篇
由来 从事软件行业一晃也5年有余,之间参与过若干个基于web的项目,所以javascript和Html基本已经相当熟悉,最近对于web前端开发比较关注,分析后发现CSS的确是自己的弱项,于是决定再加强 ...
- CF R303 div2 C. Woodcutters
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- svn在linux下的使用(svn命令行)ubuntu 删除 新增 添加 提交 状态查询 恢复
合并步骤:(1)先切换到分支:(2)svn merge trunk . (3)svn sw trunk (4)svn merge --reintegrate branch . svn merge ht ...
- 从头開始写项目Makefile(七):统一目标输出文件夹
[版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com] 上一节我们把规则单独提取出来,方便了Makefile的 ...
- cocos2d-x 3.0 新特性样例
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzMyMTMyOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- android UI进阶之弹窗的使用(2)--实现通讯录的弹窗效果
相信大家都体验过android通讯录中的弹窗效果.如图所示: android中提供了QuickContactBadge来实现这一效果.这里简单演示下. 首先创建布局文件: <?xml versi ...
- SSIS Package 配置多数据库连接语句
- .NET连接SAP系统专题:.NET调用RFC几种方式(一)
本来今天是要写一篇关于NCO3.0的东西,就是关乎.NET调用SAP的RFC的,支持VS2010和.NET 4.0等.现在网上到处都是充斥着NCO1.X和NCO2.0,需要用VS2003来使用,都是一 ...
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
- Win32 SecuritySetting
http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...