Problem Description
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.
Input
The input is given from Standard Input in the following format:
N T
A1 A2 … ANOutput
Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.
Example
Sample Input 1
3 2
100 50 200Sample Output 1
1
In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:Move from town 1 to town 2.
Buy one apple for 50 yen at town 2.
Move from town 2 to town 3.
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.
Sample Input 2
5 8
50 30 40 10 20Sample Output 2
2
Sample Input 3
10 100
7 10 4 5 9 3 6 8 2 1Sample Output 3
2
题意:n 个城镇在一条线上,有一个人要从 1 号点到 n 号点,其可以在这 n 个点上买卖苹果,初始时在 1 号点有无限的资金但没有苹果,他每次可以在当前城镇用 Ai 元买卖苹果,并移动到下一个城镇,但购买与出售的苹果数量和不能超过 T,其买卖苹果的目的是通过出售苹果来获取最大的利润而不在乎旅行结束时的苹果数量
现在另一个人要使旅行的人获取的利润最低,在其旅行前,其可以将另一个任意的城镇的苹果价格 Ai 改为一个非负整数 Ai',执行该操作的成本是 |Ai-Ai'|,其目标是将旅行的人的利润减少至少 1 元,问这个人要花费的最低总成本
思路:
第一个人要在买卖 T 个苹果之内获得最大利润,那么一定是要从两个城镇的最大价格差之间买卖,注意到只能从前向后走无法回退,那么扫一遍记录一下这些城镇中最大的贸易差
第二个人要使得第一个人获取的利润减少,那么对于这些城镇中最大的贸易差其有两个选择,要么降低起点的价格,要么提高终点的价格,由于要使其花费的成本最小,那么每次改变花费 1 元即可
考虑到这些城镇中最大贸易差存在多个的情况,只改变一组城镇的最大贸易差的价格无法保证第一个人一定从这组改变的城镇买卖,因此对于多个最大贸易差相等的城市对都要进行改变,因此还要再次扫描一遍,统计这些城镇中最大贸易差的个数,也即答案
注意到 n 最大为 1E5,从前向后扫描的时间复杂度为 O(n*n),扫描两遍会超时,因此需要从后向前扫描,每次记录最大的价格与最大贸易差,时间复杂度可以降到 O(n)
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int a[N];
int main() {
int n,t;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int maxx=-INF,temp=-INF;
for(int i=n;i>=1;i--){
temp=max(temp,a[i]);//最大的价格
maxx=max(maxx,temp-a[i]);//最大的贸易差
}
temp=-INF;
int res=0;
for(int i=n;i>=1;i--){
temp=max(temp,a[i]);
if(maxx==temp-a[i])
res++;
}
printf("%d\n",res);
return 0;
}