1. 程式人生 > >問題 L: An Invisible Hand - (2018年第二階段個人訓練賽第三場)

問題 L: An Invisible Hand - (2018年第二階段個人訓練賽第三場)

names pre put 第一個 class fin latin ash ide

題目描述

There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples.
Takahashi will begin the travel at town 1, with no apple in his possession. The actions that can be performed during the travel are as follows:
Move: When at town i (i<N), move to town i+1.
Merchandise: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for Ai yen (the currency of Japan) at town i (1≤i≤N), where Ai are distinct integers. Also, you can assume that he has an infinite supply of money.
For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T. (Note that a single apple can be counted in both.)
During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.
Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi‘s travel, Aoki can change Ai into another arbitrary non-negative integer Ai‘ for any town i, any number of times. The cost of performing this operation is |Ai?Ai‘|. After performing this operation, different towns may have equal values of Ai.
Aoki‘s objective is to decrease Takahashi‘s expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi‘s expected profit is initially at least 1 yen.

Constraints
1≤N≤105
1≤Ai≤109 (1≤i≤N)
Ai are distinct.
2≤T≤109
In the initial state, Takahashi‘s expected profit is at least 1 yen.

輸入

The input is given from Standard Input in the following format:
N T
A1 A2 … AN

輸出

Print the minimum total cost to decrease Takahashi‘s expected profit by at least 1 yen.

樣例輸入

3 2
100 50 200

樣例輸出

1

提示

In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:
1.Move from town 1 to town 2.
2.Buy one apple for 50 yen at town 2.
3.Move from town 2 to town 3.
4.Sell one apple for 200 yen at town 3.
If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1, thus the answer is 1.
There are other ways to decrease Takahashi‘s expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.

em題目的意思就是說一個商人可以從一個地方買蘋果,然後再下不知道幾個地方賣出去,每個地方都有個蘋果的價值(且不相等),他想取得最大的利潤,(畢竟商人)。而另一個競爭對手想要阻止他,哪怕只令他少賺一塊錢,他可以任意修改地方蘋果售價,但是要付出相等的代價。求最小的代價。

那我們只需要求出第一個商人最大價值出現了幾次(因為地方售價不相等,所以可以不會出現改一個地方售價影響兩個最大價值的情況),然後改動他賣出或者出售地方售價就ok,畢竟求最小那麽我們就只改動1就好 ,那麽最小代價就變成了,最大利潤出現的次數。

暴力跑肯定超時的,那麽就在輸入的時候算出來每個地方的利潤,順便記錄最大值即可。

技術分享圖片
 1
#include<iostream> 2 #include<math.h> 3 #include<cstdio> 4 5 using namespace std; 6 7 int dp[100005]; 8 int main() 9 { 10 int n,t; 11 scanf("%d%d",&n,&t); 12 int minn = 0x3f3f3f3f; 13 int maxn = 0; 14 for(int i=0;i<n;i++) 15 { 16 int a; 17 scanf("%d",&a); 18 dp[i] = a - minn>=0?a - minn:0; 19 minn = min(a,minn); 20 maxn = max(dp[i],maxn); 21 } 22 int ans = 0; 23 for(int i = 0;i<n;i++) 24 { 25 if(maxn == dp[i])ans++; 26 } 27 printf("%d\n",ans); 28 }
View Code

問題 L: An Invisible Hand - (2018年第二階段個人訓練賽第三場)