/*----------------------------------------------*\ some specification here... \*----------------------------------------------*/ #include typedef struct { float profit; int buy; int sell; } Profit; typedef struct { float price; int date; } PriceDate; /*--------------------------------------------*\ function for finding min in an array \*--------------------------------------------*/ PriceDate Min(float *prices, int l, int r) { int i; PriceDate priceDate; priceDate.price = 200000; for (i = l; i <= r; i++) if (prices[i] < priceDate.price) { priceDate.price = prices[i]; priceDate.date = i; } return priceDate; } /*--------------------------------------------------*\ function for finding max in an array \*--------------------------------------------------*/ PriceDate Max(float *prices, int l, int r) { int i; PriceDate priceDate; priceDate.price = -1; for (i = l; i <= r; i++) if (prices[i] > priceDate.price) { priceDate.price = prices[i]; priceDate.date = i; } return priceDate; } /*-------------------------------------------------*\ \*-------------------------------------------------*/ Profit GetRich(float *prices, int l, int r) { Profit profit, profitl, profitr; PriceDate minl, maxr; int m; int n = r - l + 1; if (n < 2) { profit.profit = 0; profit.buy = 0; profit.sell = 0; } else if (n == 2) { if (prices[l] < prices[l+1]) { profit.profit = prices[l+1] - prices[l]; profit.buy = l; profit.sell = l+1; //printf("end === %d %d %d\n", l, *buy, *sell); } else { profit.profit = 0; profit.buy = 0; profit.sell = 0; } } else { m = round(n/2); profitl = GetRich(prices, l, l+m-1); profitr = GetRich(prices, l+m, r); if (profitl.profit < profitr.profit) { profitl = profitr; // buyl = buyr; // selll = sellr; } minl = Min(prices, l, l+m-1); /* buy = buy + l - 1;*/ maxr = Max(prices, l+m, r); /* sell = sell + l+m-1;*/ profit.profit = maxr.price - minl.price; profit.buy = minl.date; profit.sell = maxr.date; if (profit.profit < profitl.profit) { profit = profitl; //* buy = buyl; //* sell = selll; } } // printf("p=%.2f, b=%d, s=%d\n", profit, *buy, *sell); return profit; } /*----------------------------------------------*\ \*----------------------------------------------*/ int main(int argc, char *argv[]) { float prices[] = {0,600.25,600.79,615.95,637.65,653.82,638.25,646.73,653.2,631.68,649.25,657,685.33,685.19,691.48,702.53,700.74,710.84,700.73,696.69,689.69,677.37,673.35,669.23,689.96,694.05,699.35,699.2,718.42,714.87,715.26,698.51,684.16,681.53,693,697,692.26,673.57,666,676.7,660.52,648.54,625.85,633.63,629.65,641.68,660.55,632.07,663.97,693.84,732.94,741.79,725.65,711.25,703.21,707,694.77,679.23,674.6,668.51,675.82,675.77,650.75,644.71,639.62,633.48,616,620.11,637.39,622,625.39,615.18,609.62,594.05,579.03,584.02,584.39,582.55,567.27,567.5,568.16,569,568.02,560.1,552.83,546.85,535.27,525.3,528.75,524.78,522.65,521.33,514.48,519.35,523.52,527.8,525.15,515.25,511.4,512.88,506.4,513.26,515,512.19,512.75,506.61,497.92,500.04,491.52,497.55,508.6,515.5,515.75,514.73,525.78,516.02,510,503,511.01,512.94,510,516.11,511.89,508,509.76,514,512.51,520.12,548.59,549.5,555,552.99,552.16,545.33,544.47,543.34,542.56,539.4,541.63,534.34,530.38,522.7,525.01,526.29,530.26,527.42,524.98,514.11,509.97,514.31,515.2,505.89,502.84,505.24,504.77,511.34,515.49,515.06,518.25,518.84,507.07,500.4,497.91,498.6,487.11,483.52,474.33,473.97,475.86,470.6,470.32,470.96,472.61,458,461.78,466.74,461.47,469.25,466.81,467.27,471.12,473.23,465.78,469,471.38,479.01,481.18,477.99,477.53,479.08,482.48,471.65,476.01,472.8,474.27,466.29,467.39,464.53,466.5,468.21,471.51,471.02,472.6,458.53,458.16,460.92,461.88,463.62,465,461.83,462.04,456.55,445.28,447.23,440.85,446.19,448,443.03,454.75,452.96,454.72,455.64,457.55,440.95,438.68,448.23,449.45,448.77,464.93,470.62,475.85,475.86,472.1,469.94,461.47,465.93,459.1,458.29,461.89,471.03,470.01,471.48,467.16,481.5,481.75,501.5,494.32,492.47,495.84,488.09,499.07,479.05,480.84,489.75,487.83,497.28,504.28,505,499.72,489.46,485.5,483.58,487.19,483.26,467.59,460.48,462.56,468.03,457.53,455.58,456.2,462.9,468.63,462.8,480.3,482.12,478.99,481.78,483.93,484.11,482.64,488.71,487,484.85,480.8,484.81,484.65,489.5,484.75,505,508.01,509.65,495.05,498.79,495.9,491.93,489.3,481.03,473.55,472.63,475,472.57,476.95,471.8,469.91,467.5,476.39,476.57,475.2,485.1,486.6,473.31,480.78,459.67,426.06,419.31,420.64,421.75,427.3,427.44,426.5,426.65,429,420.5,411.81,415.7,404.04,401.44,401.9,403.58,402.92,406.87,403.98,403.78,406.85,397,403.81,414.69,409.88,403.98,406.57,391.9,384.09,377.85,378.49,380.14,384.36,378.6,378.53,380.75,378.95,380.95,373.26,373.73,373.43,378.29,377.3,383.36,385.8,387.72,380.97,369.43,368.5,374.2,376.94,381,377.95,373.85,375.39,367.23,375.51,386.6,388.12,382.4,385.5,389.36,390.9,390.11,387.12,399,403.05,407.89,403.5,408.83,417.25,424.56,418.2,420.45,423.19,421.46,423.2,419.33,417.81,406.11,402.32,404.22,404.86,399.95,402.13,387.17,388.14,390.7,391,384.39,386.52,381.54,386.57,393.3,386.51,389.99,374.44,379.44,382.62,371.82,371.94,381.35,382.99,381.25,375.58,370.95,370.02,370.99,374.5,371.3,376.2,374.13,387,402.98,408.8,394.78,394.3,394.75,394.17,394.8,398.9,417.94,420.03,425.97,427.16,440.5,437.1,415,410.5,404.24,406.82,402.16,408.95,409.66,416.38,406.16,411.18,407.99,404.34,389.7,390,388.44,394.98,377.2,369.69,365.8,341.89,340.22,339.92,348.19,339.79,338.77,344.5,351.16,337.06,337.5,343,353.88,364.45,368.1,378.18,376.45,364.8,362.62,390.38,377.4,378.07,365.49,366.59,368.75,366.46,342.38,343.32,345.7,362.61,358.77,369.08,367.92,385.1,381.55,396.04,401.78,432.66,426.82,433.49,434.27,433,443.03,427.5,399.46,436.45,444.91,467.11,466.25,463.63,471.63,469.76,466.9,465.66,451.24,445.24,435.23,414.86,420.15,426.69,424.64,430.93,432.04,426.33,429.74,424.6,430.15,422.55,418.96,417.49,412.61,409.2,410.65,404.22,404.54,405.85,417.7,414.09,404.91,403.54,423.48,428.62,422.86,416.47,409.36,400.21,403.45,398.15,392.8,396.97,390.4,391.1,379.15,389.9,395.03,390.43,385.95,379.68,379.38,372.14,358.17,353.06,355.44,346.91,348.65,339.9,303.2,308.7,303.28,305,296.14,297.44,300.97,306.1,310.65,312.99,312.75,310.71,311,318.68,316.46,309.62,306,313.94,314.28,315.36,311.37,311.9,307.91,303.79,300.2,302.62,303,311.68,309.74,299.09,295.39,294.87,287.11,288.45,286.25,286,287.27,288.45,283.58,282.59,282.57,279.58,274.01,280,279.99,285.1,285.65,284,289.72,284.05,285.68,291.57,291.25,292.35,297.73,297.3,299.19,291.61,287.76,293.5,296.93,296.09,295.85,302.4,313.94,312,309.9,299.54,301.19,300.89,298.86,291.78,293.35,296.23,295.54,291.52,295.71,291.25,294.15,292.72,302,304.1,297.25,289.71,289.3,287.84,286.7,280.3,277.44,274.8,278.35,282.75,282.5,286.31,279.56,293.12,290.94,280.26,287.9,288,277.27,266,259.2,260.81,256,255.45,241.61,239.18,239.16,233.13,231.05,229.24,228.72,231.29,227.8,226.02,228.02,226.98,228.5,226.19,222.29,220,219.45,219.78,218.75,223.53,215.81,204.22,198.1,191.4,186.97,185,191.45,192.93,193.96,193.23,192.05,193.76,189.22,188.57,185.29,180.04,180.51,180.45,179.57,181.42,179.25,178.98,178.6,180.88,180.04,179.29,175.6,178.61,174.99,177.8,179.98,181.35,185.2,188.81,185.9,187.01,185.18,186.06,187.99,185.87,188.89,193.95,191.37,197.95,197.9,198.41,195.23,192.99,187.4,187.98,191.58,198.64,196.03,204.36,210.86,205.96,191.9,195.62,190.34,188.08,189.24,177.12,180.72,188.28,193.92,197.3,203.9,199.97,195.33,195.38,193.54,195.06,193.85,188.55,193.51,194.5,202.71,192.79,197.6,192.9,192.76,191.91,187.9,186.3,183.75,185.02,180.08,176.47,179.78,178.69,170.45,171.65,173.43,169.98,171.43,176.29,180.4,179.4,179.96,181.98,181.05,179.39,174.76,167.52,165.1,169.4,167.54,172.5,172.54,184.87,182,183.02,167.86,168.7,172.55,169.35,184.7,191.67,194.87,196.03,190.64,193.3,185.97,181.8,187.4,172.43,149.38,140.49,147.94,149.16,144.11,142,140.9,137.4,135.26,137.73,138.85,137.08,138.37,135.06,132.58,129.6,131.08,126.86,118.26,119.83,120.82,118.38,117.84,119.36,117.49,113.97,112,111.49,107.5,105.33,102.31,102.3,101.58,100.01,101.51,100.25,102.37,102.01,106.15,107.91,106,104.87,109.4,108.31,100.34}; // int buy, sell; // Prifits profits; // GetPrices(filename, prices); // int n = 861;/* actual numer is 861 int l = atoi (argv[1]); int r = atoi (argv[2]); Profit profit = GetRich(prices, l, r); printf("buy at %d, sell at %d, make %.3f dollars\n", profit.buy, profit.sell, profit.profit); }