问题 L: An Invisible Hand - (2018年第二阶段个人训练赛第三场)

题目描述

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 }

原文地址:https://www.cnblogs.com/iwannabe/p/9096506.html

时间: 2024-09-28 20:06:26

问题 L: An Invisible Hand - (2018年第二阶段个人训练赛第三场)的相关文章

2018年第四阶段组队训练赛第三场(BAPC2017 Preliminaries)

D.Disastrous Doubling 题目描述 A scientist, E. Collie, is going to do some experiments with bacteria.Right now, she has one bacterium. She already knows that this species of bacteria doubles itself every hour. Hence, after one hour there will be 2 bacter

2018 计蒜之道 初赛 第三场

A. 贝壳找房性价比 题解:按s排序后,斜率最大的点必定在相邻的两点之间. #pragma warning(disable:4996) #include<queue> #include<map> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #de

第九届福建省大学生程序设计竞赛 2018.8.26组队训练赛

题目链接:http://acm.fzu.edu.cn/contest/list.php?cid=158 A题题目: 题意: 给你六种操作:def, mul,mod,div, add, sub.除了看这几个字母也都知道是啥意思了,其中def是进行define. 思路: 比赛时队友写的,直接模拟,不过赛后补题时队友和我说mul时需要快速乘. 代码实现如下: 1 #include <set> 2 #include <map> 3 #include <queue> 4 #inc

2018年第四阶段组队训练赛第七场

A: Secret of Chocolate Poles 题目描述 Wendy, the master of a chocolate shop, is thinking of displaying poles of chocolate disks in the showcase. She can use three kinds of chocolate disks: white thin disks, dark thin disks, and dark thick disks. The thin

2018-2019赛季多校联合新生训练赛第六场(2018/12/15)补题题解

A 价钱统计(基础编程能力) 这个考点还是比较个性的,怎么四舍五入 解法 常规的讲如果四舍五入整数位的话,那么只需要在后面加个0.5然后强制转换一下就可以了 这个却要我们保留一位小数的四舍五入,那该怎么做呢 实际上我们只需要把这个数乘以10然后加0.5,强制转换后再除以10就可以了 代码 #include <bits/stdc++.h> using namespace std; double trans(double a) { a*=10; a+=0.5; a=int(a); a/=10; r

2019年第二阶段我要变强个人训练赛第十七场

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod=1e9+7; 5 const int N=2e6+10; 6 ll c[N],b[N]; 7 ll n,k,a,ans; 8 ll quick(ll a,ll b){ 9 ll res=1; 10 while (b){ 11 if (b&1){ 12 res=res*a%mod; 13 } 14 a=a*a%mo

郑州大学2018新生训练赛第十场题解

比赛(补题)地址:http://222.22.65.164/problemset.php 题号为:4305 -- 4309 总述:这次新生赛难度偏于平和,但涵盖方面甚广,其中一道签到题是c语言题,并且有两道是hdu一百题的原题,一道简单的最小生成树,唯一"有些难度"的应该是一道数论题(毕竟本来自己就是搞数学的).   A.沙漠骆驼 这是一道经典的递推问题,原型为HDU 2044的"一只小蜜蜂-".思路很简单,以第5个沙丘为例,到达第五个沙丘的方式有两种:从第3个向

2018-2019赛季多校联合新生训练赛第八场(2018/12/22)补题题解

感慨 这次有点感冒,昏迷程度比较大中途还溜了 感谢 感谢qut的同学的帮助!!! A 小X与三角形(数学) 公式 两边的和-两边的差-1 因为边最小得大于两边的差,边最大得小于两边的和所以说求得是一个开区间内元素的个数 代码 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll a,

2018冬令营模拟测试赛(三)

2018冬令营模拟测试赛(三) [Problem A]摧毁图状树 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见"试题描述" 输出示例 见"试题描述" 数据规模及约定 见"试题描述" 题解 这题没想到贪心 QwQ,那就没戏了-- 贪心就是每次选择一个最深的且没有被覆盖的点向上覆盖 \(k\) 层,因为这个"最深的没有被覆盖的点"不可能再有其它点引出的链覆盖它了,而它又