AtCoder Regular Contest 063---D - 高橋君と見えざる手 / An Invisible Hand(题目中“数组不重复”,“答案>=1”都是提示信息)

题目链接:

         https://atcoder.jp/contests/arc063/tasks/arc063_b

题目描述:

                         D - 高橋君と見えざる手 / An Invisible Hand

Problem Statement

There are NN towns located in a line, conveniently numbered 11 through NN. Takahashi the merchant is

going on a travel from town 11 to town NN, buying and selling apples.

Takahashi will begin the travel at town 11, with no apple in his possession.

The actions that can be performed during the travel are as follows:

  • Move: When at town ii (i<Ni<N), move to town i+1i+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 AiAi yen (the currency of Japan) at town ii (1≦i≦N1≦i≦N), where
  •  AiAi 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 TT. (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

 AiAi into another arbitrary non-negative integer A′iAi′ for any town ii, any number of

times. The cost of performing this operation is |Ai−A′i||Ai−Ai′|. After performing this

operation, different towns may have equal values of AiAi.

Aoki's objective is to decrease Takahashi's expected profit by at least 11 yen.

Find the minimum total cost to achieve it. You may assume that Takahashi's

expected profit is initially at least 11 yen.

Constraints

  • 1≦N≦1051≦N≦105
  • 1≦Ai≦1091≦Ai≦109 (1≦i≦N1≦i≦N)
  • AiAi are distinct.
  • 2≦T≦1092≦T≦109
  • In the initial state, Takahashi's expected profit is at least 11 yen.

Input

The input is given from Standard Input in the following format:

NN TT
A1A1 A2A2 ...... ANAN

Output

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

Sample Input 1 Copy

Copy

3 2
100 50 200

Sample Output 1 Copy

Copy

1

In the initial state, Takahashi can achieve the maximum profit of 150150 yen as follows:

  1. Move from town 11 to town 22.
  2. Buy one apple for 5050 yen at town 22.
  3. Move from town 22 to town 33.
  4. Sell one apple for 200200 yen at town 33.

If, for example, Aoki changes the price of an apple at town 22 from 5050 yen to 5151 yen,

Takahashi will not be able to achieve the profit of 150150 yen. The cost of performing this

operation is 11, thus the answer is 11.

There are other ways to decrease Takahashi's expected profit, such as changing the price

of an apple at town 33 from 200200 yen to 199199 yen.

Sample Input 2 Copy

Copy

5 8
50 30 40 10 20

Sample Output 2 Copy

Copy

2

Sample Input 3 Copy

Copy

10 100
7 10 4 5 9 3 6 8 2 1

Sample Output 3 Copy

Copy

2

题意:

            题意看了半个小时,大概是说一个人沿着X轴正半轴方向走,不能回头,轴上有N个城市,每个城市有一个权值value,各不相同,这个人可以选择在该城市买苹果,每个花费value元,或者卖苹果,value元/个,这样最终一定会有一种或多种方案产生某个最大收益。然后有另一个人,他可以选择这N个城市中的任意城市然后调整其权值,并产生(调整后value-调整前value)的花费,问后面这个人   使前者无法达到原来最大收益   的最小花费。

思路:

        前者想要获得最大收益,肯定是在前面一个花费较小的地方买苹果,然后去后面一个

花费较大的地方卖苹果。所以最大收益就是n*(n+1)/2个差值中最大的那些。想要破坏最大

收益,只要改动最大差值涉及的城市即可。特殊情况考虑如果有两个差值共用一个数,明

显也是要改两个城市,且任何情况下改动后不会一定出现更大的差值。

代码实现:

#include<bits/stdc++.h>
#include<string>
#define LL long long
#define INF 0x3f3f3f3f
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int N=2e5+100;
using namespace std;
LL a[N],tmp[N],tot,maxs[N];
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		tot=0;
		for(int i=1;i<=n;i++)cin>>a[i];
		for(int i=n;i>=1;i--)maxs[i]=max(maxs[i+1],a[i]);
		for(int i=1;i<n;i++)
			tmp[++tot]=maxs[i+1]-a[i];
		sort(tmp+1,tmp+1+tot);
		reverse(tmp+1,tmp+1+tot);
		LL ans=0;
		for(int i=1;i<=tot;i++)
			if(tmp[i]==tmp[1])ans++;
		cout<<ans<<endl;
	}
	return 0;
} 

The end;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值